allocate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #if !defined(STARPU_HAVE_SETENV)
  21. #warning setenv is not defined. Skipping test
  22. int main(int argc, char **argv)
  23. {
  24. return STARPU_TEST_SKIPPED;
  25. }
  26. #else
  27. int test_prefetch(unsigned memnodes)
  28. {
  29. int ret;
  30. float *buffers[4];
  31. starpu_data_handle_t handles[4];
  32. unsigned i;
  33. ssize_t available_size;
  34. buffers[0] = malloc(1*1024*512);
  35. STARPU_ASSERT(buffers[0]);
  36. starpu_variable_data_register(&handles[0], 0, (uintptr_t)buffers[0], 1*1024*512);
  37. for(i=1 ; i<memnodes ; i++)
  38. {
  39. starpu_data_prefetch_on_node(handles[0], i, 0);
  40. }
  41. for(i=1 ; i<memnodes ; i++)
  42. {
  43. available_size = starpu_memory_get_available(i);
  44. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  45. STARPU_CHECK_RETURN_VALUE_IS((int) available_size, 1*1024*512, "starpu_memory_get_available (node %u)", i);
  46. }
  47. buffers[1] = malloc(1*1024*256);
  48. STARPU_ASSERT(buffers[1]);
  49. starpu_variable_data_register(&handles[1], 0, (uintptr_t)buffers[1], 1*1024*256);
  50. for(i=1 ; i<memnodes ; i++)
  51. {
  52. starpu_data_prefetch_on_node(handles[1], i, 0);
  53. }
  54. for(i=1 ; i<memnodes ; i++)
  55. {
  56. available_size = starpu_memory_get_available(i);
  57. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  58. STARPU_CHECK_RETURN_VALUE_IS((int)available_size, 1*1024*256, "starpu_memory_get_available (node %u)", i);
  59. }
  60. buffers[2] = malloc(1*1024*600);
  61. STARPU_ASSERT(buffers[2]);
  62. starpu_variable_data_register(&handles[2], 0, (uintptr_t)buffers[2], 1*1024*600);
  63. for(i=1 ; i<memnodes ; i++)
  64. {
  65. starpu_data_prefetch_on_node(handles[2], i, 0);
  66. }
  67. for(i=1 ; i<memnodes ; i++)
  68. {
  69. available_size = starpu_memory_get_available(i);
  70. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  71. // here, we do not know which data has been cleaned, we cannot test the exact amout of available memory
  72. STARPU_CHECK_RETURN_VALUE((available_size == 0), "starpu_memory_get_available (node %u)", i);
  73. }
  74. buffers[3] = malloc(1*1024*512);
  75. STARPU_ASSERT(buffers[3]);
  76. starpu_variable_data_register(&handles[3], 0, (uintptr_t)buffers[3], 1*1024*512);
  77. for(i=0 ; i<memnodes ; i++)
  78. {
  79. starpu_data_prefetch_on_node(handles[3], i, 0);
  80. }
  81. for(i=1 ; i<memnodes ; i++)
  82. {
  83. available_size = starpu_memory_get_available(i);
  84. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  85. STARPU_CHECK_RETURN_VALUE_IS((int)available_size, 1*1024*512, "starpu_memory_get_available (node %u)", i);
  86. }
  87. for(i=0 ; i<4 ; i++)
  88. {
  89. free(buffers[i]);
  90. starpu_data_unregister(handles[i]);
  91. }
  92. #ifdef STARPU_DEVEL
  93. #warning is is normal that all memory has not been cleaned here? i was assuming the available memory to be 1G
  94. #endif
  95. // for(i=1 ; i<memnodes ; i++)
  96. // {
  97. // available_size = starpu_memory_get_available(i);
  98. // FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  99. // STARPU_CHECK_RETURN_VALUE_IS((int)available_size, 1*1024*1024, "starpu_memory_get_available (node %u)", i);
  100. // }
  101. return 0;
  102. }
  103. void test_malloc()
  104. {
  105. int ret;
  106. float *buffer;
  107. float *buffer2;
  108. float *buffer3;
  109. size_t global_size;
  110. ret = starpu_malloc_flags((void **)&buffer, 1, STARPU_MALLOC_COUNT);
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  112. FPRINTF(stderr, "Allocation succesfull for 1 b\n");
  113. ret = starpu_malloc_flags((void **)&buffer2, 1*1024*512, STARPU_MALLOC_COUNT);
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  115. FPRINTF(stderr, "Allocation succesfull for %d b\n", 1*1024*512);
  116. ret = starpu_malloc_flags((void **)&buffer3, 1*1024*512, STARPU_MALLOC_COUNT);
  117. STARPU_CHECK_RETURN_VALUE_IS(ret, -ENOMEM, "starpu_malloc_flags");
  118. FPRINTF(stderr, "Allocation failed for %d b\n", 1*1024*512);
  119. ret = starpu_malloc_flags((void **)&buffer3, 1*1024*512, 0);
  120. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  121. FPRINTF(stderr, "Allocation successful for %d b\n", 1*1024*512);
  122. starpu_free_flags(buffer3, 1*1024*512, 0);
  123. starpu_free_flags(buffer2, 1*1024*512, STARPU_MALLOC_COUNT);
  124. FPRINTF(stderr, "Freeing %d b\n", 1*1024*512);
  125. ret = starpu_malloc_flags((void **)&buffer3, 1*1024*512, STARPU_MALLOC_COUNT);
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc_flags");
  127. FPRINTF(stderr, "Allocation succesfull for %d b\n", 1*1024*512);
  128. starpu_free_flags(buffer3, 1*1024*512, STARPU_MALLOC_COUNT);
  129. starpu_free_flags(buffer, 1, STARPU_MALLOC_COUNT);
  130. }
  131. int main(int argc, char **argv)
  132. {
  133. int ret;
  134. unsigned memnodes, i;
  135. ssize_t available_size;
  136. setenv("STARPU_LIMIT_CUDA_MEM", "1", 1);
  137. setenv("STARPU_LIMIT_OPENCL_MEM", "1", 1);
  138. setenv("STARPU_LIMIT_CPU_MEM", "1", 1);
  139. ret = starpu_init(NULL);
  140. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  141. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  142. memnodes = starpu_memory_nodes_get_count();
  143. for(i=0 ; i<memnodes ; i++)
  144. {
  145. available_size = starpu_memory_get_available(i);
  146. if (available_size == -1)
  147. {
  148. FPRINTF(stderr, "Global memory size for node %u unavailable, skip the test\n", i);
  149. starpu_shutdown();
  150. return STARPU_TEST_SKIPPED;
  151. }
  152. FPRINTF(stderr, "Available memory size on node %u: %ld\n", i, available_size);
  153. STARPU_CHECK_RETURN_VALUE_IS((int)available_size, 1*1024*1024, "starpu_memory_get_available (node %u)", i);
  154. }
  155. test_malloc();
  156. ret = test_prefetch(memnodes);
  157. starpu_shutdown();
  158. return ret;
  159. }
  160. #endif