hotspot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "hotspot.h"
  2. void writeoutput(float *vect, int grid_rows, int grid_cols, char *file) {
  3. int i,j, index=0;
  4. FILE *fp;
  5. char str[STR_SIZE];
  6. if( (fp = fopen(file, "w" )) == 0 )
  7. printf( "The file was not opened\n" );
  8. for (i=0; i < grid_rows; i++)
  9. for (j=0; j < grid_cols; j++)
  10. {
  11. sprintf(str, "%d\t%g\n", index, vect[i*grid_cols+j]);
  12. fputs(str,fp);
  13. index++;
  14. }
  15. fclose(fp);
  16. }
  17. void readinput(float *vect, int grid_rows, int grid_cols, char *file) {
  18. int i,j;
  19. FILE *fp;
  20. char str[STR_SIZE];
  21. float val;
  22. if( (fp = fopen(file, "r" )) ==0 )
  23. fatal( "The file was not opened" );
  24. for (i=0; i <= grid_rows-1; i++)
  25. for (j=0; j <= grid_cols-1; j++)
  26. {
  27. if (fgets(str, STR_SIZE, fp) == NULL) fatal("Error reading file\n");
  28. if (feof(fp))
  29. fatal("not enough lines in file");
  30. //if ((sscanf(str, "%d%f", &index, &val) != 2) || (index != ((i-1)*(grid_cols-2)+j-1)))
  31. if ((sscanf(str, "%f", &val) != 1))
  32. fatal("invalid file format");
  33. vect[i*grid_cols+j] = val;
  34. }
  35. fclose(fp);
  36. }
  37. /*
  38. compute N time steps
  39. */
  40. int compute_tran_temp(cl_mem MatrixPower, cl_mem MatrixTemp[2], int col, int row, \
  41. int total_iterations, int num_iterations, int blockCols, int blockRows, int borderCols, int borderRows,
  42. float *TempCPU, float *PowerCPU)
  43. {
  44. float grid_height = chip_height / row;
  45. float grid_width = chip_width / col;
  46. float Cap = FACTOR_CHIP * SPEC_HEAT_SI * t_chip * grid_width * grid_height;
  47. float Rx = grid_width / (2.0 * K_SI * t_chip * grid_height);
  48. float Ry = grid_height / (2.0 * K_SI * t_chip * grid_width);
  49. float Rz = t_chip / (K_SI * grid_height * grid_width);
  50. float max_slope = MAX_PD / (FACTOR_CHIP * t_chip * SPEC_HEAT_SI);
  51. float step = PRECISION / max_slope;
  52. int t;
  53. int src = 0, dst = 1;
  54. cl_int error;
  55. // Determine GPU work group grid
  56. size_t global_work_size[2];
  57. global_work_size[0] = BLOCK_SIZE * blockCols;
  58. global_work_size[1] = BLOCK_SIZE * blockRows;
  59. size_t local_work_size[2];
  60. local_work_size[0] = BLOCK_SIZE;
  61. local_work_size[1] = BLOCK_SIZE;
  62. long long start_time = get_time();
  63. for (t = 0; t < total_iterations; t += num_iterations) {
  64. // Specify kernel arguments
  65. int iter = MIN(num_iterations, total_iterations - t);
  66. clSetKernelArg(kernel, 0, sizeof(int), (void *) &iter);
  67. clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &MatrixPower);
  68. clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &MatrixTemp[src]);
  69. clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &MatrixTemp[dst]);
  70. clSetKernelArg(kernel, 4, sizeof(int), (void *) &col);
  71. clSetKernelArg(kernel, 5, sizeof(int), (void *) &row);
  72. clSetKernelArg(kernel, 6, sizeof(int), (void *) &borderCols);
  73. clSetKernelArg(kernel, 7, sizeof(int), (void *) &borderRows);
  74. clSetKernelArg(kernel, 8, sizeof(float), (void *) &Cap);
  75. clSetKernelArg(kernel, 9, sizeof(float), (void *) &Rx);
  76. clSetKernelArg(kernel, 10, sizeof(float), (void *) &Ry);
  77. clSetKernelArg(kernel, 11, sizeof(float), (void *) &Rz);
  78. clSetKernelArg(kernel, 12, sizeof(float), (void *) &step);
  79. // Launch kernel
  80. error = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
  81. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  82. // Flush the queue
  83. error = clFlush(command_queue);
  84. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  85. // Swap input and output GPU matrices
  86. src = 1 - src;
  87. dst = 1 - dst;
  88. }
  89. // Wait for all operations to finish
  90. error = clFinish(command_queue);
  91. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  92. long long end_time = get_time();
  93. long long total_time = (end_time - start_time);
  94. printf("\nKernel time: %.3f seconds\n", ((float) total_time) / (1000*1000));
  95. return src;
  96. }
  97. void usage(int argc, char **argv) {
  98. fprintf(stderr, "Usage: %s <grid_rows/grid_cols> <pyramid_height> <sim_time> <temp_file> <power_file> <output_file> <platform_id> <device_id> <use_gpu>\n", argv[0]);
  99. fprintf(stderr, "\t<grid_rows/grid_cols> - number of rows/cols in the grid (positive integer)\n");
  100. fprintf(stderr, "\t<pyramid_height> - pyramid heigh(positive integer)\n");
  101. fprintf(stderr, "\t<sim_time> - number of iterations\n");
  102. fprintf(stderr, "\t<temp_file> - name of the file containing the initial temperature values of each cell\n");
  103. fprintf(stderr, "\t<power_file> - name of the file containing the dissipated power values of each cell\n");
  104. fprintf(stderr, "\t<output_file> - name of the output file\n");
  105. fprintf(stderr, "\t<platform_id> - the target platform to use\n");
  106. fprintf(stderr, "\t<device_id> - the device to use\n");
  107. fprintf(stderr, "\t<use_gpu> - 1 for GPU, 0 for CPU\n");
  108. exit(1);
  109. }
  110. int main(int argc, char** argv) {
  111. printf("WG size of kernel = %d X %d\n", BLOCK_SIZE, BLOCK_SIZE);
  112. // Command line arguments "parsing"
  113. int size;
  114. int grid_rows,grid_cols = 0;
  115. float *FilesavingTemp,*FilesavingPower; //,*MatrixOut;
  116. char *tfile, *pfile, *ofile;
  117. int total_iterations = 60;
  118. int pyramid_height = 1; // number of iterations
  119. int platform_id = 0;
  120. int device_id = 0;
  121. int use_gpu = 0;
  122. if (argc < 10)
  123. usage(argc, argv);
  124. if((grid_rows = atoi(argv[1]))<=0||
  125. (grid_cols = atoi(argv[1]))<=0||
  126. (pyramid_height = atoi(argv[2]))<=0||
  127. (total_iterations = atoi(argv[3]))<=0)
  128. usage(argc, argv);
  129. tfile=argv[4];
  130. pfile=argv[5];
  131. ofile=argv[6];
  132. platform_id = atoi(argv[7]);
  133. device_id = atoi(argv[8]);
  134. use_gpu = atoi(argv[9]);
  135. size=grid_rows*grid_cols;
  136. cl_int error;
  137. cl_uint num_platforms;
  138. // Selector for CPU/GPU
  139. cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
  140. // Get the number of platforms
  141. error = clGetPlatformIDs(0, NULL, &num_platforms);
  142. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  143. // Get the list of platforms
  144. cl_platform_id* platforms = (cl_platform_id *) malloc(sizeof(cl_platform_id) * num_platforms);
  145. error = clGetPlatformIDs(num_platforms, platforms, NULL);
  146. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  147. // Print the chosen platform, selected on the basis of the corresponding command line aegument
  148. cl_platform_id platform = platforms[platform_id];
  149. char pbuf[100];
  150. error = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL);
  151. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  152. printf("Platform: %s\n", pbuf);
  153. // Create a GPU context
  154. cl_context_properties context_properties[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform, 0};
  155. context = clCreateContextFromType(context_properties, device_type, NULL, NULL, &error);
  156. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  157. // Get and print the chosen device (if there are multiple devices, choose the first one)
  158. size_t devices_size;
  159. error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &devices_size);
  160. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  161. cl_device_id *devices = (cl_device_id *) malloc(devices_size);
  162. error = clGetContextInfo(context, CL_CONTEXT_DEVICES, devices_size, devices, NULL);
  163. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  164. // Select the device passed as command line argument
  165. device = devices[device_id];
  166. error = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(pbuf), pbuf, NULL);
  167. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  168. printf("Device: %s\n", pbuf);
  169. // Create a command queue
  170. command_queue = clCreateCommandQueue(context, device, 0, &error);
  171. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  172. // --------------- pyramid parameters ---------------
  173. int borderCols = (pyramid_height)*EXPAND_RATE/2;
  174. int borderRows = (pyramid_height)*EXPAND_RATE/2;
  175. int smallBlockCol = BLOCK_SIZE-(pyramid_height)*EXPAND_RATE;
  176. int smallBlockRow = BLOCK_SIZE-(pyramid_height)*EXPAND_RATE;
  177. int blockCols = grid_cols/smallBlockCol+((grid_cols%smallBlockCol==0)?0:1);
  178. int blockRows = grid_rows/smallBlockRow+((grid_rows%smallBlockRow==0)?0:1);
  179. FilesavingTemp = (float *) malloc(size*sizeof(float));
  180. FilesavingPower = (float *) malloc(size*sizeof(float));
  181. // MatrixOut = (float *) calloc (size, sizeof(float));
  182. if( !FilesavingPower || !FilesavingTemp) // || !MatrixOut)
  183. fatal("unable to allocate memory");
  184. // Read input data from disk
  185. readinput(FilesavingTemp, grid_rows, grid_cols, tfile);
  186. readinput(FilesavingPower, grid_rows, grid_cols, pfile);
  187. // Load kernel source from file
  188. const char *source = load_kernel_source("hotspot_kernel.cl");
  189. size_t sourceSize = strlen(source);
  190. // Compile the kernel
  191. cl_program program = clCreateProgramWithSource(context, 1, &source, &sourceSize, &error);
  192. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  193. char clOptions[110];
  194. // sprintf(clOptions,"-I../../src");
  195. sprintf(clOptions," ");
  196. #ifdef BLOCK_SIZE
  197. sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
  198. #endif
  199. // Create an executable from the kernel
  200. error = clBuildProgram(program, 1, &device, clOptions, NULL, NULL);
  201. // Show compiler warnings/errors
  202. static char log[65536]; memset(log, 0, sizeof(log));
  203. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
  204. if (strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
  205. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  206. kernel = clCreateKernel(program, "hotspot", &error);
  207. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  208. long long start_time = get_time();
  209. // Create two temperature matrices and copy the temperature input data
  210. cl_mem MatrixTemp[2];
  211. // Create input memory buffers on device
  212. MatrixTemp[0] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingTemp, &error);
  213. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  214. // Lingjie Zhang modifited at Nov 1, 2015
  215. //MatrixTemp[1] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(float) * size, NULL, &error);
  216. MatrixTemp[1] = clCreateBuffer(context, CL_MEM_READ_WRITE , sizeof(float) * size, NULL, &error);
  217. // end Lingjie Zhang modification
  218. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  219. // Copy the power input data
  220. cl_mem MatrixPower = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingPower, &error);
  221. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  222. // Perform the computation
  223. int ret = compute_tran_temp(MatrixPower, MatrixTemp, grid_cols, grid_rows, total_iterations, pyramid_height,
  224. blockCols, blockRows, borderCols, borderRows, FilesavingTemp, FilesavingPower);
  225. // Copy final temperature data back
  226. cl_float *MatrixOut = (cl_float *) clEnqueueMapBuffer(command_queue, MatrixTemp[ret], CL_TRUE, CL_MAP_READ, 0, sizeof(float) * size, 0, NULL, NULL, &error);
  227. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  228. long long end_time = get_time();
  229. printf("Total time: %.3f seconds\n", ((float) (end_time - start_time)) / (1000*1000));
  230. // Write final output to output file
  231. writeoutput(MatrixOut, grid_rows, grid_cols, ofile);
  232. error = clEnqueueUnmapMemObject(command_queue, MatrixTemp[ret], (void *) MatrixOut, 0, NULL, NULL);
  233. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  234. clReleaseMemObject(MatrixTemp[0]);
  235. clReleaseMemObject(MatrixTemp[1]);
  236. clReleaseMemObject(MatrixPower);
  237. clReleaseContext(context);
  238. return 0;
  239. }