insert_task_dyn_handles.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <config.h>
  17. #include <starpu_mpi.h>
  18. #include <starpu_config.h>
  19. #include "helper.h"
  20. #define FFACTOR 42
  21. void func_cpu(void *descr[], void *_args)
  22. {
  23. int num = starpu_task_get_current()->nbuffers;
  24. int *factor = (int *)STARPU_VARIABLE_GET_PTR(descr[num-1]);
  25. int i;
  26. for (i = 0; i < num-1; i++)
  27. {
  28. int *x = (int *)STARPU_VARIABLE_GET_PTR(descr[i]);
  29. *x = *x + 1**factor;
  30. }
  31. }
  32. struct starpu_codelet codelet =
  33. {
  34. .cpu_funcs = {func_cpu},
  35. .cpu_funcs_name = {"func_cpu"},
  36. .nbuffers = STARPU_VARIABLE_NBUFFERS,
  37. #ifdef STARPU_SIMGRID
  38. .model = &starpu_nop_perf_model,
  39. #endif
  40. };
  41. int main(int argc, char **argv)
  42. {
  43. int *x;
  44. int i, ret, loop;
  45. int rank;
  46. int factor=0;
  47. #ifdef STARPU_QUICK_CHECK
  48. int nloops = 4;
  49. #else
  50. int nloops = 16;
  51. #endif
  52. starpu_data_handle_t *data_handles;
  53. starpu_data_handle_t factor_handle;
  54. struct starpu_data_descr *descrs;
  55. int mpi_init;
  56. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  57. ret = starpu_init(NULL);
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  59. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  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. starpu_shutdown();
  68. return STARPU_TEST_SKIPPED;
  69. }
  70. x = calloc(1, (STARPU_NMAXBUFS+15) * sizeof(int));
  71. data_handles = malloc((STARPU_NMAXBUFS+15) * sizeof(starpu_data_handle_t));
  72. descrs = malloc((STARPU_NMAXBUFS+15) * sizeof(struct starpu_data_descr));
  73. for(i=0 ; i<STARPU_NMAXBUFS+15 ; i++)
  74. {
  75. starpu_variable_data_register(&data_handles[i], STARPU_MAIN_RAM, (uintptr_t)&x[i], sizeof(x[i]));
  76. starpu_mpi_data_register(data_handles[i], i, 0);
  77. descrs[i].handle = data_handles[i];
  78. descrs[i].mode = STARPU_RW;
  79. }
  80. if (rank == 1)
  81. factor=FFACTOR;
  82. starpu_variable_data_register(&factor_handle, STARPU_MAIN_RAM, (uintptr_t)&factor, sizeof(factor));
  83. starpu_mpi_data_register(factor_handle, FFACTOR, 1);
  84. for (loop = 0; loop < nloops; loop++)
  85. {
  86. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet,
  87. STARPU_DATA_MODE_ARRAY, descrs, STARPU_NMAXBUFS-1,
  88. STARPU_R, factor_handle,
  89. 0);
  90. if (ret == -ENODEV)
  91. goto enodev;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  93. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet,
  94. STARPU_DATA_MODE_ARRAY, descrs, STARPU_NMAXBUFS+15,
  95. STARPU_R, factor_handle,
  96. 0);
  97. if (ret == -ENODEV)
  98. goto enodev;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  100. }
  101. enodev:
  102. for(i=0 ; i<STARPU_NMAXBUFS+15 ; i++)
  103. {
  104. starpu_data_unregister(data_handles[i]);
  105. }
  106. starpu_data_unregister(factor_handle);
  107. free(data_handles);
  108. free(descrs);
  109. if (ret == -ENODEV)
  110. {
  111. fprintf(stderr, "WARNING: No one can execute this task\n");
  112. /* yes, we do not perform the computation but we did detect that no one
  113. * could perform the kernel, so this is not an error from StarPU */
  114. free(x);
  115. ret = STARPU_TEST_SKIPPED;
  116. }
  117. else if (rank == 0)
  118. {
  119. #ifndef STARPU_SIMGRID
  120. for(i=0 ; i<STARPU_NMAXBUFS-1 ; i++)
  121. {
  122. if (x[i] != nloops * FFACTOR * 2)
  123. {
  124. FPRINTF_MPI(stderr, "[end loop] value[%d] = %d != Expected value %d\n", i, x[i], nloops*2);
  125. ret = 1;
  126. }
  127. }
  128. for(i=STARPU_NMAXBUFS-1 ; i<STARPU_NMAXBUFS+15 ; i++)
  129. {
  130. if (x[i] != nloops * FFACTOR)
  131. {
  132. FPRINTF_MPI(stderr, "[end loop] value[%d] = %d != Expected value %d\n", i, x[i], nloops);
  133. ret = 1;
  134. }
  135. }
  136. if (ret == 0)
  137. {
  138. FPRINTF_MPI(stderr, "[end of loop] all values are correct\n");
  139. }
  140. #endif
  141. free(x);
  142. }
  143. else
  144. {
  145. FPRINTF_MPI(stderr, "[end of loop] no computation on this node\n");
  146. ret = 0;
  147. free(x);
  148. }
  149. starpu_mpi_shutdown();
  150. starpu_shutdown();
  151. if (!mpi_init)
  152. MPI_Finalize();
  153. return ret;
  154. }