memory_nodes.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <pthread.h>
  18. #include <common/config.h>
  19. #include <core/sched_policy.h>
  20. #include <datawizard/datastats.h>
  21. #include <common/fxt.h>
  22. #include "copy_driver.h"
  23. #include "memalloc.h"
  24. static struct _starpu_mem_node_descr descr;
  25. static _starpu_pthread_key_t memory_node_key;
  26. void _starpu_init_memory_nodes(void)
  27. {
  28. /* there is no node yet, subsequent nodes will be
  29. * added using _starpu_register_memory_node */
  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. pthread_rwlock_init(&descr.conditions_rwlock, NULL);
  41. descr.total_condition_count = 0;
  42. }
  43. void _starpu_deinit_memory_nodes(void)
  44. {
  45. _starpu_deinit_data_request_lists();
  46. _starpu_deinit_mem_chunk_lists();
  47. _STARPU_PTHREAD_KEY_DELETE(memory_node_key);
  48. }
  49. void _starpu_set_local_memory_node_key(unsigned *node)
  50. {
  51. _STARPU_PTHREAD_SETSPECIFIC(memory_node_key, node);
  52. }
  53. unsigned _starpu_get_local_memory_node(void)
  54. {
  55. unsigned *memory_node;
  56. memory_node = (unsigned *) _STARPU_PTHREAD_GETSPECIFIC(memory_node_key);
  57. /* in case this is called by the programmer, we assume the RAM node
  58. is the appropriate memory node ... so we return 0 XXX */
  59. if (STARPU_UNLIKELY(!memory_node))
  60. return 0;
  61. return *memory_node;
  62. }
  63. void _starpu_memory_node_worker_add(unsigned node)
  64. {
  65. descr.nworkers[node]++;
  66. }
  67. unsigned _starpu_memory_node_workers(unsigned node)
  68. {
  69. return descr.nworkers[node];
  70. }
  71. struct _starpu_mem_node_descr *_starpu_get_memory_node_description(void)
  72. {
  73. return &descr;
  74. }
  75. enum starpu_node_kind starpu_node_get_kind(uint32_t node)
  76. {
  77. return descr.nodes[node];
  78. }
  79. int _starpu_memory_node_to_devid(unsigned node)
  80. {
  81. return descr.devid[node];
  82. }
  83. unsigned starpu_memory_nodes_get_count(void)
  84. {
  85. return descr.nnodes;
  86. }
  87. unsigned _starpu_register_memory_node(enum starpu_node_kind kind, int devid)
  88. {
  89. unsigned nnodes;
  90. /* ATOMIC_ADD returns the new value ... */
  91. nnodes = STARPU_ATOMIC_ADD(&descr.nnodes, 1);
  92. descr.nodes[nnodes-1] = kind;
  93. _STARPU_TRACE_NEW_MEM_NODE(nnodes-1);
  94. descr.devid[nnodes-1] = devid;
  95. /* for now, there is no condition associated to that newly created node */
  96. descr.condition_count[nnodes-1] = 0;
  97. return (nnodes-1);
  98. }
  99. /* TODO move in a more appropriate file !! */
  100. /* Register a condition variable associated to worker which is associated to a
  101. * memory node itself. */
  102. void _starpu_memory_node_register_condition(_starpu_pthread_cond_t *cond, _starpu_pthread_mutex_t *mutex, unsigned nodeid)
  103. {
  104. unsigned cond_id;
  105. unsigned nconds_total, nconds;
  106. _STARPU_PTHREAD_RWLOCK_WRLOCK(&descr.conditions_rwlock);
  107. /* we only insert the queue if it's not already in the list */
  108. nconds = descr.condition_count[nodeid];
  109. for (cond_id = 0; cond_id < nconds; cond_id++)
  110. {
  111. if (descr.conditions_attached_to_node[nodeid][cond_id].cond == cond)
  112. {
  113. STARPU_ASSERT(descr.conditions_attached_to_node[nodeid][cond_id].mutex == mutex);
  114. /* the condition is already in the list */
  115. _STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  116. return;
  117. }
  118. }
  119. /* it was not found locally */
  120. descr.conditions_attached_to_node[nodeid][cond_id].cond = cond;
  121. descr.conditions_attached_to_node[nodeid][cond_id].mutex = mutex;
  122. descr.condition_count[nodeid]++;
  123. /* do we have to add it in the global list as well ? */
  124. nconds_total = descr.total_condition_count;
  125. for (cond_id = 0; cond_id < nconds_total; cond_id++)
  126. {
  127. if (descr.conditions_all[cond_id].cond == cond)
  128. {
  129. /* the queue is already in the global list */
  130. _STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  131. return;
  132. }
  133. }
  134. /* it was not in the global list either */
  135. descr.conditions_all[nconds_total].cond = cond;
  136. descr.conditions_all[nconds_total].mutex = mutex;
  137. descr.total_condition_count++;
  138. _STARPU_PTHREAD_RWLOCK_UNLOCK(&descr.conditions_rwlock);
  139. }
  140. unsigned starpu_worker_get_memory_node(unsigned workerid)
  141. {
  142. struct _starpu_machine_config *config = _starpu_get_machine_config();
  143. /* This workerid may either be a basic worker or a combined worker */
  144. unsigned nworkers = config->topology.nworkers;
  145. if (workerid < config->topology.nworkers)
  146. return config->workers[workerid].memory_node;
  147. /* We have a combined worker */
  148. unsigned ncombinedworkers = config->topology.ncombinedworkers;
  149. STARPU_ASSERT(workerid < ncombinedworkers + nworkers);
  150. return config->combined_workers[workerid - nworkers].memory_node;
  151. }