Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=======================================================================
- //
- // SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF END-USER LICENSE AGREEMENT FOR
- // INTEL(R) PARALLEL ADVISOR 2011.
- //
- // Copyright 2009-2010 Intel Corporation
- //
- // THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
- // NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- // PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
- //
- // ========================================================================
- #include "stdafx.h"
- #include <stdio.h>
- #include <tbb/mutex.h>
- #include <tbb/blocked_range.h>
- #include <tbb/parallel_for.h>
- #include <tbb/partitioner.h>
- typedef tbb::blocked_range<int> int_blocked_range;
- const int limit = 3000000;
- const int correctCount = 216816;
- int primes = (limit >= 2) ? 1 : 0; // two is a prime, so count it
- tbb::mutex primeMutex;
- void Tick() {
- tbb::mutex::scoped_lock lockUntilScopeExit(primeMutex);
- primes++;
- }
- bool IsPrime(int p) {
- for (int i = 3; i*i <= p; i += 2) {
- if (p/i*i == p) return false;
- }
- return true;
- }
- class LoopBody {
- public:
- LoopBody() {}
- void operator() (const int_blocked_range& range) const {
- const int upper = 3 + 2*range.end(); // expand range
- for (int p = 3 + 2*range.begin(); p < upper; p+=2) {
- if (IsPrime(p)) Tick();
- }
- }
- };
- #include <tbb/task_scheduler_init.h>
- int _tmain(int argc, _TCHAR* argv[])
- {
- tbb::task_scheduler_init tsi;
- tbb::parallel_for(
- int_blocked_range(3-3, (limit-3)/2 + 1), // make contiguous range 0..
- LoopBody(),
- tbb::auto_partitioner());
- printf("primes = %d\n", primes);
- return primes != correctCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement