starpufftx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #define PARALLEL 0
  18. #include <math.h>
  19. #include <pthread.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. #include <starpu.h>
  23. #include <config.h>
  24. #include "starpufft.h"
  25. #ifdef STARPU_USE_CUDA
  26. #define _externC extern
  27. #include "cudax_kernels.h"
  28. #endif
  29. #define _FFTW_FLAGS FFTW_ESTIMATE
  30. /* Steps for the parallel variant */
  31. enum steps {
  32. SPECIAL, TWIST1, FFT1, JOIN, TWIST2, FFT2, TWIST3, END
  33. };
  34. #define NUMBER_BITS 5
  35. #define NUMBER_SHIFT (64 - NUMBER_BITS)
  36. #define STEP_BITS 3
  37. #define STEP_SHIFT (NUMBER_SHIFT - STEP_BITS)
  38. /* Tags for the steps of the parallel variant */
  39. #define _STEP_TAG(plan, step, i) (((starpu_tag_t) plan->number << NUMBER_SHIFT) | ((starpu_tag_t)(step) << STEP_SHIFT) | (starpu_tag_t) (i))
  40. #define I_BITS STEP_SHIFT
  41. enum type {
  42. R2C,
  43. C2R,
  44. C2C
  45. };
  46. static unsigned task_per_worker[STARPU_NMAXWORKERS];
  47. static unsigned samples_per_worker[STARPU_NMAXWORKERS];
  48. static struct timeval start, submit_tasks, end;
  49. /*
  50. *
  51. * The actual kernels
  52. *
  53. */
  54. struct STARPUFFT(plan) {
  55. int number; /* uniquely identifies the plan, for starpu tags */
  56. int *n;
  57. int *n1;
  58. int *n2;
  59. int totsize;
  60. int totsize1; /* Number of first-round tasks */
  61. int totsize2; /* Size of first-round tasks */
  62. int totsize3; /* Number of second-round tasks */
  63. int totsize4; /* Size of second-round tasks */
  64. int dim;
  65. enum type type;
  66. int sign;
  67. STARPUFFT(complex) *roots[2];
  68. starpu_data_handle_t roots_handle[2];
  69. /* For each worker, we need some data */
  70. struct {
  71. #ifdef STARPU_USE_CUDA
  72. /* CUFFT plans */
  73. cufftHandle plan1_cuda, plan2_cuda;
  74. /* Sequential version */
  75. cufftHandle plan_cuda;
  76. #endif
  77. #ifdef STARPU_HAVE_FFTW
  78. /* FFTW plans */
  79. _fftw_plan plan1_cpu, plan2_cpu;
  80. /* Sequential version */
  81. _fftw_plan plan_cpu;
  82. #endif
  83. } plans[STARPU_NMAXWORKERS];
  84. /* Buffers for codelets */
  85. STARPUFFT(complex) *in, *twisted1, *fft1, *twisted2, *fft2, *out;
  86. /* corresponding starpu DSM handles */
  87. starpu_data_handle_t in_handle, *twisted1_handle, *fft1_handle, *twisted2_handle, *fft2_handle, out_handle;
  88. /* Tasks */
  89. struct starpu_task **twist1_tasks, **fft1_tasks, **twist2_tasks, **fft2_tasks, **twist3_tasks;
  90. struct starpu_task *join_task, *end_task;
  91. /* Arguments for tasks */
  92. struct STARPUFFT(args) *fft1_args, *fft2_args;
  93. };
  94. struct STARPUFFT(args) {
  95. struct STARPUFFT(plan) *plan;
  96. int i, j, jj, kk, ll, *iv, *kkv;
  97. };
  98. static void
  99. check_dims(STARPUFFT(plan) plan)
  100. {
  101. int dim;
  102. for (dim = 0; dim < plan->dim; dim++)
  103. if (plan->n[dim] & (plan->n[dim]-1)) {
  104. fprintf(stderr,"can't cope with non-power-of-2\n");
  105. STARPU_ABORT();
  106. }
  107. }
  108. static void
  109. compute_roots(STARPUFFT(plan) plan)
  110. {
  111. int dim, k;
  112. /* Compute the n-roots and m-roots of unity for twiddling */
  113. for (dim = 0; dim < plan->dim; dim++) {
  114. STARPUFFT(complex) exp = (plan->sign * 2. * 4.*atan(1.)) * _Complex_I / (STARPUFFT(complex)) plan->n[dim];
  115. plan->roots[dim] = malloc(plan->n[dim] * sizeof(**plan->roots));
  116. for (k = 0; k < plan->n[dim]; k++)
  117. plan->roots[dim][k] = cexp(exp*k);
  118. starpu_vector_data_register(&plan->roots_handle[dim], 0, (uintptr_t) plan->roots[dim], plan->n[dim], sizeof(**plan->roots));
  119. #ifdef STARPU_USE_CUDA
  120. if (plan->n[dim] > 100000) {
  121. /* prefetch the big root array on GPUs */
  122. unsigned worker;
  123. unsigned nworkers = starpu_worker_get_count();
  124. for (worker = 0; worker < nworkers; worker++)
  125. {
  126. unsigned node = starpu_worker_get_memory_node(worker);
  127. if (starpu_worker_get_type(worker) == STARPU_CUDA_WORKER)
  128. starpu_data_prefetch_on_node(plan->roots_handle[dim], node, 0);
  129. }
  130. }
  131. #endif
  132. }
  133. }
  134. #include "starpufftx1d.c"
  135. #include "starpufftx2d.c"
  136. struct starpu_task *
  137. STARPUFFT(start)(STARPUFFT(plan) plan, void *_in, void *_out)
  138. {
  139. struct starpu_task *task;
  140. int z;
  141. plan->in = _in;
  142. plan->out = _out;
  143. switch (plan->dim) {
  144. case 1: {
  145. switch (plan->type) {
  146. case C2C:
  147. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  148. if (!PARALLEL)
  149. starpu_vector_data_register(&plan->out_handle, 0, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  150. if (PARALLEL) {
  151. for (z = 0; z < plan->totsize1; z++)
  152. plan->twist1_tasks[z]->buffers[0].handle = plan->in_handle;
  153. }
  154. task = STARPUFFT(start1dC2C)(plan, plan->in_handle, plan->out_handle);
  155. break;
  156. default:
  157. STARPU_ABORT();
  158. break;
  159. }
  160. break;
  161. }
  162. case 2:
  163. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  164. if (!PARALLEL)
  165. starpu_vector_data_register(&plan->out_handle, 0, (uintptr_t) plan->out, plan->totsize, sizeof(STARPUFFT(complex)));
  166. if (PARALLEL) {
  167. for (z = 0; z < plan->totsize1; z++)
  168. plan->twist1_tasks[z]->buffers[0].handle = plan->in_handle;
  169. }
  170. task = STARPUFFT(start2dC2C)(plan, plan->in_handle, plan->out_handle);
  171. break;
  172. default:
  173. STARPU_ABORT();
  174. break;
  175. }
  176. return task;
  177. }
  178. void
  179. STARPUFFT(cleanup)(STARPUFFT(plan) plan)
  180. {
  181. if (plan->in_handle)
  182. starpu_data_unregister(plan->in_handle);
  183. if (!PARALLEL) {
  184. if (plan->out_handle)
  185. starpu_data_unregister(plan->out_handle);
  186. }
  187. }
  188. struct starpu_task *
  189. STARPUFFT(start_handle)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  190. {
  191. return STARPUFFT(start1dC2C)(plan, in, out);
  192. }
  193. void
  194. STARPUFFT(execute)(STARPUFFT(plan) plan, void *in, void *out)
  195. {
  196. memset(task_per_worker, 0, sizeof(task_per_worker));
  197. memset(samples_per_worker, 0, sizeof(task_per_worker));
  198. gettimeofday(&start, NULL);
  199. struct starpu_task *task = STARPUFFT(start)(plan, in, out);
  200. gettimeofday(&submit_tasks, NULL);
  201. starpu_task_wait(task);
  202. STARPUFFT(cleanup)(plan);
  203. gettimeofday(&end, NULL);
  204. }
  205. void
  206. STARPUFFT(execute_handle)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  207. {
  208. struct starpu_task *task = STARPUFFT(start_handle)(plan, in, out);
  209. starpu_task_wait(task);
  210. }
  211. /* Destroy FFTW plans, unregister and free buffers, and free tags */
  212. void
  213. STARPUFFT(destroy_plan)(STARPUFFT(plan) plan)
  214. {
  215. int workerid, dim, i;
  216. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  217. switch (starpu_worker_get_type(workerid)) {
  218. case STARPU_CPU_WORKER:
  219. #ifdef STARPU_HAVE_FFTW
  220. if (PARALLEL) {
  221. _FFTW(destroy_plan)(plan->plans[workerid].plan1_cpu);
  222. _FFTW(destroy_plan)(plan->plans[workerid].plan2_cpu);
  223. } else {
  224. _FFTW(destroy_plan)(plan->plans[workerid].plan_cpu);
  225. }
  226. #endif
  227. break;
  228. case STARPU_CUDA_WORKER:
  229. #ifdef STARPU_USE_CUDA
  230. /* FIXME: Can't deallocate */
  231. #endif
  232. break;
  233. default:
  234. /* Do not care, we won't be executing anything there. */
  235. break;
  236. }
  237. }
  238. if (PARALLEL) {
  239. for (i = 0; i < plan->totsize1; i++) {
  240. starpu_data_unregister(plan->twisted1_handle[i]);
  241. free(plan->twist1_tasks[i]);
  242. starpu_data_unregister(plan->fft1_handle[i]);
  243. free(plan->fft1_tasks[i]);
  244. }
  245. free(plan->twisted1_handle);
  246. free(plan->twist1_tasks);
  247. free(plan->fft1_handle);
  248. free(plan->fft1_tasks);
  249. free(plan->fft1_args);
  250. free(plan->join_task);
  251. for (i = 0; i < plan->totsize3; i++) {
  252. starpu_data_unregister(plan->twisted2_handle[i]);
  253. free(plan->twist2_tasks[i]);
  254. starpu_data_unregister(plan->fft2_handle[i]);
  255. free(plan->fft2_tasks[i]);
  256. free(plan->twist3_tasks[i]);
  257. }
  258. free(plan->end_task);
  259. free(plan->twisted2_handle);
  260. free(plan->twist2_tasks);
  261. free(plan->fft2_handle);
  262. free(plan->fft2_tasks);
  263. free(plan->twist3_tasks);
  264. free(plan->fft2_args);
  265. for (dim = 0; dim < plan->dim; dim++) {
  266. starpu_data_unregister(plan->roots_handle[dim]);
  267. free(plan->roots[dim]);
  268. }
  269. switch (plan->dim) {
  270. case 1:
  271. STARPUFFT(free_1d_tags)(plan);
  272. break;
  273. case 2:
  274. STARPUFFT(free_2d_tags)(plan);
  275. break;
  276. default:
  277. STARPU_ABORT();
  278. break;
  279. }
  280. free(plan->n1);
  281. free(plan->n2);
  282. STARPUFFT(free)(plan->twisted1);
  283. STARPUFFT(free)(plan->fft1);
  284. STARPUFFT(free)(plan->twisted2);
  285. STARPUFFT(free)(plan->fft2);
  286. }
  287. free(plan->n);
  288. free(plan);
  289. }
  290. void *
  291. STARPUFFT(malloc)(size_t n)
  292. {
  293. #ifdef STARPU_USE_CUDA
  294. void *res;
  295. starpu_malloc(&res, n);
  296. return res;
  297. #else
  298. # ifdef STARPU_HAVE_FFTW
  299. return _FFTW(malloc)(n);
  300. # else
  301. return malloc(n);
  302. # endif
  303. #endif
  304. }
  305. void
  306. STARPUFFT(free)(void *p)
  307. {
  308. #ifdef STARPU_USE_CUDA
  309. starpu_free(p);
  310. #else
  311. # ifdef STARPU_HAVE_FFTW
  312. _FFTW(free)(p);
  313. # else
  314. free(p);
  315. # endif
  316. #endif
  317. }
  318. void
  319. STARPUFFT(showstats)(FILE *out)
  320. {
  321. int worker;
  322. unsigned total;
  323. #define TIMING(begin,end) (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec))
  324. #define MSTIMING(begin,end) (TIMING(begin,end)/1000.)
  325. double paratiming = TIMING(start,end);
  326. fprintf(out, "Tasks submission took %2.2f ms\n", MSTIMING(start,submit_tasks));
  327. fprintf(out, "Tasks termination took %2.2f ms\n", MSTIMING(submit_tasks,end));
  328. fprintf(out, "Total %2.2f ms\n", MSTIMING(start,end));
  329. for (worker = 0, total = 0; worker < starpu_worker_get_count(); worker++)
  330. total += task_per_worker[worker];
  331. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  332. {
  333. if (task_per_worker[worker])
  334. {
  335. char name[32];
  336. starpu_worker_get_name(worker, name, sizeof(name));
  337. unsigned long bytes = sizeof(STARPUFFT(complex))*samples_per_worker[worker];
  338. fprintf(stderr, "\t%s -> %2.2f MB\t%2.2f\tMB/s\t%u %2.2f %%\n", name, (1.0*bytes)/(1024*1024), bytes/paratiming, task_per_worker[worker], (100.0*task_per_worker[worker])/total);
  339. }
  340. }
  341. }