reclaim.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  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. /*
  17. * This test stress the memory allocation system and should force StarPU to
  18. * reclaim memory from time to time.
  19. */
  20. #include <assert.h>
  21. #include <starpu.h>
  22. #include <common/config.h>
  23. #ifdef STARPU_HAVE_HWLOC
  24. #include <hwloc.h>
  25. #endif
  26. #ifdef STARPU_SLOW_MACHINE
  27. # define BLOCK_SIZE (64*1024)
  28. #else
  29. # define BLOCK_SIZE (64*1024*1024)
  30. #endif
  31. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  32. static unsigned ntasks = 1000;
  33. #ifdef STARPU_HAVE_HWLOC
  34. static uint64_t get_total_memory_size(void)
  35. {
  36. hwloc_topology_t hwtopology;
  37. hwloc_topology_init(&hwtopology);
  38. hwloc_topology_load(hwtopology);
  39. hwloc_obj_t root = hwloc_get_root_obj(hwtopology);
  40. return root->memory.total_memory;
  41. }
  42. #endif
  43. static void dummy_func(void *descr[], __attribute__ ((unused)) void *_args)
  44. {
  45. }
  46. static starpu_codelet dummy_cl = {
  47. .where = STARPU_CPU|STARPU_CUDA,
  48. .cpu_func = dummy_func,
  49. .cuda_func = dummy_func,
  50. .nbuffers = 3
  51. };
  52. /* Number of chunks */
  53. static int mb = 256;
  54. int main(int argc, char **argv)
  55. {
  56. int i;
  57. int taskid;
  58. #ifdef STARPU_HAVE_HWLOC
  59. /* We allocate 50% of the memory */
  60. uint64_t total_size = get_total_memory_size();
  61. mb = (int)((0.50 * total_size)/(BLOCK_SIZE));
  62. #endif
  63. /* An optional argument indicates the number of MB to allocate */
  64. if (argc > 1)
  65. mb = atoi(argv[1]);
  66. if (2*mb > ntasks)
  67. ntasks = 2*mb;
  68. FPRINTF(stderr, "Allocate %d buffers and create %u tasks\n", mb, ntasks);
  69. starpu_init(NULL);
  70. float **host_ptr_array;
  71. starpu_data_handle *handle_array;
  72. host_ptr_array = calloc(mb, sizeof(float *));
  73. handle_array = calloc(mb, sizeof(starpu_data_handle));
  74. /* Register mb buffers of 1MB */
  75. for (i = 0; i < mb; i++)
  76. {
  77. host_ptr_array[i] = malloc(BLOCK_SIZE);
  78. assert(host_ptr_array[i]);
  79. starpu_variable_data_register(&handle_array[i], 0,
  80. (uintptr_t)host_ptr_array[i], BLOCK_SIZE);
  81. assert(handle_array[i]);
  82. }
  83. for (taskid = 0; taskid < ntasks; taskid++)
  84. {
  85. struct starpu_task *task = starpu_task_create();
  86. task->cl = &dummy_cl;
  87. task->buffers[0].handle = handle_array[i%mb];
  88. task->buffers[0].mode = STARPU_RW;
  89. task->buffers[1].handle = handle_array[(i+1)%mb];
  90. task->buffers[1].mode = STARPU_R;
  91. task->buffers[2].handle = handle_array[(i+2)%mb];
  92. task->buffers[2].mode = STARPU_R;
  93. starpu_task_submit(task);
  94. }
  95. starpu_task_wait_for_all();
  96. for (i = 0; i < mb; i++)
  97. {
  98. starpu_data_unregister(handle_array[i]);
  99. free(host_ptr_array[i]);
  100. }
  101. starpu_shutdown();
  102. return 0;
  103. }