Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Help with parallelization

GUT2060
Beginner
453 Views

Hi , 

Here is a serial implementation of a simple example describing the task at hand : 

const int numIter = 1e8;
std::vector<double> Vector(1000,0);

for(int i = 0 ; i < numIter ; i++ ) 
{
   const int randomIndex = 1 + rand() % 1000;
   Vector[randomIndex] += i*3.14;
}

I'm new to TBB, and i would appreciate some help with how to parallelize this code for best performances.

Thanks

 

0 Kudos
3 Replies
NoorjahanSk_Intel
Moderator
428 Views

Hi,

 

Thanks for reaching out to us.

 

You can use construct in TBB to parallelize the code. Such as parallel_for,parallel_reduce, etc..

 

Please refer to the below link for more details:

https://oneapi-src.github.io/oneTBB/main/tbb_userguide/parallel_for_os.html

You can also refer to the ProTBB Textbook for more details.

 

Please refer to the below parallel code using oneTBB.

#include<iostream>
#include<vector>
#include <tbb/parallel_for.h>
int main()
{
    const int numIter = 1e8;
    std::vector<double> Vector(1000, 0);
   tbb::parallel_for(tbb::blocked_range<int>(0, numIter),
        [&](tbb::blocked_range<int> r)
        {
            for (int i = r.begin(); i < r.end(); ++i)
            {
                const int randomIndex = 1 + rand() % 1000;
                Vector[randomIndex] += i * 3.14;
            }
        });

return 0;
}

 

Thanks & Regards,

Noorjahan.

 

0 Kudos
NoorjahanSk_Intel
Moderator
391 Views

Hi,


We haven't heard back from you. Could you please provide an update on your issue?


Thanks & Regards,

Noorjahan.


0 Kudos
NoorjahanSk_Intel
Moderator
372 Views

Hi,


I have not heard back from you, so I will close this inquiry now. If you need further assistance, please post a new question.


Thanks & Regards,

Noorjahan.


0 Kudos
Reply