main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if(argc != 5){
  83. printf("ERROR: wrong number of arguments\n");
  84. return 0;
  85. }
  86. else{
  87. niter = atoi(argv[1]);
  88. lambda = atof(argv[2]);
  89. Nr = atoi(argv[3]); // it is 502 in the original image
  90. Nc = atoi(argv[4]); // it is 458 in the original image
  91. }
  92. time1 = get_time();
  93. //======================================================================================================================================================150
  94. // READ INPUT FROM FILE
  95. //======================================================================================================================================================150
  96. //====================================================================================================100
  97. // READ IMAGE (SIZE OF IMAGE HAS TO BE KNOWN)
  98. //====================================================================================================100
  99. image_ori_rows = 502;
  100. image_ori_cols = 458;
  101. image_ori_elem = image_ori_rows * image_ori_cols;
  102. image_ori = (fp*)malloc(sizeof(fp) * image_ori_elem);
  103. read_graphics( "../../data/srad/image.pgm",
  104. image_ori,
  105. image_ori_rows,
  106. image_ori_cols,
  107. 1);
  108. //====================================================================================================100
  109. // RESIZE IMAGE (ASSUMING COLUMN MAJOR STORAGE OF image_orig)
  110. //====================================================================================================100
  111. Ne = Nr*Nc;
  112. image = (fp*)malloc(sizeof(fp) * Ne);
  113. resize( image_ori,
  114. image_ori_rows,
  115. image_ori_cols,
  116. image,
  117. Nr,
  118. Nc,
  119. 1);
  120. //====================================================================================================100
  121. // End
  122. //====================================================================================================100
  123. time2 = get_time();
  124. //======================================================================================================================================================150
  125. // SETUP
  126. //======================================================================================================================================================150
  127. // variables
  128. r1 = 0; // top row index of ROI
  129. r2 = Nr - 1; // bottom row index of ROI
  130. c1 = 0; // left column index of ROI
  131. c2 = Nc - 1; // right column index of ROI
  132. // ROI image size
  133. NeROI = (r2-r1+1)*(c2-c1+1); // number of elements in ROI, ROI size
  134. // allocate variables for surrounding pixels
  135. mem_size_i = sizeof(int) * Nr; //
  136. iN = (int *)malloc(mem_size_i) ; // north surrounding element
  137. iS = (int *)malloc(mem_size_i) ; // south surrounding element
  138. mem_size_j = sizeof(int) * Nc; //
  139. jW = (int *)malloc(mem_size_j) ; // west surrounding element
  140. jE = (int *)malloc(mem_size_j) ; // east surrounding element
  141. // N/S/W/E indices of surrounding pixels (every element of IMAGE)
  142. for (i=0; i<Nr; i++) {
  143. iN[i] = i-1; // holds index of IMAGE row above
  144. iS[i] = i+1; // holds index of IMAGE row below
  145. }
  146. for (j=0; j<Nc; j++) {
  147. jW[j] = j-1; // holds index of IMAGE column on the left
  148. jE[j] = j+1; // holds index of IMAGE column on the right
  149. }
  150. // N/S/W/E boundary conditions, fix surrounding indices outside boundary of image
  151. iN[0] = 0; // changes IMAGE top row index from -1 to 0
  152. iS[Nr-1] = Nr-1; // changes IMAGE bottom row index from Nr to Nr-1
  153. jW[0] = 0; // changes IMAGE leftmost column index from -1 to 0
  154. jE[Nc-1] = Nc-1; // changes IMAGE rightmost column index from Nc to Nc-1
  155. time3= get_time();
  156. //======================================================================================================================================================150
  157. // KERNEL
  158. //======================================================================================================================================================150
  159. kernel_gpu_opencl_wrapper( image, // input image
  160. Nr, // IMAGE nbr of rows
  161. Nc, // IMAGE nbr of cols
  162. Ne, // IMAGE nbr of elem
  163. niter, // nbr of iterations
  164. lambda, // update step size
  165. NeROI, // ROI nbr of elements
  166. iN,
  167. iS,
  168. jE,
  169. jW,
  170. iter, // primary loop
  171. mem_size_i,
  172. mem_size_j);
  173. time4 = get_time();
  174. //======================================================================================================================================================150
  175. // WRITE OUTPUT IMAGE TO FILE
  176. //======================================================================================================================================================150
  177. write_graphics( "./output/image_out.pgm",
  178. image,
  179. Nr,
  180. Nc,
  181. 1,
  182. 255);
  183. time5 = get_time();
  184. //======================================================================================================================================================150
  185. // FREE MEMORY
  186. //======================================================================================================================================================150
  187. free(image_ori);
  188. free(image);
  189. free(iN);
  190. free(iS);
  191. free(jW);
  192. free(jE);
  193. time6 = get_time();
  194. //======================================================================================================================================================150
  195. // DISPLAY TIMING
  196. //======================================================================================================================================================150
  197. printf("Time spent in different stages of the application:\n");
  198. printf("%.12f s, %.12f % : READ COMMAND LINE PARAMETERS\n", (fp) (time1-time0) / 1000000, (fp) (time1-time0) / (fp) (time5-time0) * 100);
  199. printf("%.12f s, %.12f % : READ AND RESIZE INPUT IMAGE FROM FILE\n", (fp) (time2-time1) / 1000000, (fp) (time2-time1) / (fp) (time5-time0) * 100);
  200. printf("%.12f s, %.12f % : SETUP\n", (fp) (time3-time2) / 1000000, (fp) (time3-time2) / (fp) (time5-time0) * 100);
  201. printf("%.12f s, %.12f % : KERNEL\n", (fp) (time4-time3) / 1000000, (fp) (time4-time3) / (fp) (time5-time0) * 100);
  202. printf("%.12f s, %.12f % : WRITE OUTPUT IMAGE TO FILE\n", (fp) (time5-time4) / 1000000, (fp) (time5-time4) / (fp) (time5-time0) * 100);
  203. printf("%.12f s, %.12f % : FREE MEMORY\n", (fp) (time6-time5) / 1000000, (fp) (time6-time5) / (fp) (time5-time0) * 100);
  204. printf("Total time:\n");
  205. printf("%.12f s\n", (fp) (time5-time0) / 1000000);
  206. }
  207. //========================================================================================================================================================================================================200
  208. // END
  209. //========================================================================================================================================================================================================200