| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include <stdio.h>
- #include <stdlib.h>
-
- #ifdef __APPLE__
- #include <OpenCL/opencl.h>
- #else
- #include <CL/cl.h>
- #endif
-
- #define MAX_SOURCE_SIZE (0x100000)
-
- const char *clErrorString(cl_int error)
- {
- switch(error){
- // run-time and JIT compiler errors
- case 0: return "CL_SUCCESS";
- case -1: return "CL_DEVICE_NOT_FOUND";
- case -2: return "CL_DEVICE_NOT_AVAILABLE";
- case -3: return "CL_COMPILER_NOT_AVAILABLE";
- case -4: return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
- case -5: return "CL_OUT_OF_RESOURCES";
- case -6: return "CL_OUT_OF_HOST_MEMORY";
- case -7: return "CL_PROFILING_INFO_NOT_AVAILABLE";
- case -8: return "CL_MEM_COPY_OVERLAP";
- case -9: return "CL_IMAGE_FORMAT_MISMATCH";
- case -10: return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
- case -11: return "CL_BUILD_PROGRAM_FAILURE";
- case -12: return "CL_MAP_FAILURE";
- case -13: return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
- case -14: return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
- case -15: return "CL_COMPILE_PROGRAM_FAILURE";
- case -16: return "CL_LINKER_NOT_AVAILABLE";
- case -17: return "CL_LINK_PROGRAM_FAILURE";
- case -18: return "CL_DEVICE_PARTITION_FAILED";
- case -19: return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
-
- // compile-time errors
- case -30: return "CL_INVALID_VALUE";
- case -31: return "CL_INVALID_DEVICE_TYPE";
- case -32: return "CL_INVALID_PLATFORM";
- case -33: return "CL_INVALID_DEVICE";
- case -34: return "CL_INVALID_CONTEXT";
- case -35: return "CL_INVALID_QUEUE_PROPERTIES";
- case -36: return "CL_INVALID_COMMAND_QUEUE";
- case -37: return "CL_INVALID_HOST_PTR";
- case -38: return "CL_INVALID_MEM_OBJECT";
- case -39: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
- case -40: return "CL_INVALID_IMAGE_SIZE";
- case -41: return "CL_INVALID_SAMPLER";
- case -42: return "CL_INVALID_BINARY";
- case -43: return "CL_INVALID_BUILD_OPTIONS";
- case -44: return "CL_INVALID_PROGRAM";
- case -45: return "CL_INVALID_PROGRAM_EXECUTABLE";
- case -46: return "CL_INVALID_KERNEL_NAME";
- case -47: return "CL_INVALID_KERNEL_DEFINITION";
- case -48: return "CL_INVALID_KERNEL";
- case -49: return "CL_INVALID_ARG_INDEX";
- case -50: return "CL_INVALID_ARG_VALUE";
- case -51: return "CL_INVALID_ARG_SIZE";
- case -52: return "CL_INVALID_KERNEL_ARGS";
- case -53: return "CL_INVALID_WORK_DIMENSION";
- case -54: return "CL_INVALID_WORK_GROUP_SIZE";
- case -55: return "CL_INVALID_WORK_ITEM_SIZE";
- case -56: return "CL_INVALID_GLOBAL_OFFSET";
- case -57: return "CL_INVALID_EVENT_WAIT_LIST";
- case -58: return "CL_INVALID_EVENT";
- case -59: return "CL_INVALID_OPERATION";
- case -60: return "CL_INVALID_GL_OBJECT";
- case -61: return "CL_INVALID_BUFFER_SIZE";
- case -62: return "CL_INVALID_MIP_LEVEL";
- case -63: return "CL_INVALID_GLOBAL_WORK_SIZE";
- case -64: return "CL_INVALID_PROPERTY";
- case -65: return "CL_INVALID_IMAGE_DESCRIPTOR";
- case -66: return "CL_INVALID_COMPILER_OPTIONS";
- case -67: return "CL_INVALID_LINKER_OPTIONS";
- case -68: return "CL_INVALID_DEVICE_PARTITION_COUNT";
-
- // extension errors
- case -1000: return "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR";
- case -1001: return "CL_PLATFORM_NOT_FOUND_KHR";
- case -1002: return "CL_INVALID_D3D10_DEVICE_KHR";
- case -1003: return "CL_INVALID_D3D10_RESOURCE_KHR";
- case -1004: return "CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR";
- case -1005: return "CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR";
- default: return "Unknown OpenCL error";
- }
- }
-
- void debug(int ret) {
- if (ret != CL_SUCCESS) {
- printf(clErrorString(ret));
- printf("\n");
- }
- }
-
- int main(void) {
- printf("start\n");
-
- // Create the two input vectors
- int i;
- const int LIST_SIZE = 1024;
- int *A = (int*)malloc(sizeof(int)*LIST_SIZE);
- int *B = (int*)malloc(sizeof(int)*LIST_SIZE);
- for(i = 0; i < LIST_SIZE; i++) {
- A[i] = i;
- B[i] = LIST_SIZE - i;
- }
-
- // Load the kernel source code into the array source_str
- FILE *fp;
- char *source_str;
- size_t source_size;
-
- fp = fopen("vecadd4.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 );
-
- // Get platform and device information
- cl_device_id device_id = NULL;
- cl_uint ret_num_devices;
- cl_uint ret_num_platforms;
-
-
- cl_int ret = clGetPlatformIDs(0, NULL, &ret_num_platforms);
- debug(ret);
- cl_platform_id *platforms = NULL;
- platforms = (cl_platform_id*)malloc(ret_num_platforms*sizeof(cl_platform_id));
- ret = clGetPlatformIDs(ret_num_platforms, platforms, NULL);
- printf("ret at clGetPlatformIDs (%d) is %d\n", __LINE__, ret);
- debug(ret);
- ret = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_ALL, 1, &device_id, &ret_num_devices);
- printf("ret at clGetDeviceIDs (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Create an OpenCL context
- cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
- printf("ret at clCreateContext (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Create a command queue
- cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
- printf("ret at clCreateCommandQueue (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Create memory buffers on the device for each vector
- cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
- cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
- cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
-
- // Copy the lists A and B to their respective memory buffers
- ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), A, 0, NULL, NULL);
- printf("ret at clEnqueueWriteBuffer (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- ret = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), B, 0, NULL, NULL);
- printf("ret at clEnqueueWriteBuffer (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Create a program from the kernel source
- cl_program program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);
- printf("ret at clCreateProgramWithSource (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Build the program
- ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
- printf("ret at clBuildProgram (%d) is %d\n", __LINE__, ret);
- if (ret != CL_SUCCESS) {
- printf(clErrorString(ret));
- printf("\n");
- cl_build_status status;
- char * log;
- size_t log_size;
- clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
- log = (char*)malloc(log_size+1);
- clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
- log[log_size-1]=0;
- printf(log);
- free(log);
- }
-
-
- // Create the OpenCL kernel
- // __kernel void sha256_crypt_kernel(__global uint *data_info,__global char *plain_key, __global uint *digest) {
- // cl_kernel kernel = clCreateKernel(program, "sha256_crypt_kernel", &ret);
- cl_kernel kernel = clCreateKernel(program, "vector_add", &ret);
- printf("ret at clCreateKernel (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Set the arguments of the kernel
- ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
- printf("ret at clSetKernelArg (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
- printf("ret at clSetKernelArg (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
- printf("ret at clSetKernelArg (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- //added this to fix garbage output problem
- //ret = clSetKernelArg(kernel, 3, sizeof(int), &LIST_SIZE);
-
- // Execute the OpenCL kernel on the list
- size_t global_item_size = LIST_SIZE; // Process the entire lists
- size_t local_item_size = 8; // Divide work items into groups of 8 (12 ideally, but 1024 isn't)
- ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);
- printf("ret at clEnqueueNDRangeKernel (%d) is %d\n", __LINE__, ret);
- if (ret!=CL_SUCCESS) {
- printf(clErrorString(ret));
- printf("\n");
- }
- // Read the memory buffer C on the device to the local variable C
- int *C = (int*)malloc(sizeof(int)*LIST_SIZE);
- ret = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), C, 0, NULL, NULL);
- printf("ret at clEnqueueReadBuffer (%d) is %d\n", __LINE__, ret);
- debug(ret);
-
- // Display the result to the screen
- for(i = 0; i < 8; i++)
- printf("%d + %d = %d\n", A[i], B[i], C[i]);
-
- // Clean up
- ret = clFlush(command_queue);
- debug(ret);
- ret = clFinish(command_queue);
- debug(ret);
- ret = clReleaseKernel(kernel);
- debug(ret);
- ret = clReleaseProgram(program);
- debug(ret);
- ret = clReleaseMemObject(a_mem_obj);
- debug(ret);
- ret = clReleaseMemObject(b_mem_obj);
- debug(ret);
- ret = clReleaseMemObject(c_mem_obj);
- debug(ret);
- ret = clReleaseCommandQueue(command_queue);
- debug(ret);
- ret = clReleaseContext(context);
- debug(ret);
-
- free(A);
- free(B);
- free(C);
- return 0;
- }
|