shadow.c 6.2 KB

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