testx.c 6.8 KB

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