tree.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 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. father->nodes = (struct starpu_tree*)malloc(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. struct starpu_tree *found_tree = NULL;
  59. int i;
  60. for(i = 0; i < tree->arity; i++)
  61. {
  62. found_tree = starpu_tree_get(&tree->nodes[i], id);
  63. if(found_tree)
  64. return found_tree;
  65. }
  66. return NULL;
  67. }
  68. static struct starpu_tree* _get_down_to_leaves(struct starpu_tree *node, char *visited, char *present)
  69. {
  70. struct starpu_tree *found_tree = NULL;
  71. int i;
  72. for(i = 0; i < node->arity; i++)
  73. {
  74. if(node->nodes[i].arity == 0)
  75. {
  76. if(node->nodes[i].is_pu)
  77. {
  78. int *workerids;
  79. int nworkers = starpu_bindid_get_workerids(node->nodes[i].id, &workerids);
  80. int w;
  81. for(w = 0; w < nworkers; w++)
  82. {
  83. if(!visited[workerids[w]] && present[workerids[w]])
  84. return &node->nodes[i];
  85. }
  86. }
  87. }
  88. else
  89. {
  90. found_tree =_get_down_to_leaves(&node->nodes[i], visited, present);
  91. if(found_tree)
  92. return found_tree;
  93. }
  94. }
  95. return NULL;
  96. }
  97. struct starpu_tree* starpu_tree_get_neighbour(struct starpu_tree *tree, struct starpu_tree *node, char *visited, char *present)
  98. {
  99. struct starpu_tree *father = node == NULL ? tree : node->father;
  100. int i, st, n;
  101. for(st = 0; st < father->arity; st++)
  102. {
  103. if(&father->nodes[st] == node)
  104. break;
  105. }
  106. for(n = 0; n < father->arity; n++)
  107. {
  108. i = (st+n)%father->arity;
  109. if(&father->nodes[i] != node)
  110. {
  111. if(father->nodes[i].arity == 0)
  112. {
  113. if(father->nodes[i].is_pu)
  114. {
  115. int *workerids;
  116. int nworkers = starpu_bindid_get_workerids(father->nodes[i].id, &workerids);
  117. int w;
  118. for(w = 0; w < nworkers; w++)
  119. {
  120. if(!visited[workerids[w]] && present[workerids[w]])
  121. return &father->nodes[i];
  122. }
  123. }
  124. }
  125. else
  126. {
  127. struct starpu_tree *leaf = _get_down_to_leaves(&father->nodes[i], visited, present);
  128. if(leaf)
  129. return leaf;
  130. }
  131. }
  132. }
  133. if(tree == father)
  134. return NULL;
  135. return starpu_tree_get_neighbour(tree, father, visited, present);
  136. }
  137. void starpu_tree_free(struct starpu_tree *tree)
  138. {
  139. int i;
  140. for(i = 0; i < tree->arity; i++)
  141. starpu_tree_free(&tree->nodes[i]);
  142. free(tree->nodes);
  143. tree->nodes = NULL;
  144. tree->arity = 0;
  145. }