temporary.c 4.7 KB

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