pi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 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 "pi.h"
  18. #ifdef STARPU_USE_CUDA
  19. void cuda_kernel(void **descr, void *cl_arg);
  20. #endif
  21. static void cpu_kernel(void *descr[], void *cl_arg)
  22. {
  23. unsigned *directions = (unsigned *)STARPU_GET_VECTOR_PTR(descr[0]);
  24. unsigned nx = NSHOT_PER_TASK;
  25. TYPE *random_numbers = malloc(2*nx*sizeof(TYPE));
  26. sobolCPU(2*nx/n_dimensions, n_dimensions, directions, random_numbers);
  27. TYPE *random_numbers_x = &random_numbers[0];
  28. TYPE *random_numbers_y = &random_numbers[nx];
  29. unsigned current_cnt = 0;
  30. unsigned i;
  31. for (i = 0; i < nx; i++)
  32. {
  33. TYPE x = random_numbers_x[i];
  34. TYPE y = random_numbers_y[i];
  35. TYPE dist = (x*x + y*y);
  36. unsigned success = (dist <= 1.0);
  37. current_cnt += success;
  38. }
  39. unsigned *cnt = (unsigned *)STARPU_GET_VECTOR_PTR(descr[1]);
  40. *cnt = current_cnt;
  41. free(random_numbers);
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. unsigned i;
  46. starpu_init(NULL);
  47. /* Initialize the random number generator */
  48. unsigned *sobol_qrng_directions = malloc(n_dimensions*n_directions*sizeof(unsigned));
  49. STARPU_ASSERT(sobol_qrng_directions);
  50. initSobolDirectionVectors(n_dimensions, sobol_qrng_directions);
  51. /* Any worker may use that array now */
  52. starpu_data_handle sobol_qrng_direction_handle;
  53. starpu_register_vector_data(&sobol_qrng_direction_handle, 0,
  54. (uintptr_t)sobol_qrng_directions, n_dimensions*n_directions, sizeof(unsigned));
  55. unsigned *cnt_array = malloc(NTASKS*sizeof(unsigned));
  56. STARPU_ASSERT(cnt_array);
  57. starpu_data_handle cnt_array_handle;
  58. starpu_register_vector_data(&cnt_array_handle, 0, (uintptr_t)cnt_array, NTASKS, sizeof(unsigned));
  59. /* TODO use a write-back mechanism */
  60. struct starpu_filter_t f = {
  61. .filter_func = starpu_block_filter_func_vector,
  62. .filter_arg = NTASKS
  63. };
  64. #if 0
  65. starpu_partition_data(random_array_handle_x, &f);
  66. starpu_partition_data(random_array_handle_y, &f);
  67. #endif
  68. starpu_partition_data(cnt_array_handle, &f);
  69. static struct starpu_perfmodel_t model = {
  70. .type = STARPU_HISTORY_BASED,
  71. .symbol = "monte_carlo_pi"
  72. };
  73. struct starpu_codelet_t cl = {
  74. .where = STARPU_CPU|STARPU_CUDA,
  75. .cpu_func = cpu_kernel,
  76. #ifdef STARPU_USE_CUDA
  77. .cuda_func = cuda_kernel,
  78. #endif
  79. .nbuffers = 2,
  80. .model = &model
  81. };
  82. for (i = 0; i < NTASKS; i++)
  83. {
  84. struct starpu_task *task = starpu_task_create();
  85. task->cl = &cl;
  86. STARPU_ASSERT(starpu_get_sub_data(cnt_array_handle, 1, i));
  87. task->buffers[0].handle = sobol_qrng_direction_handle;
  88. task->buffers[0].mode = STARPU_R;
  89. task->buffers[1].handle = starpu_get_sub_data(cnt_array_handle, 1, i);
  90. task->buffers[1].mode = STARPU_W;
  91. int ret = starpu_submit_task(task);
  92. STARPU_ASSERT(!ret);
  93. }
  94. starpu_wait_all_tasks();
  95. /* Get the cnt_array back in main memory */
  96. starpu_unpartition_data(cnt_array_handle, 0);
  97. starpu_sync_data_with_mem(cnt_array_handle, STARPU_RW);
  98. /* Count the total number of entries */
  99. unsigned long total_cnt = 0;
  100. for (i = 0; i < NTASKS; i++)
  101. total_cnt += cnt_array[i];
  102. starpu_release_data_from_mem(cnt_array_handle);
  103. starpu_shutdown();
  104. /* Total surface : Pi * r^ 2 = Pi*1^2, total square surface : 2^2 = 4, probability to impact the disk: pi/4 */
  105. fprintf(stderr, "Pi approximation : %f (%ld / %ld)\n", ((TYPE)total_cnt*4)/(SIZE), total_cnt, SIZE);
  106. return 0;
  107. }