policy_register.c 3.6 KB

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