int rec_cilkified(int *a,int *b,unsigned int n) { if ( n < COARSENESS) { int sum = 0; int i; for (i = 0; i < n; ++i) { sum += a[i] * b[i]; } return sum; } else { int *leftA = a; int *rightA = a + (int)ceil(n/2); int *leftB = b; int *rightB = b + (int)ceil(n/2); int sumLeft, sumRight; sumLeft = cilk_spawn rec_cilkified(leftA, leftB, ceil(n/2)); sumRight = cilk_spawn rec_cilkified(rightA, rightB, n-ceil(n/2)); cilk_sync; return sumRight+sumLeft; } }
int rec_cilkified(int *a,int *b,unsigned int n){ int sum1=0; int sum2=0; if(n > coars){ sum1 = cilk_spawn rec_cilkified(a,b,n/2); sum2 = rec_cilkified(a+n/2, b+n/2, n-n/2); cilk_sync; return (sum1 + sum2); } for(int i=0;i<n;i++){ sum1+=a[i]*b[i]; } return sum1; }
double rec_cilkified(double * a, double * b, int n) { int split_on; int i; double suma, sumb; if(n <= COARSENESS) // go serial { suma = std::inner_product(a, a+n, b, (double)0); return suma; } else // split and recurse { split_on = n / 2; // truncting decimal suma = cilk_spawn rec_cilkified(a, b, split_on); sumb = rec_cilkified(a + split_on, b + split_on, n - split_on); cilk_sync; return suma + sumb; } return 0; }
// A simple test harness int inn_prod_driver(int n) { __cilkrts_set_param("nworkers","4"); double * a = new double[n]; double * b = new double[n]; for (int i = 0; i < n; ++i) { a[i] = i; b[i] = i; } std::random_shuffle(a, a + n); std::random_shuffle(b, b + n); double seqresult = std::inner_product(a, a+n, b, (double)0); long t1 = example_get_time(); for(int i=0; i< ITERS; ++i) { seqresult = std::inner_product(a, a+n, b, (double)0); } long t2 = example_get_time(); double seqtime = (t2-t1)/(ITERS*1000.f); std::cout << "Sequential time: " << seqtime << " seconds" << std::endl; /***********************************************************/ /******** START TESTING RECURSIVE CILKFIED VERSION *******/ /***********************************************************/ double parresult = rec_cilkified(a, b, n); t1 = example_get_time(); for(int i=0; i< ITERS; ++i) { parresult = rec_cilkified(a, b, n); } t2 = example_get_time(); double partime = (t2-t1)/(ITERS*1000.f); std::cout << "Recursive cilkified time:" << partime << " seconds" << std::endl; std::cout << "Speedup is: " << seqtime/partime << std::endl; std::cout << "Sequential result is: "<<seqresult<<std::endl; std::cout << "Recursive cilkified result is: "<<parresult<<std::endl; std::cout << "Result is " << (close(seqresult,parresult,n) ? "correct":"incorrect") << std::endl; /****************************************************************/ /******** START TESTING NESTED LOOPED CILKIFIED VERSION *******/ /****************************************************************/ parresult = loop_cilkified(a, b, n); t1 = example_get_time(); for(int i=0; i< ITERS; ++i) { //parresult = loop_cilkified(a, b, n); parresult = loop_cilkified(a, b, n); } t2 = example_get_time(); partime = (t2-t1)/(ITERS*1000.f); std::cout << "Nested loop cilkified time: " << partime << " seconds" << std::endl; std::cout << "Speedup is: " << seqtime/partime << std::endl; std::cout << "Sequential result is: "<<seqresult<<std::endl; std::cout << "Loop cilkified result is: "<<parresult<<std::endl; std::cout << "Result is " << (close(seqresult,parresult,n) ? "correct":"incorrect") << std::endl; /**************************************************************/ /******** START TESTING HYPEROBJECT CILKIFIED VERSION *******/ /**************************************************************/ parresult = hyperobject_cilkified(a, b, n); t1 = example_get_time(); for(int i=0; i< ITERS; ++i) { parresult = hyperobject_cilkified(a, b, n); } t2 = example_get_time(); partime = (t2-t1)/(ITERS*1000.f); std::cout << "Hyperobject cilkified time:" << partime << " seconds" << std::endl; std::cout << "Speedup is: " << seqtime/partime << std::endl; std::cout << "Sequential result is: "<<seqresult<<std::endl; std::cout << "Hyperobject result is: "<<parresult<<std::endl; std::cout << "Result is " << (close(seqresult,parresult,n) ? "correct":"incorrect") << std::endl; delete [] a; delete [] b; return 0; }