yuv-downscaler.c 8.7 KB

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