testx.c 6.9 KB

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