sort_data_handles.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <datawizard/filters.h>
  20. #include <datawizard/sort_data_handles.h>
  21. /* To avoid deadlocks in case we have multiple tasks accessing the same piece
  22. * of data (eg. task T1 needs A and B, and T2 needs B and A), we need to lock
  23. * them in order, so that we need a total order over data. We must also not
  24. * lock a child before its parent. */
  25. static void find_data_path(struct _starpu_data_state *data, unsigned path[])
  26. {
  27. unsigned depth = data->depth;
  28. struct _starpu_data_state *current = data;
  29. /* Compute the path from the root to the data */
  30. unsigned level; /* level is the distance between the node and the current node */
  31. for (level = 0; level < depth; level++)
  32. {
  33. STARPU_ASSERT(data);
  34. path[depth - level - 1] = current->sibling_index;
  35. current = data->father_handle;
  36. }
  37. }
  38. static int _compar_data_paths(const unsigned pathA[], unsigned depthA,
  39. const unsigned pathB[], unsigned depthB)
  40. {
  41. unsigned level;
  42. unsigned depth = STARPU_MIN(depthA, depthB);
  43. for (level = 0; level < depth; level++)
  44. {
  45. if (pathA[level] != pathB[level])
  46. return (pathA[level] < pathB[level])?-1:1;
  47. }
  48. /* If this is the same path */
  49. if (depthA == depthB)
  50. return 0;
  51. /* A is a subdata of B or B is a subdata of A, so the smallest one is
  52. * the father of the other (we take this convention). */
  53. return (depthA < depthB)?-1:1;
  54. }
  55. /* A comparision function between two handles makes it possible to use qsort to
  56. * sort a list of handles */
  57. static int _starpu_compar_handles(const struct starpu_data_descr *descrA,
  58. const struct starpu_data_descr *descrB)
  59. {
  60. struct _starpu_data_state *dataA = descrA->handle;
  61. struct _starpu_data_state *dataB = descrB->handle;
  62. /* Perhaps we have the same piece of data */
  63. if (dataA == dataB)
  64. {
  65. /* Process write requests first, this is needed for proper
  66. * locking, see _submit_job_enforce_data_deps,
  67. * _starpu_fetch_task_input, and _starpu_push_task_output */
  68. if (descrA->mode & STARPU_W)
  69. {
  70. if (descrB->mode & STARPU_W)
  71. /* Both A and B write, take the reader first */
  72. if (descrA->mode & STARPU_R)
  73. return -1;
  74. else
  75. return 1;
  76. else
  77. /* Only A writes, take it first */
  78. return -1;
  79. } else
  80. /* A doesn't write, take B before */
  81. return 1;
  82. }
  83. /* In case we have data/subdata from different trees */
  84. if (dataA->root_handle != dataB->root_handle)
  85. return ((dataA->root_handle < dataB->root_handle)?-1:1);
  86. /* Things get more complicated: we need to find the location of dataA
  87. * and dataB within the tree. */
  88. unsigned dataA_path[dataA->depth - 1];
  89. unsigned dataB_path[dataB->depth - 1];
  90. find_data_path(dataA, dataA_path);
  91. find_data_path(dataB, dataB_path);
  92. return _compar_data_paths(dataA_path, dataA->depth, dataB_path, dataB->depth);
  93. }
  94. static int _starpu_compar_buffer_descr(const void *_descrA, const void *_descrB)
  95. {
  96. const struct starpu_data_descr *descrA = (const struct starpu_data_descr *) _descrA;
  97. const struct starpu_data_descr *descrB = (const struct starpu_data_descr *) _descrB;
  98. return _starpu_compar_handles(descrA, descrB);
  99. }
  100. /* The descr array will be overwritten, so this must be a copy ! */
  101. void _starpu_sort_task_handles(struct starpu_data_descr descr[], unsigned nbuffers)
  102. {
  103. qsort(descr, nbuffers, sizeof(struct starpu_data_descr), _starpu_compar_buffer_descr);
  104. }