starpufftx.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 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. #endif
  73. #ifdef STARPU_HAVE_FFTW
  74. /* FFTW plans */
  75. _fftw_plan plan1_cpu, plan2_cpu;
  76. /* Buffers used by the plans above */
  77. _fftw_complex *in1, *out1;
  78. _fftw_complex *in2, *out2;
  79. #endif
  80. } plans[STARPU_NMAXWORKERS];
  81. /* Buffers for codelets */
  82. STARPUFFT(complex) *in, *twisted1, *fft1, *twisted2, *fft2, *out;
  83. /* corresponding starpu DSM handles */
  84. starpu_data_handle in_handle, *twisted1_handle, *fft1_handle, *twisted2_handle, *fft2_handle;
  85. /* Tasks */
  86. struct starpu_task **twist1_tasks, **fft1_tasks, **twist2_tasks, **fft2_tasks, **twist3_tasks;
  87. struct starpu_task *join_task, *end_task;
  88. /* Arguments for tasks */
  89. struct STARPUFFT(args) *fft1_args, *fft2_args;
  90. };
  91. struct STARPUFFT(args) {
  92. struct STARPUFFT(plan) *plan;
  93. int i, j, jj, kk, ll, *iv, *kkv;
  94. };
  95. static void
  96. check_dims(STARPUFFT(plan) plan)
  97. {
  98. int dim;
  99. for (dim = 0; dim < plan->dim; dim++)
  100. if (plan->n[dim] & (plan->n[dim]-1)) {
  101. fprintf(stderr,"can't cope with non-power-of-2\n");
  102. STARPU_ABORT();
  103. }
  104. }
  105. static void
  106. compute_roots(STARPUFFT(plan) plan)
  107. {
  108. int dim, k;
  109. /* Compute the n-roots and m-roots of unity for twiddling */
  110. for (dim = 0; dim < plan->dim; dim++) {
  111. STARPUFFT(complex) exp = (plan->sign * 2. * 4.*atan(1.)) * _Complex_I / (STARPUFFT(complex)) plan->n[dim];
  112. plan->roots[dim] = malloc(plan->n[dim] * sizeof(**plan->roots));
  113. for (k = 0; k < plan->n[dim]; k++)
  114. plan->roots[dim][k] = cexp(exp*k);
  115. starpu_vector_data_register(&plan->roots_handle[dim], 0, (uintptr_t) plan->roots[dim], plan->n[dim], sizeof(**plan->roots));
  116. #ifdef STARPU_USE_CUDA
  117. if (plan->n[dim] > 100000) {
  118. /* prefetch the big root array on GPUs */
  119. unsigned worker;
  120. unsigned nworkers = starpu_worker_get_count();
  121. for (worker = 0; worker < nworkers; worker++)
  122. {
  123. unsigned node = starpu_worker_get_memory_node(worker);
  124. if (starpu_worker_get_type(worker) == STARPU_CUDA_WORKER)
  125. starpu_data_prefetch_on_node(plan->roots_handle[dim], node, 0);
  126. }
  127. }
  128. #endif
  129. }
  130. }
  131. #include "starpufftx1d.c"
  132. #include "starpufftx2d.c"
  133. starpu_tag_t
  134. STARPUFFT(start)(STARPUFFT(plan) plan, void *_in, void *_out)
  135. {
  136. starpu_tag_t tag;
  137. int z;
  138. plan->in = _in;
  139. plan->out = _out;
  140. switch (plan->dim) {
  141. case 1: {
  142. switch (plan->type) {
  143. case C2C:
  144. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  145. for (z = 0; z < plan->totsize1; z++)
  146. plan->twist1_tasks[z]->buffers[0].handle = plan->in_handle;
  147. tag = STARPUFFT(start1dC2C)(plan);
  148. break;
  149. default:
  150. STARPU_ABORT();
  151. break;
  152. }
  153. break;
  154. }
  155. case 2:
  156. starpu_vector_data_register(&plan->in_handle, 0, (uintptr_t) plan->in, plan->totsize, sizeof(STARPUFFT(complex)));
  157. for (z = 0; z < plan->totsize1; z++)
  158. plan->twist1_tasks[z]->buffers[0].handle = plan->in_handle;
  159. tag = STARPUFFT(start2dC2C)(plan);
  160. break;
  161. default:
  162. STARPU_ABORT();
  163. break;
  164. }
  165. return tag;
  166. }
  167. void
  168. STARPUFFT(cleanup)(STARPUFFT(plan) plan)
  169. {
  170. starpu_data_unregister(plan->in_handle);
  171. }
  172. void
  173. STARPUFFT(execute)(STARPUFFT(plan) plan, void *in, void *out)
  174. {
  175. memset(task_per_worker, 0, sizeof(task_per_worker));
  176. memset(samples_per_worker, 0, sizeof(task_per_worker));
  177. gettimeofday(&start, NULL);
  178. starpu_tag_t tag = STARPUFFT(start)(plan, in, out);
  179. gettimeofday(&submit_tasks, NULL);
  180. starpu_tag_wait(tag);
  181. STARPUFFT(cleanup)(plan);
  182. gettimeofday(&end, NULL);
  183. }
  184. /* Destroy FFTW plans, unregister and free buffers, and free tags */
  185. void
  186. STARPUFFT(destroy_plan)(STARPUFFT(plan) plan)
  187. {
  188. int workerid, dim, i;
  189. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  190. switch (starpu_worker_get_type(workerid)) {
  191. case STARPU_CPU_WORKER:
  192. #ifdef STARPU_HAVE_FFTW
  193. _FFTW(free)(plan->plans[workerid].in1);
  194. _FFTW(free)(plan->plans[workerid].out1);
  195. _FFTW(destroy_plan)(plan->plans[workerid].plan1_cpu);
  196. _FFTW(free)(plan->plans[workerid].in2);
  197. _FFTW(free)(plan->plans[workerid].out2);
  198. _FFTW(destroy_plan)(plan->plans[workerid].plan2_cpu);
  199. #endif
  200. break;
  201. case STARPU_CUDA_WORKER:
  202. #ifdef STARPU_USE_CUDA
  203. /* FIXME: Can't deallocate */
  204. #endif
  205. break;
  206. default:
  207. STARPU_ABORT();
  208. break;
  209. }
  210. }
  211. for (i = 0; i < plan->totsize1; i++) {
  212. starpu_data_unregister(plan->twisted1_handle[i]);
  213. free(plan->twist1_tasks[i]);
  214. starpu_data_unregister(plan->fft1_handle[i]);
  215. free(plan->fft1_tasks[i]);
  216. }
  217. free(plan->twisted1_handle);
  218. free(plan->twist1_tasks);
  219. free(plan->fft1_handle);
  220. free(plan->fft1_tasks);
  221. free(plan->fft1_args);
  222. free(plan->join_task);
  223. for (i = 0; i < plan->totsize3; i++) {
  224. starpu_data_unregister(plan->twisted2_handle[i]);
  225. free(plan->twist2_tasks[i]);
  226. starpu_data_unregister(plan->fft2_handle[i]);
  227. free(plan->fft2_tasks[i]);
  228. free(plan->twist3_tasks[i]);
  229. }
  230. free(plan->end_task);
  231. free(plan->twisted2_handle);
  232. free(plan->twist2_tasks);
  233. free(plan->fft2_handle);
  234. free(plan->fft2_tasks);
  235. free(plan->twist3_tasks);
  236. free(plan->fft2_args);
  237. for (dim = 0; dim < plan->dim; dim++) {
  238. starpu_data_unregister(plan->roots_handle[dim]);
  239. free(plan->roots[dim]);
  240. }
  241. switch (plan->dim) {
  242. case 1:
  243. STARPUFFT(free_1d_tags)(plan);
  244. break;
  245. case 2:
  246. STARPUFFT(free_2d_tags)(plan);
  247. break;
  248. default:
  249. STARPU_ABORT();
  250. break;
  251. }
  252. free(plan->n);
  253. free(plan->n1);
  254. free(plan->n2);
  255. STARPUFFT(free)(plan->twisted1);
  256. STARPUFFT(free)(plan->fft1);
  257. STARPUFFT(free)(plan->twisted2);
  258. STARPUFFT(free)(plan->fft2);
  259. free(plan);
  260. }
  261. void *
  262. STARPUFFT(malloc)(size_t n)
  263. {
  264. #ifdef STARPU_USE_CUDA
  265. void *res;
  266. starpu_data_malloc_pinned_if_possible(&res, n);
  267. return res;
  268. #else
  269. # ifdef STARPU_HAVE_FFTW
  270. return _FFTW(malloc)(n);
  271. # else
  272. return malloc(n);
  273. # endif
  274. #endif
  275. }
  276. void
  277. STARPUFFT(free)(void *p)
  278. {
  279. #ifdef STARPU_USE_CUDA
  280. // TODO: FIXME
  281. #else
  282. # ifdef STARPU_HAVE_FFTW
  283. _FFTW(free)(p);
  284. # else
  285. free(p);
  286. # endif
  287. #endif
  288. }
  289. void
  290. STARPUFFT(showstats)(FILE *out)
  291. {
  292. int worker;
  293. unsigned total;
  294. #define TIMING(begin,end) (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec))
  295. #define MSTIMING(begin,end) (TIMING(begin,end)/1000.)
  296. double paratiming = TIMING(start,end);
  297. fprintf(out, "Tasks submission took %2.2f ms\n", MSTIMING(start,submit_tasks));
  298. fprintf(out, "Tasks termination took %2.2f ms\n", MSTIMING(submit_tasks,end));
  299. fprintf(out, "Total %2.2f ms\n", MSTIMING(start,end));
  300. for (worker = 0, total = 0; worker < starpu_worker_get_count(); worker++)
  301. total += task_per_worker[worker];
  302. for (worker = 0; worker < starpu_worker_get_count(); worker++)
  303. {
  304. if (task_per_worker[worker])
  305. {
  306. char name[32];
  307. starpu_worker_get_name(worker, name, 32);
  308. unsigned long bytes = sizeof(STARPUFFT(complex))*samples_per_worker[worker];
  309. 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);
  310. }
  311. }
  312. }