testx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-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. #include <complex.h>
  18. #include <math.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include <sys/time.h>
  23. #include <starpu.h>
  24. #include <starpu_config.h>
  25. #include "starpufft.h"
  26. #undef STARPU_USE_CUDA
  27. #ifdef STARPU_HAVE_FFTW
  28. #include <fftw3.h>
  29. #endif
  30. #ifdef STARPU_USE_CUDA
  31. #include <cufft.h>
  32. #endif
  33. #define SIGN (-1)
  34. /* #define SIGN (1) */
  35. #ifdef STARPU_HAVE_FFTW
  36. static void check_fftw(STARPUFFT(complex) *out, STARPUFFT(complex) *out_fftw, int size)
  37. {
  38. int i;
  39. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  40. for (i = 0; i < size; i++) {
  41. double diff = cabs(out[i]-out_fftw[i]);
  42. double diff2 = diff * diff;
  43. double size = cabs(out_fftw[i]);
  44. double size2 = size * size;
  45. if (diff > max)
  46. max = diff;
  47. tot += diff;
  48. normdiff += diff2;
  49. norm += size2;
  50. }
  51. fprintf(stderr, "\nmaximum difference %g\n", max);
  52. fprintf(stderr, "average difference %g\n", tot / size);
  53. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  54. double relmaxdiff = max / sqrt(norm);
  55. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  56. double relavgdiff = (tot / size) / sqrt(norm);
  57. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  58. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-8 || relavgdiff > 1e-8))
  59. exit(EXIT_FAILURE);
  60. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  61. exit(EXIT_FAILURE);
  62. }
  63. #endif
  64. #ifdef STARPU_USE_CUDA
  65. static void check_cuda(STARPUFFT(complex) *out, STARPUFFT(complex) *out_fftw, int size)
  66. {
  67. int i;
  68. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  69. for (i = 0; i < size; i++) {
  70. double diff = cabs(out_cuda[i]-out_fftw[i]);
  71. double diff2 = diff * diff;
  72. double size = cabs(out_fftw[i]);
  73. double size2 = size * size;
  74. if (diff > max)
  75. max = diff;
  76. tot += diff;
  77. normdiff += diff2;
  78. norm += size2;
  79. }
  80. fprintf(stderr, "\nmaximum difference %g\n", max);
  81. fprintf(stderr, "average difference %g\n", tot / size);
  82. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  83. double relmaxdiff = max / sqrt(norm);
  84. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  85. double relavgdiff = (tot / size) / sqrt(norm);
  86. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  87. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-8 || relavgdiff > 1e-8))
  88. exit(EXIT_FAILURE);
  89. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  90. exit(EXIT_FAILURE);
  91. }
  92. #endif
  93. int main(int argc, char *argv[]) {
  94. int i;
  95. struct timeval begin, end;
  96. int size;
  97. size_t bytes;
  98. int n = 0, m = 0;
  99. STARPUFFT(plan) plan;
  100. starpu_data_handle_t in_handle, out_handle;
  101. #ifdef STARPU_HAVE_FFTW
  102. _FFTW(plan) fftw_plan;
  103. #endif
  104. #ifdef STARPU_USE_CUDA
  105. cufftHandle cuda_plan;
  106. cudaError_t cures;
  107. #endif
  108. double timing;
  109. if (argc < 2 || argc > 3) {
  110. fprintf(stderr,"need one or two size of vector\n");
  111. exit(EXIT_FAILURE);
  112. }
  113. starpu_init(NULL);
  114. if (argc == 2) {
  115. n = atoi(argv[1]);
  116. /* 1D */
  117. size = n;
  118. } else if (argc == 3) {
  119. n = atoi(argv[1]);
  120. m = atoi(argv[2]);
  121. /* 2D */
  122. size = n * m;
  123. } else {
  124. assert(0);
  125. }
  126. bytes = size * sizeof(STARPUFFT(complex));
  127. STARPUFFT(complex) *in = STARPUFFT(malloc)(size * sizeof(*in));
  128. starpu_srand48(0);
  129. for (i = 0; i < size; i++)
  130. in[i] = starpu_drand48() + I * starpu_drand48();
  131. STARPUFFT(complex) *out = STARPUFFT(malloc)(size * sizeof(*out));
  132. #ifdef STARPU_HAVE_FFTW
  133. STARPUFFT(complex) *out_fftw = STARPUFFT(malloc)(size * sizeof(*out_fftw));
  134. #endif
  135. #ifdef STARPU_USE_CUDA
  136. STARPUFFT(complex) *out_cuda = STARPUFFT(malloc)(size * sizeof(*out_cuda));
  137. #endif
  138. if (argc == 2) {
  139. plan = STARPUFFT(plan_dft_1d)(n, SIGN, 0);
  140. #ifdef STARPU_HAVE_FFTW
  141. fftw_plan = _FFTW(plan_dft_1d)(n, NULL, NULL, SIGN, FFTW_ESTIMATE);
  142. #endif
  143. #ifdef STARPU_USE_CUDA
  144. if (cufftPlan1d(&cuda_plan, n, _CUFFT_C2C, 1) != CUFFT_SUCCESS)
  145. printf("erf\n");
  146. #endif
  147. } else if (argc == 3) {
  148. plan = STARPUFFT(plan_dft_2d)(n, m, SIGN, 0);
  149. #ifdef STARPU_HAVE_FFTW
  150. fftw_plan = _FFTW(plan_dft_2d)(n, m, NULL, NULL, SIGN, FFTW_ESTIMATE);
  151. #endif
  152. #ifdef STARPU_USE_CUDA
  153. STARPU_ASSERT(cufftPlan2d(&cuda_plan, n, m, _CUFFT_C2C) == CUFFT_SUCCESS);
  154. #endif
  155. } else {
  156. assert(0);
  157. }
  158. #ifdef STARPU_HAVE_FFTW
  159. gettimeofday(&begin, NULL);
  160. _FFTW(execute_dft)(fftw_plan, in, out_fftw);
  161. gettimeofday(&end, NULL);
  162. _FFTW(destroy_plan)(fftw_plan);
  163. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  164. printf("FFTW took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  165. #endif
  166. #ifdef STARPU_USE_CUDA
  167. gettimeofday(&begin, NULL);
  168. if (cufftExecC2C(cuda_plan, (cufftComplex*) in, (cufftComplex*) out_cuda, CUFFT_FORWARD) != CUFFT_SUCCESS)
  169. printf("erf2\n");
  170. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  171. STARPU_CUDA_REPORT_ERROR(cures);
  172. gettimeofday(&end, NULL);
  173. cufftDestroy(cuda_plan);
  174. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  175. printf("CUDA took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  176. #endif
  177. STARPUFFT(execute)(plan, in, out);
  178. STARPUFFT(showstats)(stdout);
  179. #ifdef STARPU_HAVE_FFTW
  180. check_fftw(out, out_fftw, size);
  181. #endif
  182. #ifdef STARPU_USE_CUDA
  183. check_cuda(out, out_cuda, size);
  184. #endif
  185. #if 1
  186. starpu_vector_data_register(&in_handle, 0, (uintptr_t) in, size, sizeof(*in));
  187. starpu_vector_data_register(&out_handle, 0, (uintptr_t) out, size, sizeof(*out));
  188. STARPUFFT(execute_handle)(plan, in_handle, out_handle);
  189. starpu_data_unregister(in_handle);
  190. starpu_data_unregister(out_handle);
  191. #ifdef STARPU_HAVE_FFTW
  192. check_fftw(out, out_fftw, size);
  193. #endif
  194. #ifdef STARPU_USE_CUDA
  195. check_cuda(out, out_cuda, size);
  196. #endif
  197. #endif
  198. STARPUFFT(showstats)(stdout);
  199. STARPUFFT(destroy_plan)(plan);
  200. printf("\n");
  201. #if 0
  202. for (i = 0; i < 16; i++)
  203. printf("(%f,%f) ", cimag(in[i]), creal(in[i]));
  204. printf("\n\n");
  205. for (i = 0; i < 16; i++)
  206. printf("(%f,%f) ", cimag(out[i]), creal(out[i]));
  207. printf("\n\n");
  208. #ifdef STARPU_HAVE_FFTW
  209. for (i = 0; i < 16; i++)
  210. printf("(%f,%f) ", cimag(out_fftw[i]), creal(out_fftw[i]));
  211. printf("\n\n");
  212. #endif
  213. #endif
  214. STARPUFFT(free)(in);
  215. STARPUFFT(free)(out);
  216. #ifdef STARPU_HAVE_FFTW
  217. STARPUFFT(free)(out_fftw);
  218. #endif
  219. #ifdef STARPU_USE_CUDA
  220. free(out_cuda);
  221. #endif
  222. starpu_shutdown();
  223. return EXIT_SUCCESS;
  224. }