memory_nodes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 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. char _starpu_worker_drives_memory[STARPU_NMAXWORKERS][STARPU_MAXNODES];
  27. struct _starpu_memory_node_descr _starpu_descr;
  28. starpu_pthread_key_t _starpu_memory_node_key STARPU_ATTRIBUTE_INTERNAL;
  29. void _starpu_memory_nodes_init(void)
  30. {
  31. /* there is no node yet, subsequent nodes will be
  32. * added using _starpu_memory_node_register */
  33. _starpu_descr.nnodes = 0;
  34. STARPU_PTHREAD_KEY_CREATE(&_starpu_memory_node_key, NULL);
  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. _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. STARPU_PTHREAD_KEY_DELETE(_starpu_memory_node_key);
  53. }
  54. #undef starpu_node_get_kind
  55. enum starpu_node_kind starpu_node_get_kind(unsigned node)
  56. {
  57. return _starpu_node_get_kind(node);
  58. }
  59. #undef starpu_memory_nodes_get_count
  60. unsigned starpu_memory_nodes_get_count(void)
  61. {
  62. return _starpu_memory_nodes_get_count();
  63. }
  64. void _starpu_memory_node_get_name(unsigned node, char *name, int size)
  65. {
  66. const char *prefix;
  67. switch (_starpu_descr.nodes[node])
  68. {
  69. case STARPU_CPU_RAM:
  70. prefix = "RAM";
  71. break;
  72. case STARPU_CUDA_RAM:
  73. prefix = "CUDA";
  74. break;
  75. case STARPU_OPENCL_RAM:
  76. prefix = "OpenCL";
  77. break;
  78. case STARPU_DISK_RAM:
  79. prefix = "Disk";
  80. break;
  81. case STARPU_MIC_RAM:
  82. prefix = "MIC";
  83. break;
  84. case STARPU_MPI_MS_RAM:
  85. prefix = "MPI_MS";
  86. break;
  87. case STARPU_SCC_RAM:
  88. prefix = "SCC_RAM";
  89. break;
  90. case STARPU_SCC_SHM:
  91. prefix = "SCC_shared";
  92. break;
  93. case STARPU_UNUSED:
  94. default:
  95. prefix = "unknown";
  96. STARPU_ASSERT(0);
  97. }
  98. snprintf(name, size, "%s %u", prefix, _starpu_descr.devid[node]);
  99. }
  100. unsigned _starpu_memory_node_register(enum starpu_node_kind kind, int devid)
  101. {
  102. unsigned node;
  103. /* ATOMIC_ADD returns the new value ... */
  104. node = STARPU_ATOMIC_ADD(&_starpu_descr.nnodes, 1) - 1;
  105. 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);
  106. _starpu_descr.nodes[node] = kind;
  107. _STARPU_TRACE_NEW_MEM_NODE(node);
  108. _starpu_descr.devid[node] = devid;
  109. /* for now, there is no condition associated to that newly created node */
  110. _starpu_descr.condition_count[node] = 0;
  111. _starpu_malloc_init(node);
  112. return node;
  113. }
  114. /* TODO move in a more appropriate file !! */
  115. /* Register a condition variable associated to worker which is associated to a
  116. * memory node itself. */
  117. void _starpu_memory_node_register_condition(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex, unsigned nodeid)
  118. {
  119. unsigned cond_id;
  120. unsigned nconds_total, nconds;
  121. STARPU_PTHREAD_RWLOCK_WRLOCK(&_starpu_descr.conditions_rwlock);
  122. /* we only insert the queue if it's not already in the list */
  123. nconds = _starpu_descr.condition_count[nodeid];
  124. for (cond_id = 0; cond_id < nconds; cond_id++)
  125. {
  126. if (_starpu_descr.conditions_attached_to_node[nodeid][cond_id].cond == cond)
  127. {
  128. STARPU_ASSERT(_starpu_descr.conditions_attached_to_node[nodeid][cond_id].mutex == mutex);
  129. /* the condition is already in the list */
  130. STARPU_PTHREAD_RWLOCK_UNLOCK(&_starpu_descr.conditions_rwlock);
  131. return;
  132. }
  133. }
  134. /* it was not found locally */
  135. _starpu_descr.conditions_attached_to_node[nodeid][cond_id].cond = cond;
  136. _starpu_descr.conditions_attached_to_node[nodeid][cond_id].mutex = mutex;
  137. _starpu_descr.condition_count[nodeid]++;
  138. /* do we have to add it in the global list as well ? */
  139. nconds_total = _starpu_descr.total_condition_count;
  140. for (cond_id = 0; cond_id < nconds_total; cond_id++)
  141. {
  142. if (_starpu_descr.conditions_all[cond_id].cond == cond)
  143. {
  144. /* the queue is already in the global list */
  145. STARPU_PTHREAD_RWLOCK_UNLOCK(&_starpu_descr.conditions_rwlock);
  146. return;
  147. }
  148. }
  149. /* it was not in the global list either */
  150. _starpu_descr.conditions_all[nconds_total].cond = cond;
  151. _starpu_descr.conditions_all[nconds_total].mutex = mutex;
  152. _starpu_descr.total_condition_count++;
  153. STARPU_PTHREAD_RWLOCK_UNLOCK(&_starpu_descr.conditions_rwlock);
  154. }
  155. #undef starpu_worker_get_memory_node
  156. unsigned starpu_worker_get_memory_node(unsigned workerid)
  157. {
  158. return _starpu_worker_get_memory_node(workerid);
  159. }
  160. /* same utility as _starpu_memory_node_add_nworkers */
  161. void _starpu_worker_drives_memory_node(struct _starpu_worker *worker, unsigned memnode)
  162. {
  163. _starpu_worker_drives_memory[worker->workerid][memnode] = 1;
  164. #ifdef STARPU_SIMGRID
  165. starpu_pthread_queue_register(&worker->wait, &_starpu_simgrid_transfer_queue[memnode]);
  166. #endif
  167. }