insert_task_dyn_handles.c 4.7 KB

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