yuv_downscaler.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011, 2013-2015, 2017 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2016 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This uses a dummy algorithm to downscale a 1920x1080 yuv film.
  20. * Each frame is split in horizontal stripes which are processed in parallel.
  21. */
  22. #include <starpu.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include "yuv_downscaler.h"
  29. static double start;
  30. static double end;
  31. static const char *filename_in_default = "hugefile.2s.yuv";
  32. static const char *filename_out_default = "hugefile.2s.out.yuv";
  33. static char filename_in[1024];
  34. static char filename_out[1024];
  35. void parse_args(int argc, char **argv)
  36. {
  37. if (argc == 3)
  38. {
  39. strncpy(filename_in, argv[1], 1023);
  40. strncpy(filename_out, argv[2], 1023);
  41. }
  42. else
  43. {
  44. snprintf(filename_in, 1024, "%s/examples/ppm_downscaler/%s", STARPU_BUILD_DIR, filename_in_default);
  45. snprintf(filename_out, 1024, "%s/examples/ppm_downscaler/%s", STARPU_BUILD_DIR, filename_out_default);
  46. }
  47. }
  48. #define FRAMESIZE sizeof(struct yuv_frame)
  49. #define NEW_FRAMESIZE sizeof(struct yuv_new_frame)
  50. void ds_kernel_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  51. {
  52. uint8_t *input = (uint8_t *)STARPU_MATRIX_GET_PTR(descr[0]);
  53. const unsigned input_ld = STARPU_MATRIX_GET_LD(descr[0]);
  54. uint8_t *output = (uint8_t *)STARPU_MATRIX_GET_PTR(descr[1]);
  55. const unsigned output_ld = STARPU_MATRIX_GET_LD(descr[1]);
  56. const unsigned ncols = STARPU_MATRIX_GET_NX(descr[0]);
  57. const unsigned nlines = STARPU_MATRIX_GET_NY(descr[0]);
  58. unsigned line, col;
  59. for (line = 0; line < nlines; line+=FACTOR)
  60. for (col = 0; col < ncols; col+=FACTOR)
  61. {
  62. unsigned sum = 0;
  63. unsigned lline, lcol;
  64. for (lline = 0; lline < FACTOR; lline++)
  65. for (lcol = 0; lcol < FACTOR; lcol++)
  66. {
  67. unsigned in_index = (lcol + col) + (lline + line)*input_ld;
  68. sum += input[in_index];
  69. }
  70. unsigned out_index = (col / FACTOR) + (line / FACTOR)*output_ld;
  71. output[out_index] = (uint8_t)(sum/(FACTOR*FACTOR));
  72. }
  73. }
  74. static struct starpu_codelet ds_codelet =
  75. {
  76. .cpu_funcs = {ds_kernel_cpu},
  77. .cpu_funcs_name = {"ds_kernel_cpu"},
  78. .nbuffers = 2, /* input -> output */
  79. .modes = {STARPU_R, STARPU_W},
  80. .model = NULL
  81. };
  82. /* each block contains BLOCK_HEIGHT consecutive lines */
  83. static struct starpu_data_filter filter_y =
  84. {
  85. .filter_func = starpu_matrix_filter_block,
  86. .nchildren= HEIGHT/BLOCK_HEIGHT
  87. };
  88. static struct starpu_data_filter filter_uv =
  89. {
  90. .filter_func = starpu_matrix_filter_block,
  91. .nchildren = (HEIGHT/2)/BLOCK_HEIGHT
  92. };
  93. int main(int argc, char **argv)
  94. {
  95. int ret;
  96. size_t sret;
  97. assert(HEIGHT % (2*BLOCK_HEIGHT) == 0);
  98. assert(HEIGHT % FACTOR == 0);
  99. parse_args(argc, argv);
  100. /* fprintf(stderr, "Reading input file ...\n"); */
  101. /* how many frames ? */
  102. struct stat stbuf;
  103. ret = stat(filename_in, &stbuf);
  104. assert(ret);
  105. size_t filesize = stbuf.st_size;
  106. unsigned nframes = filesize/FRAMESIZE;
  107. /* fprintf(stderr, "filesize %lx (FRAME SIZE %lx NEW SIZE %lx); nframes %d\n", filesize, FRAMESIZE, NEW_FRAMESIZE, nframes); */
  108. assert((filesize % sizeof(struct yuv_frame)) == 0);
  109. struct yuv_frame *yuv_in_buffer = (struct yuv_frame *) malloc(nframes*FRAMESIZE);
  110. assert(yuv_in_buffer);
  111. /* fprintf(stderr, "Alloc output file ...\n"); */
  112. struct yuv_new_frame *yuv_out_buffer = (struct yuv_new_frame *) calloc(nframes, NEW_FRAMESIZE);
  113. assert(yuv_out_buffer);
  114. /* fetch input data */
  115. FILE *f_in = fopen(filename_in, "r");
  116. assert(f_in);
  117. /* allocate room for an output buffer */
  118. FILE *f_out = fopen(filename_out, "w+");
  119. assert(f_out);
  120. sret = fread(yuv_in_buffer, FRAMESIZE, nframes, f_in);
  121. assert(sret == nframes);
  122. starpu_data_handle_t *frame_y_handle = (starpu_data_handle_t *) calloc(nframes, sizeof(starpu_data_handle_t));
  123. starpu_data_handle_t *frame_u_handle = (starpu_data_handle_t *) calloc(nframes, sizeof(starpu_data_handle_t));
  124. starpu_data_handle_t *frame_v_handle = (starpu_data_handle_t *) calloc(nframes, sizeof(starpu_data_handle_t));
  125. starpu_data_handle_t *new_frame_y_handle = (starpu_data_handle_t *) calloc(nframes, sizeof(starpu_data_handle_t));
  126. starpu_data_handle_t *new_frame_u_handle = (starpu_data_handle_t *) calloc(nframes, sizeof(starpu_data_handle_t));
  127. starpu_data_handle_t *new_frame_v_handle = (starpu_data_handle_t *) calloc(nframes, sizeof(starpu_data_handle_t));
  128. ret = starpu_init(NULL);
  129. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  130. /* register and partition all layers */
  131. unsigned frame;
  132. for (frame = 0; frame < nframes; frame++)
  133. {
  134. /* register Y layer */
  135. starpu_matrix_data_register(&frame_y_handle[frame], STARPU_MAIN_RAM,
  136. (uintptr_t)&yuv_in_buffer[frame].y,
  137. WIDTH, WIDTH, HEIGHT, sizeof(uint8_t));
  138. starpu_data_partition(frame_y_handle[frame], &filter_y);
  139. starpu_matrix_data_register(&new_frame_y_handle[frame], STARPU_MAIN_RAM,
  140. (uintptr_t)&yuv_out_buffer[frame].y,
  141. NEW_WIDTH, NEW_WIDTH, NEW_HEIGHT, sizeof(uint8_t));
  142. starpu_data_partition(new_frame_y_handle[frame], &filter_y);
  143. /* register U layer */
  144. starpu_matrix_data_register(&frame_u_handle[frame], STARPU_MAIN_RAM,
  145. (uintptr_t)&yuv_in_buffer[frame].u,
  146. WIDTH/2, WIDTH/2, HEIGHT/2, sizeof(uint8_t));
  147. starpu_data_partition(frame_u_handle[frame], &filter_uv);
  148. starpu_matrix_data_register(&new_frame_u_handle[frame], STARPU_MAIN_RAM,
  149. (uintptr_t)&yuv_out_buffer[frame].u,
  150. NEW_WIDTH/2, NEW_WIDTH/2, NEW_HEIGHT/2, sizeof(uint8_t));
  151. starpu_data_partition(new_frame_u_handle[frame], &filter_uv);
  152. /* register V layer */
  153. starpu_matrix_data_register(&frame_v_handle[frame], STARPU_MAIN_RAM,
  154. (uintptr_t)&yuv_in_buffer[frame].v,
  155. WIDTH/2, WIDTH/2, HEIGHT/2, sizeof(uint8_t));
  156. starpu_data_partition(frame_v_handle[frame], &filter_uv);
  157. starpu_matrix_data_register(&new_frame_v_handle[frame], STARPU_MAIN_RAM,
  158. (uintptr_t)&yuv_out_buffer[frame].v,
  159. NEW_WIDTH/2, NEW_WIDTH/2, NEW_HEIGHT/2, sizeof(uint8_t));
  160. starpu_data_partition(new_frame_v_handle[frame], &filter_uv);
  161. }
  162. /* how many tasks are there ? */
  163. unsigned nblocks_y = filter_y.nchildren;
  164. unsigned nblocks_uv = filter_uv.nchildren;
  165. unsigned ntasks = (nblocks_y + 2*nblocks_uv)*nframes;
  166. fprintf(stderr, "Start computation: there will be %u tasks for %u frames\n", ntasks, nframes);
  167. start = starpu_timing_now();
  168. /* do the computation */
  169. for (frame = 0; frame < nframes; frame++)
  170. {
  171. starpu_iteration_push(frame);
  172. unsigned blocky;
  173. for (blocky = 0; blocky < nblocks_y; blocky++)
  174. {
  175. struct starpu_task *task = starpu_task_create();
  176. task->cl = &ds_codelet;
  177. /* input */
  178. task->handles[0] = starpu_data_get_sub_data(frame_y_handle[frame], 1, blocky);
  179. /* output */
  180. task->handles[1] = starpu_data_get_sub_data(new_frame_y_handle[frame], 1, blocky);
  181. ret = starpu_task_submit(task);
  182. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  183. }
  184. unsigned blocku;
  185. for (blocku = 0; blocku < nblocks_uv; blocku++)
  186. {
  187. struct starpu_task *task = starpu_task_create();
  188. task->cl = &ds_codelet;
  189. /* input */
  190. task->handles[0] = starpu_data_get_sub_data(frame_u_handle[frame], 1, blocku);
  191. /* output */
  192. task->handles[1] = starpu_data_get_sub_data(new_frame_u_handle[frame], 1, blocku);
  193. ret = starpu_task_submit(task);
  194. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  195. }
  196. unsigned blockv;
  197. for (blockv = 0; blockv < nblocks_uv; blockv++)
  198. {
  199. struct starpu_task *task = starpu_task_create();
  200. task->cl = &ds_codelet;
  201. /* input */
  202. task->handles[0] = starpu_data_get_sub_data(frame_v_handle[frame], 1, blockv);
  203. /* output */
  204. task->handles[1] = starpu_data_get_sub_data(new_frame_v_handle[frame], 1, blockv);
  205. ret = starpu_task_submit(task);
  206. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  207. }
  208. starpu_iteration_pop();
  209. }
  210. /* make sure all output buffers are sync'ed */
  211. for (frame = 0; frame < nframes; frame++)
  212. {
  213. starpu_data_unregister(frame_y_handle[frame]);
  214. starpu_data_unregister(frame_u_handle[frame]);
  215. starpu_data_unregister(frame_v_handle[frame]);
  216. starpu_data_unregister(new_frame_y_handle[frame]);
  217. starpu_data_unregister(new_frame_u_handle[frame]);
  218. starpu_data_unregister(new_frame_v_handle[frame]);
  219. }
  220. free(frame_y_handle);
  221. free(frame_u_handle);
  222. free(frame_v_handle);
  223. free(new_frame_y_handle);
  224. free(new_frame_u_handle);
  225. free(new_frame_v_handle);
  226. /* There is an implicit barrier: the unregister methods will block
  227. * until the computation is done and that the result was put back into
  228. * memory. */
  229. end = starpu_timing_now();
  230. double timing = end - start;
  231. printf("# s\tFPS\n");
  232. printf("%f\t%f\n", timing/1000000, (1000000*nframes)/timing);
  233. fwrite(yuv_out_buffer, NEW_FRAMESIZE, nframes, f_out);
  234. /* partition the layers into smaller parts */
  235. starpu_shutdown();
  236. if (fclose(f_in) != 0)
  237. fprintf(stderr, "Could not close %s properly\n", filename_in);
  238. if (fclose(f_out) != 0)
  239. fprintf(stderr, "Could not close %s properly\n", filename_out);
  240. return 0;
  241. }