 # 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:

``````#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 A direct translation into OpenCL for me to study, one that was as simple as possible would be extremely appreciated.

Thank you so much~! 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?