justcurious

02-15-2010, 02:15 PM

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:

( http://codepad.org/qPcSo6P9 ) :

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

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

( http://codepad.org/qPcSo6P9 ) :

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

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

Thank you so much~! :)