data_interface.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /* there is no hierarchy yet */
  31. handle->nchildren = 0;
  32. handle->root_handle = handle;
  33. handle->father_handle = NULL;
  34. handle->sibling_index = 0; /* could be anything for the root */
  35. handle->depth = 1; /* the tree is just a node yet */
  36. handle->is_not_important = 0;
  37. handle->sequential_consistency = 1; /* enabled by default */
  38. PTHREAD_MUTEX_INIT(&handle->sequential_consistency_mutex, NULL);
  39. handle->last_submitted_mode = STARPU_R;
  40. handle->last_submitted_writer = NULL;
  41. handle->last_submitted_readers = NULL;
  42. handle->post_sync_tasks = NULL;
  43. handle->post_sync_tasks_cnt = 0;
  44. handle->wb_mask = wb_mask;
  45. /* that new data is invalid from all nodes perpective except for the
  46. * home node */
  47. unsigned node;
  48. for (node = 0; node < STARPU_MAXNODES; node++)
  49. {
  50. if (node == home_node) {
  51. /* this is the home node with the only valid copy */
  52. handle->per_node[node].state = STARPU_OWNER;
  53. handle->per_node[node].allocated = 1;
  54. handle->per_node[node].automatically_allocated = 0;
  55. handle->per_node[node].refcnt = 0;
  56. }
  57. else {
  58. /* the value is not available here yet */
  59. handle->per_node[node].state = STARPU_INVALID;
  60. handle->per_node[node].allocated = 0;
  61. handle->per_node[node].refcnt = 0;
  62. }
  63. }
  64. /* now the data is available ! */
  65. _starpu_spin_unlock(&handle->header_lock);
  66. }
  67. static starpu_data_handle _starpu_data_handle_allocate(struct starpu_data_interface_ops_t *interface_ops)
  68. {
  69. starpu_data_handle handle =
  70. calloc(1, sizeof(struct starpu_data_state_t));
  71. STARPU_ASSERT(handle);
  72. handle->ops = interface_ops;
  73. size_t interfacesize = interface_ops->interface_size;
  74. unsigned node;
  75. for (node = 0; node < STARPU_MAXNODES; node++)
  76. {
  77. handle->interface[node] = calloc(1, interfacesize);
  78. STARPU_ASSERT(handle->interface[node]);
  79. }
  80. return handle;
  81. }
  82. void _starpu_register_data_handle(starpu_data_handle *handleptr, uint32_t home_node,
  83. void *interface,
  84. struct starpu_data_interface_ops_t *ops)
  85. {
  86. starpu_data_handle handle =
  87. _starpu_data_handle_allocate(ops);
  88. STARPU_ASSERT(handleptr);
  89. *handleptr = handle;
  90. /* fill the interface fields with the appropriate method */
  91. ops->register_data_handle(handle, home_node, interface);
  92. _starpu_register_new_data(handle, home_node, 0);
  93. }
  94. /*
  95. * Stop monitoring a piece of data
  96. */
  97. void starpu_data_free_interfaces(starpu_data_handle handle)
  98. {
  99. unsigned node;
  100. for (node = 0; node < STARPU_MAXNODES; node++)
  101. free(handle->interface[node]);
  102. }
  103. void starpu_data_unregister(starpu_data_handle handle)
  104. {
  105. unsigned node;
  106. STARPU_ASSERT(handle);
  107. for (node = 0; node < STARPU_MAXNODES; node++)
  108. {
  109. starpu_local_data_state *local = &handle->per_node[node];
  110. if (local->allocated && local->automatically_allocated){
  111. /* free the data copy in a lazy fashion */
  112. _starpu_request_mem_chunk_removal(handle, node);
  113. }
  114. }
  115. starpu_data_requester_list_delete(handle->req_list);
  116. starpu_data_free_interfaces(handle);
  117. free(handle);
  118. }
  119. unsigned starpu_get_handle_interface_id(starpu_data_handle handle)
  120. {
  121. return handle->ops->interfaceid;
  122. }
  123. void *starpu_data_get_interface_on_node(starpu_data_handle handle, unsigned memory_node)
  124. {
  125. return handle->interface[memory_node];
  126. }