starpufftx3d.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 CNRS
  4. * Copyright (C) 2013,2014, 2019 Université de Bordeaux
  5. * Copyright (C) 2012,2013 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. *
  20. * Sequential version
  21. *
  22. */
  23. #ifdef __STARPU_USE_CUDA
  24. /* Perform one fft of size n,m */
  25. static void
  26. STARPUFFT(fft_3d_plan_gpu)(void *args)
  27. {
  28. STARPUFFT(plan) plan = args;
  29. cufftResult cures;
  30. int n = plan->n[0];
  31. int m = plan->n[1];
  32. int p = plan->n[2];
  33. int workerid = starpu_worker_get_id_check();
  34. cures = cufftPlan3d(&plan->plans[workerid].plan_cuda, n, m, p, _CUFFT_C2C);
  35. if (cures != CUFFT_SUCCESS)
  36. STARPU_CUFFT_REPORT_ERROR(cures);
  37. cufftSetStream(plan->plans[workerid].plan_cuda, starpu_cuda_get_local_stream());
  38. if (cures != CUFFT_SUCCESS)
  39. STARPU_CUFFT_REPORT_ERROR(cures);
  40. }
  41. static void
  42. STARPUFFT(fft_3d_kernel_gpu)(void *descr[], void *args)
  43. {
  44. STARPUFFT(plan) plan = args;
  45. cufftResult cures;
  46. _cufftComplex * restrict in = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[0]);
  47. _cufftComplex * restrict out = (_cufftComplex *)STARPU_VECTOR_GET_PTR(descr[1]);
  48. int workerid = starpu_worker_get_id_check();
  49. task_per_worker[workerid]++;
  50. cures = _cufftExecC2C(plan->plans[workerid].plan_cuda, in, out, plan->sign == -1 ? CUFFT_FORWARD : CUFFT_INVERSE);
  51. if (cures != CUFFT_SUCCESS)
  52. STARPU_CUFFT_REPORT_ERROR(cures);
  53. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  54. }
  55. #endif
  56. #ifdef STARPU_HAVE_FFTW
  57. /* Perform one fft of size n,m */
  58. static void
  59. STARPUFFT(fft_3d_kernel_cpu)(void *descr[], void *_args)
  60. {
  61. STARPUFFT(plan) plan = _args;
  62. int workerid = starpu_worker_get_id_check();
  63. task_per_worker[workerid]++;
  64. STARPUFFT(complex) * restrict in = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[0]);
  65. STARPUFFT(complex) * restrict out = (STARPUFFT(complex) *)STARPU_VECTOR_GET_PTR(descr[1]);
  66. _FFTW(execute_dft)(plan->plans[workerid].plan_cpu, in, out);
  67. }
  68. #endif
  69. static struct starpu_perfmodel STARPUFFT(fft_3d_model) = {
  70. .type = STARPU_HISTORY_BASED,
  71. .symbol = TYPE"fft_3d"
  72. };
  73. static struct starpu_codelet STARPUFFT(fft_3d_codelet) = {
  74. .where =
  75. #ifdef __STARPU_USE_CUDA
  76. STARPU_CUDA|
  77. #endif
  78. #ifdef STARPU_HAVE_FFTW
  79. STARPU_CPU|
  80. #endif
  81. 0,
  82. #ifdef __STARPU_USE_CUDA
  83. .cuda_funcs = {STARPUFFT(fft_3d_kernel_gpu)},
  84. #endif
  85. #ifdef STARPU_HAVE_FFTW
  86. .cpu_funcs = {STARPUFFT(fft_3d_kernel_cpu)},
  87. #endif
  88. CAN_EXECUTE
  89. .model = &STARPUFFT(fft_3d_model),
  90. .nbuffers = 2,
  91. .modes = {STARPU_R, STARPU_W},
  92. .name = "fft_3d_codelet"
  93. };
  94. STARPUFFT(plan)
  95. STARPUFFT(plan_dft_3d)(int n, int m, int p, int sign, unsigned flags)
  96. {
  97. unsigned workerid;
  98. if (PARALLEL) {
  99. /* TODO */
  100. STARPU_ASSERT(0);
  101. }
  102. /* TODO: flags? Automatically set FFTW_MEASURE on calibration? */
  103. STARPU_ASSERT(flags == 0);
  104. STARPUFFT(plan) plan = malloc(sizeof(*plan));
  105. memset(plan, 0, sizeof(*plan));
  106. plan->dim = 3;
  107. plan->n = malloc(plan->dim * sizeof(*plan->n));
  108. plan->n[0] = n;
  109. plan->n[1] = m;
  110. plan->n[2] = p;
  111. plan->totsize = n * m;
  112. plan->type = C2C;
  113. plan->sign = sign;
  114. /* Initialize per-worker working set */
  115. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) {
  116. switch (starpu_worker_get_type(workerid)) {
  117. case STARPU_CPU_WORKER:
  118. #ifdef STARPU_HAVE_FFTW
  119. /* fft plan: one fft of size n, m. */
  120. plan->plans[workerid].plan_cpu = _FFTW(plan_dft_3d)(n, m, p, NULL, (void*) 1, sign, _FFTW_FLAGS);
  121. STARPU_ASSERT(plan->plans[workerid].plan_cpu);
  122. #else
  123. /* #warning libstarpufft can not work correctly if libfftw3 is not installed */
  124. #endif
  125. break;
  126. case STARPU_CUDA_WORKER:
  127. break;
  128. default:
  129. /* Do not care, we won't be executing anything there. */
  130. break;
  131. }
  132. }
  133. #ifdef __STARPU_USE_CUDA
  134. starpu_execute_on_each_worker(STARPUFFT(fft_3d_plan_gpu), plan, STARPU_CUDA);
  135. #endif
  136. return plan;
  137. }
  138. /* Actually submit all the tasks. */
  139. static struct starpu_task *
  140. STARPUFFT(start3dC2C)(STARPUFFT(plan) plan, starpu_data_handle_t in, starpu_data_handle_t out)
  141. {
  142. STARPU_ASSERT(plan->type == C2C);
  143. int z;
  144. int ret;
  145. if (PARALLEL) {
  146. /* TODO */
  147. STARPU_ASSERT(0);
  148. } else /* !PARALLEL */ {
  149. struct starpu_task *task;
  150. /* Create FFT task */
  151. task = starpu_task_create();
  152. task->detach = 0;
  153. task->cl = &STARPUFFT(fft_3d_codelet);
  154. task->handles[0] = in;
  155. task->handles[1] = out;
  156. task->cl_arg = plan;
  157. ret = starpu_task_submit(task);
  158. if (ret == -ENODEV) return NULL;
  159. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  160. return task;
  161. }
  162. }