- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[CODE C]
#include
#include tbb/parallel_for.h
#include tbb/blocked_range2d.h
#include tbb/task_scheduler_init.h
#include tbb/tick_count.h
#include tbb/partitioner.h
using namespace tbb;
const size_t L = 200;
const size_t M = 200;
const size_t N = 200;
void SerialMatrtixMultiply(float c[]
for(size_t i = 0; i < M; ++i){
for(size_t j = 0; j < N; ++j){
float sum = 0;
for(size_t k = 0; k < L; ++k)
sum += a
c
}
}
}
class MatrixMultiply2D{
float (*my_a)
float (*my_b)
float (*my_c)
public:
void operator()(const blocked_range2d
float (*a)
float (*b)
float (*c)
for(size_t i = r.rows().begin(); i != r.rows().end(); ++i){
for(size_t j = r.cols().begin(); j != r.cols().end(); ++j){
float sum = 0;
for(size_t k = 0; k < L; ++k)
sum += a
c
}
}
}
MatrixMultiply2D(float c[]
{}
};
void ParallelMatrixMultiply(float c[]
parallel_for(blocked_range2d
}
int main(void){
task_scheduler_init init;
float a
float b
float c
srand(time(NULL));
for(int i = 0;i < M;i++){
for(int j = 0;j < L;j++)
a
}
for(int i = 0;i < L;i++){
for(int j = 0;j < N;j++)
b
}
tick_count t0 = tick_count::now();
SerialMatrtixMultiply(c,a,b);
tick_count t1 = tick_count::now();
std::cout << seq eslaped time : << (t1 t0).seconds() << std::endl;
t0 = tick_count::now();
ParallelMatrixMultiply(c,a,b);
t1 = tick_count::now();
std::cout << parallel eslaped time : << (t1 t0).seconds() << std::endl;
return 0;
}
The elapsed time :
seq elapsed time : 0.04437542 (s)
parallel elapsed time : 0.0111000 (s)
Parallel case is faster than sequential case.
But, when i removed "task_scheduler_init init;" and "ParallelMatrixMultiply(c,a,b);" , namely only running SerialMatrtixMultiply(c,a,b), elapsed time is 0.0000636 (s).
This is very much faster than TBB case.
Why appears this strange?
Any answer will be appricated!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page