tree.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, int *visited)
  20. {
  21. if(tree->arity == 0)
  22. {
  23. int workerids[STARPU_NMAXWORKERS];
  24. int nworkers = _starpu_worker_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_insert(struct starpu_tree *tree, int id, int level, int is_pu, int arity, struct starpu_tree *father)
  36. {
  37. tree->level = level;
  38. tree->arity = arity;
  39. tree->nodes = (struct starpu_tree**)malloc(arity*sizeof(struct starpu_tree*));
  40. int i;
  41. for(i = 0; i < arity; i++)
  42. tree->nodes[i] = (struct starpu_tree*)malloc(sizeof(struct starpu_tree));
  43. tree->id = is_pu ? id : level;
  44. tree->is_pu = is_pu;
  45. tree->father = father;
  46. }
  47. struct starpu_tree* starpu_tree_get(struct starpu_tree *tree, int id)
  48. {
  49. if(tree->arity == 0)
  50. {
  51. if(tree->is_pu && tree->id == id)
  52. return tree;
  53. else
  54. return NULL;
  55. }
  56. struct starpu_tree *found_tree = NULL;
  57. int i;
  58. for(i = 0; i < tree->arity; i++)
  59. {
  60. found_tree = starpu_tree_get(tree->nodes[i], id);
  61. if(found_tree)
  62. return found_tree;
  63. }
  64. return NULL;
  65. }
  66. struct starpu_tree* _get_down_to_leaves(struct starpu_tree *node, int *visited, int *present)
  67. {
  68. struct starpu_tree *found_tree = NULL;
  69. int i;
  70. for(i = 0; i < node->arity; i++)
  71. {
  72. if(node->nodes[i]->arity == 0)
  73. {
  74. if(node->nodes[i]->is_pu)
  75. {
  76. int workerids[STARPU_NMAXWORKERS];
  77. int nworkers = _starpu_worker_get_workerids(node->nodes[i]->id, workerids);
  78. int w;
  79. for(w = 0; w < nworkers; w++)
  80. {
  81. if(!visited[workerids[w]] && present[workerids[w]])
  82. return node->nodes[i];
  83. }
  84. }
  85. }
  86. else
  87. {
  88. found_tree =_get_down_to_leaves(node->nodes[i], visited, present);
  89. if(found_tree)
  90. return found_tree;
  91. }
  92. }
  93. return NULL;
  94. }
  95. struct starpu_tree* starpu_tree_get_neighbour(struct starpu_tree *tree, struct starpu_tree *node, int *visited, int *present)
  96. {
  97. struct starpu_tree *father = node == NULL ? tree : node->father;
  98. int i, st, n;
  99. for(st = 0; st < father->arity; st++)
  100. {
  101. if(father->nodes[st] == node)
  102. break;
  103. }
  104. for(n = 0; n < father->arity; n++)
  105. {
  106. i = (st+n)%father->arity;
  107. if(father->nodes[i] != node)
  108. {
  109. if(father->nodes[i]->arity == 0)
  110. {
  111. if(father->nodes[i]->is_pu)
  112. {
  113. int workerids[STARPU_NMAXWORKERS];
  114. int nworkers = _starpu_worker_get_workerids(father->nodes[i]->id, workerids);
  115. int w;
  116. for(w = 0; w < nworkers; w++)
  117. {
  118. if(!visited[workerids[w]] && present[workerids[w]])
  119. return father->nodes[i];
  120. }
  121. }
  122. }
  123. else
  124. {
  125. struct starpu_tree *leaf = _get_down_to_leaves(father->nodes[i], visited, present);
  126. if(leaf)
  127. return leaf;
  128. }
  129. }
  130. }
  131. if(tree == father)
  132. return NULL;
  133. return starpu_tree_get_neighbour(tree, father, visited, present);
  134. }
  135. void starpu_tree_free(struct starpu_tree *tree)
  136. {
  137. int i;
  138. for(i = 0; i < tree->arity; i++)
  139. {
  140. starpu_tree_free(tree->nodes[i]);
  141. free(tree->nodes[i]);
  142. tree->nodes[i] = NULL;
  143. }
  144. free(tree->nodes);
  145. tree->nodes = NULL;
  146. tree->arity = 0;
  147. }