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

Monte carlo simulation

balteo
Beginner
515 Views
Hello,

I am new to TBB and I am trying to build a monte carlo program that will run N simulations independent of each other and use the results of that N simulations in order to carry on working.

I am not sure whether to use parallel_for, parallel_reduce or parallel_do or some other function. Can anyone please advise?

Thanks in advance,

Julien Martin.


0 Kudos
1 Solution
robert-reed
Valued Contributor II
515 Views

Well, learning C++ by writing a parallel program will certainly give you a challenge. Threading Building Blocks uses C++ very deeply.

Can you describe the inputs for your N simulations in the form of an indexed array? You might start looking at the TBB parallel for to divide that list of simulations among the available threads, letting each take on as much work as they can accomplish. Since the simulations are completely independent of each other, there should be no interference between the threads. If some of the simulations take longer than others, the natural load balancing provided by task stealing within TBB should allow the workers to help each other finish the set.

You might try focusing on these ideas and let them guide your reading of the documentation.

View solution in original post

0 Kudos
6 Replies
smasherprog
Beginner
515 Views
0 Kudos
balteo
Beginner
515 Views
Thank you smasherprog,
I already had a look at the documentation. What I need to start with is basic guidance as to what to look for in the documentation bearing in mind the need I have.
I am completely new to TBB and rather novice in C++...
Thanks,
J.
0 Kudos
robert-reed
Valued Contributor II
516 Views

Well, learning C++ by writing a parallel program will certainly give you a challenge. Threading Building Blocks uses C++ very deeply.

Can you describe the inputs for your N simulations in the form of an indexed array? You might start looking at the TBB parallel for to divide that list of simulations among the available threads, letting each take on as much work as they can accomplish. Since the simulations are completely independent of each other, there should be no interference between the threads. If some of the simulations take longer than others, the natural load balancing provided by task stealing within TBB should allow the workers to help each other finish the set.

You might try focusing on these ideas and let them guide your reading of the documentation.

0 Kudos
balteo
Beginner
515 Views
Thanks Robert,
This API/library looks new and exciting to me.
Can anyone please tell me what these mean in c++ (see //HERE and //HERE below).
I cannot seem to be able to figure out what the ":my_a(a){}" mean and it gives me a compilation error...
Any help?
Julien.

#include "tbb/tbb.h"
using namespace tbb;
class ApplyFoo {
float *const my_a;
public:
void operator()( const blocked_range& r ) const {
float *a = my_a;
for( size_t i=r.begin(); i!=r.end(); ++i )
Foo(a);
}
ApplyFoo( float a[] ) : //HERE
my_a(a) //HERE
{}
};


0 Kudos
robert-reed
Valued Contributor II
515 Views
Your "//HERE" markers coincide witha C++ class constructor that uses an initializer (the ": my_a(a)" initializes the class variable my_a from the constructor parameter a). I didn't look at it closely enough to determine what error you might have encountered. Time to go back to your C++ training materials.
0 Kudos
balteo
Beginner
515 Views
Thanks a lot.
J.
0 Kudos
Reply