minmax_reduction.c 4.9 KB

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