yuv_downscaler.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <unistd.h>
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include "yuv_downscaler.h"
  24. struct timeval start;
  25. struct timeval end;
  26. const char *filename_in_default = "hugefile.2s.yuv";
  27. const char *filename_out_default = "hugefile.2s.out.yuv";
  28. char filename_in[1024];
  29. char filename_out[1024];
  30. void parse_args(int argc, char **argv)
  31. {
  32. if (argc == 3) {
  33. strcpy(filename_in, argv[1]);
  34. strcpy(filename_out, argv[2]);
  35. }
  36. else {
  37. sprintf(filename_in, "%s/examples/ppm_downscaler/%s", STARPU_BUILD_DIR, filename_in_default);
  38. sprintf(filename_out, "%s/examples/ppm_downscaler/%s", STARPU_BUILD_DIR, filename_out_default);
  39. }
  40. }
  41. #define FRAMESIZE sizeof(struct yuv_frame)
  42. #define NEW_FRAMESIZE sizeof(struct yuv_new_frame)
  43. static pthread_cond_t ds_callback_cond = PTHREAD_COND_INITIALIZER;
  44. static pthread_mutex_t ds_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
  45. static unsigned ds_callback_terminated = 0;
  46. static unsigned ds_callback_cnt = 0;
  47. static void ds_callback(void *arg)
  48. {
  49. unsigned val = STARPU_ATOMIC_ADD(&ds_callback_cnt, -1);
  50. if (val == 0)
  51. {
  52. fprintf(stderr, "Downscaling terminated...\n");
  53. pthread_mutex_lock(&ds_callback_mutex);
  54. ds_callback_terminated = 1;
  55. pthread_cond_signal(&ds_callback_cond);
  56. pthread_mutex_unlock(&ds_callback_mutex);
  57. }
  58. }
  59. static void ds_kernel_cpu(void *descr[], __attribute__((unused)) void *arg)
  60. {
  61. uint8_t *input = (uint8_t *)STARPU_GET_MATRIX_PTR(descr[0]);
  62. unsigned input_ld = STARPU_GET_MATRIX_LD(descr[0]);
  63. uint8_t *output = (uint8_t *)STARPU_GET_MATRIX_PTR(descr[1]);
  64. unsigned output_ld = STARPU_GET_MATRIX_LD(descr[1]);
  65. unsigned ncols = STARPU_GET_MATRIX_NX(descr[0]);
  66. unsigned nlines = STARPU_GET_MATRIX_NY(descr[0]);
  67. unsigned line, col;
  68. for (line = 0; line < nlines; line+=FACTOR)
  69. for (col = 0; col < ncols; col+=FACTOR)
  70. {
  71. unsigned sum = 0;
  72. unsigned lline, lcol;
  73. for (lline = 0; lline < FACTOR; lline++)
  74. for (lcol = 0; lcol < FACTOR; lcol++)
  75. {
  76. unsigned in_index = (lcol + col) + (lline + line)*input_ld;
  77. sum += input[in_index];
  78. }
  79. unsigned out_index = (col / FACTOR) + (line / FACTOR)*output_ld;
  80. output[out_index] = (uint8_t)(sum/(FACTOR*FACTOR));
  81. }
  82. }
  83. static struct starpu_codelet_t ds_codelet = {
  84. .where = STARPU_CPU,
  85. .cpu_func = ds_kernel_cpu,
  86. .nbuffers = 2, /* input -> output */
  87. .model = NULL
  88. };
  89. /* each block contains BLOCK_HEIGHT consecutive lines */
  90. static starpu_filter filter_y = {
  91. .filter_func = starpu_block_filter_func,
  92. .filter_arg = HEIGHT/BLOCK_HEIGHT
  93. };
  94. static starpu_filter filter_u = {
  95. .filter_func = starpu_block_filter_func,
  96. .filter_arg = (HEIGHT/2)/BLOCK_HEIGHT
  97. };
  98. static starpu_filter filter_v = {
  99. .filter_func = starpu_block_filter_func,
  100. .filter_arg = (HEIGHT/2)/BLOCK_HEIGHT
  101. };
  102. int main(int argc, char **argv)
  103. {
  104. assert(HEIGHT % (2*BLOCK_HEIGHT) == 0);
  105. assert(HEIGHT % FACTOR == 0);
  106. parse_args(argc, argv);
  107. // fprintf(stderr, "Reading input file ...\n");
  108. /* how many frames ? */
  109. struct stat stbuf;
  110. stat(filename_in, &stbuf);
  111. size_t filesize = stbuf.st_size;
  112. unsigned nframes = filesize/FRAMESIZE;
  113. // fprintf(stderr, "filesize %lx (FRAME SIZE %lx NEW SIZE %lx); nframes %d\n", filesize, FRAMESIZE, NEW_FRAMESIZE, nframes);
  114. assert((filesize % sizeof(struct yuv_frame)) == 0);
  115. /* fetch input data */
  116. FILE *f_in = fopen(filename_in, "r");
  117. assert(f_in);
  118. struct yuv_frame *yuv_in_buffer = malloc(nframes*FRAMESIZE);
  119. fread(yuv_in_buffer, FRAMESIZE, nframes, f_in);
  120. /* allocate room for an output buffer */
  121. FILE *f_out = fopen(filename_out, "w+");
  122. assert(f_out);
  123. // fprintf(stderr, "Alloc output file ...\n");
  124. struct yuv_new_frame *yuv_out_buffer = calloc(nframes, NEW_FRAMESIZE);
  125. assert(yuv_out_buffer);
  126. starpu_data_handle *frame_y_handle = calloc(nframes, sizeof(starpu_data_handle));
  127. starpu_data_handle *frame_u_handle = calloc(nframes, sizeof(starpu_data_handle));
  128. starpu_data_handle *frame_v_handle = calloc(nframes, sizeof(starpu_data_handle));
  129. starpu_data_handle *new_frame_y_handle = calloc(nframes, sizeof(starpu_data_handle));
  130. starpu_data_handle *new_frame_u_handle = calloc(nframes, sizeof(starpu_data_handle));
  131. starpu_data_handle *new_frame_v_handle = calloc(nframes, sizeof(starpu_data_handle));
  132. starpu_init(NULL);
  133. /* register and partition all layers */
  134. unsigned frame;
  135. for (frame = 0; frame < nframes; frame++)
  136. {
  137. /* register Y layer */
  138. starpu_matrix_data_register(&frame_y_handle[frame], 0,
  139. (uintptr_t)&yuv_in_buffer[frame].y,
  140. WIDTH, WIDTH, HEIGHT, sizeof(uint8_t));
  141. starpu_data_partition(frame_y_handle[frame], &filter_y);
  142. starpu_matrix_data_register(&new_frame_y_handle[frame], 0,
  143. (uintptr_t)&yuv_out_buffer[frame].y,
  144. NEW_WIDTH, NEW_WIDTH, NEW_HEIGHT, sizeof(uint8_t));
  145. starpu_data_partition(new_frame_y_handle[frame], &filter_y);
  146. /* register U layer */
  147. starpu_matrix_data_register(&frame_u_handle[frame], 0,
  148. (uintptr_t)&yuv_in_buffer[frame].u,
  149. WIDTH/2, WIDTH/2, HEIGHT/2, sizeof(uint8_t));
  150. starpu_data_partition(frame_u_handle[frame], &filter_u);
  151. starpu_matrix_data_register(&new_frame_u_handle[frame], 0,
  152. (uintptr_t)&yuv_out_buffer[frame].u,
  153. NEW_WIDTH/2, NEW_WIDTH/2, NEW_HEIGHT/2, sizeof(uint8_t));
  154. starpu_data_partition(new_frame_u_handle[frame], &filter_u);
  155. /* register V layer */
  156. starpu_matrix_data_register(&frame_v_handle[frame], 0,
  157. (uintptr_t)&yuv_in_buffer[frame].v,
  158. WIDTH/2, WIDTH/2, HEIGHT/2, sizeof(uint8_t));
  159. starpu_data_partition(frame_v_handle[frame], &filter_v);
  160. starpu_matrix_data_register(&new_frame_v_handle[frame], 0,
  161. (uintptr_t)&yuv_out_buffer[frame].v,
  162. NEW_WIDTH/2, NEW_WIDTH/2, NEW_HEIGHT/2, sizeof(uint8_t));
  163. starpu_data_partition(new_frame_v_handle[frame], &filter_v);
  164. }
  165. /* how many tasks are there ? */
  166. unsigned nblocks_y = filter_y.filter_arg;
  167. unsigned nblocks_uv = filter_u.filter_arg;
  168. ds_callback_cnt = (nblocks_y + 2*nblocks_uv)*nframes;
  169. fprintf(stderr, "Start computation: there will be %d tasks for %d frames\n", ds_callback_cnt, nframes);
  170. gettimeofday(&start, NULL);
  171. /* do the computation */
  172. for (frame = 0; frame < nframes; frame++)
  173. {
  174. unsigned blocky;
  175. for (blocky = 0; blocky < nblocks_y; blocky++)
  176. {
  177. struct starpu_task *task = starpu_task_create();
  178. task->cl = &ds_codelet;
  179. task->callback_func = ds_callback;
  180. /* input */
  181. task->buffers[0].handle = starpu_data_get_sub_data(frame_y_handle[frame], 1, blocky);
  182. task->buffers[0].mode = STARPU_R;
  183. /* output */
  184. task->buffers[1].handle = starpu_data_get_sub_data(new_frame_y_handle[frame], 1, blocky);
  185. task->buffers[1].mode = STARPU_W;
  186. starpu_task_submit(task);
  187. }
  188. unsigned blocku;
  189. for (blocku = 0; blocku < nblocks_uv; blocku++)
  190. {
  191. struct starpu_task *task = starpu_task_create();
  192. task->cl = &ds_codelet;
  193. task->callback_func = ds_callback;
  194. /* input */
  195. task->buffers[0].handle = starpu_data_get_sub_data(frame_u_handle[frame], 1, blocku);
  196. task->buffers[0].mode = STARPU_R;
  197. /* output */
  198. task->buffers[1].handle = starpu_data_get_sub_data(new_frame_u_handle[frame], 1, blocku);
  199. task->buffers[1].mode = STARPU_W;
  200. starpu_task_submit(task);
  201. }
  202. unsigned blockv;
  203. for (blockv = 0; blockv < nblocks_uv; blockv++)
  204. {
  205. struct starpu_task *task = starpu_task_create();
  206. task->cl = &ds_codelet;
  207. task->callback_func = ds_callback;
  208. /* input */
  209. task->buffers[0].handle = starpu_data_get_sub_data(frame_v_handle[frame], 1, blockv);
  210. task->buffers[0].mode = STARPU_R;
  211. /* output */
  212. task->buffers[1].handle = starpu_data_get_sub_data(new_frame_v_handle[frame], 1, blockv);
  213. task->buffers[1].mode = STARPU_W;
  214. starpu_task_submit(task);
  215. }
  216. }
  217. pthread_mutex_lock(&ds_callback_mutex);
  218. if (!ds_callback_terminated)
  219. pthread_cond_wait(&ds_callback_cond, &ds_callback_mutex);
  220. pthread_mutex_unlock(&ds_callback_mutex);
  221. gettimeofday(&end, NULL);
  222. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  223. fprintf(stderr, "Computation took %f seconds\n", timing/1000000);
  224. fprintf(stderr, "FPS %f\n", (1000000*nframes)/timing);
  225. /* make sure all output buffers are sync'ed */
  226. for (frame = 0; frame < nframes; frame++)
  227. {
  228. starpu_data_sync_with_mem(new_frame_y_handle[frame], STARPU_R);
  229. starpu_data_sync_with_mem(new_frame_u_handle[frame], STARPU_R);
  230. starpu_data_sync_with_mem(new_frame_v_handle[frame], STARPU_R);
  231. }
  232. /* partition the layers into smaller parts */
  233. starpu_shutdown();
  234. fwrite(yuv_out_buffer, NEW_FRAMESIZE, nframes, f_out);
  235. return 0;
  236. }