Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "iostream"
- #include "tbb/task_scheduler_init.h"
- #include "tbb/parallel_for.h"
- #include "tbb/parallel_reduce.h"
- #include "tbb/blocked_range.h"
- #include "tbb/tick_count.h"
- #include "math.h"
- using namespace tbb;
- using namespace std;
- const size_t arr_size = 500000; // The size of the arrays.
- int GrainSize = 20000;
- class SumMean {
- float* my_a; // Data member to take an array
- public: float sum; // Sum to be stored here
- void operator() (const blocked_range<size_t>& r)
- {
- cout << "looping" << endl;
- float *a = my_a;
- for (size_t i = r.begin(); i != r.end(); ++i)
- sum += a[i]; // Computing the sum over the range r
- }
- SumMean(SumMean& x, split) : // Takes a reference to the object and a dummy argument of type split, when invoked creates a subtask:
- my_a(x.my_a), sum(0) {}
- void join(const SumMean& y) { sum += y.sum; } // The function is invoked when a subtask finishes its work and the result is merged with the main body
- SumMean(float a[]): // Constructor to create a task
- my_a(a), sum(0)
- {}
- };
- int main()
- {
- double *a, *b, p = 0, mA = 0, mB = 0, sA = 0, sB = 0;
- a = (double*)malloc(arr_size*sizeof(double)); //Memory allocation for the arrays.
- b = (double*)malloc(arr_size*sizeof(double));
- //Serial algorithms go here (the task is to compare serial against parallel speeds.
- //Initialise the arrays again.
- for (size_t i = 0; i < arr_size; i++) { a[i] = sin(i); b[i] = cos(i); }
- //Initialise task scheduler as automatic (default).
- task_scheduler_init init;
- //Start count for parallel calculation.
- tick_count parallel_start = tick_count::now();
- cout << "Checkpoint 1" << endl;
- //Calculate mean of a in parallel_reduce
- SumMean sfa((float*)a); // Create an object sf of a special class SumFoo, constructor takes an array of floats as an argument
- parallel_reduce(blocked_range<size_t>(0, arr_size, GrainSize), sfa); // Compute parallel_reduce over the array; takes the object as the argument
- return sfa.sum; // Return computed sum
- mA = sfa.sum / arr_size;
- //Rest of code (it doesn't get past this parallel_reduce algorithm above)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement