hotspot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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>\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. exit(1);
  106. }
  107. int main(int argc, char** argv) {
  108. printf("WG size of kernel = %d X %d\n", BLOCK_SIZE, BLOCK_SIZE);
  109. cl_int error;
  110. cl_uint num_platforms;
  111. // Get the number of platforms
  112. error = clGetPlatformIDs(0, NULL, &num_platforms);
  113. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  114. // Get the list of platforms
  115. cl_platform_id* platforms = (cl_platform_id *) malloc(sizeof(cl_platform_id) * num_platforms);
  116. error = clGetPlatformIDs(num_platforms, platforms, NULL);
  117. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  118. // Print the chosen platform (if there are multiple platforms, choose the first one)
  119. cl_platform_id platform = platforms[1];
  120. char pbuf[100];
  121. error = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL);
  122. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  123. printf("Platform: %s\n", pbuf);
  124. // Create a GPU context
  125. cl_context_properties context_properties[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform, 0};
  126. context = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU, NULL, NULL, &error);
  127. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  128. // Get and print the chosen device (if there are multiple devices, choose the first one)
  129. size_t devices_size;
  130. error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &devices_size);
  131. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  132. cl_device_id *devices = (cl_device_id *) malloc(devices_size);
  133. error = clGetContextInfo(context, CL_CONTEXT_DEVICES, devices_size, devices, NULL);
  134. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  135. device = devices[0];
  136. error = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(pbuf), pbuf, NULL);
  137. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  138. printf("Device: %s\n", pbuf);
  139. // Create a command queue
  140. command_queue = clCreateCommandQueue(context, device, 0, &error);
  141. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  142. int size;
  143. int grid_rows,grid_cols = 0;
  144. float *FilesavingTemp,*FilesavingPower; //,*MatrixOut;
  145. char *tfile, *pfile, *ofile;
  146. int total_iterations = 60;
  147. int pyramid_height = 1; // number of iterations
  148. if (argc < 7)
  149. usage(argc, argv);
  150. if((grid_rows = atoi(argv[1]))<=0||
  151. (grid_cols = atoi(argv[1]))<=0||
  152. (pyramid_height = atoi(argv[2]))<=0||
  153. (total_iterations = atoi(argv[3]))<=0)
  154. usage(argc, argv);
  155. tfile=argv[4];
  156. pfile=argv[5];
  157. ofile=argv[6];
  158. size=grid_rows*grid_cols;
  159. // --------------- pyramid parameters ---------------
  160. int borderCols = (pyramid_height)*EXPAND_RATE/2;
  161. int borderRows = (pyramid_height)*EXPAND_RATE/2;
  162. int smallBlockCol = BLOCK_SIZE-(pyramid_height)*EXPAND_RATE;
  163. int smallBlockRow = BLOCK_SIZE-(pyramid_height)*EXPAND_RATE;
  164. int blockCols = grid_cols/smallBlockCol+((grid_cols%smallBlockCol==0)?0:1);
  165. int blockRows = grid_rows/smallBlockRow+((grid_rows%smallBlockRow==0)?0:1);
  166. FilesavingTemp = (float *) malloc(size*sizeof(float));
  167. FilesavingPower = (float *) malloc(size*sizeof(float));
  168. // MatrixOut = (float *) calloc (size, sizeof(float));
  169. if( !FilesavingPower || !FilesavingTemp) // || !MatrixOut)
  170. fatal("unable to allocate memory");
  171. // Read input data from disk
  172. readinput(FilesavingTemp, grid_rows, grid_cols, tfile);
  173. readinput(FilesavingPower, grid_rows, grid_cols, pfile);
  174. // Load kernel source from file
  175. const char *source = load_kernel_source("hotspot_kernel.cl");
  176. size_t sourceSize = strlen(source);
  177. // Compile the kernel
  178. cl_program program = clCreateProgramWithSource(context, 1, &source, &sourceSize, &error);
  179. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  180. char clOptions[110];
  181. // sprintf(clOptions,"-I../../src");
  182. sprintf(clOptions," ");
  183. #ifdef BLOCK_SIZE
  184. sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
  185. #endif
  186. // Create an executable from the kernel
  187. error = clBuildProgram(program, 1, &device, clOptions, NULL, NULL);
  188. // Show compiler warnings/errors
  189. static char log[65536]; memset(log, 0, sizeof(log));
  190. clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
  191. if (strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
  192. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  193. kernel = clCreateKernel(program, "hotspot", &error);
  194. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  195. long long start_time = get_time();
  196. // Create two temperature matrices and copy the temperature input data
  197. cl_mem MatrixTemp[2];
  198. // Create input memory buffers on device
  199. MatrixTemp[0] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingTemp, &error);
  200. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  201. // Lingjie Zhang modifited at Nov 1, 2015
  202. //MatrixTemp[1] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(float) * size, NULL, &error);
  203. MatrixTemp[1] = clCreateBuffer(context, CL_MEM_READ_WRITE , sizeof(float) * size, NULL, &error);
  204. // end Lingjie Zhang modification
  205. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  206. // Copy the power input data
  207. cl_mem MatrixPower = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingPower, &error);
  208. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  209. // Perform the computation
  210. int ret = compute_tran_temp(MatrixPower, MatrixTemp, grid_cols, grid_rows, total_iterations, pyramid_height,
  211. blockCols, blockRows, borderCols, borderRows, FilesavingTemp, FilesavingPower);
  212. // Copy final temperature data back
  213. cl_float *MatrixOut = (cl_float *) clEnqueueMapBuffer(command_queue, MatrixTemp[ret], CL_TRUE, CL_MAP_READ, 0, sizeof(float) * size, 0, NULL, NULL, &error);
  214. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  215. long long end_time = get_time();
  216. printf("Total time: %.3f seconds\n", ((float) (end_time - start_time)) / (1000*1000));
  217. // Write final output to output file
  218. writeoutput(MatrixOut, grid_rows, grid_cols, ofile);
  219. error = clEnqueueUnmapMemObject(command_queue, MatrixTemp[ret], (void *) MatrixOut, 0, NULL, NULL);
  220. if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
  221. clReleaseMemObject(MatrixTemp[0]);
  222. clReleaseMemObject(MatrixTemp[1]);
  223. clReleaseMemObject(MatrixPower);
  224. clReleaseContext(context);
  225. return 0;
  226. }