allocate.c 6.7 KB

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