tree.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2015 Inria
  4. * Copyright (C) 2014-2017 CNRS
  5. * Copyright (C) 2014,2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdlib.h>
  19. #include "starpu_tree.h"
  20. #include "workers.h"
  21. void starpu_tree_reset_visited(struct starpu_tree *tree, char *visited)
  22. {
  23. if(tree->arity == 0)
  24. {
  25. int *workerids;
  26. int nworkers = starpu_bindid_get_workerids(tree->id, &workerids);
  27. int w;
  28. for(w = 0; w < nworkers; w++)
  29. {
  30. visited[workerids[w]] = 0;
  31. }
  32. }
  33. int i;
  34. for(i = 0; i < tree->arity; i++)
  35. starpu_tree_reset_visited(&tree->nodes[i], visited);
  36. }
  37. void starpu_tree_prepare_children(unsigned arity, struct starpu_tree *father)
  38. {
  39. _STARPU_MALLOC(father->nodes, arity*sizeof(struct starpu_tree));
  40. father->arity = arity;
  41. }
  42. void starpu_tree_insert(struct starpu_tree *tree, int id, int level, int is_pu, int arity, struct starpu_tree *father)
  43. {
  44. tree->level = level;
  45. tree->arity = arity;
  46. tree->nodes = NULL;
  47. tree->id = is_pu ? id : level;
  48. tree->is_pu = is_pu;
  49. tree->father = father;
  50. }
  51. struct starpu_tree* starpu_tree_get(struct starpu_tree *tree, int id)
  52. {
  53. if(tree->arity == 0)
  54. {
  55. if(tree->is_pu && tree->id == id)
  56. return tree;
  57. else
  58. return NULL;
  59. }
  60. int i;
  61. for(i = 0; i < tree->arity; i++)
  62. {
  63. struct starpu_tree *found_tree = starpu_tree_get(&tree->nodes[i], id);
  64. if(found_tree)
  65. return found_tree;
  66. }
  67. return NULL;
  68. }
  69. static struct starpu_tree* _get_down_to_leaves(struct starpu_tree *node, char *visited, char *present)
  70. {
  71. struct starpu_tree *found_tree = NULL;
  72. int i;
  73. for(i = 0; i < node->arity; i++)
  74. {
  75. if(node->nodes[i].arity == 0)
  76. {
  77. if(node->nodes[i].is_pu)
  78. {
  79. int *workerids;
  80. int nworkers = starpu_bindid_get_workerids(node->nodes[i].id, &workerids);
  81. int w;
  82. for(w = 0; w < nworkers; w++)
  83. {
  84. if(!visited[workerids[w]] && present[workerids[w]])
  85. return &node->nodes[i];
  86. }
  87. }
  88. }
  89. else
  90. {
  91. found_tree =_get_down_to_leaves(&node->nodes[i], visited, present);
  92. if(found_tree)
  93. return found_tree;
  94. }
  95. }
  96. return NULL;
  97. }
  98. struct starpu_tree* starpu_tree_get_neighbour(struct starpu_tree *tree, struct starpu_tree *node, char *visited, char *present)
  99. {
  100. struct starpu_tree *father = node == NULL ? tree : node->father;
  101. int st, n;
  102. for(st = 0; st < father->arity; st++)
  103. {
  104. if(&father->nodes[st] == node)
  105. break;
  106. }
  107. for(n = 0; n < father->arity; n++)
  108. {
  109. int i = (st+n)%father->arity;
  110. if(&father->nodes[i] != node)
  111. {
  112. if(father->nodes[i].arity == 0)
  113. {
  114. if(father->nodes[i].is_pu)
  115. {
  116. int *workerids;
  117. int nworkers = starpu_bindid_get_workerids(father->nodes[i].id, &workerids);
  118. int w;
  119. for(w = 0; w < nworkers; w++)
  120. {
  121. if(!visited[workerids[w]] && present[workerids[w]])
  122. return &father->nodes[i];
  123. }
  124. }
  125. }
  126. else
  127. {
  128. struct starpu_tree *leaf = _get_down_to_leaves(&father->nodes[i], visited, present);
  129. if(leaf)
  130. return leaf;
  131. }
  132. }
  133. }
  134. if(tree == father)
  135. return NULL;
  136. return starpu_tree_get_neighbour(tree, father, visited, present);
  137. }
  138. void starpu_tree_free(struct starpu_tree *tree)
  139. {
  140. int i;
  141. for(i = 0; i < tree->arity; i++)
  142. starpu_tree_free(&tree->nodes[i]);
  143. free(tree->nodes);
  144. tree->nodes = NULL;
  145. tree->arity = 0;
  146. }