yuv_downscaler.c 9.0 KB

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