sort_data_handles.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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 <starpu.h>
  17. #include <common/config.h>
  18. #include <datawizard/filters.h>
  19. #include <datawizard/sort_data_handles.h>
  20. /* To avoid deadlocks in case we have multiple tasks accessing the same piece
  21. * of data (eg. task T1 needs A and B, and T2 needs B and A), we need to lock
  22. * them in order, so that we need a total order over data. We must also not
  23. * lock a child before its parent. */
  24. static void find_data_path(struct _starpu_data_state *data, unsigned path[])
  25. {
  26. unsigned depth = data->depth;
  27. struct _starpu_data_state *current = data;
  28. /* Compute the path from the root to the data */
  29. unsigned level; /* level is the distance between the node and the current node */
  30. for (level = 0; level < depth; level++)
  31. {
  32. path[depth - level - 1] = current->sibling_index;
  33. current = current->father_handle;
  34. }
  35. }
  36. static int _compar_data_paths(const unsigned pathA[], unsigned depthA,
  37. const unsigned pathB[], unsigned depthB)
  38. {
  39. unsigned level;
  40. unsigned depth = STARPU_MIN(depthA, depthB);
  41. for (level = 0; level < depth; level++)
  42. {
  43. if (pathA[level] != pathB[level])
  44. return (pathA[level] < pathB[level])?-1:1;
  45. }
  46. /* If this is the same path */
  47. if (depthA == depthB)
  48. return 0;
  49. /* A is a subdata of B or B is a subdata of A, so the smallest one is
  50. * the father of the other (we take this convention). */
  51. return (depthA < depthB)?-1:1;
  52. }
  53. /* A comparision function between two handles makes it possible to use qsort to
  54. * sort a list of handles */
  55. static int _starpu_compar_handles(const struct _starpu_data_descr *descrA,
  56. const struct _starpu_data_descr *descrB)
  57. {
  58. struct _starpu_data_state *dataA = descrA->handle;
  59. struct _starpu_data_state *dataB = descrB->handle;
  60. /* Perhaps we have the same piece of data */
  61. if (dataA == dataB)
  62. {
  63. /* Process write requests first, this is needed for proper
  64. * locking, see _submit_job_enforce_data_deps,
  65. * _starpu_fetch_task_input, and _starpu_push_task_output */
  66. if (descrA->mode & STARPU_W)
  67. {
  68. if (descrB->mode & STARPU_W)
  69. /* Both A and B write, take the reader first */
  70. if (descrA->mode & STARPU_R)
  71. return -1;
  72. else
  73. return 1;
  74. else
  75. /* Only A writes, take it first */
  76. return -1;
  77. }
  78. else
  79. /* A doesn't write, take B before */
  80. return 1;
  81. }
  82. /* Put arbitered accesses after non-arbitered */
  83. if (dataA->arbiter && !(dataB->arbiter))
  84. return 1;
  85. if (dataB->arbiter && !(dataA->arbiter))
  86. return -1;
  87. if (dataA->arbiter != dataB->arbiter)
  88. /* Both are arbitered, sort by arbiter pointer order */
  89. return (dataA->arbiter < dataB->arbiter)?-1:1;
  90. /* If both are arbitered by the same arbiter (or they are both not
  91. * arbitered), we'll sort them by handle */
  92. /* In case we have data/subdata from different trees */
  93. if (dataA->root_handle != dataB->root_handle)
  94. return (dataA->root_handle < dataB->root_handle)?-1:1;
  95. /* Things get more complicated: we need to find the location of dataA
  96. * and dataB within the tree. */
  97. unsigned dataA_path[dataA->depth];
  98. unsigned dataB_path[dataB->depth];
  99. find_data_path(dataA, dataA_path);
  100. find_data_path(dataB, dataB_path);
  101. return _compar_data_paths(dataA_path, dataA->depth, dataB_path, dataB->depth);
  102. }
  103. static int _starpu_compar_buffer_descr(const void *_descrA, const void *_descrB)
  104. {
  105. const struct _starpu_data_descr *descrA = (const struct _starpu_data_descr *) _descrA;
  106. const struct _starpu_data_descr *descrB = (const struct _starpu_data_descr *) _descrB;
  107. return _starpu_compar_handles(descrA, descrB);
  108. }
  109. /* The descr array will be overwritten, so this must be a copy ! */
  110. void _starpu_sort_task_handles(struct _starpu_data_descr descr[], unsigned nbuffers)
  111. {
  112. qsort(descr, nbuffers, sizeof(descr[0]), _starpu_compar_buffer_descr);
  113. }