root / Pi / OpenCL / PiOpenCL.c @ 152
Historique | Voir | Annoter | Télécharger (11,33 ko)
| 1 |
// Pi Dart Dash in OpenCL in C, illustrative example
|
|---|---|
| 2 |
//
|
| 3 |
// Emmanuel Quemener <emmanuel.quemener@ens-lyon.fr
|
| 4 |
// Copyleft Cecill v2
|
| 5 |
//
|
| 6 |
// -h : print the documentation and detect devices as (platform,device)
|
| 7 |
//
|
| 8 |
// classical use:
|
| 9 |
// #1 OpenCL Plateform ID: get this information with -h option
|
| 10 |
// #2 OpenCL Device ID: get this information with -h option
|
| 11 |
// #3 Minimal number of iterations:
|
| 12 |
// #4 Parallel Rate: scattering global work in parts executed //ly
|
| 13 |
// #5 Loops (to improve statistics)
|
| 14 |
//
|
| 15 |
|
| 16 |
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
|
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <CL/cl.h> |
| 21 |
#include <stdint.h> |
| 22 |
#include <sys/time.h> |
| 23 |
|
| 24 |
int DetectOpenCLDevices(void) |
| 25 |
{
|
| 26 |
int i, j;
|
| 27 |
char* value;
|
| 28 |
size_t valueSize; |
| 29 |
cl_uint platformCount; |
| 30 |
cl_platform_id* platforms; |
| 31 |
cl_uint deviceCount; |
| 32 |
cl_device_id* devices; |
| 33 |
cl_uint maxComputeUnits; |
| 34 |
cl_int maxWorkGroupSize; |
| 35 |
cl_int maxWorkItemSizes; |
| 36 |
cl_device_type dev_type; |
| 37 |
|
| 38 |
// get all platforms
|
| 39 |
clGetPlatformIDs(0, NULL, &platformCount); |
| 40 |
platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
|
| 41 |
clGetPlatformIDs(platformCount, platforms, NULL);
|
| 42 |
|
| 43 |
printf("OpenCL statistics: %d platform(s) detected\n\n",platformCount);
|
| 44 |
|
| 45 |
for (i = 0; i < platformCount; i++) { |
| 46 |
|
| 47 |
// get all devices
|
| 48 |
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount); |
| 49 |
devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
|
| 50 |
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL);
|
| 51 |
|
| 52 |
// for each device print critical attributes
|
| 53 |
for (j = 0; j < deviceCount; j++) { |
| 54 |
|
| 55 |
// print device name
|
| 56 |
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &valueSize); |
| 57 |
value = (char*) malloc(valueSize);
|
| 58 |
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, valueSize, value, NULL);
|
| 59 |
printf("Device (%d,%d): %s\n",i, j, value);
|
| 60 |
free(value); |
| 61 |
|
| 62 |
// print type device CPU/GPU/ACCELERATOR
|
| 63 |
clGetDeviceInfo(devices[j], CL_DEVICE_TYPE, sizeof(dev_type), &dev_type, NULL); |
| 64 |
printf("\tDevice Type: ");
|
| 65 |
if(dev_type & CL_DEVICE_TYPE_GPU)
|
| 66 |
printf("CL_DEVICE_TYPE_GPU ");
|
| 67 |
if(dev_type & CL_DEVICE_TYPE_CPU)
|
| 68 |
printf("CL_DEVICE_TYPE_CPU ");
|
| 69 |
if(dev_type & CL_DEVICE_TYPE_ACCELERATOR)
|
| 70 |
printf("CL_DEVICE_TYPE_ACCELERATOR ");
|
| 71 |
if(dev_type & CL_DEVICE_TYPE_DEFAULT)
|
| 72 |
printf("CL_DEVICE_TYPE_DEFAULT ");
|
| 73 |
printf("\n");
|
| 74 |
|
| 75 |
// print device vendor
|
| 76 |
clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, 0, NULL, &valueSize); |
| 77 |
value = (char*) malloc(valueSize);
|
| 78 |
clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, valueSize, value, NULL);
|
| 79 |
printf("\tDevice vendor: %s\n", value);
|
| 80 |
free(value); |
| 81 |
|
| 82 |
// print hardware device version
|
| 83 |
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 0, NULL, &valueSize); |
| 84 |
value = (char*) malloc(valueSize);
|
| 85 |
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, valueSize, value, NULL);
|
| 86 |
printf("\tHardware version: %s\n", value);
|
| 87 |
free(value); |
| 88 |
|
| 89 |
// print software driver version
|
| 90 |
clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, 0, NULL, &valueSize); |
| 91 |
value = (char*) malloc(valueSize);
|
| 92 |
clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, valueSize, value, NULL);
|
| 93 |
printf("\tSoftware version: %s\n", value);
|
| 94 |
free(value); |
| 95 |
|
| 96 |
// print c version supported by compiler for device
|
| 97 |
clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 0, NULL, &valueSize); |
| 98 |
value = (char*) malloc(valueSize);
|
| 99 |
clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, valueSize, value, NULL);
|
| 100 |
printf("\tOpenCL C version: %s\n", value);
|
| 101 |
free(value); |
| 102 |
|
| 103 |
// print parallel compute units
|
| 104 |
clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS, |
| 105 |
sizeof(maxComputeUnits), &maxComputeUnits, NULL); |
| 106 |
printf("\tParallel compute units: %d\n", maxComputeUnits);
|
| 107 |
|
| 108 |
// print max work group size
|
| 109 |
clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_GROUP_SIZE, |
| 110 |
sizeof(maxWorkGroupSize), &maxWorkGroupSize, NULL); |
| 111 |
printf("\tMaximum Work Group Size: %d\n", maxWorkGroupSize);
|
| 112 |
|
| 113 |
// print max work items size
|
| 114 |
clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_ITEM_SIZES, |
| 115 |
sizeof(maxWorkItemSizes), &maxWorkItemSizes, NULL); |
| 116 |
printf("\tMaximum Work Item Sizes: %d\n", maxWorkItemSizes);
|
| 117 |
|
| 118 |
} |
| 119 |
printf("\n");
|
| 120 |
free(devices); |
| 121 |
} |
| 122 |
|
| 123 |
free(platforms); |
| 124 |
return 0; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
const char* OpenCLSource[] = { |
| 129 |
"// Marsaglia RNG very simple implementation \n",
|
| 130 |
"#define znew ((z=36969*(z&65535)+(z>>16))<<16) \n",
|
| 131 |
"#define wnew ((w=18000*(w&65535)+(w>>16))&65535) \n",
|
| 132 |
"#define MWC (znew+wnew) \n",
|
| 133 |
"#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5)) \n",
|
| 134 |
"#define CONG (jcong=69069*jcong+1234567) \n",
|
| 135 |
"#define KISS ((MWC^CONG)+SHR3) \n",
|
| 136 |
"#define MWCfp MWC * 2.328306435454494e-10f \n",
|
| 137 |
"#define KISSfp KISS * 2.328306435454494e-10f \n",
|
| 138 |
"#define CONGfp CONG * 2.328306435454494e-10f \n",
|
| 139 |
"#define SHR3fp SHR3 * 2.328306435454494e-10f \n",
|
| 140 |
"#define THEONE 1.e0f \n",
|
| 141 |
"ulong MainLoop(ulong iterations,uint seed_z,uint seed_w,size_t work)",
|
| 142 |
"{",
|
| 143 |
" uint z=seed_z+work;",
|
| 144 |
" uint w=seed_w+work;",
|
| 145 |
" ulong total=0;",
|
| 146 |
" for (ulong i=0;i<iterations;i++)",
|
| 147 |
"{",
|
| 148 |
" float x=(float)MWCfp ;",
|
| 149 |
" float y=(float)MWCfp ;",
|
| 150 |
" ulong inside=((x*x+y*y) <= THEONE) ? 1:0;",
|
| 151 |
" total+=inside;",
|
| 152 |
"}",
|
| 153 |
" return(total);",
|
| 154 |
"}",
|
| 155 |
"__kernel void MainLoopGlobal(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)",
|
| 156 |
"{",
|
| 157 |
" ulong total=MainLoop(iterations,seed_z,seed_w,get_global_id(0));",
|
| 158 |
" barrier(CLK_GLOBAL_MEM_FENCE);",
|
| 159 |
" s[get_global_id(0)]=(ulong)total;",
|
| 160 |
"}",
|
| 161 |
"__kernel void MainLoopLocal(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)",
|
| 162 |
"{",
|
| 163 |
" ulong total=2;",
|
| 164 |
" barrier(CLK_LOCAL_MEM_FENCE);",
|
| 165 |
" s[get_local_id(0)]=total;",
|
| 166 |
"}",
|
| 167 |
"__kernel void MainLoopHybrid(__global ulong *s,ulong iterations,uint seed_w,uint seed_z)",
|
| 168 |
"{",
|
| 169 |
" ulong total=3;",
|
| 170 |
" barrier(CLK_GLOBAL_MEM_FENCE || CLK_LOCAL_MEM_FENCE);",
|
| 171 |
" s[get_global_id(0)]=total;",
|
| 172 |
"}"
|
| 173 |
}; |
| 174 |
|
| 175 |
int main(int argc, char **argv) |
| 176 |
{
|
| 177 |
if ((argc==1)|| |
| 178 |
(strcmp(argv[1],"-h")==0)|| |
| 179 |
(strcmp(argv[1],"--help")==0)) |
| 180 |
{
|
| 181 |
printf("\nPerforms a Pi estimation by Dart Dash:\n\n"
|
| 182 |
"\t#1 OpenCL Plateform ID\n"
|
| 183 |
"\t#2 OpenCL Device ID\n"
|
| 184 |
"\t#3 Minimal number of iterations\n"
|
| 185 |
"\t#4 Parallel Rate\n"
|
| 186 |
"\t#5 Loops\n\n");
|
| 187 |
DetectOpenCLDevices(); |
| 188 |
} |
| 189 |
else
|
| 190 |
{
|
| 191 |
|
| 192 |
int MyPlatform=atoi(argv[1]); |
| 193 |
int MyDevice=atoi(argv[2]); |
| 194 |
|
| 195 |
struct timeval tv1,tv2;
|
| 196 |
struct timezone tz;
|
| 197 |
|
| 198 |
uint64_t Iterations=1000000;
|
| 199 |
if (argc>3) { |
| 200 |
Iterations=(uint64_t)atoll(argv[3]);
|
| 201 |
} |
| 202 |
|
| 203 |
uint32_t ParallelRate=64;
|
| 204 |
if (argc>4) { |
| 205 |
ParallelRate=(uint32_t)atoi(argv[4]);
|
| 206 |
} |
| 207 |
|
| 208 |
uint32_t Loops=1;
|
| 209 |
if (argc>5) { |
| 210 |
Loops=(uint32_t)atoi(argv[5]);
|
| 211 |
} |
| 212 |
|
| 213 |
cl_int err; |
| 214 |
cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 }; |
| 215 |
|
| 216 |
// Detect, scan, get & initialize platform and device
|
| 217 |
cl_uint platformCount; |
| 218 |
cl_platform_id* platforms; |
| 219 |
cl_uint deviceCount; |
| 220 |
cl_device_id* devices; |
| 221 |
size_t valueSize; |
| 222 |
|
| 223 |
/* Setup OpenCL environment. */
|
| 224 |
|
| 225 |
// Get all platforms
|
| 226 |
err = clGetPlatformIDs(0, NULL, &platformCount); |
| 227 |
platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
|
| 228 |
err = clGetPlatformIDs(platformCount, platforms, NULL);
|
| 229 |
|
| 230 |
// Get Device defined
|
| 231 |
err = clGetDeviceIDs(platforms[MyPlatform], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount); |
| 232 |
devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
|
| 233 |
err = clGetDeviceIDs(platforms[MyPlatform], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL);
|
| 234 |
|
| 235 |
// print device name
|
| 236 |
err = clGetDeviceInfo(devices[MyDevice], CL_DEVICE_NAME, 0, NULL, &valueSize); |
| 237 |
char* deviceName=(char*)malloc(valueSize); |
| 238 |
err = clGetDeviceInfo(devices[MyDevice], CL_DEVICE_NAME, valueSize, deviceName, NULL);
|
| 239 |
err = clGetDeviceInfo(devices[MyDevice], CL_DEVICE_VENDOR, 0, NULL, &valueSize); |
| 240 |
char* vendorName=(char*)malloc(valueSize); |
| 241 |
err = clGetDeviceInfo(devices[MyDevice], CL_DEVICE_VENDOR, valueSize, vendorName, NULL);
|
| 242 |
printf("\nDevice (%d,%d):\n\t- vendor: %s\n\t- device: %s\n\n",MyPlatform,MyDevice, vendorName,deviceName);
|
| 243 |
free(deviceName); |
| 244 |
free(vendorName); |
| 245 |
|
| 246 |
props[1] = (cl_context_properties)platforms[MyPlatform];
|
| 247 |
|
| 248 |
cl_context GPUContext = clCreateContext(props, 1, &devices[MyDevice], NULL, NULL, &err); |
| 249 |
cl_command_queue cqCommandQueue = clCreateCommandQueue(GPUContext,devices[MyDevice], 0, &err);
|
| 250 |
|
| 251 |
cl_mem GPUInside = clCreateBuffer(GPUContext, CL_MEM_WRITE_ONLY, |
| 252 |
sizeof(uint64_t) * ParallelRate, NULL, NULL); |
| 253 |
|
| 254 |
// 51 is the number of line for OpenCL code
|
| 255 |
cl_program OpenCLProgram = clCreateProgramWithSource(GPUContext, 44,OpenCLSource,NULL,NULL); |
| 256 |
clBuildProgram(OpenCLProgram, 0, NULL, NULL, NULL, NULL); |
| 257 |
cl_kernel OpenCLMainLoopGlobal = clCreateKernel(OpenCLProgram, "MainLoopGlobal", NULL); |
| 258 |
cl_kernel OpenCLMainLoopLocal = clCreateKernel(OpenCLProgram, "MainLoopLocal", NULL); |
| 259 |
cl_kernel OpenCLMainLoopHybrid = clCreateKernel(OpenCLProgram, "MainLoopHybrid", NULL); |
| 260 |
|
| 261 |
// Divide the total number of iterations by the parallel rate
|
| 262 |
// Add +1 to the number of per work iterations if division not integer
|
| 263 |
uint64_t IterationsEach=((Iterations%ParallelRate)==0)?Iterations/ParallelRate:Iterations/ParallelRate+1; |
| 264 |
// Initialize seeds for MWC RNG generator from Marsaglia
|
| 265 |
uint32_t seed_w=110271;
|
| 266 |
uint32_t seed_z=101008;
|
| 267 |
|
| 268 |
// Set the values of arguments for OpenCL function call
|
| 269 |
clSetKernelArg(OpenCLMainLoopGlobal, 0, sizeof(cl_mem),&GPUInside); |
| 270 |
clSetKernelArg(OpenCLMainLoopGlobal, 1, sizeof(uint64_t),&IterationsEach); |
| 271 |
clSetKernelArg(OpenCLMainLoopGlobal, 2, sizeof(uint32_t),&seed_w); |
| 272 |
clSetKernelArg(OpenCLMainLoopGlobal, 3, sizeof(uint32_t),&seed_z); |
| 273 |
|
| 274 |
size_t WorkSize[1] = {ParallelRate}; // one dimensional Range |
| 275 |
|
| 276 |
uint64_t HostInside[ParallelRate]; |
| 277 |
|
| 278 |
// Set start timer
|
| 279 |
gettimeofday(&tv1, &tz); |
| 280 |
printf("Loops: ");
|
| 281 |
for (uint32_t loop=0;loop<Loops;loop++) { |
| 282 |
// Execute the OpenCL kernel with datas
|
| 283 |
clEnqueueNDRangeKernel(cqCommandQueue, OpenCLMainLoopGlobal, 1, NULL, |
| 284 |
WorkSize, NULL, 0, NULL, NULL); |
| 285 |
|
| 286 |
clEnqueueReadBuffer(cqCommandQueue, GPUInside, CL_TRUE, 0,
|
| 287 |
ParallelRate * sizeof(uint64_t), HostInside, 0, NULL, NULL); |
| 288 |
printf("%i ",loop);
|
| 289 |
} |
| 290 |
printf("\n\n");
|
| 291 |
|
| 292 |
// Set stop timer
|
| 293 |
gettimeofday(&tv2, &tz); |
| 294 |
|
| 295 |
clReleaseKernel(OpenCLMainLoopGlobal); |
| 296 |
clReleaseProgram(OpenCLProgram); |
| 297 |
clReleaseCommandQueue(cqCommandQueue); |
| 298 |
clReleaseContext(GPUContext); |
| 299 |
clReleaseMemObject(GPUInside); |
| 300 |
|
| 301 |
uint64_t inside=0;
|
| 302 |
for (int i= 0; i < ParallelRate; i++) { |
| 303 |
inside+=HostInside[i]; |
| 304 |
} |
| 305 |
|
| 306 |
double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L + |
| 307 |
(tv2.tv_usec-tv1.tv_usec))/1000000./(double)Loops; |
| 308 |
|
| 309 |
double itops=(double)(ParallelRate*IterationsEach)/elapsed; |
| 310 |
|
| 311 |
printf("Inside/Total %ld %ld\nParallelRate %i\nElapsed Time %.2f\nItops %.0f\nPi estimation %f\n\n",inside,ParallelRate*IterationsEach,ParallelRate,elapsed,itops,(4.*(float)inside/((float)(ParallelRate)*(float)(IterationsEach)))); |
| 312 |
|
| 313 |
return 0; |
| 314 |
} |
| 315 |
} |
| 316 |
|