starpu_mpi_select_node.c 3.6 KB

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