policy_register.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015,2017 CNRS
  4. * Copyright (C) 2015,2017 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_init(NULL);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  58. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  60. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  61. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  62. if (size < 2)
  63. {
  64. if (rank == 0)
  65. FPRINTF(stderr, "We need at least 2 processes.\n");
  66. starpu_mpi_shutdown();
  67. starpu_shutdown();
  68. if (!mpi_init)
  69. MPI_Finalize();
  70. return STARPU_TEST_SKIPPED;
  71. }
  72. if (rank == 0)
  73. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  74. else
  75. starpu_variable_data_register(&handles[0], -1, (uintptr_t)NULL, sizeof(int));
  76. starpu_mpi_data_register(handles[0], 10, 0);
  77. if (rank == 1)
  78. starpu_variable_data_register(&handles[1], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  79. else
  80. starpu_variable_data_register(&handles[1], -1, (uintptr_t)NULL, sizeof(int));
  81. starpu_mpi_data_register(handles[1], 20, 1);
  82. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_1);
  83. starpu_mpi_node_selection_set_current_policy(policy);
  84. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  85. STARPU_W, handles[0], STARPU_W, handles[1],
  86. 0);
  87. FPRINTF_MPI(stderr, "Task %p\n", task);
  88. if (rank == 1)
  89. {
  90. STARPU_ASSERT_MSG(task, "Task should be executed by rank 1\n");
  91. task->destroy = 0;
  92. starpu_task_destroy(task);
  93. }
  94. else
  95. {
  96. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 1\n");
  97. }
  98. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_0);
  99. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  100. STARPU_W, handles[0], STARPU_W, handles[1],
  101. STARPU_NODE_SELECTION_POLICY, policy,
  102. 0);
  103. FPRINTF_MPI(stderr, "Task %p\n", task);
  104. if (rank == 0)
  105. {
  106. STARPU_ASSERT_MSG(task, "Task should be executed by rank 0\n");
  107. task->destroy = 0;
  108. starpu_task_destroy(task);
  109. }
  110. else
  111. {
  112. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 0\n");
  113. }
  114. starpu_data_unregister(handles[0]);
  115. starpu_data_unregister(handles[1]);
  116. starpu_mpi_shutdown();
  117. starpu_shutdown();
  118. if (!mpi_init)
  119. MPI_Finalize();
  120. return 0;
  121. }