memory_nodes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017,2019 Université de Bordeaux
  4. * Copyright (C) 2011-2013,2016,2017 Inria
  5. * Copyright (C) 2010-2015,2017,2018,2019 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 <common/config.h>
  19. #include <core/sched_policy.h>
  20. #include <datawizard/datastats.h>
  21. #include <datawizard/memory_manager.h>
  22. #include <datawizard/memory_nodes.h>
  23. #include <datawizard/malloc.h>
  24. #include <common/fxt.h>
  25. #include <datawizard/copy_driver.h>
  26. #include <datawizard/memalloc.h>
  27. #include <datawizard/node_ops.h>
  28. char _starpu_worker_drives_memory[STARPU_NMAXWORKERS][STARPU_MAXNODES];
  29. struct _starpu_memory_node_descr _starpu_descr;
  30. void _starpu_memory_nodes_init(void)
  31. {
  32. /* there is no node yet, subsequent nodes will be
  33. * added using _starpu_memory_node_register */
  34. _starpu_descr.nnodes = 0;
  35. unsigned i;
  36. for (i = 0; i < STARPU_MAXNODES; i++)
  37. {
  38. _starpu_descr.nodes[i] = STARPU_UNUSED;
  39. _starpu_descr.nworkers[i] = 0;
  40. }
  41. memset(&_starpu_worker_drives_memory, 0, sizeof(_starpu_worker_drives_memory));
  42. STARPU_HG_DISABLE_CHECKING(_starpu_worker_drives_memory);
  43. _starpu_init_mem_chunk_lists();
  44. _starpu_init_data_request_lists();
  45. _starpu_memory_manager_init();
  46. STARPU_PTHREAD_RWLOCK_INIT(&_starpu_descr.conditions_rwlock, NULL);
  47. _starpu_descr.total_condition_count = 0;
  48. }
  49. void _starpu_memory_nodes_deinit(void)
  50. {
  51. _starpu_deinit_data_request_lists();
  52. _starpu_deinit_mem_chunk_lists();
  53. STARPU_PTHREAD_RWLOCK_DESTROY(&_starpu_descr.conditions_rwlock);
  54. }
  55. #undef starpu_node_get_kind
  56. enum starpu_node_kind starpu_node_get_kind(unsigned node)
  57. {
  58. return _starpu_node_get_kind(node);
  59. }
  60. #undef starpu_memory_nodes_get_count
  61. unsigned starpu_memory_nodes_get_count(void)
  62. {
  63. return _starpu_memory_nodes_get_count();
  64. }
  65. int starpu_memory_node_get_name(unsigned node, char *name, size_t size)
  66. {
  67. const char *prefix = _starpu_node_get_prefix(_starpu_descr.nodes[node]);
  68. return snprintf(name, size, "%s %d", prefix, _starpu_descr.devid[node]);
  69. }
  70. unsigned _starpu_memory_node_register(enum starpu_node_kind kind, int devid)
  71. {
  72. unsigned node;
  73. /* ATOMIC_ADD returns the new value ... */
  74. node = STARPU_ATOMIC_ADD(&_starpu_descr.nnodes, 1) - 1;
  75. STARPU_ASSERT_MSG(node < STARPU_MAXNODES,"Too many nodes (%u) for maximum %d. Use configure option --enable-maxnodes=xxx to update the maximum number of nodes.", node, STARPU_MAXNODES);
  76. _starpu_descr.nodes[node] = kind;
  77. _STARPU_TRACE_NEW_MEM_NODE(node);
  78. _starpu_descr.devid[node] = devid;
  79. /* for now, there is no condition associated to that newly created node */
  80. _starpu_descr.condition_count[node] = 0;
  81. _starpu_malloc_init(node);
  82. return node;
  83. }
  84. /* TODO move in a more appropriate file !! */
  85. /* Register a condition variable associated to worker which is associated to a
  86. * memory node itself. */
  87. void _starpu_memory_node_register_condition(struct _starpu_worker *worker, starpu_pthread_cond_t *cond, unsigned nodeid)
  88. {
  89. unsigned cond_id;
  90. unsigned nconds_total, nconds;
  91. STARPU_PTHREAD_RWLOCK_WRLOCK(&_starpu_descr.conditions_rwlock);
  92. /* we only insert the queue if it's not already in the list */
  93. nconds = _starpu_descr.condition_count[nodeid];
  94. for (cond_id = 0; cond_id < nconds; cond_id++)
  95. {
  96. if (_starpu_descr.conditions_attached_to_node[nodeid][cond_id].cond == cond)
  97. {
  98. STARPU_ASSERT(_starpu_descr.conditions_attached_to_node[nodeid][cond_id].worker == worker);
  99. /* the condition is already in the list */
  100. STARPU_PTHREAD_RWLOCK_UNLOCK(&_starpu_descr.conditions_rwlock);
  101. return;
  102. }
  103. }
  104. /* it was not found locally */
  105. _starpu_descr.conditions_attached_to_node[nodeid][cond_id].cond = cond;
  106. _starpu_descr.conditions_attached_to_node[nodeid][cond_id].worker = worker;
  107. _starpu_descr.condition_count[nodeid]++;
  108. /* do we have to add it in the global list as well ? */
  109. nconds_total = _starpu_descr.total_condition_count;
  110. for (cond_id = 0; cond_id < nconds_total; cond_id++)
  111. {
  112. if (_starpu_descr.conditions_all[cond_id].cond == cond)
  113. {
  114. /* the queue is already in the global list */
  115. STARPU_PTHREAD_RWLOCK_UNLOCK(&_starpu_descr.conditions_rwlock);
  116. return;
  117. }
  118. }
  119. /* it was not in the global list either */
  120. _starpu_descr.conditions_all[nconds_total].cond = cond;
  121. _starpu_descr.conditions_all[nconds_total].worker = worker;
  122. _starpu_descr.total_condition_count++;
  123. STARPU_PTHREAD_RWLOCK_UNLOCK(&_starpu_descr.conditions_rwlock);
  124. }
  125. #undef starpu_worker_get_memory_node
  126. unsigned starpu_worker_get_memory_node(unsigned workerid)
  127. {
  128. return _starpu_worker_get_memory_node(workerid);
  129. }
  130. /* same utility as _starpu_memory_node_add_nworkers */
  131. void _starpu_worker_drives_memory_node(struct _starpu_worker *worker, unsigned memnode)
  132. {
  133. if (! _starpu_worker_drives_memory[worker->workerid][memnode])
  134. {
  135. _starpu_worker_drives_memory[worker->workerid][memnode] = 1;
  136. #ifdef STARPU_SIMGRID
  137. starpu_pthread_queue_register(&worker->wait, &_starpu_simgrid_transfer_queue[memnode]);
  138. #endif
  139. _starpu_memory_node_register_condition(worker, &worker->sched_cond, memnode);
  140. }
  141. }
  142. unsigned starpu_worker_get_local_memory_node(void)
  143. {
  144. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  145. if (!worker)
  146. return STARPU_MAIN_RAM;
  147. return worker->memory_node;
  148. }
  149. int starpu_memory_node_get_devid(unsigned node)
  150. {
  151. return _starpu_descr.devid[node];
  152. }