policy_register.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015,2017 CNRS
  4. * Copyright (C) 2015,2017,2018 Université de Bordeaux
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu_mpi.h>
  18. #include "helper.h"
  19. void func_cpu(void *descr[], void *_args)
  20. {
  21. (void)descr;
  22. (void)_args;
  23. }
  24. struct starpu_codelet mycodelet =
  25. {
  26. .cpu_funcs = {func_cpu},
  27. .nbuffers = 2,
  28. .modes = {STARPU_W, STARPU_W},
  29. .model = &starpu_perfmodel_nop,
  30. };
  31. int starpu_mpi_select_node_my_policy_0(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  32. {
  33. (void) me;
  34. (void) nb_nodes;
  35. (void) nb_data;
  36. starpu_data_handle_t data = descr[0].handle;
  37. return starpu_data_get_rank(data);
  38. }
  39. int starpu_mpi_select_node_my_policy_1(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  40. {
  41. (void) me;
  42. (void) nb_nodes;
  43. (void) nb_data;
  44. starpu_data_handle_t data = descr[1].handle;
  45. return starpu_data_get_rank(data);
  46. }
  47. int main(int argc, char **argv)
  48. {
  49. int ret;
  50. int rank, size;
  51. int policy;
  52. struct starpu_task *task;
  53. starpu_data_handle_t handles[2];
  54. int mpi_init;
  55. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  56. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  58. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  59. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  60. if (size < 2)
  61. {
  62. if (rank == 0)
  63. FPRINTF(stderr, "We need at least 2 processes.\n");
  64. starpu_mpi_shutdown();
  65. if (!mpi_init)
  66. MPI_Finalize();
  67. return STARPU_TEST_SKIPPED;
  68. }
  69. if (rank == 0)
  70. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  71. else
  72. starpu_variable_data_register(&handles[0], -1, (uintptr_t)NULL, sizeof(int));
  73. starpu_mpi_data_register(handles[0], 10, 0);
  74. if (rank == 1)
  75. starpu_variable_data_register(&handles[1], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  76. else
  77. starpu_variable_data_register(&handles[1], -1, (uintptr_t)NULL, sizeof(int));
  78. starpu_mpi_data_register(handles[1], 20, 1);
  79. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_1);
  80. starpu_mpi_node_selection_set_current_policy(policy);
  81. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  82. STARPU_W, handles[0], STARPU_W, handles[1],
  83. 0);
  84. FPRINTF_MPI(stderr, "Task %p\n", task);
  85. if (rank == 1)
  86. {
  87. STARPU_ASSERT_MSG(task, "Task should be executed by rank 1\n");
  88. task->destroy = 0;
  89. starpu_task_destroy(task);
  90. }
  91. else
  92. {
  93. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 1\n");
  94. }
  95. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_0);
  96. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  97. STARPU_W, handles[0], STARPU_W, handles[1],
  98. STARPU_NODE_SELECTION_POLICY, policy,
  99. 0);
  100. FPRINTF_MPI(stderr, "Task %p\n", task);
  101. if (rank == 0)
  102. {
  103. STARPU_ASSERT_MSG(task, "Task should be executed by rank 0\n");
  104. task->destroy = 0;
  105. starpu_task_destroy(task);
  106. }
  107. else
  108. {
  109. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 0\n");
  110. }
  111. starpu_data_unregister(handles[0]);
  112. starpu_data_unregister(handles[1]);
  113. starpu_mpi_shutdown();
  114. if (!mpi_init)
  115. MPI_Finalize();
  116. return 0;
  117. }