allocate.c 6.8 KB

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