Advertisement
nex036ara

primes_cilk

Mar 25th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. //=======================================================================
  2. //
  3. // SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF END-USER LICENSE AGREEMENT FOR
  4. // INTEL(R) PARALLEL ADVISOR 2011.
  5. //
  6. // Copyright 2009-2010 Intel Corporation
  7. //
  8. // THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
  9. // NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  10. // PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
  11. //
  12. // ========================================================================
  13.  
  14. #include "stdafx.h"
  15. #include <stdio.h>  
  16. #include <cilk/cilk.h>
  17. #include <cilk/reducer_opadd.h>
  18.  
  19. const int limit         = 3000000;
  20. const int correctCount  = 216816;
  21. cilk::reducer_opadd<int> primes((limit >= 2) ? 1 : 0);  // two is a prime, so count it;
  22.  
  23. void Tick() {
  24.     primes++;
  25. }
  26.  
  27. bool IsPrime(int p) {
  28.     for (int i = 3; i*i <= p; i += 2) {
  29.         if (p/i*i == p) return false;
  30.     }
  31.     return true;
  32. }
  33.  
  34. int _tmain(int argc, _TCHAR* argv[])
  35. {
  36.     cilk_for (int p = 3; p <= limit; p += 2) {
  37.         if (IsPrime(p)) Tick();
  38.     }
  39.  
  40.     printf("primes = %d\n", primes.get_value());
  41.     return primes.get_value() != correctCount;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement