3D.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <CL/cl.h>
  5. #include "CL_helper.h"
  6. #ifndef DEVICE
  7. #define DEVICE CL_DEVICE_TYPE_DEFAULT
  8. #endif
  9. #define TOL (0.001)
  10. #define STR_SIZE (256)
  11. #define MAX_PD (3.0e6)
  12. /* required precision in degrees */
  13. #define PRECISION 0.001
  14. #define SPEC_HEAT_SI 1.75e6
  15. #define K_SI 100
  16. /* capacitance fitting factor */
  17. #define FACTOR_CHIP 0.5
  18. #define WG_SIZE_X (64)
  19. #define WG_SIZE_Y (4)
  20. float t_chip = 0.0005;
  21. float chip_height = 0.016;
  22. float chip_width = 0.016;
  23. float amb_temp = 80.0;
  24. void usage(int argc, char **argv)
  25. {
  26. fprintf(stderr, "Usage: %s <rows/cols> <layers> <iterations> <powerFile> <tempFile> <outputFile>\n", argv[0]);
  27. fprintf(stderr, "\t<rows/cols> - number of rows/cols in the grid (positive integer)\n");
  28. fprintf(stderr, "\t<layers> - number of layers in the grid (positive integer)\n");
  29. fprintf(stderr, "\t<iteration> - number of iterations\n");
  30. fprintf(stderr, "\t<powerFile> - name of the file containing the initial power values of each cell\n");
  31. fprintf(stderr, "\t<tempFile> - name of the file containing the initial temperature values of each cell\n");
  32. fprintf(stderr, "\t<outputFile - output file\n");
  33. exit(1);
  34. }
  35. int main(int argc, char** argv)
  36. {
  37. if (argc != 7)
  38. {
  39. usage(argc,argv);
  40. }
  41. char *pfile, *tfile, *ofile;
  42. int iterations = atoi(argv[3]);
  43. pfile = argv[4];
  44. tfile = argv[5];
  45. ofile = argv[6];
  46. int numCols = atoi(argv[1]);
  47. int numRows = atoi(argv[1]);
  48. int layers = atoi(argv[2]);
  49. /* calculating parameters*/
  50. float dx = chip_height/numRows;
  51. float dy = chip_width/numCols;
  52. float dz = t_chip/layers;
  53. float Cap = FACTOR_CHIP * SPEC_HEAT_SI * t_chip * dx * dy;
  54. float Rx = dy / (2.0 * K_SI * t_chip * dx);
  55. float Ry = dx / (2.0 * K_SI * t_chip * dy);
  56. float Rz = dz / (K_SI * dx * dy);
  57. float max_slope = MAX_PD / (FACTOR_CHIP * t_chip * SPEC_HEAT_SI);
  58. float dt = PRECISION / max_slope;
  59. float ce, cw, cn, cs, ct, cb, cc;
  60. float stepDivCap = dt / Cap;
  61. ce = cw = stepDivCap/ Rx;
  62. cn = cs = stepDivCap/ Ry;
  63. ct = cb = stepDivCap/ Rz;
  64. cc = 1.0 - (2.0*ce + 2.0*cn + 3.0*ct);
  65. int err;
  66. int size = numCols * numRows * layers;
  67. float* tIn = (float*) calloc(size,sizeof(float));
  68. float* pIn = (float*) calloc(size,sizeof(float));
  69. float* tempCopy = (float*)malloc(size * sizeof(float));
  70. float* tempOut = (float*) calloc(size,sizeof(float));
  71. int i = 0;
  72. int count = size;
  73. readinput(tIn,numRows, numCols, layers, tfile);
  74. readinput(pIn,numRows, numCols, layers, pfile);
  75. size_t global[2];
  76. size_t local[2];
  77. memcpy(tempCopy,tIn, size * sizeof(float));
  78. cl_device_id device_id;
  79. cl_context context;
  80. cl_command_queue commands;
  81. cl_program program;
  82. cl_kernel ko_vadd;
  83. cl_mem d_a;
  84. cl_mem d_b;
  85. cl_mem d_c;
  86. const char *KernelSource = load_kernel_source("hotspotKernel.cl");
  87. cl_uint numPlatforms;
  88. err = clGetPlatformIDs(0, NULL, &numPlatforms);
  89. if (err != CL_SUCCESS || numPlatforms <= 0)
  90. {
  91. printf("Error: Failed to find a platform!\n%s\n",err_code(err));
  92. return EXIT_FAILURE;
  93. }
  94. cl_platform_id Platform[numPlatforms];
  95. err = clGetPlatformIDs(numPlatforms, Platform, NULL);
  96. if (err != CL_SUCCESS || numPlatforms <= 0)
  97. {
  98. printf("Error: Failed to get the platform!\n%s\n",err_code(err));
  99. return EXIT_FAILURE;
  100. }
  101. for (i = 0; i < numPlatforms; i++)
  102. {
  103. err = clGetDeviceIDs(Platform[i], DEVICE, 1, &device_id, NULL);
  104. if (err == CL_SUCCESS)
  105. {
  106. break;
  107. }
  108. }
  109. if (device_id == NULL)
  110. {
  111. printf("Error: Failed to create a device group!\n%s\n",err_code(err));
  112. return EXIT_FAILURE;
  113. }
  114. err = output_device_info(device_id);
  115. context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
  116. if (!context)
  117. {
  118. printf("Error: Failed to create a compute context!\n%s\n", err_code(err));
  119. return EXIT_FAILURE;
  120. }
  121. commands = clCreateCommandQueue(context, device_id, 0, &err);
  122. if (!commands)
  123. {
  124. printf("Error: Failed to create a command commands!\n%s\n", err_code(err));
  125. return EXIT_FAILURE;
  126. }
  127. program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
  128. if (!program)
  129. {
  130. printf("Error: Failed to create compute program!\n%s\n", err_code(err));
  131. return EXIT_FAILURE;
  132. }
  133. err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
  134. if (err != CL_SUCCESS)
  135. {
  136. size_t len;
  137. char buffer[2048];
  138. printf("Error: Failed to build program executable!\n%s\n", err_code(err));
  139. clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
  140. printf("%s\n", buffer);
  141. return EXIT_FAILURE;
  142. }
  143. ko_vadd = clCreateKernel(program, "hotspotOpt1", &err);
  144. if (!ko_vadd || err != CL_SUCCESS)
  145. {
  146. printf("Error: Failed to create compute kernel!\n%s\n", err_code(err));
  147. return EXIT_FAILURE;
  148. }
  149. d_a = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float) * count, NULL, NULL);
  150. d_b = clCreateBuffer(context, CL_MEM_READ_ONLY , sizeof(float) * count, NULL, NULL);
  151. d_c = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(float) * count, NULL, NULL);
  152. if (!d_a || !d_b || !d_c)
  153. {
  154. printf("Error: Failed to allocate device memory!\n");
  155. exit(1);
  156. }
  157. err = clEnqueueWriteBuffer(commands, d_a, CL_TRUE, 0, sizeof(float) * count, tIn, 0, NULL, NULL);
  158. if (err != CL_SUCCESS)
  159. {
  160. printf("Error: Failed to write tIn to source array!\n%s\n", err_code(err));
  161. exit(1);
  162. }
  163. err = clEnqueueWriteBuffer(commands, d_b, CL_TRUE, 0, sizeof(float) * count, pIn, 0, NULL, NULL);
  164. if (err != CL_SUCCESS)
  165. {
  166. printf("Error: Failed to write pIn to source array!\n%s\n", err_code(err));
  167. exit(1);
  168. }
  169. err = clEnqueueWriteBuffer(commands, d_c, CL_TRUE, 0, sizeof(float) * count, tempOut, 0, NULL, NULL);
  170. if (err != CL_SUCCESS)
  171. {
  172. printf("Error: Failed to write tempOut to source array!\n%s\n", err_code(err));
  173. exit(1);
  174. }
  175. long long start = get_time();
  176. int j;
  177. for(j = 0; j < iterations; j++)
  178. {
  179. err = clSetKernelArg(ko_vadd, 0, sizeof(cl_mem), &d_b);
  180. err |= clSetKernelArg(ko_vadd, 1, sizeof(cl_mem), &d_a);
  181. err |= clSetKernelArg(ko_vadd, 2, sizeof(cl_mem), &d_c);
  182. err |= clSetKernelArg(ko_vadd, 3, sizeof(float), &stepDivCap);
  183. err |= clSetKernelArg(ko_vadd, 4, sizeof(int), &numCols);
  184. err |= clSetKernelArg(ko_vadd, 5, sizeof(int), &numRows);
  185. err |= clSetKernelArg(ko_vadd, 6, sizeof(int), &layers);
  186. err |= clSetKernelArg(ko_vadd, 7, sizeof(float), &ce);
  187. err |= clSetKernelArg(ko_vadd, 8, sizeof(float), &cw);
  188. err |= clSetKernelArg(ko_vadd, 9, sizeof(float), &cn);
  189. err |= clSetKernelArg(ko_vadd, 10, sizeof(float), &cs);
  190. err |= clSetKernelArg(ko_vadd, 11, sizeof(float), &ct);
  191. err |= clSetKernelArg(ko_vadd, 12, sizeof(float), &cb);
  192. err |= clSetKernelArg(ko_vadd, 13, sizeof(float), &cc);
  193. if (err != CL_SUCCESS)
  194. {
  195. printf("Error: Failed to set kernel arguments!\n");
  196. exit(1);
  197. }
  198. global[0] = numCols;
  199. global[1] = numRows;
  200. local[0] = WG_SIZE_X;
  201. local[1] = WG_SIZE_Y;
  202. err = clEnqueueNDRangeKernel(commands, ko_vadd, 2, NULL, global, local, 0, NULL, NULL);
  203. if (err)
  204. {
  205. printf("Error: Failed to execute kernel!\n%s\n", err_code(err));
  206. return EXIT_FAILURE;
  207. }
  208. cl_mem temp = d_a;
  209. d_a = d_c;
  210. d_c = temp;
  211. }
  212. clFinish(commands);
  213. long long stop = get_time();
  214. err = clEnqueueReadBuffer( commands, d_c, CL_TRUE, 0, sizeof(float) * count, tempOut, 0, NULL, NULL );
  215. if (err != CL_SUCCESS)
  216. {
  217. printf("Error: Failed to read output array!\n%s\n", err_code(err));
  218. exit(1);
  219. }
  220. float* answer = (float*)calloc(size, sizeof(float));
  221. computeTempCPU(pIn, tempCopy, answer, numCols, numRows, layers, Cap, Rx, Ry, Rz, dt, amb_temp, iterations);
  222. float acc = accuracy(tempOut,answer,numRows*numCols*layers);
  223. float time = (float)((stop - start)/(1000.0 * 1000.0));
  224. printf("Time: %.3f (s)\n",time);
  225. printf("Accuracy: %e\n",acc);
  226. writeoutput(tempOut,numRows,numCols,layers,ofile);
  227. clReleaseMemObject(d_a);
  228. clReleaseMemObject(d_b);
  229. clReleaseMemObject(d_c);
  230. clReleaseProgram(program);
  231. clReleaseKernel(ko_vadd);
  232. clReleaseCommandQueue(commands);
  233. clReleaseContext(context);
  234. return 0;
  235. }