reclaim.c 3.0 KB

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