sort_data_handles.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 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_t *data, unsigned path[])
  26. {
  27. unsigned depth = data->depth;
  28. struct starpu_data_state_t *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(struct starpu_data_state_t *dataA,
  58. struct starpu_data_state_t *dataB)
  59. {
  60. /* Perhaps we have the same piece of data */
  61. if (dataA == dataB)
  62. return 0;
  63. /* In case we have data/subdata from different trees */
  64. if (dataA->root_handle != dataB->root_handle)
  65. return ((dataA->root_handle < dataB->root_handle)?-1:1);
  66. /* Things get more complicated: we need to find the location of dataA
  67. * and dataB within the tree. */
  68. unsigned dataA_path[dataA->depth - 1];
  69. unsigned dataB_path[dataB->depth - 1];
  70. find_data_path(dataA, dataA_path);
  71. find_data_path(dataB, dataB_path);
  72. return _compar_data_paths(dataA_path, dataA->depth, dataB_path, dataB->depth);
  73. }
  74. static int _starpu_compar_buffer_descr(const void *_descrA, const void *_descrB)
  75. {
  76. const starpu_buffer_descr *descrA = _descrA;
  77. const starpu_buffer_descr *descrB = _descrB;
  78. return _starpu_compar_handles(descrA->handle, descrB->handle);
  79. }
  80. /* The descr array will be overwritten, so this must be a copy ! */
  81. void _starpu_sort_task_handles(starpu_buffer_descr descr[], unsigned nbuffers)
  82. {
  83. qsort(descr, nbuffers, sizeof(starpu_buffer_descr), _starpu_compar_buffer_descr);
  84. }