Created
July 21, 2015 13:20
-
-
Save g2384/184983b0cd21f9450f24 to your computer and use it in GitHub Desktop.
wrapper template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <time.h> | |
using namespace std; | |
#ifdef __APPLE__ | |
#include <OpenCL/opencl.h> | |
#else | |
#include <CL/cl.h> | |
#endif | |
#define MAX_SOURCE_SIZE (0x100000) | |
#define LENGTH 4194304 | |
int main(void){ | |
cl_platform_id platform_id[100]; | |
cl_device_id device_id[100]; | |
cl_uint ret_num_devices; | |
cl_uint ret_num_platforms; | |
cl_event prof_event; | |
cl_int errNum = clGetPlatformIDs(100, platform_id, &ret_num_platforms); | |
for (int i = 0; i<ret_num_platforms; i++) | |
{ | |
char buffer[10240]; | |
cout << " Platform "<< i << endl; | |
//errNum = clGetPlatformInfo(platform_id[i], CL_PLATFORM_PROFILE, 10240, buffer, NULL); | |
//cout << " PROFILE = %s" << buffer << endl; | |
errNum = clGetPlatformInfo(platform_id[i], CL_PLATFORM_VERSION, 10240, buffer, NULL); | |
cout << " VERSION = %s" << buffer << endl; | |
errNum = clGetPlatformInfo(platform_id[i], CL_PLATFORM_NAME, 10240, buffer, NULL); | |
cout << " NAME = %s" << buffer << endl; | |
//errNum = clGetPlatformInfo(platform_id[i], CL_PLATFORM_VENDOR, 10240, buffer, NULL); | |
//cout << " VENDOR = %s" << buffer << endl; | |
//errNum = clGetPlatformInfo(platform_id[i], CL_PLATFORM_EXTENSIONS, 10240, buffer, NULL); | |
//cout << " EXTENSIONS = %s" << buffer << endl; | |
} | |
errNum = clGetDeviceIDs(platform_id[0], CL_DEVICE_TYPE_GPU, 100, device_id, &ret_num_devices); | |
cl_context context = clCreateContext(NULL, 1, device_id, NULL, NULL, &errNum); | |
cl_command_queue command_queue = clCreateCommandQueue(context, device_id[0], CL_QUEUE_PROFILING_ENABLE, &errNum); | |
srand(time(NULL)); | |
for (int i = 0; i < ret_num_devices; i++) | |
{ | |
char buffer[10240]; | |
cl_uint buf_uint; | |
cl_ulong buf_ulong; | |
size_t buf_size[3]; | |
cout << " Device: " << i << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_NAME, sizeof(buffer), buffer, NULL); | |
cout << " DEVICE_NAME = " << buffer << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_VENDOR, sizeof(buffer), buffer, NULL); | |
cout << " DEVICE_VENDOR = " << buffer << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_VERSION, sizeof(buffer), buffer, NULL); | |
cout << " DEVICE_VERSION = " << buffer << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DRIVER_VERSION, sizeof(buffer), buffer, NULL); | |
cout << " DRIVER_VERSION = " << buffer << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(buf_uint), &buf_uint, NULL); | |
cout << " DEVICE_MAX_COMPUTE_UNITS = " << (unsigned int)buf_uint << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(buf_uint), &buf_uint, NULL); | |
cout << " DEVICE_MAX_CLOCK_FREQUENCY =" << (unsigned int)buf_uint << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(buf_ulong), &buf_ulong, NULL); | |
cout << " DEVICE_GLOBAL_MEM_SIZE = " << (unsigned long long)buf_ulong << endl; | |
errNum = clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(buf_size), &buf_size, NULL); | |
cout << " CL_DEVICE_MAX_WORK_ITEM_SIZES = (" << buf_size[0] << ' ' << buf_size[1] << ' ' << buf_size[2] << ')' << endl; | |
} | |
FILE *fp; | |
char *source_str; | |
size_t source_size; | |
fp = fopen("SOAP_test.cl", "r"); | |
if (!fp) { | |
fprintf(stderr, "Failed to load kernel.\n"); | |
exit(1); | |
} | |
source_str = (char*)malloc(MAX_SOURCE_SIZE); | |
source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp); | |
fclose(fp); | |
float *arr_x = (float*)malloc(sizeof(float) * LENGTH); | |
float *arr_y = (float*)malloc(sizeof(float) * LENGTH); | |
float *arr_z = (float*)malloc(sizeof(float) * LENGTH); | |
for (int iter_i = 0; iter_i<LENGTH; iter_i++){ | |
arr_x[iter_i] = iter_i; | |
arr_y[iter_i] = iter_i; | |
arr_z[iter_i] = iter_i; | |
} | |
cl_mem x_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, LENGTH * sizeof(float), NULL, &errNum); | |
cl_mem y_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LENGTH * sizeof(float), NULL, &errNum); | |
cl_mem z_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LENGTH * sizeof(float), NULL, &errNum); | |
errNum = clEnqueueWriteBuffer(command_queue, y_mem_obj, CL_TRUE, 0, LENGTH * sizeof(float), arr_y, 0, NULL, NULL); | |
errNum = clEnqueueWriteBuffer(command_queue, z_mem_obj, CL_TRUE, 0, LENGTH * sizeof(float), arr_z, 0, NULL, NULL); | |
cl_program program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &errNum); | |
errNum = clBuildProgram(program, 1, device_id, NULL, NULL, NULL); | |
cl_kernel kernel = clCreateKernel(program, "Livermorec_hydro_fragment_soap", &errNum); | |
errNum = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&x_mem_obj); | |
errNum = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&y_mem_obj); | |
errNum = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&z_mem_obj); | |
size_t global_size[3] = { 2048, 2048, 1 }; | |
size_t local_size[3] = { 1, 1, 1 }; | |
errNum = clEnqueueNDRangeKernel(command_queue, kernel, 3, NULL, global_size, local_size, 0, NULL, &prof_event); | |
errNum = clEnqueueReadBuffer(command_queue, x_mem_obj, CL_TRUE, 0, LENGTH * sizeof(float), arr_x, 0, NULL, NULL); | |
cl_ulong startTime, endTime; | |
clFinish(command_queue); | |
errNum = clGetEventProfilingInfo(prof_event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &startTime, NULL); | |
errNum = clGetEventProfilingInfo(prof_event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &endTime, NULL); | |
cout << "#pragma SOAP time = " << endTime - startTime << "ns" << endl; | |
errNum = clFlush(command_queue); | |
errNum = clFinish(command_queue); | |
errNum = clReleaseKernel(kernel); | |
errNum = clReleaseProgram(program); | |
errNum = clReleaseCommandQueue(command_queue); | |
errNum = clReleaseContext(context); | |
for (int iter_i = 0; iter_i < LENGTH; iter_i++){ | |
//cout << arr_x[iter_i] << ' ' << iter_i << endl; | |
if (arr_x[iter_i] != iter_i * 2){ | |
cout << arr_x[iter_i] << ' ' << iter_i << endl; | |
iter_i = LENGTH; | |
} | |
} | |
free(arr_x); | |
errNum = clReleaseMemObject(x_mem_obj); | |
free(arr_y); | |
errNum = clReleaseMemObject(y_mem_obj); | |
free(arr_z); | |
errNum = clReleaseMemObject(z_mem_obj); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment