memory_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013, 2015 CNRS
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/utils.h>
  18. #include <common/thread.h>
  19. #include <datawizard/memory_manager.h>
  20. #include <starpu_stdlib.h>
  21. static size_t global_size[STARPU_MAXNODES];
  22. static size_t used_size[STARPU_MAXNODES];
  23. /* This is used as an optimization to avoid to wake up allocating threads for
  24. * each and every deallocation, only to find that there is still not enough
  25. * room. */
  26. /* Minimum amount being waited for */
  27. static size_t waiting_size[STARPU_MAXNODES];
  28. static starpu_pthread_mutex_t lock_nodes[STARPU_MAXNODES];
  29. static starpu_pthread_cond_t cond_nodes[STARPU_MAXNODES];
  30. int _starpu_memory_manager_init()
  31. {
  32. int i;
  33. for(i=0 ; i<STARPU_MAXNODES ; i++)
  34. {
  35. global_size[i] = 0;
  36. used_size[i] = 0;
  37. /* This is accessed for statistics outside the lock, don't care
  38. * about that */
  39. STARPU_HG_DISABLE_CHECKING(used_size[i]);
  40. waiting_size[i] = 0;
  41. STARPU_PTHREAD_MUTEX_INIT(&lock_nodes[i], NULL);
  42. STARPU_PTHREAD_COND_INIT(&cond_nodes[i], NULL);
  43. }
  44. return 0;
  45. }
  46. void _starpu_memory_manager_set_global_memory_size(unsigned node, size_t size)
  47. {
  48. STARPU_PTHREAD_MUTEX_LOCK(&lock_nodes[node]);
  49. if (!global_size[node])
  50. {
  51. global_size[node] = size;
  52. _STARPU_DEBUG("Global size for node %d is %ld\n", node, (long)global_size[node]);
  53. }
  54. else
  55. {
  56. STARPU_ASSERT(global_size[node] == size);
  57. }
  58. STARPU_PTHREAD_MUTEX_UNLOCK(&lock_nodes[node]);
  59. }
  60. size_t _starpu_memory_manager_get_global_memory_size(unsigned node)
  61. {
  62. return global_size[node];
  63. }
  64. int starpu_memory_allocate(unsigned node, size_t size, int flags)
  65. {
  66. int ret;
  67. STARPU_PTHREAD_MUTEX_LOCK(&lock_nodes[node]);
  68. if (flags & STARPU_MEMORY_WAIT)
  69. {
  70. while (used_size[node] + size > global_size[node])
  71. {
  72. /* Tell deallocators we need this amount */
  73. if (!waiting_size[node] || size < waiting_size[node])
  74. waiting_size[node] = size;
  75. /* Wait for it */
  76. STARPU_PTHREAD_COND_WAIT(&cond_nodes[node], &lock_nodes[node]);
  77. }
  78. /* And take it */
  79. used_size[node] += size;
  80. ret = 0;
  81. }
  82. else if (flags & STARPU_MEMORY_OVERFLOW
  83. || global_size[node] == 0
  84. || used_size[node] + size <= global_size[node])
  85. {
  86. used_size[node] += size;
  87. ret = 0;
  88. }
  89. else
  90. {
  91. ret = -ENOMEM;
  92. }
  93. STARPU_PTHREAD_MUTEX_UNLOCK(&lock_nodes[node]);
  94. return ret;
  95. }
  96. void starpu_memory_deallocate(unsigned node, size_t size)
  97. {
  98. STARPU_PTHREAD_MUTEX_LOCK(&lock_nodes[node]);
  99. used_size[node] -= size;
  100. /* If there's now room for waiters, wake them */
  101. if (waiting_size[node] &&
  102. global_size[node] - used_size[node] >= waiting_size[node])
  103. {
  104. /* And have those not happy enough tell us the size again */
  105. waiting_size[node] = 0;
  106. STARPU_PTHREAD_COND_BROADCAST(&cond_nodes[node]);
  107. }
  108. STARPU_PTHREAD_MUTEX_UNLOCK(&lock_nodes[node]);
  109. }
  110. starpu_ssize_t starpu_memory_get_total(unsigned node)
  111. {
  112. if (global_size[node] == 0)
  113. return -1;
  114. else
  115. return global_size[node];
  116. }
  117. starpu_ssize_t starpu_memory_get_available(unsigned node)
  118. {
  119. starpu_ssize_t ret;
  120. if (global_size[node] == 0)
  121. return -1;
  122. ret = global_size[node] - used_size[node];
  123. return ret;
  124. }
  125. void starpu_memory_wait_available(unsigned node, size_t size)
  126. {
  127. STARPU_PTHREAD_MUTEX_LOCK(&lock_nodes[node]);
  128. while (used_size[node] + size > global_size[node])
  129. {
  130. /* Tell deallocators we need this amount */
  131. if (!waiting_size[node] || size < waiting_size[node])
  132. waiting_size[node] = size;
  133. /* Wait for it */
  134. STARPU_PTHREAD_COND_WAIT(&cond_nodes[node], &lock_nodes[node]);
  135. }
  136. STARPU_PTHREAD_MUTEX_UNLOCK(&lock_nodes[node]);
  137. }
  138. int _starpu_memory_manager_test_allocate_size(unsigned node, size_t size)
  139. {
  140. int ret;
  141. STARPU_PTHREAD_MUTEX_LOCK(&lock_nodes[node]);
  142. if (global_size[node] == 0)
  143. ret = 1;
  144. else if (used_size[node] + size <= global_size[node])
  145. ret = 1;
  146. else
  147. ret = 0;
  148. STARPU_PTHREAD_MUTEX_UNLOCK(&lock_nodes[node]);
  149. return ret;
  150. }