write_back.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012, 2015-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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 <datawizard/datawizard.h>
  18. #include <datawizard/write_back.h>
  19. #include <core/dependencies/data_concurrency.h>
  20. static void wt_callback(void *arg)
  21. {
  22. starpu_data_handle_t handle = (starpu_data_handle_t) arg;
  23. _starpu_spin_lock(&handle->header_lock);
  24. if (!_starpu_notify_data_dependencies(handle))
  25. _starpu_spin_unlock(&handle->header_lock);
  26. }
  27. void _starpu_write_through_data(starpu_data_handle_t handle, unsigned requesting_node,
  28. uint32_t write_through_mask)
  29. {
  30. if ((write_through_mask & ~(1<<requesting_node)) == 0)
  31. {
  32. /* nothing will be done ... */
  33. return;
  34. }
  35. /* first commit all changes onto the nodes specified by the mask */
  36. unsigned node, max;
  37. for (node = 0, max = starpu_memory_nodes_get_count(); node < max; node++)
  38. {
  39. if (write_through_mask & (1<<node))
  40. {
  41. /* we need to commit the buffer on that node */
  42. if (node != requesting_node)
  43. {
  44. int cpt = 0;
  45. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  46. {
  47. cpt++;
  48. __starpu_datawizard_progress(1, 1);
  49. }
  50. if (cpt == STARPU_SPIN_MAXTRY)
  51. _starpu_spin_lock(&handle->header_lock);
  52. /* We need to keep a Read lock to avoid letting writers corrupt our copy. */
  53. STARPU_ASSERT(handle->current_mode != STARPU_REDUX);
  54. STARPU_ASSERT(handle->current_mode != STARPU_SCRATCH);
  55. handle->refcnt++;
  56. handle->busy_count++;
  57. handle->current_mode = STARPU_R;
  58. struct _starpu_data_request *r;
  59. r = _starpu_create_request_to_fetch_data(handle, &handle->per_node[node],
  60. STARPU_R, 1, 1, wt_callback, handle, 0, "_starpu_write_through_data");
  61. /* If no request was created, the handle was already up-to-date on the
  62. * node */
  63. if (r)
  64. _starpu_spin_unlock(&handle->header_lock);
  65. }
  66. }
  67. }
  68. }
  69. void starpu_data_set_wt_mask(starpu_data_handle_t handle, uint32_t wt_mask)
  70. {
  71. handle->wt_mask = wt_mask;
  72. /* in case the data has some children, set their wt_mask as well */
  73. if (handle->nchildren > 0)
  74. {
  75. unsigned child;
  76. for (child = 0; child < handle->nchildren; child++)
  77. {
  78. starpu_data_handle_t handle_child = starpu_data_get_child(handle, child);
  79. starpu_data_set_wt_mask(handle_child, wt_mask);
  80. }
  81. }
  82. }