Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "tbb/task_scheduler_init.h"
  4. #include "tbb/parallel_for.h"
  5. #include "tbb/parallel_reduce.h"
  6. #include "tbb/blocked_range.h"
  7. #include "tbb/tick_count.h"
  8. #include "math.h"
  9.  
  10. using namespace tbb;
  11. using namespace std;
  12.  
  13. const size_t arr_size = 500000; // The size of the arrays.
  14. int GrainSize = 20000;
  15.  
  16. class SumMean {
  17. float* my_a; // Data member to take an array
  18. public: float sum; // Sum to be stored here
  19. void operator() (const blocked_range<size_t>& r)
  20. {
  21. cout << "looping" << endl;
  22. float *a = my_a;
  23. for (size_t i = r.begin(); i != r.end(); ++i)
  24. sum += a[i]; // Computing the sum over the range r
  25. }
  26.  
  27. SumMean(SumMean& x, split) : // Takes a reference to the object and a dummy argument of type split, when invoked creates a subtask:
  28. my_a(x.my_a), sum(0) {}
  29.  
  30. 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
  31.  
  32. SumMean(float a[]): // Constructor to create a task
  33. my_a(a), sum(0)
  34. {}
  35. };
  36.  
  37. int main()
  38. {
  39.  
  40. double *a, *b, p = 0, mA = 0, mB = 0, sA = 0, sB = 0;
  41. a = (double*)malloc(arr_size*sizeof(double)); //Memory allocation for the arrays.
  42. b = (double*)malloc(arr_size*sizeof(double));
  43.  
  44. //Serial algorithms go here (the task is to compare serial against parallel speeds.
  45.  
  46. //Initialise the arrays again.
  47.  
  48.  
  49. for (size_t i = 0; i < arr_size; i++) { a[i] = sin(i); b[i] = cos(i); }
  50.  
  51. //Initialise task scheduler as automatic (default).
  52. task_scheduler_init init;
  53.  
  54. //Start count for parallel calculation.
  55. tick_count parallel_start = tick_count::now();
  56.  
  57. cout << "Checkpoint 1" << endl;
  58.  
  59. //Calculate mean of a in parallel_reduce
  60. SumMean sfa((float*)a); // Create an object sf of a special class SumFoo, constructor takes an array of floats as an argument
  61. parallel_reduce(blocked_range<size_t>(0, arr_size, GrainSize), sfa); // Compute parallel_reduce over the array; takes the object as the argument
  62. return sfa.sum; // Return computed sum
  63. mA = sfa.sum / arr_size;
  64.  
  65. //Rest of code (it doesn't get past this parallel_reduce algorithm above)
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement