starpu_mpi_select_node.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014, 2015, 2016, 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 <stdarg.h>
  17. #include <mpi.h>
  18. #include <starpu.h>
  19. #include <starpu_mpi.h>
  20. #include <starpu_data.h>
  21. #include <starpu_mpi_private.h>
  22. #include <starpu_mpi_select_node.h>
  23. #include <datawizard/coherency.h>
  24. static int _current_policy = STARPU_MPI_NODE_SELECTION_MOST_R_DATA;
  25. static int _last_predefined_policy = STARPU_MPI_NODE_SELECTION_MOST_R_DATA;
  26. static starpu_mpi_select_node_policy_func_t _policies[_STARPU_MPI_NODE_SELECTION_MAX_POLICY];
  27. int _starpu_mpi_select_node_with_most_R_data(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data);
  28. void _starpu_mpi_select_node_init()
  29. {
  30. int i;
  31. _policies[STARPU_MPI_NODE_SELECTION_MOST_R_DATA] = _starpu_mpi_select_node_with_most_R_data;
  32. for(i=_last_predefined_policy+1 ; i<_STARPU_MPI_NODE_SELECTION_MAX_POLICY ; i++)
  33. _policies[i] = NULL;
  34. }
  35. int starpu_mpi_node_selection_get_current_policy()
  36. {
  37. return _current_policy;
  38. }
  39. int starpu_mpi_node_selection_set_current_policy(int policy)
  40. {
  41. STARPU_ASSERT_MSG(_policies[policy] != NULL, "Policy %d invalid.\n", policy);
  42. _current_policy = policy;
  43. return 0;
  44. }
  45. int starpu_mpi_node_selection_register_policy(starpu_mpi_select_node_policy_func_t policy_func)
  46. {
  47. int i=_last_predefined_policy+1;
  48. // Look for a unregistered policy
  49. while(i<_STARPU_MPI_NODE_SELECTION_MAX_POLICY)
  50. {
  51. if (_policies[i] == NULL)
  52. break;
  53. i++;
  54. }
  55. STARPU_ASSERT_MSG(i<_STARPU_MPI_NODE_SELECTION_MAX_POLICY, "No unused policy available. Unregister existing policies before registering a new one.");
  56. _policies[i] = policy_func;
  57. return i;
  58. }
  59. int starpu_mpi_node_selection_unregister_policy(int policy)
  60. {
  61. STARPU_ASSERT_MSG(policy > _last_predefined_policy, "Policy %d invalid. Only user-registered policies can be unregistered\n", policy);
  62. _policies[policy] = NULL;
  63. return 0;
  64. }
  65. int _starpu_mpi_select_node_with_most_R_data(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data)
  66. {
  67. size_t *size_on_nodes;
  68. size_t max_size;
  69. int i;
  70. int xrank = 0;
  71. (void)me;
  72. _STARPU_MPI_CALLOC(size_on_nodes, nb_nodes, sizeof(size_t));
  73. for(i= 0 ; i<nb_data ; i++)
  74. {
  75. starpu_data_handle_t data = descr[i].handle;
  76. enum starpu_data_access_mode mode = descr[i].mode;
  77. if (mode & STARPU_R)
  78. {
  79. int rank = starpu_data_get_rank(data);
  80. size_on_nodes[rank] += data->ops->get_size(data);
  81. }
  82. }
  83. max_size = 0;
  84. for(i=0 ; i<nb_nodes ; i++)
  85. {
  86. if (size_on_nodes[i] > max_size)
  87. {
  88. max_size = size_on_nodes[i];
  89. xrank = i;
  90. }
  91. }
  92. free(size_on_nodes);
  93. return xrank;
  94. }
  95. int _starpu_mpi_select_node(int me, int nb_nodes, struct starpu_data_descr *descr, int nb_data, int policy)
  96. {
  97. int ppolicy = policy == STARPU_MPI_NODE_SELECTION_CURRENT_POLICY ? _current_policy : policy;
  98. STARPU_ASSERT_MSG(ppolicy < _STARPU_MPI_NODE_SELECTION_MAX_POLICY, "Invalid policy %d\n", ppolicy);
  99. STARPU_ASSERT_MSG(_policies[ppolicy], "Unregistered policy %d\n", ppolicy);
  100. starpu_mpi_select_node_policy_func_t func = _policies[ppolicy];
  101. return func(me, nb_nodes, descr, nb_data);
  102. }