pi.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (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 A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "SobolQRNG/sobol.h"
  17. #include "SobolQRNG/sobol_gold.h"
  18. #include "pi.h"
  19. #include <sys/time.h>
  20. #ifdef STARPU_USE_CUDA
  21. void cuda_kernel(void **descr, void *cl_arg);
  22. #endif
  23. /* default value */
  24. static unsigned ntasks = 1024;
  25. static void cpu_kernel(void *descr[], void *cl_arg)
  26. {
  27. unsigned *directions = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  28. unsigned nx = NSHOT_PER_TASK;
  29. TYPE *random_numbers = malloc(2*nx*sizeof(TYPE));
  30. sobolCPU(2*nx/n_dimensions, n_dimensions, directions, random_numbers);
  31. TYPE *random_numbers_x = &random_numbers[0];
  32. TYPE *random_numbers_y = &random_numbers[nx];
  33. unsigned current_cnt = 0;
  34. unsigned i;
  35. for (i = 0; i < nx; i++)
  36. {
  37. TYPE x = random_numbers_x[i];
  38. TYPE y = random_numbers_y[i];
  39. TYPE dist = (x*x + y*y);
  40. unsigned success = (dist <= 1.0);
  41. current_cnt += success;
  42. }
  43. unsigned *cnt = (unsigned *)STARPU_VECTOR_GET_PTR(descr[1]);
  44. *cnt = current_cnt;
  45. free(random_numbers);
  46. }
  47. static void parse_args(int argc, char **argv)
  48. {
  49. int i;
  50. for (i = 1; i < argc; i++) {
  51. if (strcmp(argv[i], "-ntasks") == 0) {
  52. char *argptr;
  53. ntasks = strtol(argv[++i], &argptr, 10);
  54. }
  55. }
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. unsigned i;
  60. parse_args(argc, argv);
  61. starpu_init(NULL);
  62. /* Initialize the random number generator */
  63. unsigned *sobol_qrng_directions = malloc(n_dimensions*n_directions*sizeof(unsigned));
  64. STARPU_ASSERT(sobol_qrng_directions);
  65. initSobolDirectionVectors(n_dimensions, sobol_qrng_directions);
  66. /* Any worker may use that array now */
  67. starpu_data_handle sobol_qrng_direction_handle;
  68. starpu_vector_data_register(&sobol_qrng_direction_handle, 0,
  69. (uintptr_t)sobol_qrng_directions, n_dimensions*n_directions, sizeof(unsigned));
  70. unsigned *cnt_array = malloc(ntasks*sizeof(unsigned));
  71. STARPU_ASSERT(cnt_array);
  72. starpu_data_handle cnt_array_handle;
  73. starpu_vector_data_register(&cnt_array_handle, 0, (uintptr_t)cnt_array, ntasks, sizeof(unsigned));
  74. /* Use a write-through policy : when the data is modified on an
  75. * accelerator, we know that it will only be modified once and be
  76. * accessed by the CPU later on */
  77. starpu_data_set_wt_mask(cnt_array_handle, (1<<0));
  78. struct starpu_data_filter f = {
  79. .filter_func = starpu_block_filter_func_vector,
  80. .nchildren = ntasks,
  81. .get_nchildren = NULL,
  82. .get_child_ops = NULL
  83. };
  84. starpu_data_partition(cnt_array_handle, &f);
  85. static struct starpu_perfmodel_t model = {
  86. .type = STARPU_HISTORY_BASED,
  87. .symbol = "monte_carlo_pi"
  88. };
  89. struct starpu_codelet_t cl = {
  90. .where = STARPU_CPU|STARPU_CUDA,
  91. .cpu_func = cpu_kernel,
  92. #ifdef STARPU_USE_CUDA
  93. .cuda_func = cuda_kernel,
  94. #endif
  95. .nbuffers = 2,
  96. .model = &model
  97. };
  98. struct timeval start;
  99. struct timeval end;
  100. gettimeofday(&start, NULL);
  101. for (i = 0; i < ntasks; i++)
  102. {
  103. struct starpu_task *task = starpu_task_create();
  104. task->cl = &cl;
  105. STARPU_ASSERT(starpu_data_get_sub_data(cnt_array_handle, 1, i));
  106. task->buffers[0].handle = sobol_qrng_direction_handle;
  107. task->buffers[0].mode = STARPU_R;
  108. task->buffers[1].handle = starpu_data_get_sub_data(cnt_array_handle, 1, i);
  109. task->buffers[1].mode = STARPU_W;
  110. int ret = starpu_task_submit(task);
  111. STARPU_ASSERT(!ret);
  112. }
  113. starpu_task_wait_for_all();
  114. /* Get the cnt_array back in main memory */
  115. starpu_data_unpartition(cnt_array_handle, 0);
  116. starpu_data_acquire(cnt_array_handle, STARPU_RW);
  117. /* Count the total number of entries */
  118. unsigned long total_cnt = 0;
  119. for (i = 0; i < ntasks; i++)
  120. total_cnt += cnt_array[i];
  121. gettimeofday(&end, NULL);
  122. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  123. unsigned long total_shot_cnt = ntasks * NSHOT_PER_TASK;
  124. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4, probability to impact the disk: pi/4 */
  125. fprintf(stderr, "Pi approximation : %f (%ld / %ld)\n", ((TYPE)total_cnt*4)/(total_shot_cnt), total_cnt, total_shot_cnt);
  126. fprintf(stderr, "Total time : %f ms\n", timing/1000.0);
  127. fprintf(stderr, "Speed : %f GShot/s\n", total_shot_cnt/(1e3*timing));
  128. starpu_data_release(cnt_array_handle);
  129. starpu_display_codelet_stats(&cl);
  130. starpu_shutdown();
  131. return 0;
  132. }