Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29264 Discussions

selecting a case with a probability

sjenitzer
Beginner
1,186 Views
Hi everyone,

I have got a problem with optimizing my code. In my code I need to select from 6 different groups, all with a probability beta of happening. Since I need to select from these cases many times I am looking for a fast way to do this.
I am doing it now with a do loop, but I don't think this is optimal. Does anyone no a better method?

Thanks in advance,
Bart
my code:
test.f90
0 Kudos
6 Replies
TimP
Honored Contributor III
1,186 Views
Giving up on the important step of understanding why you are doing this, I'll take a narrow point of view focused on your source code.
You could use your pre-computed sums of your beta coefficients in your search loop, so as to remove that step from your loop.
If you extend your search to a large enough table to make it pay off, you would consider a binary search.
No doubt, you could invent a faster pseudo random number generator, if you have no particular quality criteria. If you made your arrays bigger, the time spent in RANDOM_NUMBER would be less significant.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,186 Views

Bart,

In your search loop you should be able to replace

if (rho
with

if (rho

Let (instruct) the optimizer to unroll the loop.

Also,

CALL RANDOM_NUMBER(rho)

in your test loop is going to mess up any timing results. Pre-compute your list of random numbers.

Jim Dempsey
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,186 Views

And I agree with tim18, when m is large (currently 6), then consider a binary search.

Jim
0 Kudos
sjenitzer
Beginner
1,186 Views
Thanks for all the quick responses!

I tried to send a simplified version of the code, to make the problem more clear.... I'll be more elaborate next time :)

I will replace the sum(beta) with betasum.

The problem is not that m becomes large, but ni becomes very large. There are ni new particles created and they are put in m groups.
The lambda describes the behaviour of the particle. I also do not know how many particles there are going to be created. This makes it hard to pre-calculate the random numbers.
(also how does the pre-calculation of random numbers speed up the calculation?)




0 Kudos
jimdempseyatthecove
Honored Contributor III
1,186 Views

Use RANDOM_NUMBER to obtain ni random numbers in one call (into array passed in on call). Then distribute the ni random numbers into your particles. When linking with a multi-threaded library RANDOM enters and exits a critical section for each random number. Whereas RANDOM_NUMBER enters and exits a critical section for each call to RANDOM_NUMBER regardless of the number of random numbers returned. Therefore if RANDOM_NUMBER is use to grab ni random numbers at once, then the number of times you enter/leave its critical section is reduced by a factor of 1/ni. Critical sections are speed-bumps for your program (avoid them whenever possible).

The sample code indicated, for any given iteration, you know in advance what ni is. ni may vary with each iteraton. Your call to RANDOM_NUMBER can therefore grab the number it needs.

Should the number of particles not be known in advance (your DO I=1,ni has an exit), then use RANDOM_NUMBER to obtain a pool of random numbers. e.g. 100. Then as you need random numbers, pick the next one out of the pool. When the pool expires, refill with another 100 numbers. (replace 100 with whatever gives satisfactory results).

Jim Dempsey
0 Kudos
sjenitzer
Beginner
1,186 Views

Great, thanks.
This is a nice new insight!

Bart
0 Kudos
Reply