1 #include <algorithm> // for std::max
2 #include <cassert>
3 #include <cstdio>
4 #include <chrono>
5 #include <iostream>
6 #include <iomanip>
7 #include <thread>
8 #include <random>
9 #include <cmath>
10
11 extern int M, N;
12 extern int B;
13 extern int MB;
14 extern int NB;
15 extern double **matrix;
16
17 // nominal operations
calc(double v0,double v1)18 inline double calc(double v0, double v1) {
19 return (v0 == v1) ? std::pow(v0/v1, 4.0f) : std::max(v0,v1);
20 //return std::max(v0, v1);
21 }
22
23 // initialize the matrix
init_matrix()24 inline void init_matrix(){
25 matrix = new double *[M];
26 for ( int i = 0; i < M; ++i ) matrix[i] = new double [N];
27 for(int i=0; i<M; ++i){
28 for(int j=0; j<N ; ++j){
29 matrix[i][j] = i*N + j;
30 }
31 }
32 }
33
34 // destroy the matrix
destroy_matrix()35 inline void destroy_matrix() {
36 for ( int i = 0; i < M; ++i ) {
37 delete [] matrix[i];
38 }
39 delete [] matrix;
40 }
41
42 //computation given block row index i, block col index j
block_computation(int i,int j)43 inline int block_computation(int i, int j){
44 // When testing taskflow
45 return i + j;
46
47 //int start_i = i*B;
48 //int end_i = (i*B+B > M) ? M : i*B+B;
49 //int start_j = j*B;
50 //int end_j = (j*B+B > N) ? N : j*B+B;
51 //for ( int ii = start_i; ii < end_i; ++ii ) {
52 // for ( int jj = start_j; jj < end_j; ++jj ) {
53 // double v0 = ii == 0 ? 0 : matrix[ii-1][jj];
54 // double v1 = jj == 0 ? 0 : matrix[ii][jj-1];
55 // matrix[ii][jj] = ii==0 && jj==0 ? 1 : calc(v0,v1);
56 // }
57 //}
58 }
59
60
61 //computation given block row index i, block col index j
framework_computation(int i,int j)62 inline void framework_computation(int i, int j){
63 // When testing framework
64 int start_i = i*B;
65 int end_i = (i*B+B > M) ? M : i*B+B;
66 int start_j = j*B;
67 int end_j = (j*B+B > N) ? N : j*B+B;
68
69 for ( int ii = start_i; ii < end_i; ++ii ) {
70 for ( int jj = start_j; jj < end_j; ++jj ) {
71 matrix[ii][jj] += ii == 0 ? 0.0 : matrix[ii-1][jj];
72 matrix[ii][jj] += ii >= M-1 ? 0.0 : matrix[ii+1][jj];
73 matrix[ii][jj] += jj == 0 ? 0.0 : matrix[ii][jj-1];
74 matrix[ii][jj] += jj >= N-1 ? 0.0 : matrix[ii][jj+1];
75 matrix[ii][jj] *= 0.25;
76 //matrix[ii][jj] = matrix[ii][jj];
77 }
78 }
79 }
80
81
82
83 std::chrono::microseconds measure_time_taskflow(unsigned);
84 std::chrono::microseconds measure_time_omp(unsigned);
85 std::chrono::microseconds measure_time_tbb(unsigned);
86
87 std::chrono::microseconds measure_time_taskflow(unsigned, unsigned);
88 std::chrono::microseconds measure_time_omp(unsigned, unsigned);
89 std::chrono::microseconds measure_time_tbb(unsigned, unsigned);
90
91