write_back.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012,2014-2016 Université de Bordeaux
  4. * Copyright (C) 2012,2013,2016,2017 Inria
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <datawizard/datawizard.h>
  19. #include <datawizard/write_back.h>
  20. #include <core/dependencies/data_concurrency.h>
  21. static void wt_callback(void *arg)
  22. {
  23. starpu_data_handle_t handle = (starpu_data_handle_t) arg;
  24. _starpu_spin_lock(&handle->header_lock);
  25. if (!_starpu_notify_data_dependencies(handle))
  26. _starpu_spin_unlock(&handle->header_lock);
  27. }
  28. void _starpu_write_through_data(starpu_data_handle_t handle, unsigned requesting_node,
  29. uint32_t write_through_mask)
  30. {
  31. if ((write_through_mask & ~(1<<requesting_node)) == 0)
  32. {
  33. /* nothing will be done ... */
  34. return;
  35. }
  36. /* first commit all changes onto the nodes specified by the mask */
  37. unsigned node, max;
  38. for (node = 0, max = starpu_memory_nodes_get_count(); node < max; node++)
  39. {
  40. if (write_through_mask & (1<<node))
  41. {
  42. /* we need to commit the buffer on that node */
  43. if (node != requesting_node)
  44. {
  45. int cpt = 0;
  46. while (cpt < STARPU_SPIN_MAXTRY && _starpu_spin_trylock(&handle->header_lock))
  47. {
  48. cpt++;
  49. __starpu_datawizard_progress(1, 1);
  50. }
  51. if (cpt == STARPU_SPIN_MAXTRY)
  52. _starpu_spin_lock(&handle->header_lock);
  53. /* We need to keep a Read lock to avoid letting writers corrupt our copy. */
  54. STARPU_ASSERT(handle->current_mode != STARPU_REDUX);
  55. STARPU_ASSERT(handle->current_mode != STARPU_SCRATCH);
  56. handle->refcnt++;
  57. handle->busy_count++;
  58. handle->current_mode = STARPU_R;
  59. struct _starpu_data_request *r;
  60. r = _starpu_create_request_to_fetch_data(handle, &handle->per_node[node],
  61. STARPU_R, 1, 1, wt_callback, handle, 0, "_starpu_write_through_data");
  62. /* If no request was created, the handle was already up-to-date on the
  63. * node */
  64. if (r)
  65. _starpu_spin_unlock(&handle->header_lock);
  66. }
  67. }
  68. }
  69. }
  70. void starpu_data_set_wt_mask(starpu_data_handle_t handle, uint32_t wt_mask)
  71. {
  72. handle->wt_mask = wt_mask;
  73. /* in case the data has some children, set their wt_mask as well */
  74. if (handle->nchildren > 0)
  75. {
  76. unsigned child;
  77. for (child = 0; child < handle->nchildren; child++)
  78. {
  79. starpu_data_handle_t handle_child = starpu_data_get_child(handle, child);
  80. starpu_data_set_wt_mask(handle_child, wt_mask);
  81. }
  82. }
  83. }