temporary.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. };
  44. int main(int argc, char **argv)
  45. {
  46. int rank, size, n;
  47. int ret;
  48. int a;
  49. int val0, val1;
  50. starpu_data_handle_t data0, data1, tmp0, tmp, tmp2;
  51. ret = starpu_init(NULL);
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  53. ret = starpu_mpi_init(&argc, &argv, 1);
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  55. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  56. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  57. if (starpu_mpi_cache_is_enabled() == 0) 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. starpu_data_unregister(data0);
  105. skip:
  106. starpu_mpi_shutdown();
  107. starpu_shutdown();
  108. if (rank == 0)
  109. STARPU_ASSERT_MSG(val0 == 24, "%d should be %d\n", val0, 16 * size);
  110. if (rank == 1)
  111. STARPU_ASSERT_MSG(val1 == 24, "%d should be %d\n", val0, 16 * size);
  112. return 0;
  113. }