shadow.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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. #include <starpu_cuda.h>
  43. /* Shadow width */
  44. #define SHADOW 2
  45. #define NX 30
  46. #define PARTS 3
  47. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  48. void cpu_func(void *buffers[], void *cl_arg)
  49. {
  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. /* length of the shadowed source vector */
  68. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  69. /* local copy of the shadowed source vector pointer */
  70. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  71. /* length of the destination vector */
  72. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  73. /* local copy of the destination vector pointer */
  74. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  75. /* If things go right, sizes should match */
  76. STARPU_ASSERT(n == n2);
  77. cudaMemcpyAsync(val2, val, n*sizeof(*val), cudaMemcpyDeviceToDevice, starpu_cuda_get_local_stream());
  78. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  79. }
  80. #endif
  81. int main(int argc, char **argv)
  82. {
  83. unsigned i, j;
  84. int vector[NX + 2*SHADOW];
  85. int vector2[NX + PARTS*2*SHADOW];
  86. starpu_data_handle_t handle, handle2;
  87. int ret;
  88. struct starpu_codelet cl =
  89. {
  90. .where = STARPU_CPU
  91. #ifdef STARPU_USE_CUDA
  92. |STARPU_CUDA
  93. #endif
  94. ,
  95. .cpu_funcs = {cpu_func, NULL},
  96. #ifdef STARPU_USE_CUDA
  97. .cuda_funcs = {cuda_func, NULL},
  98. #endif
  99. .nbuffers = 2,
  100. .modes = {STARPU_R, STARPU_W}
  101. };
  102. for(i=0 ; i<NX ; i++) vector[SHADOW+i] = i;
  103. for(i=0 ; i<SHADOW ; i++) vector[i] = vector[i+NX];
  104. for(i=0 ; i<SHADOW ; i++) vector[SHADOW+NX+i] = vector[SHADOW+i];
  105. FPRINTF(stderr,"IN Vector: ");
  106. for(i=0 ; i<NX + 2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  107. FPRINTF(stderr,"\n");
  108. ret = starpu_init(NULL);
  109. if (ret == -ENODEV)
  110. exit(77);
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  112. /* Declare source vector to StarPU */
  113. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX + 2*SHADOW, sizeof(vector[0]));
  114. /* Declare destination vector to StarPU */
  115. starpu_vector_data_register(&handle2, 0, (uintptr_t)vector2, NX + PARTS*2*SHADOW, sizeof(vector[0]));
  116. /* Partition the source vector in PARTS sub-vectors with shadows */
  117. /* NOTE: the resulting handles should only be used in read-only mode,
  118. * as StarPU will not know how the overlapping parts would have to be
  119. * combined. */
  120. struct starpu_data_filter f =
  121. {
  122. .filter_func = starpu_block_shadow_filter_func_vector,
  123. .nchildren = PARTS,
  124. .filter_arg_ptr = (void*)(uintptr_t) SHADOW /* Shadow width */
  125. };
  126. starpu_data_partition(handle, &f);
  127. /* Partition the destination vector in PARTS sub-vectors */
  128. struct starpu_data_filter f2 =
  129. {
  130. .filter_func = starpu_block_filter_func_vector,
  131. .nchildren = PARTS,
  132. };
  133. starpu_data_partition(handle2, &f2);
  134. /* Submit a task on each sub-vector */
  135. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  136. {
  137. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  138. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 1, i);
  139. struct starpu_task *task = starpu_task_create();
  140. task->handles[0] = sub_handle;
  141. task->handles[1] = sub_handle2;
  142. task->cl = &cl;
  143. task->synchronous = 1;
  144. ret = starpu_task_submit(task);
  145. if (ret == -ENODEV) goto enodev;
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  147. }
  148. starpu_data_unpartition(handle, 0);
  149. starpu_data_unpartition(handle2, 0);
  150. starpu_data_unregister(handle);
  151. starpu_data_unregister(handle2);
  152. starpu_shutdown();
  153. FPRINTF(stderr,"OUT Vector: ");
  154. for(i=0 ; i<NX + PARTS*2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector2[i]);
  155. FPRINTF(stderr,"\n");
  156. for(i=0 ; i<PARTS ; i++)
  157. for (j=0 ; j<NX/PARTS ; j++)
  158. STARPU_ASSERT(vector2[i*(NX/PARTS+2*SHADOW)+j] == vector[i*(NX/PARTS)+j]);
  159. return 0;
  160. enodev:
  161. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  162. starpu_shutdown();
  163. return 77;
  164. }