Sin descripción

vecadd3.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #ifdef __APPLE__
  4. #include <OpenCL/opencl.h>
  5. #else
  6. #include <CL/cl.h>
  7. #endif
  8. #define MAX_SOURCE_SIZE (0x100000)
  9. int main(void) {
  10. printf("started running\n");
  11. // Create the two input vectors
  12. int i;
  13. const int LIST_SIZE = 1024;
  14. int *A = (int*)malloc(sizeof(int)*LIST_SIZE);
  15. int *B = (int*)malloc(sizeof(int)*LIST_SIZE);
  16. for(i = 0; i < LIST_SIZE; i++) {
  17. A[i] = i;
  18. B[i] = LIST_SIZE - i;
  19. }
  20. // Load the kernel source code into the array source_str
  21. FILE *fp;
  22. char *source_str;
  23. size_t source_size;
  24. fp = fopen("vecadd3.cl", "r");
  25. if (!fp) {
  26. fprintf(stderr, "Failed to load kernel.\n");
  27. exit(1);
  28. }
  29. source_str = (char*)malloc(MAX_SOURCE_SIZE);
  30. source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);
  31. fclose( fp );
  32. printf("kernel loading done\n");
  33. // Get platform and device information
  34. cl_device_id device_id = NULL;
  35. cl_uint ret_num_devices;
  36. cl_uint ret_num_platforms;
  37. cl_int ret = clGetPlatformIDs(0, NULL, &ret_num_platforms);
  38. cl_platform_id *platforms = NULL;
  39. platforms = (cl_platform_id*)malloc(ret_num_platforms*sizeof(cl_platform_id));
  40. ret = clGetPlatformIDs(ret_num_platforms, platforms, NULL);
  41. printf("ret at %d is %d\n", __LINE__, ret);
  42. ret = clGetDeviceIDs( platforms[0], CL_DEVICE_TYPE_ALL, 1, &device_id, &ret_num_devices);
  43. printf("ret at %d is %d\n", __LINE__, ret);
  44. // Create an OpenCL context
  45. cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
  46. printf("ret at %d is %d\n", __LINE__, ret);
  47. // Create a command queue
  48. cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
  49. printf("ret at %d is %d\n", __LINE__, ret);
  50. // Create memory buffers on the device for each vector
  51. cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
  52. cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
  53. cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY, LIST_SIZE * sizeof(int), NULL, &ret);
  54. // Copy the lists A and B to their respective memory buffers
  55. ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), A, 0, NULL, NULL);
  56. printf("ret at %d is %d\n", __LINE__, ret);
  57. ret = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), B, 0, NULL, NULL);
  58. printf("ret at %d is %d\n", __LINE__, ret);
  59. printf("before building\n");
  60. // Create a program from the kernel source
  61. cl_program program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);
  62. printf("ret at %d is %d\n", __LINE__, ret);
  63. // Build the program
  64. ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
  65. printf("ret at %d is %d\n", __LINE__, ret);
  66. printf("after building\n");
  67. // Create the OpenCL kernel
  68. cl_kernel kernel = clCreateKernel(program, "vector_add", &ret);
  69. printf("ret at %d is %d\n", __LINE__, ret);
  70. // Set the arguments of the kernel
  71. ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_mem_obj);
  72. printf("ret at %d is %d\n", __LINE__, ret);
  73. ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_mem_obj);
  74. printf("ret at %d is %d\n", __LINE__, ret);
  75. ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&c_mem_obj);
  76. printf("ret at %d is %d\n", __LINE__, ret);
  77. //added this to fix garbage output problem
  78. //ret = clSetKernelArg(kernel, 3, sizeof(int), &LIST_SIZE);
  79. printf("before execution\n");
  80. // Execute the OpenCL kernel on the list
  81. size_t global_item_size = LIST_SIZE; // Process the entire lists
  82. size_t local_item_size = 12; // Divide work items into groups of 12
  83. ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);
  84. printf("after execution\n");
  85. // Read the memory buffer C on the device to the local variable C
  86. int *C = (int*)malloc(sizeof(int)*LIST_SIZE);
  87. ret = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0, LIST_SIZE * sizeof(int), C, 0, NULL, NULL);
  88. printf("after copying\n");
  89. // Display the result to the screen
  90. for(i = 0; i < 8; i++)
  91. printf("%d + %d = %d\n", A[i], B[i], C[i]);
  92. // Clean up
  93. ret = clFlush(command_queue);
  94. ret = clFinish(command_queue);
  95. ret = clReleaseKernel(kernel);
  96. ret = clReleaseProgram(program);
  97. ret = clReleaseMemObject(a_mem_obj);
  98. ret = clReleaseMemObject(b_mem_obj);
  99. ret = clReleaseMemObject(c_mem_obj);
  100. ret = clReleaseCommandQueue(command_queue);
  101. ret = clReleaseContext(context);
  102. free(A);
  103. free(B);
  104. free(C);
  105. return 0;
  106. }