Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallel more than one nested loops with tbb

Tags:

c++

tbb

What is the best way to parallel three nested independent loops with tbb?

for(int i=0; i<100; i++){
    for(int j=0; j<100; j++){
        for(int k=0; k<100; k++){
            printf("Hello World \n");
        }
     }
 }
like image 817
user3562182 Avatar asked Apr 18 '15 16:04

user3562182


1 Answers

There are basically two ways for nested loops in TBB.

  1. Since TBB is designed to perfectly support nested parallelism, just write nested parallel fors:

    tbb::parallel_for(0, 100, [](int i){
        tbb::parallel_for(0, 100, [](int j){
            tbb::parallel_for(0, 100, [](int k){
                printf("Hello World %d/%d/%d\n", i, j, k);
            });
        });
    });
    

    This variant works well when the loops belong to different modules or/and libraries.

  2. Otherwise, collapse two or three nested loops using blocked_range2d or blocked_range3d. It can additionally help to optimize cache locality and thus increase performance even on a single thread when accessing arrays:

    tbb::parallel_for( tbb::blocked_range3d<int>(0, 100, 0, 100, 0, 100),
        []( const tbb::blocked_range3d<int> &r ) {
            for(int i=r.pages().begin(), i_end=r.pages().end(); i<i_end; i++){
                for(int j=r.rows().begin(), j_end=r.rows().end(); j<j_end; j++){
                    for(int k=r.cols().begin(), k_end=r.cols().end(); k<k_end; k++){
                        printf("Hello World %d\n", matrix3d[i][j][k]);
                    }
                }
            }
    });
    
like image 75
Anton Avatar answered Sep 19 '22 12:09

Anton