write_back.c 2.8 KB

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