allocate.c 6.6 KB

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