memory_nodes.c 6.0 KB

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