data_interface.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 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. /*
  18. * Start monitoring a piece of data
  19. */
  20. static void _starpu_register_new_data(starpu_data_handle handle,
  21. uint32_t home_node, uint32_t wb_mask)
  22. {
  23. STARPU_ASSERT(handle);
  24. /* initialize the new lock */
  25. handle->req_list = starpu_data_requester_list_new();
  26. handle->refcnt = 0;
  27. _starpu_spin_init(&handle->header_lock);
  28. /* first take care to properly lock the data */
  29. _starpu_spin_lock(&handle->header_lock);
  30. /* we assume that all nodes may use that data */
  31. handle->nnodes = STARPU_MAXNODES;
  32. /* there is no hierarchy yet */
  33. handle->nchildren = 0;
  34. handle->root_handle = handle;
  35. handle->father_handle = NULL;
  36. handle->sibling_index = 0; /* could be anything for the root */
  37. handle->depth = 1; /* the tree is just a node yet */
  38. handle->is_not_important = 0;
  39. handle->sequential_consistency = 1; /* enabled by default */
  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->last_submitted_cg_apps = NULL;
  45. handle->current_cg_apps = NULL;
  46. handle->last_submitted_sync_task_apps = NULL;
  47. handle->wb_mask = wb_mask;
  48. /* that new data is invalid from all nodes perpective except for the
  49. * home node */
  50. unsigned node;
  51. for (node = 0; node < STARPU_MAXNODES; node++)
  52. {
  53. if (node == home_node) {
  54. /* this is the home node with the only valid copy */
  55. handle->per_node[node].state = STARPU_OWNER;
  56. handle->per_node[node].allocated = 1;
  57. handle->per_node[node].automatically_allocated = 0;
  58. handle->per_node[node].refcnt = 0;
  59. }
  60. else {
  61. /* the value is not available here yet */
  62. handle->per_node[node].state = STARPU_INVALID;
  63. handle->per_node[node].allocated = 0;
  64. handle->per_node[node].refcnt = 0;
  65. }
  66. }
  67. /* now the data is available ! */
  68. _starpu_spin_unlock(&handle->header_lock);
  69. }
  70. static starpu_data_handle _starpu_data_handle_allocate(struct starpu_data_interface_ops_t *interface_ops)
  71. {
  72. starpu_data_handle handle =
  73. calloc(1, sizeof(struct starpu_data_state_t));
  74. STARPU_ASSERT(handle);
  75. handle->ops = interface_ops;
  76. size_t interfacesize = interface_ops->interface_size;
  77. unsigned node;
  78. for (node = 0; node < STARPU_MAXNODES; node++)
  79. {
  80. handle->interface[node] = calloc(1, interfacesize);
  81. STARPU_ASSERT(handle->interface[node]);
  82. }
  83. return handle;
  84. }
  85. void _starpu_register_data_handle(starpu_data_handle *handleptr, uint32_t home_node,
  86. void *interface,
  87. struct starpu_data_interface_ops_t *ops)
  88. {
  89. starpu_data_handle handle =
  90. _starpu_data_handle_allocate(ops);
  91. STARPU_ASSERT(handleptr);
  92. *handleptr = handle;
  93. /* fill the interface fields with the appropriate method */
  94. ops->register_data_handle(handle, home_node, interface);
  95. _starpu_register_new_data(handle, home_node, 0);
  96. }
  97. /*
  98. * Stop monitoring a piece of data
  99. */
  100. void starpu_data_free_interfaces(starpu_data_handle handle)
  101. {
  102. unsigned node;
  103. for (node = 0; node < STARPU_MAXNODES; node++)
  104. free(handle->interface[node]);
  105. }
  106. void starpu_data_unregister(starpu_data_handle handle)
  107. {
  108. unsigned node;
  109. STARPU_ASSERT(handle);
  110. for (node = 0; node < STARPU_MAXNODES; node++)
  111. {
  112. starpu_local_data_state *local = &handle->per_node[node];
  113. if (local->allocated && local->automatically_allocated){
  114. /* free the data copy in a lazy fashion */
  115. _starpu_request_mem_chunk_removal(handle, node);
  116. }
  117. }
  118. starpu_data_requester_list_delete(handle->req_list);
  119. starpu_data_free_interfaces(handle);
  120. free(handle);
  121. }
  122. unsigned starpu_get_handle_interface_id(starpu_data_handle handle)
  123. {
  124. return handle->ops->interfaceid;
  125. }
  126. void *starpu_data_get_interface_on_node(starpu_data_handle handle, unsigned memory_node)
  127. {
  128. return handle->interface[memory_node];
  129. }