minmax_reduction.c 5.2 KB

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