main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // #ifdef __cplusplus
  2. // extern "C" {
  3. // #endif
  4. //========================================================================================================================================================================================================200
  5. //======================================================================================================================================================150
  6. //====================================================================================================100
  7. //==================================================50
  8. //========================================================================================================================================================================================================200
  9. // INCLUDE/DEFINE
  10. //========================================================================================================================================================================================================200
  11. //======================================================================================================================================================150
  12. // LIBRARIES
  13. //======================================================================================================================================================150
  14. #include <stdio.h> // (in path known to compiler) needed by printf
  15. #include <stdlib.h> // (in path known to compiler) needed by malloc, free
  16. //======================================================================================================================================================150
  17. // HEADER
  18. //======================================================================================================================================================150
  19. #include "./main.h" // (in current path)
  20. //======================================================================================================================================================150
  21. // UTILITIES
  22. //======================================================================================================================================================150
  23. #include "./util/graphics/graphics.h" // (in specified path)
  24. #include "./util/graphics/resize.h" // (in specified path)
  25. #include "./util/timer/timer.h" // (in specified path)
  26. //======================================================================================================================================================150
  27. // KERNEL
  28. //======================================================================================================================================================150
  29. #include "./kernel/kernel_gpu_opencl_wrapper.h"
  30. //======================================================================================================================================================150
  31. // End
  32. //======================================================================================================================================================150
  33. //========================================================================================================================================================================================================200
  34. // MAIN FUNCTION
  35. //========================================================================================================================================================================================================200
  36. int
  37. main( int argc,
  38. char* argv []){
  39. printf("WG size of kernel = %d \n", NUMBER_THREADS);
  40. //======================================================================================================================================================150
  41. // VARIABLES
  42. //======================================================================================================================================================150
  43. // time
  44. long long time0;
  45. long long time1;
  46. long long time2;
  47. long long time3;
  48. long long time4;
  49. long long time5;
  50. long long time6;
  51. // inputs image, input paramenters
  52. fp* image_ori; // originalinput image
  53. int image_ori_rows;
  54. int image_ori_cols;
  55. long image_ori_elem;
  56. // inputs image, input paramenters
  57. fp* image; // input image
  58. int Nr,Nc; // IMAGE nbr of rows/cols/elements
  59. long Ne;
  60. // algorithm parameters
  61. int niter; // nbr of iterations
  62. fp lambda; // update step size
  63. // size of IMAGE
  64. int r1,r2,c1,c2; // row/col coordinates of uniform ROI
  65. long NeROI; // ROI nbr of elements
  66. // surrounding pixel indicies
  67. int* iN;
  68. int* iS;
  69. int* jE;
  70. int* jW;
  71. // counters
  72. int iter; // primary loop
  73. long i; // image row
  74. long j; // image col
  75. // memory sizes
  76. int mem_size_i;
  77. int mem_size_j;
  78. time0 = get_time();
  79. //======================================================================================================================================================150
  80. // INPUT ARGUMENTS
  81. //======================================================================================================================================================150
  82. // Rewritten parameters parsing for selecting platform and device and old
  83. // parameters
  84. // Additional variables for platform and device selection
  85. int platform_idx = 0;
  86. int device_idx = 0;
  87. int use_gpu = 0;
  88. int opt;
  89. extern char *optarg;
  90. while ((opt = getopt(argc, argv, "i:l:r:c:p:d:g:")) != -1 ) {
  91. switch(opt){
  92. case 'i':
  93. niter = atoi(optarg);
  94. break;
  95. case 'l':
  96. lambda = atoi(optarg);
  97. break;
  98. case 'r':
  99. Nr = atoi(optarg);
  100. break;
  101. case 'c':
  102. Nc = atoi(optarg);
  103. break;
  104. case 'p':
  105. platform_idx = atoi(optarg);
  106. break;
  107. case 'd':
  108. device_idx = atoi(optarg);
  109. break;
  110. case 'g':
  111. use_gpu = atoi(optarg);
  112. break;
  113. case ':':
  114. fprintf(stderr, "missing argument\n");
  115. break;
  116. default:
  117. fprintf(stderr, "Usage: %s - <iteration> -l <lambda> -r <rows> -c <columns> -p <platform> -d <device> -g <use_gpu>\n",
  118. argv[0]);
  119. exit(EXIT_FAILURE);
  120. }
  121. }
  122. time1 = get_time();
  123. //======================================================================================================================================================150
  124. // READ INPUT FROM FILE
  125. //======================================================================================================================================================150
  126. //====================================================================================================100
  127. // READ IMAGE (SIZE OF IMAGE HAS TO BE KNOWN)
  128. //====================================================================================================100
  129. image_ori_rows = 502;
  130. image_ori_cols = 458;
  131. image_ori_elem = image_ori_rows * image_ori_cols;
  132. image_ori = (fp*)malloc(sizeof(fp) * image_ori_elem);
  133. read_graphics( "../../data/srad/image.pgm",
  134. image_ori,
  135. image_ori_rows,
  136. image_ori_cols,
  137. 1);
  138. //====================================================================================================100
  139. // RESIZE IMAGE (ASSUMING COLUMN MAJOR STORAGE OF image_orig)
  140. //====================================================================================================100
  141. Ne = Nr*Nc;
  142. image = (fp*)malloc(sizeof(fp) * Ne);
  143. resize( image_ori,
  144. image_ori_rows,
  145. image_ori_cols,
  146. image,
  147. Nr,
  148. Nc,
  149. 1);
  150. //====================================================================================================100
  151. // End
  152. //====================================================================================================100
  153. time2 = get_time();
  154. //======================================================================================================================================================150
  155. // SETUP
  156. //======================================================================================================================================================150
  157. // variables
  158. r1 = 0; // top row index of ROI
  159. r2 = Nr - 1; // bottom row index of ROI
  160. c1 = 0; // left column index of ROI
  161. c2 = Nc - 1; // right column index of ROI
  162. // ROI image size
  163. NeROI = (r2-r1+1)*(c2-c1+1); // number of elements in ROI, ROI size
  164. // allocate variables for surrounding pixels
  165. mem_size_i = sizeof(int) * Nr; //
  166. iN = (int *)malloc(mem_size_i) ; // north surrounding element
  167. iS = (int *)malloc(mem_size_i) ; // south surrounding element
  168. mem_size_j = sizeof(int) * Nc; //
  169. jW = (int *)malloc(mem_size_j) ; // west surrounding element
  170. jE = (int *)malloc(mem_size_j) ; // east surrounding element
  171. // N/S/W/E indices of surrounding pixels (every element of IMAGE)
  172. for (i=0; i<Nr; i++) {
  173. iN[i] = i-1; // holds index of IMAGE row above
  174. iS[i] = i+1; // holds index of IMAGE row below
  175. }
  176. for (j=0; j<Nc; j++) {
  177. jW[j] = j-1; // holds index of IMAGE column on the left
  178. jE[j] = j+1; // holds index of IMAGE column on the right
  179. }
  180. // N/S/W/E boundary conditions, fix surrounding indices outside boundary of image
  181. iN[0] = 0; // changes IMAGE top row index from -1 to 0
  182. iS[Nr-1] = Nr-1; // changes IMAGE bottom row index from Nr to Nr-1
  183. jW[0] = 0; // changes IMAGE leftmost column index from -1 to 0
  184. jE[Nc-1] = Nc-1; // changes IMAGE rightmost column index from Nc to Nc-1
  185. time3= get_time();
  186. //======================================================================================================================================================150
  187. // KERNEL
  188. //======================================================================================================================================================150
  189. kernel_gpu_opencl_wrapper(image, // input image
  190. Nr, // IMAGE nbr of rows
  191. Nc, // IMAGE nbr of cols
  192. Ne, // IMAGE nbr of elem
  193. niter, // nbr of iterations
  194. lambda, // update step size
  195. NeROI, // ROI nbr of elements
  196. iN,
  197. iS,
  198. jE,
  199. jW,
  200. iter, // primary loop
  201. mem_size_i,
  202. mem_size_j,
  203. platform_idx,
  204. device_idx,
  205. use_gpu);
  206. time4 = get_time();
  207. //======================================================================================================================================================150
  208. // WRITE OUTPUT IMAGE TO FILE
  209. //======================================================================================================================================================150
  210. write_graphics( "./output/image_out.pgm",
  211. image,
  212. Nr,
  213. Nc,
  214. 1,
  215. 255);
  216. time5 = get_time();
  217. //======================================================================================================================================================150
  218. // FREE MEMORY
  219. //======================================================================================================================================================150
  220. free(image_ori);
  221. free(image);
  222. free(iN);
  223. free(iS);
  224. free(jW);
  225. free(jE);
  226. time6 = get_time();
  227. //======================================================================================================================================================150
  228. // DISPLAY TIMING
  229. //======================================================================================================================================================150
  230. printf("Time spent in different stages of the application:\n");
  231. printf("%.12f s, %.12f % : READ COMMAND LINE PARAMETERS\n", (fp) (time1-time0) / 1000000, (fp) (time1-time0) / (fp) (time5-time0) * 100);
  232. printf("%.12f s, %.12f % : READ AND RESIZE INPUT IMAGE FROM FILE\n", (fp) (time2-time1) / 1000000, (fp) (time2-time1) / (fp) (time5-time0) * 100);
  233. printf("%.12f s, %.12f % : SETUP\n", (fp) (time3-time2) / 1000000, (fp) (time3-time2) / (fp) (time5-time0) * 100);
  234. printf("%.12f s, %.12f % : KERNEL\n", (fp) (time4-time3) / 1000000, (fp) (time4-time3) / (fp) (time5-time0) * 100);
  235. printf("%.12f s, %.12f % : WRITE OUTPUT IMAGE TO FILE\n", (fp) (time5-time4) / 1000000, (fp) (time5-time4) / (fp) (time5-time0) * 100);
  236. printf("%.12f s, %.12f % : FREE MEMORY\n", (fp) (time6-time5) / 1000000, (fp) (time6-time5) / (fp) (time5-time0) * 100);
  237. printf("Total time:\n");
  238. printf("%.12f s\n", (fp) (time5-time0) / 1000000);
  239. }
  240. //========================================================================================================================================================================================================200
  241. // END
  242. //========================================================================================================================================================================================================200