memory_nodes.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 CNRS
  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 <datawizard/memory_nodes.h>
  22. #include <datawizard/malloc.h>
  23. #include <common/fxt.h>
  24. #include "copy_driver.h"
  25. #include "memalloc.h"
  26. static struct _starpu_memory_node_descr descr;
  27. static starpu_pthread_key_t memory_node_key;
  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. descr.nnodes = 0;
  33. STARPU_PTHREAD_KEY_CREATE(&memory_node_key, NULL);
  34. unsigned i;
  35. for (i = 0; i < STARPU_MAXNODES; i++)
  36. {
  37. descr.nodes[i] = STARPU_UNUSED;
  38. descr.nworkers[i] = 0;
  39. }
  40. _starpu_init_mem_chunk_lists();
  41. _starpu_init_data_request_lists();
  42. _starpu_memory_manager_init();
  43. STARPU_PTHREAD_RWLOCK_INIT(&descr.conditions_rwlock, NULL);
  44. descr.total_condition_count = 0;
  45. }
  46. void _starpu_memory_nodes_deinit(void)
  47. {
  48. _starpu_deinit_data_request_lists();
  49. _starpu_deinit_mem_chunk_lists();
  50. STARPU_PTHREAD_RWLOCK_DESTROY(&descr.conditions_rwlock);
  51. STARPU_PTHREAD_KEY_DELETE(memory_node_key);
  52. }
  53. void _starpu_memory_node_set_local_key(unsigned *node)
  54. {
  55. STARPU_PTHREAD_SETSPECIFIC(memory_node_key, node);
  56. }
  57. unsigned _starpu_memory_node_get_local_key(void)
  58. {
  59. unsigned *memory_node;
  60. memory_node = (unsigned *) STARPU_PTHREAD_GETSPECIFIC(memory_node_key);
  61. /* in case this is called by the programmer, we assume the RAM node
  62. is the appropriate memory node ... XXX */
  63. if (STARPU_UNLIKELY(!memory_node))
  64. return STARPU_MAIN_RAM;
  65. return *memory_node;
  66. }
  67. void _starpu_memory_node_add_nworkers(unsigned node)
  68. {
  69. descr.nworkers[node]++;
  70. }
  71. unsigned _starpu_memory_node_get_nworkers(unsigned node)
  72. {
  73. return descr.nworkers[node];
  74. }
  75. struct _starpu_memory_node_descr *_starpu_memory_node_get_description(void)
  76. {
  77. return &descr;
  78. }
  79. enum starpu_node_kind starpu_node_get_kind(unsigned node)
  80. {
  81. return descr.nodes[node];
  82. }
  83. int _starpu_memory_node_get_devid(unsigned node)
  84. {
  85. return descr.devid[node];
  86. }
  87. unsigned starpu_memory_nodes_get_count(void)
  88. {
  89. return descr.nnodes;
  90. }
  91. void _starpu_memory_node_get_name(unsigned node, char *name, int size)
  92. {
  93. const char *prefix;
  94. switch (descr.nodes[node])
  95. {
  96. case STARPU_CPU_RAM:
  97. prefix = "RAM";
  98. break;
  99. case STARPU_CUDA_RAM:
  100. prefix = "CUDA";
  101. break;
  102. case STARPU_OPENCL_RAM:
  103. prefix = "OpenCL";
  104. break;
  105. case STARPU_DISK_RAM:
  106. prefix = "Disk";
  107. break;
  108. case STARPU_MIC_RAM:
  109. prefix = "MIC";
  110. break;
  111. case STARPU_SCC_RAM:
  112. prefix = "SCC_RAM";
  113. break;
  114. case STARPU_SCC_SHM:
  115. prefix = "SCC_shared";
  116. break;
  117. case STARPU_UNUSED:
  118. default:
  119. prefix = "unknown";
  120. STARPU_ASSERT(0);
  121. }
  122. snprintf(name, size, "%s %u", prefix, descr.devid[node]);
  123. }
  124. unsigned _starpu_memory_node_register(enum starpu_node_kind kind, int devid)
  125. {
  126. unsigned node;
  127. /* ATOMIC_ADD returns the new value ... */
  128. node = STARPU_ATOMIC_ADD(&descr.nnodes, 1) - 1;
  129. 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);
  130. descr.nodes[node] = kind;
  131. _STARPU_TRACE_NEW_MEM_NODE(node);
  132. descr.devid[node] = devid;
  133. /* for now, there is no condition associated to that newly created node */
  134. descr.condition_count[node] = 0;
  135. _starpu_malloc_init(node);
  136. return node;
  137. }
  138. #ifdef STARPU_SIMGRID
  139. void _starpu_simgrid_memory_node_set_host(unsigned node, msg_host_t host)
  140. {
  141. descr.host[node] = host;
  142. }
  143. msg_host_t _starpu_simgrid_memory_node_get_host(unsigned node)
  144. {
  145. return descr.host[node];
  146. }
  147. #endif
  148. /* TODO move in a more appropriate file !! */
  149. /* Register a condition variable associated to worker which is associated to a
  150. * memory node itself. */
  151. void _starpu_memory_node_register_condition(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex, unsigned nodeid)
  152. {
  153. unsigned cond_id;
  154. unsigned nconds_total, nconds;
  155. STARPU_PTHREAD_RWLOCK_WRLOCK(&descr.conditions_rwlock);
  156. /* we only insert the queue if it's not already in the list */
  157. nconds = descr.condition_count[nodeid];
  158. for (cond_id = 0; cond_id < nconds; cond_id++)
  159. {
  160. if (descr.conditions_attached_to_node[nodeid][cond_id].cond == cond)
  161. {
  162. STARPU_ASSERT(descr.conditions_attached_to_node[nodeid][cond_id].mutex == mutex);
  163. /* the condition is already in the list */
  164. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  165. return;
  166. }
  167. }
  168. /* it was not found locally */
  169. descr.conditions_attached_to_node[nodeid][cond_id].cond = cond;
  170. descr.conditions_attached_to_node[nodeid][cond_id].mutex = mutex;
  171. descr.condition_count[nodeid]++;
  172. /* do we have to add it in the global list as well ? */
  173. nconds_total = descr.total_condition_count;
  174. for (cond_id = 0; cond_id < nconds_total; cond_id++)
  175. {
  176. if (descr.conditions_all[cond_id].cond == cond)
  177. {
  178. /* the queue is already in the global list */
  179. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  180. return;
  181. }
  182. }
  183. /* it was not in the global list either */
  184. descr.conditions_all[nconds_total].cond = cond;
  185. descr.conditions_all[nconds_total].mutex = mutex;
  186. descr.total_condition_count++;
  187. STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  188. }
  189. unsigned starpu_worker_get_memory_node(unsigned workerid)
  190. {
  191. struct _starpu_machine_config *config = _starpu_get_machine_config();
  192. /* This workerid may either be a basic worker or a combined worker */
  193. unsigned nworkers = config->topology.nworkers;
  194. if (workerid < config->topology.nworkers)
  195. return config->workers[workerid].memory_node;
  196. /* We have a combined worker */
  197. unsigned ncombinedworkers = config->topology.ncombinedworkers;
  198. STARPU_ASSERT_MSG(workerid < ncombinedworkers + nworkers, "Bad workerid %u, maximum %u", workerid, ncombinedworkers + nworkers);
  199. return config->combined_workers[workerid - nworkers].memory_node;
  200. }