data_interface.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <core/dependencies/data_concurrency.h>
  18. /*
  19. * Start monitoring a piece of data
  20. */
  21. static void _starpu_register_new_data(starpu_data_handle handle,
  22. uint32_t home_node, uint32_t wb_mask)
  23. {
  24. STARPU_ASSERT(handle);
  25. /* initialize the new lock */
  26. handle->req_list = starpu_data_requester_list_new();
  27. handle->refcnt = 0;
  28. _starpu_spin_init(&handle->header_lock);
  29. /* first take care to properly lock the data */
  30. _starpu_spin_lock(&handle->header_lock);
  31. /* there is no hierarchy yet */
  32. handle->nchildren = 0;
  33. handle->root_handle = handle;
  34. handle->father_handle = NULL;
  35. handle->sibling_index = 0; /* could be anything for the root */
  36. handle->depth = 1; /* the tree is just a node yet */
  37. handle->is_not_important = 0;
  38. handle->sequential_consistency =
  39. starpu_data_get_default_sequential_consistency_flag();
  40. PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  41. handle->last_submitted_mode = STARPU_R;
  42. handle->last_submitted_writer = NULL;
  43. handle->last_submitted_readers = NULL;
  44. handle->post_sync_tasks = NULL;
  45. handle->post_sync_tasks_cnt = 0;
  46. #ifdef STARPU_USE_FXT
  47. handle->last_submitted_ghost_writer_id_is_valid = 0;
  48. handle->last_submitted_ghost_writer_id = 0;
  49. handle->last_submitted_ghost_readers_id = NULL;
  50. #endif
  51. handle->wb_mask = wb_mask;
  52. /* Store some values directly in the handle not to recompute them all
  53. * the time. */
  54. handle->data_size = handle->ops->get_size(handle);
  55. handle->footprint = _starpu_compute_data_footprint(handle);
  56. handle->home_node = home_node;
  57. /* that new data is invalid from all nodes perpective except for the
  58. * home node */
  59. unsigned node;
  60. for (node = 0; node < STARPU_MAXNODES; node++)
  61. {
  62. if (node == home_node) {
  63. /* this is the home node with the only valid copy */
  64. handle->per_node[node].state = STARPU_OWNER;
  65. handle->per_node[node].allocated = 1;
  66. handle->per_node[node].automatically_allocated = 0;
  67. handle->per_node[node].refcnt = 0;
  68. }
  69. else {
  70. /* the value is not available here yet */
  71. handle->per_node[node].state = STARPU_INVALID;
  72. handle->per_node[node].allocated = 0;
  73. handle->per_node[node].refcnt = 0;
  74. }
  75. }
  76. /* now the data is available ! */
  77. _starpu_spin_unlock(&handle->header_lock);
  78. }
  79. static starpu_data_handle _starpu_data_handle_allocate(struct starpu_data_interface_ops_t *interface_ops)
  80. {
  81. starpu_data_handle handle =
  82. calloc(1, sizeof(struct starpu_data_state_t));
  83. STARPU_ASSERT(handle);
  84. handle->ops = interface_ops;
  85. size_t interfacesize = interface_ops->interface_size;
  86. unsigned node;
  87. for (node = 0; node < STARPU_MAXNODES; node++)
  88. {
  89. handle->interface[node] = calloc(1, interfacesize);
  90. STARPU_ASSERT(handle->interface[node]);
  91. }
  92. return handle;
  93. }
  94. void starpu_data_register(starpu_data_handle *handleptr, uint32_t home_node,
  95. void *interface,
  96. struct starpu_data_interface_ops_t *ops)
  97. {
  98. starpu_data_handle handle =
  99. _starpu_data_handle_allocate(ops);
  100. STARPU_ASSERT(handleptr);
  101. *handleptr = handle;
  102. /* fill the interface fields with the appropriate method */
  103. ops->register_data_handle(handle, home_node, interface);
  104. _starpu_register_new_data(handle, home_node, 0);
  105. }
  106. /*
  107. * Stop monitoring a piece of data
  108. */
  109. void _starpu_data_free_interfaces(starpu_data_handle handle)
  110. {
  111. unsigned node;
  112. for (node = 0; node < STARPU_MAXNODES; node++)
  113. free(handle->interface[node]);
  114. }
  115. struct unregister_callback_arg {
  116. unsigned memory_node;
  117. starpu_data_handle handle;
  118. unsigned terminated;
  119. pthread_mutex_t mutex;
  120. pthread_cond_t cond;
  121. };
  122. static void _starpu_data_unregister_fetch_data_callback(void *_arg)
  123. {
  124. int ret;
  125. struct unregister_callback_arg *arg = _arg;
  126. starpu_data_handle handle = arg->handle;
  127. STARPU_ASSERT(handle);
  128. ret = _starpu_fetch_data_on_node(handle, arg->memory_node, STARPU_R, 0, NULL, NULL);
  129. STARPU_ASSERT(!ret);
  130. /* unlock the caller */
  131. PTHREAD_MUTEX_LOCK(&arg->mutex);
  132. arg->terminated = 1;
  133. PTHREAD_COND_SIGNAL(&arg->cond);
  134. PTHREAD_MUTEX_UNLOCK(&arg->mutex);
  135. }
  136. void starpu_data_unregister(starpu_data_handle handle)
  137. {
  138. unsigned node;
  139. /* If sequential consistency is enabled, wait until data is available */
  140. _starpu_data_wait_until_available(handle, STARPU_RW);
  141. /* Fetch data in the home of the data to ensure we have a valid copy
  142. * where we registered it */
  143. int home_node = handle->home_node;
  144. if (home_node >= 0)
  145. {
  146. struct unregister_callback_arg arg;
  147. arg.handle = handle;
  148. arg.memory_node = (unsigned)home_node;
  149. arg.terminated = 0;
  150. PTHREAD_MUTEX_INIT(&arg.mutex, NULL);
  151. PTHREAD_COND_INIT(&arg.cond, NULL);
  152. if (!_starpu_attempt_to_submit_data_request_from_apps(handle, STARPU_R,
  153. _starpu_data_unregister_fetch_data_callback, &arg))
  154. {
  155. /* no one has locked this data yet, so we proceed immediately */
  156. int ret = _starpu_fetch_data_on_node(handle, home_node, STARPU_R, 0, NULL, NULL);
  157. STARPU_ASSERT(!ret);
  158. }
  159. else {
  160. PTHREAD_MUTEX_LOCK(&arg.mutex);
  161. while (!arg.terminated)
  162. PTHREAD_COND_WAIT(&arg.cond, &arg.mutex);
  163. PTHREAD_MUTEX_UNLOCK(&arg.mutex);
  164. }
  165. }
  166. /* Destroy the data now */
  167. STARPU_ASSERT(handle);
  168. for (node = 0; node < STARPU_MAXNODES; node++)
  169. {
  170. starpu_local_data_state *local = &handle->per_node[node];
  171. if (local->allocated && local->automatically_allocated){
  172. /* free the data copy in a lazy fashion */
  173. _starpu_request_mem_chunk_removal(handle, node);
  174. }
  175. }
  176. starpu_data_requester_list_delete(handle->req_list);
  177. _starpu_data_free_interfaces(handle);
  178. free(handle);
  179. }
  180. unsigned starpu_get_handle_interface_id(starpu_data_handle handle)
  181. {
  182. return handle->ops->interfaceid;
  183. }
  184. void *starpu_data_get_interface_on_node(starpu_data_handle handle, unsigned memory_node)
  185. {
  186. return handle->interface[memory_node];
  187. }