temporary.c 4.7 KB

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