main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. //========================================================================================================================================================================================================200
  5. //======================================================================================================================================================150
  6. //====================================================================================================100
  7. //==================================================50
  8. //========================================================================================================================================================================================================200
  9. // INFORMATION
  10. //========================================================================================================================================================================================================200
  11. //======================================================================================================================================================150
  12. // UPDATE
  13. //======================================================================================================================================================150
  14. // 2009.12 Lukasz G. Szafaryn
  15. // -- entire code written
  16. //======================================================================================================================================================150
  17. // DESCRIPTION
  18. //======================================================================================================================================================150
  19. // Description
  20. //======================================================================================================================================================150
  21. // USE
  22. //======================================================================================================================================================150
  23. // How to run
  24. //========================================================================================================================================================================================================200
  25. // DEFINE/INCLUDE
  26. //========================================================================================================================================================================================================200
  27. //======================================================================================================================================================150
  28. // LIBRARIES
  29. //======================================================================================================================================================150
  30. #include <stdio.h> // (in path known to compiler) needed by printf
  31. #include <stdlib.h> // (in path known to compiler) needed by malloc
  32. #include <stdbool.h> // (in path known to compiler) needed by true/false
  33. //======================================================================================================================================================150
  34. // UTILITIES
  35. //======================================================================================================================================================150
  36. #include "./util/timer/timer.h" // (in path specified here)
  37. #include "./util/num/num.h" // (in path specified here)
  38. //======================================================================================================================================================150
  39. // MAIN FUNCTION HEADER
  40. //======================================================================================================================================================150
  41. #include "./main.h" // (in the current directory)
  42. //======================================================================================================================================================150
  43. // KERNEL
  44. //======================================================================================================================================================150
  45. #include "./kernel/kernel_gpu_opencl_wrapper.h" // (in library path specified here)
  46. //========================================================================================================================================================================================================200
  47. // MAIN FUNCTION
  48. //========================================================================================================================================================================================================200
  49. int
  50. main( int argc,
  51. char *argv [])
  52. {
  53. //======================================================================================================================================================150
  54. // CPU/MCPU VARIABLES
  55. //======================================================================================================================================================150
  56. // timer
  57. long long time0;
  58. time0 = get_time();
  59. // timer
  60. long long time1;
  61. long long time2;
  62. long long time3;
  63. long long time4;
  64. long long time5;
  65. long long time6;
  66. long long time7;
  67. // counters
  68. int i, j, k, l, m, n;
  69. // system memory
  70. par_str par_cpu;
  71. dim_str dim_cpu;
  72. box_str* box_cpu;
  73. FOUR_VECTOR* rv_cpu;
  74. fp* qv_cpu;
  75. FOUR_VECTOR* fv_cpu;
  76. int nh;
  77. printf("WG size of kernel = %d \n", NUMBER_THREADS);
  78. time1 = get_time();
  79. //======================================================================================================================================================150
  80. // CHECK INPUT ARGUMENTS
  81. //======================================================================================================================================================150
  82. // assing default values
  83. dim_cpu.arch_arg = 0;
  84. dim_cpu.cores_arg = 1;
  85. dim_cpu.boxes1d_arg = 1;
  86. // go through arguments
  87. if(argc==3){
  88. for(dim_cpu.cur_arg=1; dim_cpu.cur_arg<argc; dim_cpu.cur_arg++){
  89. // check if -boxes1d
  90. if(strcmp(argv[dim_cpu.cur_arg], "-boxes1d")==0){
  91. // check if value provided
  92. if(argc>=dim_cpu.cur_arg+1){
  93. // check if value is a number
  94. if(isInteger(argv[dim_cpu.cur_arg+1])==1){
  95. dim_cpu.boxes1d_arg = atoi(argv[dim_cpu.cur_arg+1]);
  96. if(dim_cpu.boxes1d_arg<0){
  97. printf("ERROR: Wrong value to -boxes1d argument, cannot be <=0\n");
  98. return 0;
  99. }
  100. dim_cpu.cur_arg = dim_cpu.cur_arg+1;
  101. }
  102. // value is not a number
  103. else{
  104. printf("ERROR: Value to -boxes1d argument in not a number\n");
  105. return 0;
  106. }
  107. }
  108. // value not provided
  109. else{
  110. printf("ERROR: Missing value to -boxes1d argument\n");
  111. return 0;
  112. }
  113. }
  114. // unknown
  115. else{
  116. printf("ERROR: Unknown argument\n");
  117. return 0;
  118. }
  119. }
  120. // Print configuration
  121. printf("Configuration used: arch = %d, cores = %d, boxes1d = %d\n", dim_cpu.arch_arg, dim_cpu.cores_arg, dim_cpu.boxes1d_arg);
  122. }
  123. else{
  124. printf("Provide boxes1d argument, example: -boxes1d 16");
  125. return 0;
  126. }
  127. time2 = get_time();
  128. //======================================================================================================================================================150
  129. // INPUTS
  130. //======================================================================================================================================================150
  131. par_cpu.alpha = 0.5;
  132. time3 = get_time();
  133. //======================================================================================================================================================150
  134. // DIMENSIONS
  135. //======================================================================================================================================================150
  136. // total number of boxes
  137. dim_cpu.number_boxes = dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg; // 8*8*8=512
  138. // how many particles space has in each direction
  139. dim_cpu.space_elem = dim_cpu.number_boxes * NUMBER_PAR_PER_BOX; //512*100=51,200
  140. dim_cpu.space_mem = dim_cpu.space_elem * sizeof(FOUR_VECTOR);
  141. dim_cpu.space_mem2 = dim_cpu.space_elem * sizeof(fp);
  142. // box array
  143. dim_cpu.box_mem = dim_cpu.number_boxes * sizeof(box_str);
  144. time4 = get_time();
  145. //======================================================================================================================================================150
  146. // SYSTEM MEMORY
  147. //======================================================================================================================================================150
  148. //====================================================================================================100
  149. // BOX
  150. //====================================================================================================100
  151. // allocate boxes
  152. box_cpu = (box_str*)malloc(dim_cpu.box_mem);
  153. // initialize number of home boxes
  154. nh = 0;
  155. // home boxes in z direction
  156. for(i=0; i<dim_cpu.boxes1d_arg; i++){
  157. // home boxes in y direction
  158. for(j=0; j<dim_cpu.boxes1d_arg; j++){
  159. // home boxes in x direction
  160. for(k=0; k<dim_cpu.boxes1d_arg; k++){
  161. // current home box
  162. box_cpu[nh].x = k;
  163. box_cpu[nh].y = j;
  164. box_cpu[nh].z = i;
  165. box_cpu[nh].number = nh;
  166. box_cpu[nh].offset = nh * NUMBER_PAR_PER_BOX;
  167. // initialize number of neighbor boxes
  168. box_cpu[nh].nn = 0;
  169. // neighbor boxes in z direction
  170. for(l=-1; l<2; l++){
  171. // neighbor boxes in y direction
  172. for(m=-1; m<2; m++){
  173. // neighbor boxes in x direction
  174. for(n=-1; n<2; n++){
  175. // check if (this neighbor exists) and (it is not the same as home box)
  176. if( (((i+l)>=0 && (j+m)>=0 && (k+n)>=0)==true && ((i+l)<dim_cpu.boxes1d_arg && (j+m)<dim_cpu.boxes1d_arg && (k+n)<dim_cpu.boxes1d_arg)==true) &&
  177. (l==0 && m==0 && n==0)==false ){
  178. // current neighbor box
  179. box_cpu[nh].nei[box_cpu[nh].nn].x = (k+n);
  180. box_cpu[nh].nei[box_cpu[nh].nn].y = (j+m);
  181. box_cpu[nh].nei[box_cpu[nh].nn].z = (i+l);
  182. box_cpu[nh].nei[box_cpu[nh].nn].number = (box_cpu[nh].nei[box_cpu[nh].nn].z * dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg) +
  183. (box_cpu[nh].nei[box_cpu[nh].nn].y * dim_cpu.boxes1d_arg) +
  184. box_cpu[nh].nei[box_cpu[nh].nn].x;
  185. box_cpu[nh].nei[box_cpu[nh].nn].offset = box_cpu[nh].nei[box_cpu[nh].nn].number * NUMBER_PAR_PER_BOX;
  186. // increment neighbor box
  187. box_cpu[nh].nn = box_cpu[nh].nn + 1;
  188. }
  189. } // neighbor boxes in x direction
  190. } // neighbor boxes in y direction
  191. } // neighbor boxes in z direction
  192. // increment home box
  193. nh = nh + 1;
  194. } // home boxes in x direction
  195. } // home boxes in y direction
  196. } // home boxes in z direction
  197. //====================================================================================================100
  198. // PARAMETERS, DISTANCE, CHARGE AND FORCE
  199. //====================================================================================================100
  200. // random generator seed set to random value - time in this case
  201. srand(time(NULL));
  202. // input (distances)
  203. rv_cpu = (FOUR_VECTOR*)malloc(dim_cpu.space_mem);
  204. for(i=0; i<dim_cpu.space_elem; i=i+1){
  205. rv_cpu[i].v = (rand()%10 + 1) / 10.0; // get a number in the range 0.1 - 1.0
  206. // rv_cpu[i].v = 0.1; // get a number in the range 0.1 - 1.0
  207. rv_cpu[i].x = (rand()%10 + 1) / 10.0; // get a number in the range 0.1 - 1.0
  208. // rv_cpu[i].x = 0.2; // get a number in the range 0.1 - 1.0
  209. rv_cpu[i].y = (rand()%10 + 1) / 10.0; // get a number in the range 0.1 - 1.0
  210. // rv_cpu[i].y = 0.3; // get a number in the range 0.1 - 1.0
  211. rv_cpu[i].z = (rand()%10 + 1) / 10.0; // get a number in the range 0.1 - 1.0
  212. // rv_cpu[i].z = 0.4; // get a number in the range 0.1 - 1.0
  213. }
  214. // input (charge)
  215. qv_cpu = (fp*)malloc(dim_cpu.space_mem2);
  216. for(i=0; i<dim_cpu.space_elem; i=i+1){
  217. qv_cpu[i] = (rand()%10 + 1) / 10.0; // get a number in the range 0.1 - 1.0
  218. // qv_cpu[i] = 0.5; // get a number in the range 0.1 - 1.0
  219. }
  220. // output (forces)
  221. fv_cpu = (FOUR_VECTOR*)malloc(dim_cpu.space_mem);
  222. for(i=0; i<dim_cpu.space_elem; i=i+1){
  223. fv_cpu[i].v = 0; // set to 0, because kernels keeps adding to initial value
  224. fv_cpu[i].x = 0; // set to 0, because kernels keeps adding to initial value
  225. fv_cpu[i].y = 0; // set to 0, because kernels keeps adding to initial value
  226. fv_cpu[i].z = 0; // set to 0, because kernels keeps adding to initial value
  227. }
  228. time5 = get_time();
  229. //======================================================================================================================================================150
  230. // KERNEL
  231. //======================================================================================================================================================150
  232. //====================================================================================================100
  233. // GPU_OPENCL
  234. //====================================================================================================100
  235. kernel_gpu_opencl_wrapper( par_cpu,
  236. dim_cpu,
  237. box_cpu,
  238. rv_cpu,
  239. qv_cpu,
  240. fv_cpu);
  241. time6 = get_time();
  242. //======================================================================================================================================================150
  243. // SYSTEM MEMORY DEALLOCATION
  244. //======================================================================================================================================================150
  245. // dump results
  246. #ifdef OUTPUT
  247. FILE *fptr;
  248. fptr = fopen("result.txt", "w");
  249. for(i=0; i<dim_cpu.space_elem; i=i+1){
  250. fprintf(fptr, "%f, %f, %f, %f\n", fv_cpu[i].v, fv_cpu[i].x, fv_cpu[i].y, fv_cpu[i].z);
  251. }
  252. fclose(fptr);
  253. #endif
  254. free(rv_cpu);
  255. free(qv_cpu);
  256. free(fv_cpu);
  257. free(box_cpu);
  258. time7 = get_time();
  259. //======================================================================================================================================================150
  260. // DISPLAY TIMING
  261. //======================================================================================================================================================150
  262. // printf("Time spent in different stages of the application:\n");
  263. // printf("%15.12f s, %15.12f % : VARIABLES\n", (float) (time1-time0) / 1000000, (float) (time1-time0) / (float) (time7-time0) * 100);
  264. // printf("%15.12f s, %15.12f % : INPUT ARGUMENTS\n", (float) (time2-time1) / 1000000, (float) (time2-time1) / (float) (time7-time0) * 100);
  265. // printf("%15.12f s, %15.12f % : INPUTS\n", (float) (time3-time2) / 1000000, (float) (time3-time2) / (float) (time7-time0) * 100);
  266. // printf("%15.12f s, %15.12f % : dim_cpu\n", (float) (time4-time3) / 1000000, (float) (time4-time3) / (float) (time7-time0) * 100);
  267. // printf("%15.12f s, %15.12f % : SYS MEM: ALO\n", (float) (time5-time4) / 1000000, (float) (time5-time4) / (float) (time7-time0) * 100);
  268. // printf("%15.12f s, %15.12f % : KERNEL: COMPUTE\n", (float) (time6-time5) / 1000000, (float) (time6-time5) / (float) (time7-time0) * 100);
  269. // printf("%15.12f s, %15.12f % : SYS MEM: FRE\n", (float) (time7-time6) / 1000000, (float) (time7-time6) / (float) (time7-time0) * 100);
  270. // printf("Total time:\n");
  271. // printf("%.12f s\n", (float) (time7-time0) / 1000000);
  272. //======================================================================================================================================================150
  273. // RETURN
  274. //======================================================================================================================================================150
  275. return 0.0; // always returns 0.0
  276. }
  277. //========================================================================================================================================================================================================200
  278. // END
  279. //========================================================================================================================================================================================================200
  280. #ifdef __cplusplus
  281. }
  282. #endif