minmax_reduction.c 4.3 KB

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