minmax_reduction.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011,2013-2015 Université de Bordeaux
  4. * Copyright (C) 2012,2013 Inria
  5. * Copyright (C) 2011-2013,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 the minimum and maximum values of a big vector, using data
  20. * reduction to optimize the computation.
  21. */
  22. #include <assert.h>
  23. #include <float.h>
  24. #include <limits.h>
  25. #include <starpu.h>
  26. #ifdef STARPU_QUICK_CHECK
  27. static unsigned _nblocks = 512;
  28. static unsigned _entries_per_bock = 64;
  29. #else
  30. static unsigned _nblocks = 8192;
  31. static unsigned _entries_per_bock = 1024;
  32. #endif
  33. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  34. #define TYPE double
  35. #define TYPE_MAX DBL_MAX
  36. #define TYPE_MIN DBL_MIN
  37. static TYPE *_x;
  38. static starpu_data_handle_t *_x_handles;
  39. /* The first element (resp. second) stores the min element (resp. max). */
  40. static TYPE _minmax[2];
  41. static starpu_data_handle_t _minmax_handle;
  42. /*
  43. * Codelet to create a neutral element
  44. */
  45. void minmax_neutral_cpu_func(void *descr[], void *cl_arg)
  46. {
  47. (void)cl_arg;
  48. TYPE *array = (TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  49. /* Initialize current min to the greatest possible value. */
  50. array[0] = TYPE_MAX;
  51. /* Initialize current max to the smallest possible value. */
  52. array[1] = TYPE_MIN;
  53. }
  54. static struct starpu_codelet minmax_init_codelet =
  55. {
  56. .cpu_funcs = {minmax_neutral_cpu_func},
  57. .cpu_funcs_name = {"minmax_neutral_cpu_func"},
  58. .modes = {STARPU_W},
  59. .nbuffers = 1,
  60. .name = "init"
  61. };
  62. /*
  63. * Codelet to perform the reduction of two elements
  64. */
  65. void minmax_redux_cpu_func(void *descr[], void *cl_arg)
  66. {
  67. (void)cl_arg;
  68. TYPE *array_dst = (TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  69. TYPE *array_src = (TYPE *)STARPU_VARIABLE_GET_PTR(descr[1]);
  70. /* Compute the min value */
  71. TYPE min_dst = array_dst[0];
  72. TYPE min_src = array_src[0];
  73. array_dst[0] = STARPU_MIN(min_dst, min_src);
  74. /* Compute the max value */
  75. TYPE max_dst = array_dst[1];
  76. TYPE max_src = array_src[1];
  77. array_dst[1] = STARPU_MAX(max_dst, max_src);
  78. }
  79. static struct starpu_codelet minmax_redux_codelet =
  80. {
  81. .cpu_funcs = {minmax_redux_cpu_func},
  82. .cpu_funcs_name = {"minmax_redux_cpu_func"},
  83. .modes = {STARPU_RW, STARPU_R},
  84. .nbuffers = 2,
  85. .name = "redux"
  86. };
  87. /*
  88. * Compute max/min within a vector and update the min/max value
  89. */
  90. void minmax_cpu_func(void *descr[], void *cl_arg)
  91. {
  92. (void)cl_arg;
  93. /* The array containing the values */
  94. TYPE *local_array = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  95. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  96. TYPE *minmax = (TYPE *)STARPU_VARIABLE_GET_PTR(descr[1]);
  97. TYPE local_min = minmax[0];
  98. TYPE local_max = minmax[1];
  99. /* Compute the min and the max elements in the array */
  100. unsigned i;
  101. for (i = 0; i < n; i++)
  102. {
  103. TYPE val = local_array[i];
  104. local_min = STARPU_MIN(local_min, val);
  105. local_max = STARPU_MAX(local_max, val);
  106. }
  107. minmax[0] = local_min;
  108. minmax[1] = local_max;
  109. }
  110. static struct starpu_codelet minmax_codelet =
  111. {
  112. .cpu_funcs = {minmax_cpu_func},
  113. .cpu_funcs_name = {"minmax_cpu_func"},
  114. .nbuffers = 2,
  115. .modes = {STARPU_R, STARPU_REDUX},
  116. .name = "minmax"
  117. };
  118. /*
  119. * Tasks initialization
  120. */
  121. int main(void)
  122. {
  123. unsigned long i;
  124. int ret;
  125. /* Not supported yet */
  126. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  127. return 77;
  128. ret = starpu_init(NULL);
  129. if (ret == -ENODEV)
  130. return 77;
  131. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  132. unsigned long nelems = _nblocks*_entries_per_bock;
  133. size_t size = nelems*sizeof(TYPE);
  134. _x = (TYPE *) malloc(size);
  135. _x_handles = (starpu_data_handle_t *) calloc(_nblocks, sizeof(starpu_data_handle_t));
  136. assert(_x && _x_handles);
  137. /* Initialize the vector with random values */
  138. starpu_srand48(0);
  139. for (i = 0; i < nelems; i++)
  140. _x[i] = (TYPE)starpu_drand48();
  141. unsigned block;
  142. for (block = 0; block < _nblocks; block++)
  143. {
  144. uintptr_t block_start = (uintptr_t)&_x[_entries_per_bock*block];
  145. starpu_vector_data_register(&_x_handles[block], STARPU_MAIN_RAM, block_start,
  146. _entries_per_bock, sizeof(TYPE));
  147. }
  148. /* Initialize current min */
  149. _minmax[0] = TYPE_MAX;
  150. /* Initialize current max */
  151. _minmax[1] = TYPE_MIN;
  152. starpu_variable_data_register(&_minmax_handle, STARPU_MAIN_RAM, (uintptr_t)_minmax, 2*sizeof(TYPE));
  153. /* Set the methods to define neutral elements and to perform the reduction operation */
  154. starpu_data_set_reduction_methods(_minmax_handle, &minmax_redux_codelet, &minmax_init_codelet);
  155. for (block = 0; block < _nblocks; block++)
  156. {
  157. struct starpu_task *task = starpu_task_create();
  158. task->cl = &minmax_codelet;
  159. task->handles[0] = _x_handles[block];
  160. task->handles[1] = _minmax_handle;
  161. ret = starpu_task_submit(task);
  162. if (ret)
  163. {
  164. STARPU_ASSERT(ret == -ENODEV);
  165. FPRINTF(stderr, "This test can only run on CPUs, but there are no CPU workers (this is not a bug).\n");
  166. return 77;
  167. }
  168. }
  169. for (block = 0; block < _nblocks; block++)
  170. {
  171. starpu_data_unregister(_x_handles[block]);
  172. }
  173. starpu_data_unregister(_minmax_handle);
  174. FPRINTF(stderr, "Min : %e\n", _minmax[0]);
  175. FPRINTF(stderr, "Max : %e\n", _minmax[1]);
  176. STARPU_ASSERT(_minmax[0] <= _minmax[1]);
  177. free(_x);
  178. free(_x_handles);
  179. starpu_shutdown();
  180. return 0;
  181. }