policy_register.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /* Dummy cost function for simgrid */
  24. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  25. {
  26. return 0.000001;
  27. }
  28. static struct starpu_perfmodel dumb_model =
  29. {
  30. .type = STARPU_COMMON,
  31. .cost_function = cost_function
  32. };
  33. struct starpu_codelet mycodelet =
  34. {
  35. .cpu_funcs = {func_cpu},
  36. .nbuffers = 2,
  37. .modes = {STARPU_W, STARPU_W},
  38. .model = &dumb_model
  39. };
  40. int starpu_mpi_select_node_my_policy_0(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  41. {
  42. (void) me;
  43. (void) nb_nodes;
  44. (void) nb_data;
  45. starpu_data_handle_t data = descr[0].handle;
  46. return starpu_data_get_rank(data);
  47. }
  48. int starpu_mpi_select_node_my_policy_1(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  49. {
  50. (void) me;
  51. (void) nb_nodes;
  52. (void) nb_data;
  53. starpu_data_handle_t data = descr[1].handle;
  54. return starpu_data_get_rank(data);
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. int ret;
  59. int rank, size;
  60. int policy;
  61. struct starpu_task *task;
  62. starpu_data_handle_t handles[2];
  63. MPI_Init(&argc, &argv);
  64. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  65. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  66. if (size < 2)
  67. {
  68. if (rank == 0)
  69. FPRINTF(stderr, "We need at least 2 processes.\n");
  70. MPI_Finalize();
  71. return STARPU_TEST_SKIPPED;
  72. }
  73. ret = starpu_init(NULL);
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  75. ret = starpu_mpi_init(NULL, NULL, 0);
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  78. if (rank == 0)
  79. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  80. else
  81. starpu_variable_data_register(&handles[0], -1, (uintptr_t)NULL, sizeof(int));
  82. starpu_mpi_data_register(handles[0], 10, 0);
  83. if (rank == 1)
  84. starpu_variable_data_register(&handles[1], STARPU_MAIN_RAM, (uintptr_t)&policy, sizeof(int));
  85. else
  86. starpu_variable_data_register(&handles[1], -1, (uintptr_t)NULL, sizeof(int));
  87. starpu_mpi_data_register(handles[1], 20, 1);
  88. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_1);
  89. starpu_mpi_node_selection_set_current_policy(policy);
  90. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  91. STARPU_W, handles[0], STARPU_W, handles[1],
  92. 0);
  93. FPRINTF_MPI(stderr, "Task %p\n", task);
  94. if (rank == 1)
  95. {
  96. STARPU_ASSERT_MSG(task, "Task should be executed by rank 1\n");
  97. task->destroy = 0;
  98. starpu_task_destroy(task);
  99. }
  100. else
  101. {
  102. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 1\n");
  103. }
  104. policy = starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_my_policy_0);
  105. task = starpu_mpi_task_build(MPI_COMM_WORLD, &mycodelet,
  106. STARPU_W, handles[0], STARPU_W, handles[1],
  107. STARPU_NODE_SELECTION_POLICY, policy,
  108. 0);
  109. FPRINTF_MPI(stderr, "Task %p\n", task);
  110. if (rank == 0)
  111. {
  112. STARPU_ASSERT_MSG(task, "Task should be executed by rank 0\n");
  113. task->destroy = 0;
  114. starpu_task_destroy(task);
  115. }
  116. else
  117. {
  118. STARPU_ASSERT_MSG(task == NULL, "Task should be executed by rank 0\n");
  119. }
  120. starpu_data_unregister(handles[0]);
  121. starpu_data_unregister(handles[1]);
  122. starpu_mpi_shutdown();
  123. starpu_shutdown();
  124. MPI_Finalize();
  125. return 0;
  126. }