shadow.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This examplifies the use of the shadow filter: a source "vector" of NX
  19. * elements (plus 2*SHADOW wrap-around elements) is partitioned into vectors
  20. * with some shadowing, and these are copied into a destination "vector2" of
  21. * NRPARTS*(NX/NPARTS+2*SHADOW) elements, partitioned in the traditionnal way,
  22. * thus showing how shadowing shows up.
  23. *
  24. * For instance, with NX=8, SHADOW=1, and NPARTS=4:
  25. *
  26. * vector
  27. * x0 x1 x2 x3 x4 x5 x6 x7 x8 x9
  28. *
  29. * is partitioned into 4 pieces:
  30. *
  31. * x0 x1 x2 x3
  32. * x2 x3 x4 x5
  33. * x4 x5 x6 x7
  34. * x6 x7 x8 x9
  35. *
  36. * which are copied into the 4 destination subparts of vector2, thus getting in
  37. * the end:
  38. *
  39. * x0 x1 x2 x3 x2 x3 x4 x5 x4 x5 x6 x7 x6 x7 x8 x9
  40. */
  41. #include <starpu.h>
  42. /* Shadow width */
  43. #define SHADOW 2
  44. #define NX 30
  45. #define PARTS 3
  46. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  47. void cpu_func(void *buffers[], void *cl_arg)
  48. {
  49. (void)cl_arg;
  50. unsigned i;
  51. /* length of the shadowed source vector */
  52. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  53. /* local copy of the shadowed source vector pointer */
  54. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  55. /* length of the destination vector */
  56. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  57. /* local copy of the destination vector pointer */
  58. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  59. /* If things go right, sizes should match */
  60. STARPU_ASSERT(n == n2);
  61. for (i = 0; i < n; i++)
  62. val2[i] = val[i];
  63. }
  64. #ifdef STARPU_USE_CUDA
  65. void cuda_func(void *buffers[], void *cl_arg)
  66. {
  67. (void)cl_arg;
  68. /* length of the shadowed source vector */
  69. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  70. /* local copy of the shadowed source vector pointer */
  71. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  72. /* length of the destination vector */
  73. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  74. /* local copy of the destination vector pointer */
  75. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  76. /* If things go right, sizes should match */
  77. STARPU_ASSERT(n == n2);
  78. cudaMemcpyAsync(val2, val, n*sizeof(*val), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  79. }
  80. #endif
  81. int main(void)
  82. {
  83. unsigned j;
  84. int vector[NX + 2*SHADOW];
  85. int vector2[NX + PARTS*2*SHADOW];
  86. starpu_data_handle_t handle, handle2;
  87. int ret, i;
  88. struct starpu_codelet cl =
  89. {
  90. .cpu_funcs = {cpu_func},
  91. .cpu_funcs_name = {"cpu_func"},
  92. #ifdef STARPU_USE_CUDA
  93. .cuda_funcs = {cuda_func},
  94. .cuda_flags = {STARPU_CUDA_ASYNC},
  95. #endif
  96. .nbuffers = 2,
  97. .modes = {STARPU_R, STARPU_W}
  98. };
  99. for(i=0 ; i<NX ; i++) vector[SHADOW+i] = i;
  100. for(i=0 ; i<SHADOW ; i++) vector[i] = vector[i+NX];
  101. for(i=0 ; i<SHADOW ; i++) vector[SHADOW+NX+i] = vector[SHADOW+i];
  102. FPRINTF(stderr,"IN Vector: ");
  103. for(i=0 ; i<NX + 2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  104. FPRINTF(stderr,"\n");
  105. ret = starpu_init(NULL);
  106. if (ret == -ENODEV)
  107. exit(77);
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  109. /* Declare source vector to StarPU */
  110. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX + 2*SHADOW, sizeof(vector[0]));
  111. /* Declare destination vector to StarPU */
  112. starpu_vector_data_register(&handle2, STARPU_MAIN_RAM, (uintptr_t)vector2, NX + PARTS*2*SHADOW, sizeof(vector[0]));
  113. /* Partition the source vector in PARTS sub-vectors with shadows */
  114. /* NOTE: the resulting handles should only be used in read-only mode,
  115. * as StarPU will not know how the overlapping parts would have to be
  116. * combined. */
  117. struct starpu_data_filter f =
  118. {
  119. .filter_func = starpu_vector_filter_block_shadow,
  120. .nchildren = PARTS,
  121. .filter_arg_ptr = (void*)(uintptr_t) SHADOW /* Shadow width */
  122. };
  123. starpu_data_partition(handle, &f);
  124. /* Partition the destination vector in PARTS sub-vectors */
  125. struct starpu_data_filter f2 =
  126. {
  127. .filter_func = starpu_vector_filter_block,
  128. .nchildren = PARTS,
  129. };
  130. starpu_data_partition(handle2, &f2);
  131. /* Submit a task on each sub-vector */
  132. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  133. {
  134. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  135. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 1, i);
  136. struct starpu_task *task = starpu_task_create();
  137. task->handles[0] = sub_handle;
  138. task->handles[1] = sub_handle2;
  139. task->cl = &cl;
  140. task->synchronous = 1;
  141. ret = starpu_task_submit(task);
  142. if (ret == -ENODEV) goto enodev;
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  144. }
  145. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  146. starpu_data_unpartition(handle2, STARPU_MAIN_RAM);
  147. starpu_data_unregister(handle);
  148. starpu_data_unregister(handle2);
  149. starpu_shutdown();
  150. FPRINTF(stderr,"OUT Vector: ");
  151. for(i=0 ; i<NX + PARTS*2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector2[i]);
  152. FPRINTF(stderr,"\n");
  153. for(i=0 ; i<PARTS ; i++)
  154. for (j=0 ; j<NX/PARTS ; j++)
  155. STARPU_ASSERT(vector2[i*(NX/PARTS+2*SHADOW)+j] == vector[i*(NX/PARTS)+j]);
  156. return 0;
  157. enodev:
  158. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  159. starpu_shutdown();
  160. return 77;
  161. }