helping a beginner with basic OpenCL (simple monte carlo)

Hi,

I am just a total novice with both C and openCL, so please excuse me for my lack of knowledge. :oops: I was wondering if someone could help show me how to use OpenCL since I don’t understand the specifications enough to do it myself.

Here is a real example I’ve ridiculously simplified, let’s say: “what are the chances that a random integer between 0 and 99 inclusive is 42?”.

in C I tried it as:
( C code - 16 lines - codepad ) :

#include <stdio.h>
#include <stdlib.h>

int main(void){

int randnum;
int numruns = 10000;
int num42 = 0;
int i;
for(i = 0; i<numruns; i++){
randnum = rand()%100;
if (randnum == 42){num42 += 1;}
}
printf("got 'forty-two' %d times out of %d which is %f percent", num42, numruns, (float)num42/numruns*100);
return 0;
}

I’m very very sorry for my terrible C style. In particular, I know that rand()%100 is not a truly random way to get an integer… as I mentioned I am JUST starting out learning both C and OpenCL, so even the above was very hard for me to get to compile.

My question is, how would I take the same basic algorithm and do it in OpenCL? Obviously after it works I would like to increase numruns from ten thousand to ten billion :smiley:

A direct translation into OpenCL for me to study, one that was as simple as possible would be extremely appreciated.

Thank you so much~! :slight_smile:

I haven’t gotten a single reply, so I did a search. It seems my request might not have been as “simple” as I had assumed: http://www.macresearch.org/opencl_episode3,

Thanks for the reply, but if there is no random function in the OpenCL kernel language, how can we do Monte Carlo simulations? Can we just write a kernel function that calls a compiled executable where the Monte Carlo would be coded?

A reply to which states:

Random number generators are a little tricky on massively parallel systems, due to contention over the random number generator state, or the cost of keeping many different copies of random state, and how to update random state cooperatively if some of the workitems don’t make the function call. It is obviously not impossible, though. It just hasn’t been high enough on the totem pole to get Khronos to do what research is required to make sure it can be implemented cheaply on existing hardware before requiring it in a specification. If you want this to happen, you can kick it along by implementing some efficient random generator on a variety of different OpenCL devices and attaching your work to a feature request at Khronos’s bug site. Or, maybe just write a paper.
In the mean time, it should be simple in most cases to fill an array with random numbers and pass that to the kernel.

Now, if it is a “little tricky” then I, as a beginner, have no hope! Could someone please help me understand the above paragraph, ie how to do it in actual openCL code? Thank you very much!

All the best,
JC

You have basically two options for random numbers: pass in a pre-computed array of them (which means you incur the overhead of calculating them on the host and copying them over) or have each work-item calculate its own random numbers from its own seed.

OpenCL is a low-level implementation so you can get high-performance. This means a lot of stuff has to be done by hand. In particular, random number generators vary from very fast (not very random) to slow (but very random). The right choice will depend on your algorithm. If you’ve been using rand() before you may be in for some surprises as the degree of randomness it provides is rather limited.