policy_register.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015, 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. #include <starpu_mpi.h>
  17. #include "helper.h"
  18. void func_cpu(void *descr[], void *_args)
  19. {
  20. (void)descr;
  21. (void)_args;
  22. }
  23. struct starpu_codelet mycodelet =
  24. {
  25. .cpu_funcs = {func_cpu},
  26. .nbuffers = 2,
  27. .modes = {STARPU_W, STARPU_W},
  28. .model = &starpu_nop_perf_model,
  29. };
  30. int starpu_mpi_select_node_my_policy_0(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  31. {
  32. (void) me;
  33. (void) nb_nodes;
  34. (void) nb_data;
  35. starpu_data_handle_t data = descr[0].handle;
  36. return starpu_data_get_rank(data);
  37. }
  38. int starpu_mpi_select_node_my_policy_1(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  39. {
  40. (void) me;
  41. (void) nb_nodes;
  42. (void) nb_data;
  43. starpu_data_handle_t data = descr[1].handle;
  44. return starpu_data_get_rank(data);
  45. }
  46. int main(int argc, char **argv)
  47. {
  48. int ret;
  49. int rank, size;
  50. int policy;
  51. struct starpu_task *task;
  52. starpu_data_handle_t handles[2];
  53. int mpi_init;
  54. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  55. ret = starpu_init(NULL);
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. ret = starpu_mpi_init(&argc, &argv, mpi_init);
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  59. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  60. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  61. if (size < 2)
  62. {
  63. if (rank == 0)
  64. FPRINTF(stderr, "We need at least 2 processes.\n");
  65. starpu_mpi_shutdown();
  66. starpu_shutdown();
  67. if (!mpi_init)
  68. MPI_Finalize();
  69. return STARPU_TEST_SKIPPED;
  70. }
  71. if (rank == 0)
  72. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  73. else
  74. starpu_variable_data_register(&handles[0], -1, (uintptr_t)NULL, sizeof(int));
  75. starpu_mpi_data_register(handles[0], 10, 0);
  76. if (rank == 1)
  77. starpu_variable_data_register(&handles[1], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  78. else
  79. starpu_variable_data_register(&handles[1], -1, (uintptr_t)NULL, sizeof(int));
  80. starpu_mpi_data_register(handles[1], 20, 1);
  81. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_1);
  82. starpu_mpi_node_selection_set_current_policy(policy);
  83. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  84. STARPU_W, handles[0], STARPU_W, handles[1],
  85. 0);
  86. FPRINTF_MPI(stderr, "Task %p\n", task);
  87. if (rank == 1)
  88. {
  89. STARPU_ASSERT_MSG(task, "Task should be executed by rank 1\n");
  90. task->destroy = 0;
  91. starpu_task_destroy(task);
  92. }
  93. else
  94. {
  95. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 1\n");
  96. }
  97. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_0);
  98. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  99. STARPU_W, handles[0], STARPU_W, handles[1],
  100. STARPU_NODE_SELECTION_POLICY, policy,
  101. 0);
  102. FPRINTF_MPI(stderr, "Task %p\n", task);
  103. if (rank == 0)
  104. {
  105. STARPU_ASSERT_MSG(task, "Task should be executed by rank 0\n");
  106. task->destroy = 0;
  107. starpu_task_destroy(task);
  108. }
  109. else
  110. {
  111. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 0\n");
  112. }
  113. starpu_data_unregister(handles[0]);
  114. starpu_data_unregister(handles[1]);
  115. starpu_mpi_shutdown();
  116. starpu_shutdown();
  117. if (!mpi_init)
  118. MPI_Finalize();
  119. return 0;
  120. }