memory_nodes.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012, 2014-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2013 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. #ifndef __MEMORY_NODES_H__
  18. #define __MEMORY_NODES_H__
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <datawizard/coherency.h>
  22. #include <datawizard/memalloc.h>
  23. #include <common/utils.h>
  24. #include <core/workers.h>
  25. #ifdef STARPU_SIMGRID
  26. #include <core/simgrid.h>
  27. #endif
  28. #define _STARPU_MEMORY_NODE_TUPLE(node1,node2) (node1 | (node2 << 4))
  29. #define _STARPU_MEMORY_NODE_TUPLE_FIRST(tuple) (tuple & 0x0F)
  30. #define _STARPU_MEMORY_NODE_TUPLE_SECOND(tuple) (tuple & 0xF0)
  31. extern char _starpu_worker_drives_memory[STARPU_NMAXWORKERS][STARPU_MAXNODES];
  32. struct _starpu_cond_and_mutex
  33. {
  34. starpu_pthread_cond_t *cond;
  35. starpu_pthread_mutex_t *mutex;
  36. };
  37. struct _starpu_memory_node_descr
  38. {
  39. unsigned nnodes;
  40. enum starpu_node_kind nodes[STARPU_MAXNODES];
  41. /* Get the device id associated to this node, or -1 if not applicable */
  42. int devid[STARPU_MAXNODES];
  43. unsigned nworkers[STARPU_MAXNODES];
  44. #ifdef STARPU_SIMGRID
  45. msg_host_t host[STARPU_MAXNODES];
  46. #endif
  47. // TODO move this 2 lists outside struct _starpu_memory_node_descr
  48. /* Every worker is associated to a condition variable on which the
  49. * worker waits when there is task available. It is possible that
  50. * multiple worker share the same condition variable, so we maintain a
  51. * list of all these condition variables so that we can wake up all
  52. * worker attached to a memory node that are waiting on a task. */
  53. starpu_pthread_rwlock_t conditions_rwlock;
  54. struct _starpu_cond_and_mutex conditions_attached_to_node[STARPU_MAXNODES][STARPU_NMAXWORKERS];
  55. struct _starpu_cond_and_mutex conditions_all[STARPU_MAXNODES*STARPU_NMAXWORKERS];
  56. /* the number of queues attached to each node */
  57. unsigned total_condition_count;
  58. unsigned condition_count[STARPU_MAXNODES];
  59. };
  60. extern struct _starpu_memory_node_descr _starpu_descr;
  61. void _starpu_memory_nodes_init(void);
  62. void _starpu_memory_nodes_deinit(void);
  63. extern starpu_pthread_key_t _starpu_memory_node_key STARPU_ATTRIBUTE_INTERNAL;
  64. static inline void _starpu_memory_node_set_local_key(unsigned *node)
  65. {
  66. STARPU_PTHREAD_SETSPECIFIC(_starpu_memory_node_key, node);
  67. }
  68. static inline unsigned _starpu_memory_node_get_local_key(void)
  69. {
  70. unsigned *memory_node;
  71. memory_node = (unsigned *) STARPU_PTHREAD_GETSPECIFIC(_starpu_memory_node_key);
  72. /* in case this is called by the programmer, we assume the RAM node
  73. is the appropriate memory node ... XXX */
  74. if (STARPU_UNLIKELY(!memory_node))
  75. return STARPU_MAIN_RAM;
  76. return *memory_node;
  77. }
  78. static inline void _starpu_memory_node_add_nworkers(unsigned node)
  79. {
  80. _starpu_descr.nworkers[node]++;
  81. }
  82. /* same utility as _starpu_memory_node_add_nworkers */
  83. static inline void _starpu_worker_drives_memory_node(unsigned worker_id, unsigned memnode)
  84. {
  85. _starpu_worker_drives_memory[worker_id][memnode] = 1;
  86. }
  87. static inline unsigned _starpu_memory_node_get_nworkers(unsigned node)
  88. {
  89. return _starpu_descr.nworkers[node];
  90. }
  91. #ifdef STARPU_SIMGRID
  92. static inline void _starpu_simgrid_memory_node_set_host(unsigned node, msg_host_t host)
  93. {
  94. _starpu_descr.host[node] = host;
  95. }
  96. static inline msg_host_t _starpu_simgrid_memory_node_get_host(unsigned node)
  97. {
  98. return _starpu_descr.host[node];
  99. }
  100. #endif
  101. unsigned _starpu_memory_node_register(enum starpu_node_kind kind, int devid);
  102. //void _starpu_memory_node_attach_queue(struct starpu_jobq_s *q, unsigned nodeid);
  103. void _starpu_memory_node_register_condition(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex, unsigned memory_node);
  104. static inline int _starpu_memory_node_get_devid(unsigned node)
  105. {
  106. return _starpu_descr.devid[node];
  107. }
  108. void _starpu_memory_node_get_name(unsigned node, char *name, int size);
  109. static inline struct _starpu_memory_node_descr *_starpu_memory_node_get_description(void)
  110. {
  111. return &_starpu_descr;
  112. }
  113. static inline enum starpu_node_kind _starpu_node_get_kind(unsigned node)
  114. {
  115. return _starpu_descr.nodes[node];
  116. }
  117. #define starpu_node_get_kind _starpu_node_get_kind
  118. static inline unsigned _starpu_memory_nodes_get_count(void)
  119. {
  120. return _starpu_descr.nnodes;
  121. }
  122. #define starpu_memory_nodes_get_count _starpu_memory_nodes_get_count
  123. static inline unsigned _starpu_worker_get_memory_node(unsigned workerid)
  124. {
  125. struct _starpu_machine_config *config = _starpu_get_machine_config();
  126. /* This workerid may either be a basic worker or a combined worker */
  127. unsigned nworkers = config->topology.nworkers;
  128. if (workerid < config->topology.nworkers)
  129. return config->workers[workerid].memory_node;
  130. /* We have a combined worker */
  131. unsigned ncombinedworkers STARPU_ATTRIBUTE_UNUSED = config->topology.ncombinedworkers;
  132. STARPU_ASSERT_MSG(workerid < ncombinedworkers + nworkers, "Bad workerid %u, maximum %u", workerid, ncombinedworkers + nworkers);
  133. return config->combined_workers[workerid - nworkers].memory_node;
  134. }
  135. #define starpu_worker_get_memory_node _starpu_worker_get_memory_node
  136. #endif // __MEMORY_NODES_H__