reclaim.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2017 Université de Bordeaux
  4. * Copyright (C) 2012,2013 Inria
  5. * Copyright (C) 2011,2012,2015-2017 CNRS
  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 <assert.h>
  19. #include <starpu.h>
  20. #ifdef STARPU_HAVE_HWLOC
  21. #include <hwloc.h>
  22. #endif
  23. #include "../helper.h"
  24. #if !defined(STARPU_HAVE_SETENV)
  25. #warning setenv is not defined. Skipping test
  26. int main(void)
  27. {
  28. return STARPU_TEST_SKIPPED;
  29. }
  30. #else
  31. /*
  32. * Stress the memory allocation system and force StarPU to reclaim memory from
  33. * time to time.
  34. */
  35. #ifdef STARPU_QUICK_CHECK
  36. # define BLOCK_SIZE (64*1024)
  37. static unsigned ntasks = 250;
  38. #else
  39. # define BLOCK_SIZE (64*1024*1024)
  40. static unsigned ntasks = 1000;
  41. #endif
  42. #ifdef STARPU_HAVE_HWLOC
  43. static uint64_t get_total_memory_size(void)
  44. {
  45. uint64_t size;
  46. hwloc_topology_t hwtopology;
  47. hwloc_topology_init(&hwtopology);
  48. hwloc_topology_load(hwtopology);
  49. hwloc_obj_t root = hwloc_get_root_obj(hwtopology);
  50. #if HWLOC_API_VERSION >= 0x00020000
  51. size = root->total_memory;
  52. #else
  53. size = root->memory.total_memory;
  54. #endif
  55. hwloc_topology_destroy(hwtopology);
  56. return size;
  57. }
  58. #endif
  59. void dummy_func(void *descr[], void *_args)
  60. {
  61. }
  62. static unsigned int i = 0;
  63. void f(void *arg)
  64. {
  65. printf("%u\n", ++i);
  66. }
  67. static struct starpu_codelet dummy_cl =
  68. {
  69. .cpu_funcs = {dummy_func},
  70. .cuda_funcs = {dummy_func},
  71. .opencl_funcs = {dummy_func},
  72. .cpu_funcs_name = {"dummy_func"},
  73. .nbuffers = 3,
  74. .modes = {STARPU_RW, STARPU_R, STARPU_R}
  75. };
  76. /* Number of chunks */
  77. static unsigned mb = 16;
  78. int main(int argc, char **argv)
  79. {
  80. unsigned j, taskid;
  81. int ret;
  82. #ifdef STARPU_HAVE_HWLOC
  83. /* We allocate 50% of the memory */
  84. uint64_t total_size = get_total_memory_size();
  85. /* On x86_64-freebsd8.2, hwloc 1.3 returns 0 as the total memory
  86. * size, so sanity-check what we have. */
  87. if (total_size > 0)
  88. mb = (int)((0.50 * total_size)/(BLOCK_SIZE));
  89. #endif
  90. setenv("STARPU_LIMIT_OPENCL_MEM", "1000", 1);
  91. ret = starpu_initialize(NULL, &argc, &argv);
  92. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  94. /* An optional argument indicates the number of MB to allocate */
  95. if (argc > 1)
  96. mb = atoi(argv[1]);
  97. if (2*mb > ntasks)
  98. ntasks = 2*mb;
  99. #ifdef STARPU_QUICK_CHECK
  100. mb /= 100;
  101. if (mb == 0)
  102. mb = 1;
  103. #endif
  104. FPRINTF(stderr, "Allocate %u buffers of size %d and create %u tasks\n", mb, BLOCK_SIZE, ntasks);
  105. float **host_ptr_array;
  106. starpu_data_handle_t *handle_array;
  107. host_ptr_array = calloc(mb, sizeof(float *));
  108. STARPU_ASSERT(host_ptr_array);
  109. handle_array = calloc(mb, sizeof(starpu_data_handle_t));
  110. STARPU_ASSERT(handle_array);
  111. /* Register mb buffers of 1MB */
  112. for (j = 0; j < mb; j++)
  113. {
  114. size_t size = starpu_lrand48()%BLOCK_SIZE + 1;
  115. host_ptr_array[j] = calloc(size, 1);
  116. if (host_ptr_array[j] == NULL)
  117. {
  118. mb = j;
  119. FPRINTF(stderr, "Cannot allocate more than %u buffers\n", mb);
  120. break;
  121. }
  122. starpu_variable_data_register(&handle_array[j], STARPU_MAIN_RAM, (uintptr_t)host_ptr_array[j], size);
  123. STARPU_ASSERT(handle_array[j]);
  124. }
  125. for (taskid = 0; taskid < ntasks; taskid++)
  126. {
  127. struct starpu_task *task = starpu_task_create();
  128. task->cl = &dummy_cl;
  129. task->handles[0] = handle_array[taskid%mb];
  130. task->handles[1] = handle_array[(taskid+1)%mb];
  131. task->handles[2] = handle_array[(taskid+2)%mb];
  132. task->callback_func = f;
  133. task->callback_arg = NULL;
  134. ret = starpu_task_submit(task);
  135. if (ret == -ENODEV) goto enodev;
  136. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  137. }
  138. for (j = 0; j < mb; j++)
  139. {
  140. if ( j%20 == 0 )
  141. starpu_data_unregister_submit(handle_array[j]);
  142. }
  143. ret = starpu_task_wait_for_all();
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  145. for (j = 0; j < mb; j++)
  146. {
  147. if ( j%20 != 0 )
  148. starpu_data_unregister(handle_array[j]);
  149. free(host_ptr_array[j]);
  150. }
  151. free(host_ptr_array);
  152. free(handle_array);
  153. starpu_shutdown();
  154. return EXIT_SUCCESS;
  155. enodev:
  156. fprintf(stderr, "WARNING: No one can execute this task\n");
  157. /* yes, we do not perform the computation but we did detect that no one
  158. * could perform the kernel, so this is not an error from StarPU */
  159. starpu_shutdown();
  160. return STARPU_TEST_SKIPPED;
  161. }
  162. #endif