starpufftx.c 9.2 KB

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