tree.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <stdlib.h>
  17. #include "starpu_tree.h"
  18. #include "workers.h"
  19. void starpu_tree_reset_visited(struct starpu_tree *tree, char *visited)
  20. {
  21. if(tree->arity == 0)
  22. {
  23. int *workerids;
  24. int nworkers = starpu_bindid_get_workerids(tree->id, &workerids);
  25. int w;
  26. for(w = 0; w < nworkers; w++)
  27. {
  28. visited[workerids[w]] = 0;
  29. }
  30. }
  31. int i;
  32. for(i = 0; i < tree->arity; i++)
  33. starpu_tree_reset_visited(&tree->nodes[i], visited);
  34. }
  35. void starpu_tree_prepare_children(unsigned arity, struct starpu_tree *father)
  36. {
  37. _STARPU_MALLOC(father->nodes, arity*sizeof(struct starpu_tree));
  38. father->arity = arity;
  39. }
  40. void starpu_tree_insert(struct starpu_tree *tree, int id, int level, int is_pu, int arity, struct starpu_tree *father)
  41. {
  42. tree->level = level;
  43. tree->arity = arity;
  44. tree->nodes = NULL;
  45. tree->id = is_pu ? id : level;
  46. tree->is_pu = is_pu;
  47. tree->father = father;
  48. }
  49. struct starpu_tree* starpu_tree_get(struct starpu_tree *tree, int id)
  50. {
  51. if(tree->arity == 0)
  52. {
  53. if(tree->is_pu && tree->id == id)
  54. return tree;
  55. else
  56. return NULL;
  57. }
  58. int i;
  59. for(i = 0; i < tree->arity; i++)
  60. {
  61. struct starpu_tree *found_tree = starpu_tree_get(&tree->nodes[i], id);
  62. if(found_tree)
  63. return found_tree;
  64. }
  65. return NULL;
  66. }
  67. static struct starpu_tree* _get_down_to_leaves(struct starpu_tree *node, char *visited, char *present)
  68. {
  69. struct starpu_tree *found_tree = NULL;
  70. int i;
  71. for(i = 0; i < node->arity; i++)
  72. {
  73. if(node->nodes[i].arity == 0)
  74. {
  75. if(node->nodes[i].is_pu)
  76. {
  77. int *workerids;
  78. int nworkers = starpu_bindid_get_workerids(node->nodes[i].id, &workerids);
  79. int w;
  80. for(w = 0; w < nworkers; w++)
  81. {
  82. if(!visited[workerids[w]] && present[workerids[w]])
  83. return &node->nodes[i];
  84. }
  85. }
  86. }
  87. else
  88. {
  89. found_tree =_get_down_to_leaves(&node->nodes[i], visited, present);
  90. if(found_tree)
  91. return found_tree;
  92. }
  93. }
  94. return NULL;
  95. }
  96. struct starpu_tree* starpu_tree_get_neighbour(struct starpu_tree *tree, struct starpu_tree *node, char *visited, char *present)
  97. {
  98. struct starpu_tree *father = node == NULL ? tree : node->father;
  99. int st, n;
  100. if (father == NULL) return NULL;
  101. if (father == tree && father->arity == 0)
  102. return tree;
  103. for(st = 0; st < father->arity; st++)
  104. {
  105. if(&father->nodes[st] == node)
  106. break;
  107. }
  108. for(n = 0; n < father->arity; n++)
  109. {
  110. int i = (st+n)%father->arity;
  111. if(&father->nodes[i] != node)
  112. {
  113. if(father->nodes[i].arity == 0)
  114. {
  115. if(father->nodes[i].is_pu)
  116. {
  117. int *workerids;
  118. int nworkers = starpu_bindid_get_workerids(father->nodes[i].id, &workerids);
  119. int w;
  120. for(w = 0; w < nworkers; w++)
  121. {
  122. if(!visited[workerids[w]] && present[workerids[w]])
  123. return &father->nodes[i];
  124. }
  125. }
  126. }
  127. else
  128. {
  129. struct starpu_tree *leaf = _get_down_to_leaves(&father->nodes[i], visited, present);
  130. if(leaf)
  131. return leaf;
  132. }
  133. }
  134. }
  135. if(tree == father)
  136. return NULL;
  137. return starpu_tree_get_neighbour(tree, father, visited, present);
  138. }
  139. void starpu_tree_free(struct starpu_tree *tree)
  140. {
  141. int i;
  142. for(i = 0; i < tree->arity; i++)
  143. starpu_tree_free(&tree->nodes[i]);
  144. free(tree->nodes);
  145. tree->nodes = NULL;
  146. tree->arity = 0;
  147. }