temporary.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 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. /* This tests that one can register temporary data0 on each MPI node which can mix with common data0 */
  17. #include <starpu_mpi.h>
  18. #include "helper.h"
  19. static void func_add(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  20. {
  21. int *a = (void*) STARPU_VARIABLE_GET_PTR(descr[0]);
  22. const int *b = (void*) STARPU_VARIABLE_GET_PTR(descr[1]);
  23. const int *c = (void*) STARPU_VARIABLE_GET_PTR(descr[2]);
  24. *a = *b + *c;
  25. FPRINTF_MPI(stderr, "%d + %d = %d\n", *b, *c, *a);
  26. }
  27. /* Dummy cost function for simgrid */
  28. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  29. {
  30. return 0.000001;
  31. }
  32. static struct starpu_perfmodel dumb_model =
  33. {
  34. .type = STARPU_COMMON,
  35. .cost_function = cost_function
  36. };
  37. static struct starpu_codelet codelet_add =
  38. {
  39. .cpu_funcs = {func_add},
  40. .nbuffers = 3,
  41. .modes = {STARPU_W, STARPU_R, STARPU_R},
  42. .model = &dumb_model,
  43. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  44. };
  45. int main(int argc, char **argv)
  46. {
  47. int rank, size, n;
  48. int ret;
  49. int a;
  50. int val0 = 0, val1 = 0;
  51. starpu_data_handle_t data0, data1, tmp0, tmp, tmp2;
  52. ret = starpu_init(NULL);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  54. ret = starpu_mpi_init(&argc, &argv, 1);
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  56. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  57. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  58. if (size < 2)
  59. {
  60. if (rank == 0)
  61. FPRINTF(stderr, "We need at least 2 processes.\n");
  62. starpu_mpi_shutdown();
  63. starpu_shutdown();
  64. return STARPU_TEST_SKIPPED;
  65. }
  66. if (starpu_mpi_cache_is_enabled() == 0)
  67. goto skip;
  68. if (rank == 0)
  69. {
  70. val0 = 1;
  71. starpu_variable_data_register(&data0, STARPU_MAIN_RAM, (uintptr_t)&val0, sizeof(val0));
  72. starpu_variable_data_register(&data1, -1, (uintptr_t)NULL, sizeof(val0));
  73. starpu_variable_data_register(&tmp0, -1, (uintptr_t)NULL, sizeof(val0));
  74. starpu_mpi_data_register(tmp0, -1, 0);
  75. }
  76. else if (rank == 1)
  77. {
  78. starpu_variable_data_register(&data0, -1, (uintptr_t)NULL, sizeof(val0));
  79. starpu_variable_data_register(&data1, STARPU_MAIN_RAM, (uintptr_t)&val1, sizeof(val1));
  80. tmp0 = NULL;
  81. }
  82. else
  83. {
  84. starpu_variable_data_register(&data0, -1, (uintptr_t)NULL, sizeof(val0));
  85. starpu_variable_data_register(&data1, -1, (uintptr_t)NULL, sizeof(val0));
  86. tmp0 = NULL;
  87. }
  88. starpu_variable_data_register(&tmp, -1, (uintptr_t)NULL, sizeof(val0));
  89. starpu_variable_data_register(&tmp2, -1, (uintptr_t)NULL, sizeof(val0));
  90. starpu_mpi_data_register(data0, 42, 0);
  91. starpu_mpi_data_register(data1, 43, 1);
  92. starpu_mpi_data_register(tmp, 44, 0);
  93. starpu_mpi_data_register(tmp2, -1, STARPU_MPI_PER_NODE);
  94. /* Test temporary data0 on node 0 only */
  95. starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet_add, STARPU_W, tmp0, STARPU_R, data0, STARPU_R, data0, 0);
  96. starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet_add, STARPU_W, data0, STARPU_R, tmp0, STARPU_R, tmp0, 0);
  97. starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet_add, STARPU_W, tmp, STARPU_R, data0, STARPU_R, data0, 0);
  98. /* Now make some tmp per-node, so that each node replicates the computation */
  99. for (n = 0; n < size; n++)
  100. if (n != 0)
  101. /* Get the value on all nodes */
  102. starpu_mpi_get_data_on_node_detached(MPI_COMM_WORLD, tmp, n, NULL, NULL);
  103. starpu_mpi_data_set_rank(tmp, STARPU_MPI_PER_NODE);
  104. /* This task writes to a per-node data, so will be executed by all nodes */
  105. starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet_add, STARPU_W, tmp2, STARPU_R, tmp, STARPU_R, tmp, 0);
  106. /* All MPI nodes have computed the value (no MPI communication here!) */
  107. starpu_data_acquire_on_node(tmp2, STARPU_MAIN_RAM, STARPU_R);
  108. STARPU_ASSERT(*(int*)starpu_data_handle_to_pointer(tmp2, STARPU_MAIN_RAM) == 16);
  109. starpu_data_release_on_node(tmp2, STARPU_MAIN_RAM);
  110. /* And nodes 0 and 1 do something with it */
  111. starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet_add, STARPU_W, data0, STARPU_R, tmp, STARPU_R, tmp2, 0);
  112. starpu_mpi_task_insert(MPI_COMM_WORLD, &codelet_add, STARPU_W, data1, STARPU_R, tmp, STARPU_R, tmp2, 0);
  113. starpu_task_wait_for_all();
  114. if (rank == 0)
  115. {
  116. starpu_data_unregister(tmp0);
  117. }
  118. starpu_data_unregister(data0);
  119. starpu_data_unregister(data1);
  120. starpu_data_unregister(tmp);
  121. starpu_data_unregister(tmp2);
  122. skip:
  123. starpu_mpi_shutdown();
  124. starpu_shutdown();
  125. if (rank == 0)
  126. STARPU_ASSERT_MSG(val0 == 24, "%d should be %d\n", val0, 16 * size);
  127. if (rank == 1)
  128. STARPU_ASSERT_MSG(val1 == 24, "%d should be %d\n", val0, 16 * size);
  129. return 0;
  130. }