allocate.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015,2017 CNRS
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2013-2017 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. #include <stdlib.h>
  21. #include <datawizard/memory_manager.h>
  22. /* Stress data allocation on a GPU, triggering eviction */
  23. #define SIZE_LIMIT 128
  24. #define STR_LIMIT "128"
  25. #define SIZE_ALLOC 128
  26. #if !defined(STARPU_HAVE_SETENV)
  27. #warning setenv is not defined. Skipping test
  28. int main(int argc, char **argv)
  29. {
  30. return STARPU_TEST_SKIPPED;
  31. }
  32. #else
  33. static
  34. int test_prefetch(unsigned memnodes)
  35. {
  36. float *buffers[4];
  37. starpu_data_handle_t handles[4];
  38. unsigned i;
  39. starpu_ssize_t available_size;
  40. if (starpu_get_env_number_default("STARPU_DIDUSE_BARRIER", 0))
  41. /* This would hang */
  42. return STARPU_TEST_SKIPPED;
  43. buffers[0] = malloc(SIZE_ALLOC*1024*512);
  44. STARPU_ASSERT(buffers[0]);
  45. /* Prefetch half the memory */
  46. starpu_variable_data_register(&handles[0], STARPU_MAIN_RAM, (uintptr_t)buffers[0], SIZE_ALLOC*1024*512);
  47. for(i=1 ; i<memnodes ; i++)
  48. {
  49. starpu_data_prefetch_on_node(handles[0], i, 0);
  50. }
  51. for(i=1 ; i<memnodes ; i++)
  52. {
  53. available_size = starpu_memory_get_available(i);
  54. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  55. STARPU_CHECK_RETURN_VALUE_IS((int) available_size, SIZE_ALLOC*1024*512, "starpu_memory_get_available (node %u)", i);
  56. }
  57. /* Prefetch a quarter of the memory */
  58. buffers[1] = malloc(SIZE_ALLOC*1024*256);
  59. STARPU_ASSERT(buffers[1]);
  60. starpu_variable_data_register(&handles[1], STARPU_MAIN_RAM, (uintptr_t)buffers[1], SIZE_ALLOC*1024*256);
  61. for(i=1 ; i<memnodes ; i++)
  62. {
  63. starpu_data_prefetch_on_node(handles[1], i, 0);
  64. }
  65. for(i=1 ; i<memnodes ; i++)
  66. {
  67. available_size = starpu_memory_get_available(i);
  68. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  69. STARPU_CHECK_RETURN_VALUE_IS((int)available_size, SIZE_ALLOC*1024*256, "starpu_memory_get_available (node %u)", i);
  70. }
  71. /* Fetch a bit more than half of the memory, it should be able to push previous data out */
  72. buffers[2] = malloc(SIZE_ALLOC*1024*600);
  73. STARPU_ASSERT(buffers[2]);
  74. starpu_variable_data_register(&handles[2], STARPU_MAIN_RAM, (uintptr_t)buffers[2], SIZE_ALLOC*1024*600);
  75. for(i=1 ; i<memnodes ; i++)
  76. {
  77. starpu_data_fetch_on_node(handles[2], i, 0);
  78. }
  79. for(i=1 ; i<memnodes ; i++)
  80. {
  81. available_size = starpu_memory_get_available(i);
  82. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  83. // here, we do not know which data has been cleaned, we cannot test the exact amout of available memory
  84. STARPU_CHECK_RETURN_VALUE((available_size == 0), "starpu_memory_get_available (node %u)", i);
  85. }
  86. /* Fetch half of the memory, it should be able to push previous data out */
  87. buffers[3] = malloc(SIZE_ALLOC*1024*512);
  88. STARPU_ASSERT(buffers[3]);
  89. starpu_variable_data_register(&handles[3], STARPU_MAIN_RAM, (uintptr_t)buffers[3], SIZE_ALLOC*1024*512);
  90. for(i=0 ; i<memnodes ; i++)
  91. {
  92. starpu_data_fetch_on_node(handles[3], i, 0);
  93. }
  94. for(i=1 ; i<memnodes ; i++)
  95. {
  96. available_size = starpu_memory_get_available(i);
  97. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  98. STARPU_CHECK_RETURN_VALUE_IS((int)available_size, SIZE_ALLOC*1024*512, "starpu_memory_get_available (node %u)", i);
  99. }
  100. for(i=0 ; i<4 ; i++)
  101. {
  102. starpu_data_unregister(handles[i]);
  103. free(buffers[i]);
  104. }
  105. for(i=1 ; i<memnodes ; i++)
  106. {
  107. available_size = starpu_memory_get_available(i);
  108. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  109. /* STARPU_CHECK_RETURN_VALUE_IS((int)available_size, SIZE_ALLOC*1024*1024, "starpu_memory_get_available (node %u)", i); */
  110. }
  111. return 0;
  112. }
  113. static
  114. void test_malloc()
  115. {
  116. int ret;
  117. float *buffer;
  118. float *buffer2;
  119. float *buffer3;
  120. /* Allocate one byte */
  121. ret = starpu_malloc_flags((void **)&buffer, 1, STARPU_MALLOC_COUNT);
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  123. FPRINTF(stderr, "Allocation succesfull for 1 b\n");
  124. /* Allocate half the memory */
  125. ret = starpu_malloc_flags((void **)&buffer2, SIZE_ALLOC*1024*512, STARPU_MALLOC_COUNT);
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  127. FPRINTF(stderr, "Allocation succesfull for %d b\n", SIZE_ALLOC*1024*512);
  128. /* Try to allocate the other half, should fail */
  129. ret = starpu_malloc_flags((void **)&buffer3, SIZE_ALLOC*1024*512, STARPU_MALLOC_COUNT);
  130. STARPU_CHECK_RETURN_VALUE_IS(ret, -ENOMEM, "starpu_malloc_flags");
  131. FPRINTF(stderr, "Allocation failed for %d b\n", SIZE_ALLOC*1024*512);
  132. /* Try to allocate the other half without counting it, should succeed */
  133. ret = starpu_malloc_flags((void **)&buffer3, SIZE_ALLOC*1024*512, 0);
  134. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  135. FPRINTF(stderr, "Allocation successful for %d b\n", SIZE_ALLOC*1024*512);
  136. starpu_free_flags(buffer3, SIZE_ALLOC*1024*512, 0);
  137. /* Free the initial half-memory allocation */
  138. starpu_free_flags(buffer2, SIZE_ALLOC*1024*512, STARPU_MALLOC_COUNT);
  139. FPRINTF(stderr, "Freeing %d b\n", SIZE_ALLOC*1024*512);
  140. /* Should not be able to allocate half the memory again */
  141. ret = starpu_malloc_flags((void **)&buffer3, SIZE_ALLOC*1024*512, STARPU_MALLOC_COUNT);
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  143. FPRINTF(stderr, "Allocation succesfull for %d b\n", SIZE_ALLOC*1024*512);
  144. starpu_free_flags(buffer3, SIZE_ALLOC*1024*512, STARPU_MALLOC_COUNT);
  145. starpu_free_flags(buffer, 1, STARPU_MALLOC_COUNT);
  146. }
  147. int main(void)
  148. {
  149. int ret;
  150. unsigned memnodes, i;
  151. setenv("STARPU_LIMIT_CUDA_MEM", STR_LIMIT, 1);
  152. setenv("STARPU_LIMIT_OPENCL_MEM", STR_LIMIT, 1);
  153. setenv("STARPU_LIMIT_CPU_MEM", STR_LIMIT, 1);
  154. ret = starpu_init(NULL);
  155. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  156. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  157. memnodes = starpu_memory_nodes_get_count();
  158. for(i=0 ; i<memnodes ; i++)
  159. {
  160. starpu_ssize_t available_size;
  161. available_size = starpu_memory_get_available(i);
  162. if (available_size == -1)
  163. {
  164. FPRINTF(stderr, "Global memory size for node %u unavailable, skip the test\n", i);
  165. starpu_shutdown();
  166. return STARPU_TEST_SKIPPED;
  167. }
  168. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  169. STARPU_CHECK_RETURN_VALUE_IS((int)available_size, SIZE_LIMIT*1024*1024, "starpu_memory_get_available (node %u)", i);
  170. }
  171. test_malloc();
  172. ret = test_prefetch(memnodes);
  173. starpu_shutdown();
  174. return ret;
  175. }
  176. #endif