Dear Rhys,

sorry, but I do not get your point. Could you elaborate on it in more detail?

Furthermore, I do not fully get what Altro is doing. I have the impression
that he is calculating the random numbers in advance.

That is what I do 'not' suggest with my C++11 wrapper but simply make the
random number generator available to each thread seeded differently.

So, finally, if the main part of the calculating in Altros code happens in
the for lopp, why not directly initialize the random number generator
there, see the appended minimal example 'me-altro.c'

Best wishes and keep uns informed,
-- Klaus.





>> I would simply use a wrapper around a C++ std::vector who holds as many
>> C++11 random number generators (engines) as you use threads in your
>> code.
>> By calling a random number, the engine in the ith index of the vector is
>> used, where i is the thread id.
>
> If your threads aren't doing much work beyond random number
> generation, you might see a less-than-ideal speedup due to contention
> on the contiguous std::vector memory.
>
> Not that you will see such a thing, but suspect it if you run into a wall.
>
> - Rhys
>
/* me-altro.c */

#include <gsl/gsl_rng.h>
#include <iostream>
#include "omp.h"

int main()
{
	int const n_threads = 8;

	omp_set_num_threads( n_threads );

	int const DIM_ENSEMBLE = 10;

	#pragma omp parallel for
	for(int i = 0; i < DIM_ENSEMBLE; ++i)
	{
		gsl_rng* r = gsl_rng_alloc( gsl_rng_mt19937 );
		gsl_rng_set( r, omp_get_thread_num() + 1 );

		#pragma omp critical
		std::cout << "hello from thread "		<< omp_get_thread_num()
							<< " with random number "	<< gsl_rng_uniform_pos( r )
							<< std::endl;

		gsl_rng_free( r );
	}

	return 0;
}

Reply via email to