testx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. struct starpu_conf conf;
  118. #ifdef STARPU_DEVEL
  119. # warning we disable cuda for now, as the test keeps failing when running on cuda devices
  120. #endif
  121. starpu_conf_init(&conf);
  122. conf.ncuda = 0;
  123. ret = starpu_init(&conf);
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  125. if (argc == 1)
  126. {
  127. n = 42;
  128. /* 1D */
  129. size = n;
  130. }
  131. else if (argc == 2)
  132. {
  133. n = atoi(argv[1]);
  134. /* 1D */
  135. size = n;
  136. }
  137. else if (argc == 3)
  138. {
  139. n = atoi(argv[1]);
  140. m = atoi(argv[2]);
  141. /* 2D */
  142. size = n * m;
  143. }
  144. else
  145. {
  146. assert(0);
  147. }
  148. bytes = size * sizeof(STARPUFFT(complex));
  149. STARPUFFT(complex) *in = STARPUFFT(malloc)(size * sizeof(*in));
  150. starpu_srand48(0);
  151. for (i = 0; i < size; i++)
  152. in[i] = starpu_drand48() + I * starpu_drand48();
  153. STARPUFFT(complex) *out = STARPUFFT(malloc)(size * sizeof(*out));
  154. #ifdef STARPU_HAVE_FFTW
  155. STARPUFFT(complex) *out_fftw = STARPUFFT(malloc)(size * sizeof(*out_fftw));
  156. #endif
  157. #ifdef STARPU_USE_CUDA
  158. STARPUFFT(complex) *out_cuda = STARPUFFT(malloc)(size * sizeof(*out_cuda));
  159. #endif
  160. if (argc <= 2)
  161. {
  162. plan = STARPUFFT(plan_dft_1d)(n, SIGN, 0);
  163. #ifdef STARPU_HAVE_FFTW
  164. fftw_plan = _FFTW(plan_dft_1d)(n, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  165. #endif
  166. #ifdef STARPU_USE_CUDA
  167. if (cufftPlan1d(&cuda_plan, n, _CUFFT_C2C, 1) != CUFFT_SUCCESS)
  168. printf("erf\n");
  169. #endif
  170. }
  171. else if (argc == 3)
  172. {
  173. plan = STARPUFFT(plan_dft_2d)(n, m, SIGN, 0);
  174. #ifdef STARPU_HAVE_FFTW
  175. fftw_plan = _FFTW(plan_dft_2d)(n, m, NULL, (void*) 1, SIGN, FFTW_ESTIMATE);
  176. #endif
  177. #ifdef STARPU_USE_CUDA
  178. STARPU_ASSERT(cufftPlan2d(&cuda_plan, n, m, _CUFFT_C2C) == CUFFT_SUCCESS);
  179. #endif
  180. }
  181. else
  182. {
  183. assert(0);
  184. }
  185. #ifdef STARPU_HAVE_FFTW
  186. gettimeofday(&begin, NULL);
  187. _FFTW(execute_dft)(fftw_plan, in, out_fftw);
  188. gettimeofday(&end, NULL);
  189. _FFTW(destroy_plan)(fftw_plan);
  190. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  191. printf("FFTW took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  192. #endif
  193. #ifdef STARPU_USE_CUDA
  194. gettimeofday(&begin, NULL);
  195. if (cufftExecC2C(cuda_plan, (cufftComplex*) in, (cufftComplex*) out_cuda, CUFFT_FORWARD) != CUFFT_SUCCESS)
  196. printf("erf2\n");
  197. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  198. STARPU_CUDA_REPORT_ERROR(cures);
  199. gettimeofday(&end, NULL);
  200. cufftDestroy(cuda_plan);
  201. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  202. printf("CUDA took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  203. #endif
  204. STARPUFFT(execute)(plan, in, out);
  205. STARPUFFT(showstats)(stdout);
  206. #ifdef STARPU_HAVE_FFTW
  207. check_fftw(out, out_fftw, size);
  208. #endif
  209. #ifdef STARPU_USE_CUDA
  210. check_cuda(out, out_cuda, size);
  211. #endif
  212. #if 1
  213. starpu_vector_data_register(&in_handle, 0, (uintptr_t) in, size, sizeof(*in));
  214. starpu_vector_data_register(&out_handle, 0, (uintptr_t) out, size, sizeof(*out));
  215. STARPUFFT(execute_handle)(plan, in_handle, out_handle);
  216. starpu_data_unregister(in_handle);
  217. starpu_data_unregister(out_handle);
  218. #ifdef STARPU_HAVE_FFTW
  219. check_fftw(out, out_fftw, size);
  220. #endif
  221. #ifdef STARPU_USE_CUDA
  222. check_cuda(out, out_cuda, size);
  223. #endif
  224. #endif
  225. STARPUFFT(showstats)(stdout);
  226. STARPUFFT(destroy_plan)(plan);
  227. printf("\n");
  228. #if 0
  229. for (i = 0; i < 16; i++)
  230. printf("(%f,%f) ", cimag(in[i]), creal(in[i]));
  231. printf("\n\n");
  232. for (i = 0; i < 16; i++)
  233. printf("(%f,%f) ", cimag(out[i]), creal(out[i]));
  234. printf("\n\n");
  235. #ifdef STARPU_HAVE_FFTW
  236. for (i = 0; i < 16; i++)
  237. printf("(%f,%f) ", cimag(out_fftw[i]), creal(out_fftw[i]));
  238. printf("\n\n");
  239. #endif
  240. #endif
  241. STARPUFFT(free)(in);
  242. STARPUFFT(free)(out);
  243. #ifdef STARPU_HAVE_FFTW
  244. STARPUFFT(free)(out_fftw);
  245. #endif
  246. #ifdef STARPU_USE_CUDA
  247. free(out_cuda);
  248. #endif
  249. starpu_shutdown();
  250. return EXIT_SUCCESS;
  251. }