pi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <starpu.h>
  17. #include <stdio.h>
  18. #define NTASKS (32*1024)
  19. #define NSHOT_PER_TASK 1024
  20. #define SIZE (NTASKS*NSHOT_PER_TASK)
  21. #define TYPE float
  22. static void cpu_kernel(void *descr[], void *cl_arg)
  23. {
  24. TYPE *random_numbers_x = (TYPE *)STARPU_GET_VECTOR_PTR(descr[0]);
  25. TYPE *random_numbers_y = (TYPE *)STARPU_GET_VECTOR_PTR(descr[1]);
  26. unsigned nx = STARPU_GET_VECTOR_NX(descr[0]);
  27. unsigned current_cnt = 0;
  28. unsigned i;
  29. for (i = 0; i < nx; i++)
  30. {
  31. TYPE x = random_numbers_x[i];
  32. TYPE y = random_numbers_y[i];
  33. TYPE dist = (x*x + y*y);
  34. unsigned success = (dist <= 1.0);
  35. current_cnt += success;
  36. }
  37. unsigned *cnt = (unsigned *)STARPU_GET_VECTOR_PTR(descr[2]);
  38. *cnt = current_cnt;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. unsigned i;
  43. starpu_init(NULL);
  44. TYPE *random_array_x;
  45. starpu_malloc_pinned_if_possible((void **)&random_array_x, SIZE*sizeof(TYPE));
  46. STARPU_ASSERT(random_array_x);
  47. TYPE *random_array_y;
  48. starpu_malloc_pinned_if_possible((void **)&random_array_y, SIZE*sizeof(TYPE));
  49. STARPU_ASSERT(random_array_y);
  50. unsigned *cnt_array;
  51. starpu_malloc_pinned_if_possible((void **)&cnt_array, NTASKS*sizeof(unsigned));
  52. STARPU_ASSERT(cnt_array);
  53. /* First generate an array of random numbers */
  54. for (i = 0; i < SIZE; i++)
  55. {
  56. random_array_x[i] = (((TYPE)rand()/(TYPE)RAND_MAX)*2.0 - 1.0);
  57. random_array_y[i] = (((TYPE)rand()/(TYPE)RAND_MAX)*2.0 - 1.0);
  58. }
  59. /* Register the entire array */
  60. starpu_data_handle random_array_handle_x;
  61. starpu_register_vector_data(&random_array_handle_x, 0, (uintptr_t)random_array_x, SIZE, sizeof(TYPE));
  62. starpu_data_handle random_array_handle_y;
  63. starpu_register_vector_data(&random_array_handle_y, 0, (uintptr_t)random_array_y, SIZE, sizeof(TYPE));
  64. starpu_data_handle cnt_array_handle;
  65. starpu_register_vector_data(&cnt_array_handle, 0, (uintptr_t)cnt_array, NTASKS, sizeof(unsigned));
  66. /* TODO use a write-back mechanism */
  67. struct starpu_filter_t f = {
  68. .filter_func = starpu_block_filter_func_vector,
  69. .filter_arg = NTASKS
  70. };
  71. starpu_partition_data(random_array_handle_x, &f);
  72. starpu_partition_data(random_array_handle_y, &f);
  73. starpu_partition_data(cnt_array_handle, &f);
  74. struct starpu_codelet_t cl = {
  75. .where = STARPU_CPU,
  76. .cpu_func = cpu_kernel,
  77. .cuda_func = NULL, /* TODO */
  78. .nbuffers = 3,
  79. .model = NULL /* TODO */
  80. };
  81. for (i = 0; i < NTASKS; i++)
  82. {
  83. struct starpu_task *task = starpu_task_create();
  84. task->cl = &cl;
  85. task->buffers[0].handle = starpu_get_sub_data(random_array_handle_x, 1, i);
  86. task->buffers[0].mode = STARPU_R;
  87. task->buffers[1].handle = starpu_get_sub_data(random_array_handle_y, 1, i);
  88. task->buffers[1].mode = STARPU_R;
  89. task->buffers[2].handle = starpu_get_sub_data(cnt_array_handle, 1, i);
  90. task->buffers[2].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 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 (%d / %d)\n", ((TYPE)total_cnt*4)/(SIZE), total_cnt, SIZE);
  106. return 0;
  107. }