@@ -68,6 +68,119 @@ The compiled client program examples can be found under the `bin` subdirectory
6868of the installation package (` $CLRNG_ROOT/bin ` under Linux).
6969Note that the examples expect an OpenCL GPU device to be available.
7070
71+ ## Simple example
72+
73+ The simple example below shows how to use clRNG to generate random numbers
74+ by directly using device side headers (.clh) in your OpenCL kernel.
75+
76+ ``` c
77+ #include < stdlib.h>
78+ #include < string.h>
79+
80+ #include " clRNG.h"
81+ #include " mrg31k3p.h"
82+
83+ int main ( void )
84+ {
85+ cl_int err;
86+ cl_platform_id platform = 0;
87+ cl_device_id device = 0;
88+ cl_context_properties props[ 3] = { CL_CONTEXT_PLATFORM, 0, 0 };
89+ cl_context ctx = 0;
90+ cl_command_queue queue = 0;
91+ cl_program program = 0;
92+ cl_kernel kernel = 0;
93+ cl_event event = 0;
94+ cl_mem bufIn, bufOut;
95+ float * out;
96+ char * clrng_root;
97+ char include_str[ 1024] ;
98+ char build_log[ 4096] ;
99+ size_t i = 0;
100+ size_t numWorkItems = 64;
101+ clrngMrg31k3pStream * streams = 0;
102+ size_t streamBufferSize = 0;
103+ size_t kernelLines = 0;
104+
105+ /* Sample kernel that calls clRNG device-side interfaces to generate random numbers */
106+ const char *kernelSrc[] = {
107+ " #define CLRNG_SINGLE_PRECISION \n",
108+ " #include <mrg31k3p.clh> \n",
109+ " \n",
110+ " __kernel void example(__global clrngMrg31k3pHostStream *streams, \n",
111+ " __global float *out) \n",
112+ " { \n",
113+ " int gid = get_global_id(0); \n",
114+ " \n",
115+ " clrngMrg31k3pStream workItemStream; \n",
116+ " clrngMrg31k3pCopyOverStreamsFromGlobal(1, &workItemStream, \n",
117+ " &streams[gid]); \n",
118+ " \n",
119+ " out[gid] = clrngMrg31k3pRandomU01(&workItemStream); \n",
120+ " } \n",
121+ " \n",
122+ };
123+
124+ /* Setup OpenCL environment. */
125+ err = clGetPlatformIDs( 1, &platform, NULL );
126+ err = clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL );
127+
128+ props[1] = (cl_context_properties)platform;
129+ ctx = clCreateContext( props, 1, &device, NULL, NULL, &err );
130+ queue = clCreateCommandQueue( ctx, device, 0, &err );
131+
132+ /* Make sure CLRNG_ROOT is specified to get library path */
133+ clrng_root = getenv("CLRNG_ROOT");
134+ if(clrng_root == NULL) printf("\nSpecify environment variable CLRNG_ROOT as described\n");
135+ strcpy(include_str, "-I ");
136+ strcat(include_str, clrng_root);
137+ strcat(include_str, "/cl/include");
138+
139+ /* Create sample kernel */
140+ kernelLines = sizeof(kernelSrc) / sizeof(kernelSrc[0]);
141+ program = clCreateProgramWithSource(ctx, kernelLines, kernelSrc, NULL, &err);
142+ err = clBuildProgram(program, 1, &device, include_str, NULL, NULL);
143+ if(err != CL_SUCCESS)
144+ {
145+ printf("\nclBuildProgram has failed\n");
146+ clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 4096, build_log, NULL);
147+ printf("%s", build_log);
148+ }
149+ kernel = clCreateKernel(program, "example", &err);
150+
151+ /* Create streams */
152+ streams = clrngMrg31k3pCreateStreams(NULL, numWorkItems, &streamBufferSize, (clrngStatus *)&err);
153+
154+ /* Create buffers for the kernel */
155+ bufIn = clCreateBuffer(ctx, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, streamBufferSize, streams, &err);
156+ bufOut = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, numWorkItems * sizeof(cl_float), NULL, &err);
157+
158+ /* Setup the kernel */
159+ err = clSetKernelArg(kernel, 0, sizeof(bufIn), &bufIn);
160+ err = clSetKernelArg(kernel, 1, sizeof(bufOut), &bufOut);
161+
162+ /* Execute the kernel and read back results */
163+ err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &numWorkItems, NULL, 0, NULL, &event);
164+ err = clWaitForEvents(1, &event);
165+ out = (float *)malloc(numWorkItems * sizeof(out[0]));
166+ err = clEnqueueReadBuffer(queue, bufOut, CL_TRUE, 0, numWorkItems * sizeof(out[0]), out, 0, NULL, NULL);
167+
168+ /* Release allocated resources */
169+ clReleaseEvent(event);
170+ free(out);
171+ clReleaseMemObject(bufIn);
172+ clReleaseMemObject(bufOut);
173+
174+ clReleaseKernel(kernel);
175+ clReleaseProgram(program);
176+
177+ clReleaseCommandQueue(queue);
178+ clReleaseContext(ctx);
179+
180+ return 0;
181+ }
182+ ```
183+
71184
72185## Building the documentation manually
73186
0 commit comments