pi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011, 2013-2015 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2016, 2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This computes Pi by using drawing random coordinates (thanks to the sobol
  20. * generator) and check whether they fall within one quarter of a circle. The
  21. * proportion gives an approximation of Pi. For each task, we draw a number of
  22. * coordinates, and we gather the number of successful draws.
  23. *
  24. * TODO: use curandGenerateUniform instead of the sobol generator, like pi_redux.c does
  25. */
  26. #include "SobolQRNG/sobol.h"
  27. #include "SobolQRNG/sobol_gold.h"
  28. #include "pi.h"
  29. #ifdef STARPU_USE_CUDA
  30. void cuda_kernel(void **descr, void *cl_arg);
  31. #endif
  32. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  33. /* default value */
  34. static unsigned ntasks = 1024;
  35. static unsigned long long nshot_per_task = 16*1024*1024ULL;
  36. void cpu_kernel(void *descr[], void *cl_arg)
  37. {
  38. (void)cl_arg;
  39. unsigned *directions = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  40. unsigned nx = nshot_per_task;
  41. TYPE *random_numbers = malloc(2*nx*sizeof(TYPE));
  42. sobolCPU(2*nx/n_dimensions, n_dimensions, directions, random_numbers);
  43. TYPE *random_numbers_x = &random_numbers[0];
  44. TYPE *random_numbers_y = &random_numbers[nx];
  45. unsigned current_cnt = 0;
  46. unsigned i;
  47. for (i = 0; i < nx; i++)
  48. {
  49. TYPE x = random_numbers_x[i];
  50. TYPE y = random_numbers_y[i];
  51. TYPE dist = (x*x + y*y);
  52. unsigned success = (dist <= 1.0);
  53. current_cnt += success;
  54. }
  55. unsigned *cnt = (unsigned *)STARPU_VECTOR_GET_PTR(descr[1]);
  56. *cnt = current_cnt;
  57. free(random_numbers);
  58. }
  59. /* The amount of work does not depend on the data size at all :) */
  60. static size_t size_base(struct starpu_task *task, unsigned nimpl)
  61. {
  62. (void)task;
  63. (void)nimpl;
  64. return nshot_per_task;
  65. }
  66. static void parse_args(int argc, char **argv)
  67. {
  68. int i;
  69. for (i = 1; i < argc; i++)
  70. {
  71. if (strcmp(argv[i], "-ntasks") == 0)
  72. {
  73. char *argptr;
  74. ntasks = strtol(argv[++i], &argptr, 10);
  75. }
  76. if (strcmp(argv[i], "-nshot") == 0)
  77. {
  78. char *argptr;
  79. nshot_per_task = strtol(argv[++i], &argptr, 10);
  80. }
  81. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
  82. {
  83. fprintf(stderr,"Usage: %s [options...]\n", argv[0]);
  84. fprintf(stderr,"\n");
  85. fprintf(stderr,"Options:\n");
  86. fprintf(stderr,"-ntasks <n> select the number of tasks\n");
  87. fprintf(stderr,"-nshot <n> select the number of shot per task\n");
  88. exit(0);
  89. }
  90. }
  91. }
  92. static struct starpu_perfmodel model =
  93. {
  94. .type = STARPU_HISTORY_BASED,
  95. .size_base = size_base,
  96. .symbol = "monte_carlo_pi"
  97. };
  98. static struct starpu_codelet pi_cl =
  99. {
  100. .cpu_funcs = {cpu_kernel},
  101. .cpu_funcs_name = {"cpu_kernel"},
  102. #ifdef STARPU_USE_CUDA
  103. .cuda_funcs = {cuda_kernel},
  104. #endif
  105. .nbuffers = 2,
  106. .modes = {STARPU_R, STARPU_W},
  107. .model = &model
  108. };
  109. int main(int argc, char **argv)
  110. {
  111. unsigned i;
  112. int ret;
  113. parse_args(argc, argv);
  114. ret = starpu_init(NULL);
  115. if (ret == -ENODEV) return 77;
  116. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  117. /* Initialize the random number generator */
  118. unsigned *sobol_qrng_directions = malloc(n_dimensions*n_directions*sizeof(unsigned));
  119. STARPU_ASSERT(sobol_qrng_directions);
  120. initSobolDirectionVectors(n_dimensions, sobol_qrng_directions);
  121. /* Any worker may use that array now */
  122. starpu_data_handle_t sobol_qrng_direction_handle;
  123. starpu_vector_data_register(&sobol_qrng_direction_handle, STARPU_MAIN_RAM,
  124. (uintptr_t)sobol_qrng_directions, n_dimensions*n_directions, sizeof(unsigned));
  125. unsigned *cnt_array = calloc(ntasks, sizeof(unsigned));
  126. STARPU_ASSERT(cnt_array);
  127. starpu_data_handle_t cnt_array_handle;
  128. starpu_vector_data_register(&cnt_array_handle, STARPU_MAIN_RAM, (uintptr_t)cnt_array, ntasks, sizeof(unsigned));
  129. /* Use a write-through policy : when the data is modified on an
  130. * accelerator, we know that it will only be modified once and be
  131. * accessed by the CPU later on */
  132. starpu_data_set_wt_mask(cnt_array_handle, (1<<0));
  133. struct starpu_data_filter f =
  134. {
  135. .filter_func = starpu_vector_filter_block,
  136. .nchildren = ntasks
  137. };
  138. starpu_data_partition(cnt_array_handle, &f);
  139. double start;
  140. double end;
  141. start = starpu_timing_now();
  142. for (i = 0; i < ntasks; i++)
  143. {
  144. struct starpu_task *task = starpu_task_create();
  145. task->cl = &pi_cl;
  146. STARPU_ASSERT(starpu_data_get_sub_data(cnt_array_handle, 1, i));
  147. task->handles[0] = sobol_qrng_direction_handle;
  148. task->handles[1] = starpu_data_get_sub_data(cnt_array_handle, 1, i);
  149. ret = starpu_task_submit(task);
  150. STARPU_ASSERT(!ret);
  151. }
  152. starpu_task_wait_for_all();
  153. /* Get the cnt_array back in main memory */
  154. starpu_data_unpartition(cnt_array_handle, STARPU_MAIN_RAM);
  155. starpu_data_unregister(cnt_array_handle);
  156. starpu_data_unregister(sobol_qrng_direction_handle);
  157. /* Count the total number of entries */
  158. unsigned long total_cnt = 0;
  159. for (i = 0; i < ntasks; i++)
  160. total_cnt += cnt_array[i];
  161. end = starpu_timing_now();
  162. double timing = end - start;
  163. unsigned long total_shot_cnt = ntasks * nshot_per_task;
  164. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4, probability to impact the disk: pi/4 */
  165. FPRINTF(stderr, "Pi approximation : %f (%lu / %lu)\n", ((TYPE)total_cnt*4)/(total_shot_cnt), total_cnt, total_shot_cnt);
  166. FPRINTF(stderr, "Total time : %f ms\n", timing/1000.0);
  167. FPRINTF(stderr, "Speed : %f GShot/s\n", total_shot_cnt/(1e3*timing));
  168. if (!getenv("STARPU_SSILENT")) starpu_codelet_display_stats(&pi_cl);
  169. starpu_shutdown();
  170. return 0;
  171. }