shadow.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 copied into a destination
  20. * vector of NX+NPARTS*2*SHADOW elements, thus showing how shadowing shows up.
  21. */
  22. #include <starpu.h>
  23. #include <starpu_cuda.h>
  24. /* Shadow width */
  25. #define SHADOW 2
  26. #define NX 30
  27. #define PARTS 3
  28. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  29. void cpu_func(void *buffers[], void *cl_arg)
  30. {
  31. unsigned i;
  32. /* length of the shadowed source vector */
  33. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  34. /* local copy of the shadowed source vector pointer */
  35. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  36. /* length of the destination vector */
  37. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  38. /* local copy of the destination vector pointer */
  39. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  40. /* If things go right, sizes should match */
  41. STARPU_ASSERT(n == n2);
  42. for (i = 0; i < n; i++)
  43. val2[i] = val[i];
  44. }
  45. #ifdef STARPU_USE_CUDA
  46. void cuda_func(void *buffers[], void *cl_arg)
  47. {
  48. /* length of the shadowed source vector */
  49. unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
  50. /* local copy of the shadowed source vector pointer */
  51. int *val = (int *)STARPU_VECTOR_GET_PTR(buffers[0]);
  52. /* length of the destination vector */
  53. unsigned n2 = STARPU_VECTOR_GET_NX(buffers[1]);
  54. /* local copy of the destination vector pointer */
  55. int *val2 = (int *)STARPU_VECTOR_GET_PTR(buffers[1]);
  56. /* If things go right, sizes should match */
  57. STARPU_ASSERT(n == n2);
  58. cudaMemcpy(val2, val, n*sizeof(*val), cudaMemcpyDeviceToDevice);
  59. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  60. }
  61. #endif
  62. int main(int argc, char **argv)
  63. {
  64. unsigned i, j;
  65. int vector[NX + 2*SHADOW];
  66. int vector2[NX + PARTS*2*SHADOW];
  67. starpu_data_handle_t handle, handle2;
  68. int factor=1;
  69. int ret;
  70. struct starpu_codelet cl =
  71. {
  72. .where = STARPU_CPU
  73. #ifdef STARPU_USE_CUDA
  74. |STARPU_CUDA
  75. #endif
  76. ,
  77. .cpu_funcs = {cpu_func, NULL},
  78. #ifdef STARPU_USE_CUDA
  79. .cuda_funcs = {cuda_func, NULL},
  80. #endif
  81. .nbuffers = 2,
  82. .modes = {STARPU_R, STARPU_W}
  83. };
  84. for(i=0 ; i<NX ; i++) vector[SHADOW+i] = i;
  85. for(i=0 ; i<SHADOW ; i++) vector[i] = vector[i+NX];
  86. for(i=0 ; i<SHADOW ; i++) vector[SHADOW+NX+i] = vector[SHADOW+i];
  87. FPRINTF(stderr,"IN Vector: ");
  88. for(i=0 ; i<NX + 2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector[i]);
  89. FPRINTF(stderr,"\n");
  90. ret = starpu_init(NULL);
  91. if (ret == -ENODEV)
  92. exit(77);
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  94. /* Declare source vector to StarPU */
  95. starpu_vector_data_register(&handle, 0, (uintptr_t)vector, NX + 2*SHADOW, sizeof(vector[0]));
  96. /* Declare destination vector to StarPU */
  97. starpu_vector_data_register(&handle2, 0, (uintptr_t)vector2, NX + PARTS*2*SHADOW, sizeof(vector[0]));
  98. /* Partition the source vector in PARTS sub-vectors with shadows */
  99. struct starpu_data_filter f =
  100. {
  101. .filter_func = starpu_block_shadow_filter_func_vector,
  102. .nchildren = PARTS,
  103. .filter_arg_ptr = (void*)(uintptr_t) SHADOW /* Shadow width */
  104. };
  105. starpu_data_partition(handle, &f);
  106. /* Partition the destination vector in PARTS sub-vectors */
  107. struct starpu_data_filter f2 =
  108. {
  109. .filter_func = starpu_block_filter_func_vector,
  110. .nchildren = PARTS,
  111. };
  112. starpu_data_partition(handle2, &f2);
  113. /* Submit a task on each sub-vector */
  114. for (i=0; i<starpu_data_get_nb_children(handle); i++)
  115. {
  116. starpu_data_handle_t sub_handle = starpu_data_get_sub_data(handle, 1, i);
  117. starpu_data_handle_t sub_handle2 = starpu_data_get_sub_data(handle2, 1, i);
  118. struct starpu_task *task = starpu_task_create();
  119. factor *= 10;
  120. task->handles[0] = sub_handle;
  121. task->handles[1] = sub_handle2;
  122. task->cl = &cl;
  123. task->synchronous = 1;
  124. task->cl_arg = &factor;
  125. task->cl_arg_size = sizeof(factor);
  126. ret = starpu_task_submit(task);
  127. if (ret == -ENODEV) goto enodev;
  128. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  129. }
  130. starpu_data_unpartition(handle, 0);
  131. starpu_data_unpartition(handle2, 0);
  132. starpu_data_unregister(handle);
  133. starpu_data_unregister(handle2);
  134. starpu_shutdown();
  135. FPRINTF(stderr,"OUT Vector: ");
  136. for(i=0 ; i<NX + PARTS*2*SHADOW ; i++) FPRINTF(stderr, "%5d ", vector2[i]);
  137. FPRINTF(stderr,"\n");
  138. for(i=0 ; i<PARTS ; i++)
  139. for (j=0 ; j<NX/PARTS ; j++)
  140. STARPU_ASSERT(vector2[i*(NX/PARTS+2*SHADOW)+j] == vector[i*(NX/PARTS)+j]);
  141. return 0;
  142. enodev:
  143. FPRINTF(stderr, "WARNING: No one can execute this task\n");
  144. starpu_shutdown();
  145. return 77;
  146. }