insert_task_dyn_handles.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2017 CNRS
  4. * Copyright (C) 2015,2017 Université de Bordeaux
  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. #include <starpu.h>
  18. #include <starpu_mpi.h>
  19. #include <starpu_config.h>
  20. #include "helper.h"
  21. #define FFACTOR 42
  22. void func_cpu(void *descr[], void *_args)
  23. {
  24. (void)_args;
  25. int num = starpu_task_get_current()->nbuffers;
  26. int *factor = (int *)STARPU_VARIABLE_GET_PTR(descr[num-1]);
  27. int i;
  28. for (i = 0; i < num-1; i++)
  29. {
  30. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[i]);
  31. *x = *x + 1**factor;
  32. }
  33. }
  34. struct starpu_codelet codelet =
  35. {
  36. .cpu_funcs = {func_cpu},
  37. .cpu_funcs_name = {"func_cpu"},
  38. .nbuffers = STARPU_VARIABLE_NBUFFERS,
  39. #ifdef STARPU_SIMGRID
  40. .model = &starpu_perfmodel_nop,
  41. #endif
  42. };
  43. int main(int argc, char **argv)
  44. {
  45. int *x;
  46. int i, ret, loop;
  47. int rank;
  48. int factor=0;
  49. #ifdef STARPU_QUICK_CHECK
  50. int nloops = 4;
  51. #else
  52. int nloops = 16;
  53. #endif
  54. starpu_data_handle_t *data_handles;
  55. starpu_data_handle_t factor_handle;
  56. struct starpu_data_descr *descrs;
  57. int mpi_init;
  58. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  59. ret = starpu_init(NULL);
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  61. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  62. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  63. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  64. if (starpu_cpu_worker_get_count() == 0)
  65. {
  66. if (rank == 0)
  67. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  68. starpu_mpi_shutdown();
  69. starpu_shutdown();
  70. return STARPU_TEST_SKIPPED;
  71. }
  72. x = calloc(1, (STARPU_NMAXBUFS+15) * sizeof(int));
  73. data_handles = malloc((STARPU_NMAXBUFS+15) * sizeof(starpu_data_handle_t));
  74. descrs = malloc((STARPU_NMAXBUFS+15) * sizeof(struct starpu_data_descr));
  75. for(i=0 ; i<STARPU_NMAXBUFS+15 ; i++)
  76. {
  77. starpu_variable_data_register(&data_handles[i], STARPU_MAIN_RAM, (uintptr_t)&x[i], sizeof(x[i]));
  78. starpu_mpi_data_register(data_handles[i], i, 0);
  79. descrs[i].handle = data_handles[i];
  80. descrs[i].mode = STARPU_RW;
  81. }
  82. if (rank == 1)
  83. factor=FFACTOR;
  84. starpu_variable_data_register(&factor_handle, STARPU_MAIN_RAM, (uintptr_t)&factor, sizeof(factor));
  85. starpu_mpi_data_register(factor_handle, FFACTOR, 1);
  86. for (loop = 0; loop < nloops; loop++)
  87. {
  88. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet,
  89. STARPU_DATA_MODE_ARRAY, descrs, STARPU_NMAXBUFS-1,
  90. STARPU_R, factor_handle,
  91. 0);
  92. if (ret == -ENODEV)
  93. goto enodev;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  95. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet,
  96. STARPU_DATA_MODE_ARRAY, descrs, STARPU_NMAXBUFS+15,
  97. STARPU_R, factor_handle,
  98. 0);
  99. if (ret == -ENODEV)
  100. goto enodev;
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  102. }
  103. enodev:
  104. for(i=0 ; i<STARPU_NMAXBUFS+15 ; i++)
  105. {
  106. starpu_data_unregister(data_handles[i]);
  107. }
  108. starpu_data_unregister(factor_handle);
  109. free(data_handles);
  110. free(descrs);
  111. if (ret == -ENODEV)
  112. {
  113. fprintf(stderr, "WARNING: No one can execute this task\n");
  114. /* yes, we do not perform the computation but we did detect that no one
  115. * could perform the kernel, so this is not an error from StarPU */
  116. free(x);
  117. ret = STARPU_TEST_SKIPPED;
  118. }
  119. else if (rank == 0)
  120. {
  121. #ifndef STARPU_SIMGRID
  122. for(i=0 ; i<STARPU_NMAXBUFS-1 ; i++)
  123. {
  124. if (x[i] != nloops * FFACTOR * 2)
  125. {
  126. FPRINTF_MPI(stderr, "[end loop] value[%d] = %d != Expected value %d\n", i, x[i], nloops*2);
  127. ret = 1;
  128. }
  129. }
  130. for(i=STARPU_NMAXBUFS-1 ; i<STARPU_NMAXBUFS+15 ; i++)
  131. {
  132. if (x[i] != nloops * FFACTOR)
  133. {
  134. FPRINTF_MPI(stderr, "[end loop] value[%d] = %d != Expected value %d\n", i, x[i], nloops);
  135. ret = 1;
  136. }
  137. }
  138. if (ret == 0)
  139. {
  140. FPRINTF_MPI(stderr, "[end of loop] all values are correct\n");
  141. }
  142. #endif
  143. free(x);
  144. }
  145. else
  146. {
  147. FPRINTF_MPI(stderr, "[end of loop] no computation on this node\n");
  148. ret = 0;
  149. free(x);
  150. }
  151. starpu_mpi_shutdown();
  152. starpu_shutdown();
  153. if (!mpi_init)
  154. MPI_Finalize();
  155. return ret;
  156. }