insert_task_dyn_handles.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2017 CNRS
  4. * Copyright (C) 2015,2017,2018 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_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  61. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  62. if (starpu_cpu_worker_get_count() == 0)
  63. {
  64. if (rank == 0)
  65. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  66. starpu_mpi_shutdown();
  67. return STARPU_TEST_SKIPPED;
  68. }
  69. x = calloc(1, (STARPU_NMAXBUFS+15) * sizeof(int));
  70. data_handles = malloc((STARPU_NMAXBUFS+15) * sizeof(starpu_data_handle_t));
  71. descrs = malloc((STARPU_NMAXBUFS+15) * sizeof(struct starpu_data_descr));
  72. for(i=0 ; i<STARPU_NMAXBUFS+15 ; i++)
  73. {
  74. starpu_variable_data_register(&data_handles[i], STARPU_MAIN_RAM, (uintptr_t)&x[i], sizeof(x[i]));
  75. starpu_mpi_data_register(data_handles[i], i, 0);
  76. descrs[i].handle = data_handles[i];
  77. descrs[i].mode = STARPU_RW;
  78. }
  79. if (rank == 1)
  80. factor=FFACTOR;
  81. starpu_variable_data_register(&factor_handle, STARPU_MAIN_RAM, (uintptr_t)&factor, sizeof(factor));
  82. starpu_mpi_data_register(factor_handle, FFACTOR, 1);
  83. for (loop = 0; loop < nloops; loop++)
  84. {
  85. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet,
  86. STARPU_DATA_MODE_ARRAY, descrs, STARPU_NMAXBUFS-1,
  87. STARPU_R, factor_handle,
  88. 0);
  89. if (ret == -ENODEV)
  90. goto enodev;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  92. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet,
  93. STARPU_DATA_MODE_ARRAY, descrs, STARPU_NMAXBUFS+15,
  94. STARPU_R, factor_handle,
  95. 0);
  96. if (ret == -ENODEV)
  97. goto enodev;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  99. }
  100. enodev:
  101. for(i=0 ; i<STARPU_NMAXBUFS+15 ; i++)
  102. {
  103. starpu_data_unregister(data_handles[i]);
  104. }
  105. starpu_data_unregister(factor_handle);
  106. free(data_handles);
  107. free(descrs);
  108. if (ret == -ENODEV)
  109. {
  110. fprintf(stderr, "WARNING: No one can execute this task\n");
  111. /* yes, we do not perform the computation but we did detect that no one
  112. * could perform the kernel, so this is not an error from StarPU */
  113. free(x);
  114. ret = STARPU_TEST_SKIPPED;
  115. }
  116. else if (rank == 0)
  117. {
  118. #ifndef STARPU_SIMGRID
  119. for(i=0 ; i<STARPU_NMAXBUFS-1 ; i++)
  120. {
  121. if (x[i] != nloops * FFACTOR * 2)
  122. {
  123. FPRINTF_MPI(stderr, "[end loop] value[%d] = %d != Expected value %d\n", i, x[i], nloops*2);
  124. ret = 1;
  125. }
  126. }
  127. for(i=STARPU_NMAXBUFS-1 ; i<STARPU_NMAXBUFS+15 ; i++)
  128. {
  129. if (x[i] != nloops * FFACTOR)
  130. {
  131. FPRINTF_MPI(stderr, "[end loop] value[%d] = %d != Expected value %d\n", i, x[i], nloops);
  132. ret = 1;
  133. }
  134. }
  135. if (ret == 0)
  136. {
  137. FPRINTF_MPI(stderr, "[end of loop] all values are correct\n");
  138. }
  139. #endif
  140. free(x);
  141. }
  142. else
  143. {
  144. FPRINTF_MPI(stderr, "[end of loop] no computation on this node\n");
  145. ret = 0;
  146. free(x);
  147. }
  148. starpu_mpi_shutdown();
  149. if (!mpi_init)
  150. MPI_Finalize();
  151. return ret;
  152. }