pi_redux.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. *
  5. * StarPU 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. * StarPU 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 A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <stdlib.h>
  18. #include <sys/time.h>
  19. #include <starpu_config.h>
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. #define PI 3.14159265358979323846
  22. #if defined(STARPU_USE_CUDA) && !defined(STARPU_HAVE_CURAND)
  23. #warning CURAND is required to run that example on CUDA devices
  24. #endif
  25. #ifdef STARPU_HAVE_CURAND
  26. #include <cuda.h>
  27. #include <curand.h>
  28. #include <starpu_cuda.h>
  29. #endif
  30. #define NSHOT_PER_TASK (1024*1024)
  31. /* default value */
  32. static unsigned long ntasks = 1024;
  33. static unsigned long ntasks_warmup = 0;
  34. static unsigned use_redux = 1;
  35. static unsigned do_warmup = 0;
  36. /*
  37. * Initialization of the Random Number Generators (RNG)
  38. */
  39. #ifdef STARPU_HAVE_CURAND
  40. /* RNG for the CURAND library */
  41. static curandGenerator_t curandgens[STARPU_NMAXWORKERS];
  42. #endif
  43. /* state for the erand48 function : note the huge padding to avoid false-sharing */
  44. #define PADDING 1024
  45. static unsigned short xsubi[STARPU_NMAXWORKERS*PADDING];
  46. static struct drand48_data randbuffer[STARPU_NMAXWORKERS*PADDING];
  47. /* Function to initialize the random number generator in the current worker */
  48. static void init_rng(void *arg __attribute__((unused)))
  49. {
  50. #ifdef STARPU_HAVE_CURAND
  51. curandStatus_t res;
  52. #endif
  53. int workerid = starpu_worker_get_id();
  54. switch (starpu_worker_get_type(workerid)) {
  55. case STARPU_CPU_WORKER:
  56. /* create a seed */
  57. starpu_srand48_r((long int)workerid, &randbuffer[PADDING*workerid]);
  58. xsubi[0 + PADDING*workerid] = (unsigned short)workerid;
  59. xsubi[1 + PADDING*workerid] = (unsigned short)workerid;
  60. xsubi[2 + PADDING*workerid] = (unsigned short)workerid;
  61. break;
  62. #ifdef STARPU_HAVE_CURAND
  63. case STARPU_CUDA_WORKER:
  64. /* Create a RNG */
  65. res = curandCreateGenerator(&curandgens[workerid],
  66. CURAND_RNG_PSEUDO_DEFAULT);
  67. STARPU_ASSERT(res == CURAND_STATUS_SUCCESS);
  68. /* Seed it with worker's id */
  69. res = curandSetPseudoRandomGeneratorSeed(curandgens[workerid],
  70. (unsigned long long)workerid);
  71. STARPU_ASSERT(res == CURAND_STATUS_SUCCESS);
  72. break;
  73. #endif
  74. default:
  75. STARPU_ABORT();
  76. break;
  77. }
  78. }
  79. static void parse_args(int argc, char **argv)
  80. {
  81. int i;
  82. for (i = 1; i < argc; i++) {
  83. if (strcmp(argv[i], "-ntasks") == 0) {
  84. char *argptr;
  85. ntasks = strtol(argv[++i], &argptr, 10);
  86. }
  87. if (strcmp(argv[i], "-noredux") == 0) {
  88. use_redux = 0;
  89. }
  90. if (strcmp(argv[i], "-warmup") == 0) {
  91. do_warmup = 1;
  92. ntasks_warmup = 8; /* arbitrary number of warmup tasks */
  93. }
  94. if (strcmp(argv[i], "-h") == 0) {
  95. fprintf(stderr, "Usage: %s [-ntasks n] [-noredux] [-warmup] [-h]\n", argv[0]);
  96. exit(-1);
  97. }
  98. }
  99. }
  100. /*
  101. * Monte-carlo kernel
  102. */
  103. static void pi_func_cpu(void *descr[], void *cl_arg __attribute__ ((unused)))
  104. {
  105. int workerid = starpu_worker_get_id();
  106. unsigned short *worker_xsub;
  107. worker_xsub = &xsubi[PADDING*workerid];
  108. struct drand48_data *buffer;
  109. buffer = &randbuffer[PADDING*workerid];
  110. unsigned long local_cnt = 0;
  111. /* Fill the scratchpad with random numbers */
  112. int i;
  113. for (i = 0; i < NSHOT_PER_TASK; i++)
  114. {
  115. double randx, randy;
  116. starpu_erand48_r(worker_xsub, buffer, &randx);
  117. starpu_erand48_r(worker_xsub, buffer, &randy);
  118. double x = (2.0*randx - 1.0);
  119. double y = (2.0*randy - 1.0);
  120. double dist = x*x + y*y;
  121. if (dist < 1.0)
  122. local_cnt++;
  123. }
  124. /* Put the contribution of that task into the counter */
  125. unsigned long *cnt = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  126. *cnt = *cnt + local_cnt;
  127. }
  128. extern void pi_redux_cuda_kernel(float *x, float *y, unsigned n, unsigned long *shot_cnt);
  129. #ifdef STARPU_HAVE_CURAND
  130. static void pi_func_cuda(void *descr[], void *cl_arg __attribute__ ((unused)))
  131. {
  132. cudaError_t cures;
  133. curandStatus_t res;
  134. int workerid = starpu_worker_get_id();
  135. /* CURAND is a bit silly: it assumes that any error is fatal. Calling
  136. * cudaGetLastError resets the last error value. */
  137. cures = cudaGetLastError();
  138. /* if (cures)
  139. STARPU_CUDA_REPORT_ERROR(cures); */
  140. /* Fill the scratchpad with random numbers. Note that both x and y
  141. * arrays are in stored the same vector. */
  142. float *scratchpad_xy = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  143. res = curandGenerateUniform(curandgens[workerid], scratchpad_xy, 2*NSHOT_PER_TASK);
  144. STARPU_ASSERT(res == CURAND_STATUS_SUCCESS);
  145. float *x = &scratchpad_xy[0];
  146. float *y = &scratchpad_xy[NSHOT_PER_TASK];
  147. unsigned long *shot_cnt = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  148. pi_redux_cuda_kernel(x, y, NSHOT_PER_TASK, shot_cnt);
  149. }
  150. #endif
  151. static struct starpu_codelet_t pi_cl = {
  152. .where =
  153. #ifdef STARPU_HAVE_CURAND
  154. STARPU_CUDA|
  155. #endif
  156. STARPU_CPU,
  157. .cpu_func = pi_func_cpu,
  158. #ifdef STARPU_HAVE_CURAND
  159. .cuda_func = pi_func_cuda,
  160. #endif
  161. .nbuffers = 2,
  162. .model = NULL
  163. };
  164. /*
  165. * Codelets to implement reduction
  166. */
  167. static void init_cpu_func(void *descr[], void *cl_arg)
  168. {
  169. unsigned long *val = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  170. *val = 0;
  171. }
  172. #ifdef STARPU_HAVE_CURAND
  173. static void init_cuda_func(void *descr[], void *cl_arg)
  174. {
  175. unsigned long *val = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  176. cudaMemset(val, 0, sizeof(unsigned long));
  177. cudaThreadSynchronize();
  178. }
  179. #endif
  180. static struct starpu_codelet_t init_codelet = {
  181. .where =
  182. #ifdef STARPU_HAVE_CURAND
  183. STARPU_CUDA|
  184. #endif
  185. STARPU_CPU,
  186. .cpu_func = init_cpu_func,
  187. #ifdef STARPU_HAVE_CURAND
  188. .cuda_func = init_cuda_func,
  189. #endif
  190. .nbuffers = 1
  191. };
  192. #ifdef STARPU_HAVE_CURAND
  193. /* Dummy implementation of the addition of two unsigned longs in CUDA */
  194. static void redux_cuda_func(void *descr[], void *cl_arg)
  195. {
  196. unsigned long *d_a = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  197. unsigned long *d_b = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  198. unsigned long h_a, h_b;
  199. cudaMemcpy(&h_a, d_a, sizeof(h_a), cudaMemcpyDeviceToHost);
  200. cudaMemcpy(&h_b, d_b, sizeof(h_b), cudaMemcpyDeviceToHost);
  201. h_a += h_b;
  202. cudaMemcpy(d_a, &h_a, sizeof(h_a), cudaMemcpyHostToDevice);
  203. };
  204. #endif
  205. static void redux_cpu_func(void *descr[], void *cl_arg)
  206. {
  207. unsigned long *a = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[0]);
  208. unsigned long *b = (unsigned long *)STARPU_VARIABLE_GET_PTR(descr[1]);
  209. *a = *a + *b;
  210. };
  211. static struct starpu_codelet_t redux_codelet = {
  212. .where =
  213. #ifdef STARPU_HAVE_CURAND
  214. STARPU_CUDA|
  215. #endif
  216. STARPU_CPU,
  217. .cpu_func = redux_cpu_func,
  218. #ifdef STARPU_HAVE_CURAND
  219. .cuda_func = redux_cuda_func,
  220. #endif
  221. .nbuffers = 2
  222. };
  223. /*
  224. * Main program
  225. */
  226. int main(int argc, char **argv)
  227. {
  228. unsigned i;
  229. parse_args(argc, argv);
  230. starpu_init(NULL);
  231. /* Launch a Random Number Generator (RNG) on each worker */
  232. starpu_execute_on_each_worker(init_rng, NULL, STARPU_CPU|STARPU_CUDA);
  233. /* Create a scratchpad data */
  234. starpu_data_handle xy_scratchpad_handle;
  235. starpu_vector_data_register(&xy_scratchpad_handle, -1, (uintptr_t)NULL,
  236. 2*NSHOT_PER_TASK, sizeof(float));
  237. /* Create a variable that will be used to count the number of shots
  238. * that actually hit the unit circle when shooting randomly in
  239. * [-1,1]^2. */
  240. unsigned long shot_cnt = 0;
  241. starpu_data_handle shot_cnt_handle;
  242. starpu_variable_data_register(&shot_cnt_handle, 0,
  243. (uintptr_t)&shot_cnt, sizeof(shot_cnt));
  244. starpu_data_set_reduction_methods(shot_cnt_handle,
  245. &redux_codelet, &init_codelet);
  246. struct timeval start;
  247. struct timeval end;
  248. for (i = 0; i < ntasks_warmup; i++)
  249. {
  250. struct starpu_task *task = starpu_task_create();
  251. task->cl = &pi_cl;
  252. task->buffers[0].handle = xy_scratchpad_handle;
  253. task->buffers[0].mode = STARPU_SCRATCH;
  254. task->buffers[1].handle = shot_cnt_handle;
  255. task->buffers[1].mode = use_redux?STARPU_REDUX:STARPU_RW;
  256. int ret = starpu_task_submit(task);
  257. STARPU_ASSERT(!ret);
  258. }
  259. gettimeofday(&start, NULL);
  260. for (i = 0; i < ntasks; i++)
  261. {
  262. struct starpu_task *task = starpu_task_create();
  263. task->cl = &pi_cl;
  264. task->buffers[0].handle = xy_scratchpad_handle;
  265. task->buffers[0].mode = STARPU_SCRATCH;
  266. task->buffers[1].handle = shot_cnt_handle;
  267. task->buffers[1].mode = use_redux?STARPU_REDUX:STARPU_RW;
  268. int ret = starpu_task_submit(task);
  269. STARPU_ASSERT(!ret);
  270. }
  271. starpu_data_unregister(shot_cnt_handle);
  272. gettimeofday(&end, NULL);
  273. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  274. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4,
  275. * probability to impact the disk: pi/4 */
  276. unsigned long total = (ntasks + ntasks_warmup)*NSHOT_PER_TASK;
  277. double pi_approx = ((double)shot_cnt*4.0)/total;
  278. FPRINTF(stderr, "Reductions? %s\n", use_redux?"yes":"no");
  279. FPRINTF(stderr, "Pi approximation : %lf (%ld / %ld)\n", pi_approx, shot_cnt, total);
  280. FPRINTF(stderr, "Error %le \n", pi_approx - PI);
  281. FPRINTF(stderr, "Total time : %f ms\n", timing/1000.0);
  282. FPRINTF(stderr, "Speed : %f GShot/s\n", total/(1e3*timing));
  283. starpu_shutdown();
  284. if (abs(pi_approx - PI) > 1.0)
  285. return 1;
  286. return 0;
  287. }