testx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR in PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <complex.h>
  17. #include <math.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <assert.h>
  21. #include <sys/time.h>
  22. #include <starpu.h>
  23. #include <starpu_config.h>
  24. #include "starpufft.h"
  25. #undef STARPU_USE_CUDA
  26. #ifdef STARPU_HAVE_FFTW
  27. #include <fftw3.h>
  28. #endif
  29. #ifdef STARPU_USE_CUDA
  30. #include <cufft.h>
  31. #endif
  32. #define SIGN (-1)
  33. //#define SIGN (1)
  34. int main(int argc, char *argv[]) {
  35. int i;
  36. struct timeval begin, end;
  37. int size;
  38. size_t bytes;
  39. int n = 0, m = 0;
  40. STARPUFFT(plan) plan;
  41. #ifdef STARPU_HAVE_FFTW
  42. _FFTW(plan) fftw_plan;
  43. #endif
  44. #ifdef STARPU_USE_CUDA
  45. cufftHandle cuda_plan;
  46. cudaError_t cures;
  47. #endif
  48. double timing;
  49. if (argc < 2 || argc > 3) {
  50. fprintf(stderr,"need one or two size of vector\n");
  51. exit(EXIT_FAILURE);
  52. }
  53. starpu_init(NULL);
  54. if (argc == 2) {
  55. n = atoi(argv[1]);
  56. /* 1D */
  57. size = n;
  58. } else if (argc == 3) {
  59. n = atoi(argv[1]);
  60. m = atoi(argv[2]);
  61. /* 2D */
  62. size = n * m;
  63. } else {
  64. assert(0);
  65. }
  66. bytes = size * sizeof(STARPUFFT(complex));
  67. STARPUFFT(complex) *in = STARPUFFT(malloc)(size * sizeof(*in));
  68. starpu_srand48(0);
  69. for (i = 0; i < size; i++)
  70. in[i] = starpu_drand48() + I * starpu_drand48();
  71. STARPUFFT(complex) *out = STARPUFFT(malloc)(size * sizeof(*out));
  72. #ifdef STARPU_HAVE_FFTW
  73. STARPUFFT(complex) *out_fftw = STARPUFFT(malloc)(size * sizeof(*out_fftw));
  74. #endif
  75. #ifdef STARPU_USE_CUDA
  76. STARPUFFT(complex) *out_cuda = malloc(size * sizeof(*out_cuda));
  77. #endif
  78. if (argc == 2) {
  79. plan = STARPUFFT(plan_dft_1d)(n, SIGN, 0);
  80. #ifdef STARPU_HAVE_FFTW
  81. fftw_plan = _FFTW(plan_dft_1d)(n, in, out_fftw, SIGN, FFTW_ESTIMATE);
  82. #endif
  83. #ifdef STARPU_USE_CUDA
  84. if (cufftPlan1d(&cuda_plan, n, _CUFFT_C2C, 1) != CUFFT_SUCCESS)
  85. printf("erf\n");
  86. #endif
  87. } else if (argc == 3) {
  88. plan = STARPUFFT(plan_dft_2d)(n, m, SIGN, 0);
  89. #ifdef STARPU_HAVE_FFTW
  90. fftw_plan = _FFTW(plan_dft_2d)(n, m, in, out_fftw, SIGN, FFTW_ESTIMATE);
  91. #endif
  92. #ifdef STARPU_USE_CUDA
  93. STARPU_ASSERT(cufftPlan2d(&cuda_plan, n, m, _CUFFT_C2C) == CUFFT_SUCCESS);
  94. #endif
  95. } else {
  96. assert(0);
  97. }
  98. #ifdef STARPU_HAVE_FFTW
  99. gettimeofday(&begin, NULL);
  100. _FFTW(execute)(fftw_plan);
  101. gettimeofday(&end, NULL);
  102. _FFTW(destroy_plan)(fftw_plan);
  103. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  104. printf("FFTW took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  105. #endif
  106. #ifdef STARPU_USE_CUDA
  107. gettimeofday(&begin, NULL);
  108. if (cufftExecC2C(cuda_plan, (cufftComplex*) in, (cufftComplex*) out_cuda, CUFFT_FORWARD) != CUFFT_SUCCESS)
  109. printf("erf2\n");
  110. if ((cures = cudaThreadSynchronize()) != cudaSuccess)
  111. STARPU_CUDA_REPORT_ERROR(cures);
  112. gettimeofday(&end, NULL);
  113. cufftDestroy(cuda_plan);
  114. timing = (double)((end.tv_sec - begin.tv_sec)*1000000 + (end.tv_usec - begin.tv_usec));
  115. printf("CUDA took %2.2f ms (%2.2f MB/s)\n\n", timing/1000, bytes/timing);
  116. #endif
  117. STARPUFFT(execute)(plan, in, out);
  118. STARPUFFT(showstats)(stdout);
  119. STARPUFFT(destroy_plan)(plan);
  120. printf("\n");
  121. #if 0
  122. for (i = 0; i < 16; i++)
  123. printf("(%f,%f) ", cimag(in[i]), creal(in[i]));
  124. printf("\n\n");
  125. for (i = 0; i < 16; i++)
  126. printf("(%f,%f) ", cimag(out[i]), creal(out[i]));
  127. printf("\n\n");
  128. #ifdef STARPU_HAVE_FFTW
  129. for (i = 0; i < 16; i++)
  130. printf("(%f,%f) ", cimag(out_fftw[i]), creal(out_fftw[i]));
  131. printf("\n\n");
  132. #endif
  133. #endif
  134. #ifdef STARPU_HAVE_FFTW
  135. {
  136. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  137. for (i = 0; i < size; i++) {
  138. double diff = cabs(out[i]-out_fftw[i]);
  139. double diff2 = diff * diff;
  140. double size = cabs(out_fftw[i]);
  141. double size2 = size * size;
  142. if (diff > max)
  143. max = diff;
  144. tot += diff;
  145. normdiff += diff2;
  146. norm += size2;
  147. }
  148. fprintf(stderr, "\nmaximum difference %g\n", max);
  149. fprintf(stderr, "average difference %g\n", tot / size);
  150. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  151. double relmaxdiff = max / sqrt(norm);
  152. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  153. double relavgdiff = (tot / size) / sqrt(norm);
  154. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  155. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-8 || relavgdiff > 1e-8))
  156. return EXIT_FAILURE;
  157. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  158. return EXIT_FAILURE;
  159. }
  160. #endif
  161. #ifdef STARPU_USE_CUDA
  162. {
  163. double max = 0., tot = 0., norm = 0., normdiff = 0.;
  164. for (i = 0; i < size; i++) {
  165. double diff = cabs(out_cuda[i]-out_fftw[i]);
  166. double diff2 = diff * diff;
  167. double size = cabs(out_fftw[i]);
  168. double size2 = size * size;
  169. if (diff > max)
  170. max = diff;
  171. tot += diff;
  172. normdiff += diff2;
  173. norm += size2;
  174. }
  175. fprintf(stderr, "\nmaximum difference %g\n", max);
  176. fprintf(stderr, "average difference %g\n", tot / size);
  177. fprintf(stderr, "difference norm %g\n", sqrt(normdiff));
  178. double relmaxdiff = max / sqrt(norm);
  179. fprintf(stderr, "relative maximum difference %g\n", relmaxdiff);
  180. double relavgdiff = (tot / size) / sqrt(norm);
  181. fprintf(stderr, "relative average difference %g\n", relavgdiff);
  182. if (!strcmp(TYPE, "f") && (relmaxdiff > 1e-8 || relavgdiff > 1e-8))
  183. return EXIT_FAILURE;
  184. if (!strcmp(TYPE, "") && (relmaxdiff > 1e-16 || relavgdiff > 1e-16))
  185. return EXIT_FAILURE;
  186. }
  187. #endif
  188. STARPUFFT(free)(in);
  189. STARPUFFT(free)(out);
  190. #ifdef STARPU_HAVE_FFTW
  191. STARPUFFT(free)(out_fftw);
  192. #endif
  193. #ifdef STARPU_USE_CUDA
  194. free(out_cuda);
  195. #endif
  196. starpu_shutdown();
  197. return EXIT_SUCCESS;
  198. }