reclaim.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012, 2015-2017 Université de Bordeaux
  4. * Copyright (C) 2012, 2016, 2017 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <assert.h>
  18. #include <starpu.h>
  19. #include <common/config.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(int argc, char **argv)
  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. size = root->memory.total_memory;
  51. hwloc_topology_destroy(hwtopology);
  52. return size;
  53. }
  54. #endif
  55. void dummy_func(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  56. {
  57. }
  58. static unsigned int i = 0;
  59. void f(void *arg)
  60. {
  61. printf("%u\n", ++i);
  62. }
  63. static struct starpu_codelet dummy_cl =
  64. {
  65. .cpu_funcs = {dummy_func},
  66. .cuda_funcs = {dummy_func},
  67. .opencl_funcs = {dummy_func},
  68. .cpu_funcs_name = {"dummy_func"},
  69. .nbuffers = 3,
  70. .modes = {STARPU_RW, STARPU_R, STARPU_R}
  71. };
  72. /* Number of chunks */
  73. static unsigned mb = 16;
  74. int main(int argc, char **argv)
  75. {
  76. unsigned j, taskid;
  77. int ret;
  78. #ifdef STARPU_HAVE_HWLOC
  79. /* We allocate 50% of the memory */
  80. uint64_t total_size = get_total_memory_size();
  81. /* On x86_64-freebsd8.2, hwloc 1.3 returns 0 as the total memory
  82. * size, so sanity-check what we have. */
  83. if (total_size > 0)
  84. mb = (int)((0.50 * total_size)/(BLOCK_SIZE));
  85. #endif
  86. setenv("STARPU_LIMIT_OPENCL_MEM", "1000", 1);
  87. ret = starpu_initialize(NULL, &argc, &argv);
  88. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  90. /* An optional argument indicates the number of MB to allocate */
  91. if (argc > 1)
  92. mb = atoi(argv[1]);
  93. if (2*mb > ntasks)
  94. ntasks = 2*mb;
  95. #ifdef STARPU_QUICK_CHECK
  96. mb /= 100;
  97. if (mb == 0)
  98. mb = 1;
  99. #endif
  100. FPRINTF(stderr, "Allocate %u buffers of size %d and create %u tasks\n", mb, BLOCK_SIZE, ntasks);
  101. float **host_ptr_array;
  102. starpu_data_handle_t *handle_array;
  103. host_ptr_array = calloc(mb, sizeof(float *));
  104. STARPU_ASSERT(host_ptr_array);
  105. handle_array = calloc(mb, sizeof(starpu_data_handle_t));
  106. STARPU_ASSERT(handle_array);
  107. /* Register mb buffers of 1MB */
  108. for (j = 0; j < mb; j++)
  109. {
  110. size_t size = starpu_lrand48()%BLOCK_SIZE + 1;
  111. host_ptr_array[j] = calloc(size, 1);
  112. if (host_ptr_array[j] == NULL)
  113. {
  114. mb = j;
  115. FPRINTF(stderr, "Cannot allocate more than %u buffers\n", mb);
  116. break;
  117. }
  118. starpu_variable_data_register(&handle_array[j], STARPU_MAIN_RAM, (uintptr_t)host_ptr_array[j], size);
  119. STARPU_ASSERT(handle_array[j]);
  120. }
  121. for (taskid = 0; taskid < ntasks; taskid++)
  122. {
  123. struct starpu_task *task = starpu_task_create();
  124. task->cl = &dummy_cl;
  125. task->handles[0] = handle_array[taskid%mb];
  126. task->handles[1] = handle_array[(taskid+1)%mb];
  127. task->handles[2] = handle_array[(taskid+2)%mb];
  128. task->callback_func = f;
  129. task->callback_arg = NULL;
  130. ret = starpu_task_submit(task);
  131. if (ret == -ENODEV) goto enodev;
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  133. }
  134. for (j = 0; j < mb; j++)
  135. {
  136. if ( j%20 == 0 )
  137. starpu_data_unregister_submit(handle_array[j]);
  138. }
  139. ret = starpu_task_wait_for_all();
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  141. for (j = 0; j < mb; j++)
  142. {
  143. if ( j%20 != 0 )
  144. starpu_data_unregister(handle_array[j]);
  145. free(host_ptr_array[j]);
  146. }
  147. free(host_ptr_array);
  148. free(handle_array);
  149. starpu_shutdown();
  150. return EXIT_SUCCESS;
  151. enodev:
  152. fprintf(stderr, "WARNING: No one can execute this task\n");
  153. /* yes, we do not perform the computation but we did detect that no one
  154. * could perform the kernel, so this is not an error from StarPU */
  155. starpu_shutdown();
  156. return STARPU_TEST_SKIPPED;
  157. }
  158. #endif