memory_nodes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <common/config.h>
  18. #include <core/sched_policy.h>
  19. #include <datawizard/datastats.h>
  20. #include <datawizard/memory_manager.h>
  21. #include <common/fxt.h>
  22. #include "copy_driver.h"
  23. #include "memalloc.h"
  24. static struct _starpu_memory_node_descr descr;
  25. static starpu_pthread_key_t memory_node_key;
  26. void _starpu_memory_nodes_init(void)
  27. {
  28. /* there is no node yet, subsequent nodes will be
  29. * added using _starpu_memory_node_register */
  30. descr.nnodes = 0;
  31. STARPU_PTHREAD_KEY_CREATE(&memory_node_key, NULL);
  32. unsigned i;
  33. for (i = 0; i < STARPU_MAXNODES; i++)
  34. {
  35. descr.nodes[i] = STARPU_UNUSED;
  36. descr.nworkers[i] = 0;
  37. }
  38. _starpu_init_mem_chunk_lists();
  39. _starpu_init_data_request_lists();
  40. _starpu_memory_manager_init();
  41. STARPU_PTHREAD_RWLOCK_INIT(&descr.conditions_rwlock, NULL);
  42. descr.total_condition_count = 0;
  43. }
  44. void _starpu_memory_nodes_deinit(void)
  45. {
  46. _starpu_deinit_data_request_lists();
  47. _starpu_deinit_mem_chunk_lists();
  48. STARPU_PTHREAD_RWLOCK_DESTROY(&descr.conditions_rwlock);
  49. STARPU_PTHREAD_KEY_DELETE(memory_node_key);
  50. }
  51. void _starpu_memory_node_set_local_key(unsigned *node)
  52. {
  53. STARPU_PTHREAD_SETSPECIFIC(memory_node_key, node);
  54. }
  55. unsigned _starpu_memory_node_get_local_key(void)
  56. {
  57. unsigned *memory_node;
  58. memory_node = (unsigned *) STARPU_PTHREAD_GETSPECIFIC(memory_node_key);
  59. /* in case this is called by the programmer, we assume the RAM node
  60. is the appropriate memory node ... XXX */
  61. if (STARPU_UNLIKELY(!memory_node))
  62. return STARPU_MAIN_RAM;
  63. return *memory_node;
  64. }
  65. void _starpu_memory_node_add_nworkers(unsigned node)
  66. {
  67. descr.nworkers[node]++;
  68. }
  69. unsigned _starpu_memory_node_get_nworkers(unsigned node)
  70. {
  71. return descr.nworkers[node];
  72. }
  73. struct _starpu_memory_node_descr *_starpu_memory_node_get_description(void)
  74. {
  75. return &descr;
  76. }
  77. enum starpu_node_kind starpu_node_get_kind(unsigned node)
  78. {
  79. return descr.nodes[node];
  80. }
  81. int _starpu_memory_node_get_devid(unsigned node)
  82. {
  83. return descr.devid[node];
  84. }
  85. unsigned starpu_memory_nodes_get_count(void)
  86. {
  87. return descr.nnodes;
  88. }
  89. unsigned _starpu_memory_node_register(enum starpu_node_kind kind, int devid)
  90. {
  91. unsigned node;
  92. /* ATOMIC_ADD returns the new value ... */
  93. node = STARPU_ATOMIC_ADD(&descr.nnodes, 1) - 1;
  94. STARPU_ASSERT_MSG(node < STARPU_MAXNODES,"Too many nodes (%u) for maximum %u. Use configure option --enable-maxnodes=xxx to update the maximum number of nodes.", node, STARPU_MAXNODES);
  95. descr.nodes[node] = kind;
  96. _STARPU_TRACE_NEW_MEM_NODE(node);
  97. descr.devid[node] = devid;
  98. /* for now, there is no condition associated to that newly created node */
  99. descr.condition_count[node] = 0;
  100. return node;
  101. }
  102. #ifdef STARPU_SIMGRID
  103. void _starpu_simgrid_memory_node_set_host(unsigned node, msg_host_t host)
  104. {
  105. descr.host[node] = host;
  106. }
  107. msg_host_t _starpu_simgrid_memory_node_get_host(unsigned node)
  108. {
  109. return descr.host[node];
  110. }
  111. #endif
  112. /* TODO move in a more appropriate file !! */
  113. /* Register a condition variable associated to worker which is associated to a
  114. * memory node itself. */
  115. void _starpu_memory_node_register_condition(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex, unsigned nodeid)
  116. {
  117. unsigned cond_id;
  118. unsigned nconds_total, nconds;
  119. STARPU_PTHREAD_RWLOCK_WRLOCK(&descr.conditions_rwlock);
  120. /* we only insert the queue if it's not already in the list */
  121. nconds = descr.condition_count[nodeid];
  122. for (cond_id = 0; cond_id < nconds; cond_id++)
  123. {
  124. if (descr.conditions_attached_to_node[nodeid][cond_id].cond == cond)
  125. {
  126. STARPU_ASSERT(descr.conditions_attached_to_node[nodeid][cond_id].mutex == mutex);
  127. /* the condition is already in the list */
  128. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  129. return;
  130. }
  131. }
  132. /* it was not found locally */
  133. descr.conditions_attached_to_node[nodeid][cond_id].cond = cond;
  134. descr.conditions_attached_to_node[nodeid][cond_id].mutex = mutex;
  135. descr.condition_count[nodeid]++;
  136. /* do we have to add it in the global list as well ? */
  137. nconds_total = descr.total_condition_count;
  138. for (cond_id = 0; cond_id < nconds_total; cond_id++)
  139. {
  140. if (descr.conditions_all[cond_id].cond == cond)
  141. {
  142. /* the queue is already in the global list */
  143. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  144. return;
  145. }
  146. }
  147. /* it was not in the global list either */
  148. descr.conditions_all[nconds_total].cond = cond;
  149. descr.conditions_all[nconds_total].mutex = mutex;
  150. descr.total_condition_count++;
  151. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  152. }
  153. unsigned starpu_worker_get_memory_node(unsigned workerid)
  154. {
  155. struct _starpu_machine_config *config = _starpu_get_machine_config();
  156. /* This workerid may either be a basic worker or a combined worker */
  157. unsigned nworkers = config->topology.nworkers;
  158. if (workerid < config->topology.nworkers)
  159. return config->workers[workerid].memory_node;
  160. /* We have a combined worker */
  161. unsigned ncombinedworkers = config->topology.ncombinedworkers;
  162. STARPU_ASSERT_MSG(workerid < ncombinedworkers + nworkers, "Bad workerid %u, maximum %u", workerid, ncombinedworkers + nworkers);
  163. return config->combined_workers[workerid - nworkers].memory_node;
  164. }