You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.1 KiB
83 lines
2.1 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#ifdef __APPLE__
|
|
#include <OpenCL/opencl.h>
|
|
#else
|
|
#include <CL/cl.h>
|
|
#endif
|
|
|
|
#define KERNEL "part8.cl"
|
|
|
|
void cpu_3d_loop (int x, int y, int z) {
|
|
for (int i = 0; i < x; i++) {
|
|
for (int j = 0; j < y; j++) {
|
|
for (int k = 0; k < z; k++) {
|
|
printf("CPU %d,%d,%d\n", i, j, k);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
|
|
cl_platform_id platform; cl_device_id device; cl_context context;
|
|
cl_program program; cl_kernel kernel; cl_command_queue queue;
|
|
cl_mem kernelBuffer;
|
|
|
|
FILE* programHandle; char *programBuffer; char *programLog;
|
|
size_t programSize; char hostBuffer[32];
|
|
|
|
int x = 4;
|
|
int y = 3;
|
|
int z = 2;
|
|
|
|
cpu_3d_loop(x, y, z);
|
|
|
|
// get first available sdk and gpu and create context
|
|
clGetPlatformIDs(1, &platform, NULL);
|
|
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
|
|
context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);
|
|
|
|
// get size of kernel source
|
|
programHandle = fopen(KERNEL, "r");
|
|
fseek(programHandle, 0, SEEK_END);
|
|
programSize = ftell(programHandle);
|
|
rewind(programHandle);
|
|
|
|
// read kernel source into buffer
|
|
programBuffer = (char*) malloc(programSize + 1);
|
|
programBuffer[programSize] = '\0';
|
|
assert (programSize == fread(programBuffer, sizeof(char), programSize, programHandle));
|
|
|
|
fclose(programHandle);
|
|
|
|
// create and build program
|
|
program = clCreateProgramWithSource(context, 1,
|
|
(const char**) &programBuffer, &programSize, NULL);
|
|
free(programBuffer);
|
|
clBuildProgram(program, 1, &device, "-Werror -cl-std=CL1.1", NULL, NULL);
|
|
|
|
// create kernel and command queue
|
|
kernel = clCreateKernel(program, "ndrange_parallelism", NULL);
|
|
queue = clCreateCommandQueue(context, device, 0, NULL);
|
|
|
|
size_t globalWorkSize[3] = {x, y, z};
|
|
|
|
// execute kernel, read back the output and print to screen
|
|
clEnqueueNDRangeKernel(
|
|
queue, kernel, 3, NULL, globalWorkSize, NULL, 0, NULL, NULL);
|
|
|
|
clFlush(queue);
|
|
clFinish(queue);
|
|
clReleaseKernel(kernel);
|
|
clReleaseProgram(program);
|
|
clReleaseMemObject(kernelBuffer);
|
|
clReleaseCommandQueue(queue);
|
|
clReleaseContext(context);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// vim: set ft=c ts=4 sw=4:
|