overlap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  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 <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include "../helper.h"
  24. #include <common/thread.h>
  25. /*
  26. * Check that working on a partitioned vector gets overlapping of prefetches etc.
  27. */
  28. #ifdef STARPU_QUICK_CHECK
  29. #define NTASKS 100
  30. #else
  31. #define NTASKS 10000
  32. #endif
  33. #define VECTORSIZE 1024
  34. #define TASKDURATION 24U
  35. #define SYMBOL "sleep"
  36. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  37. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  38. static unsigned finished = 0;
  39. static unsigned cnt = NTASKS;
  40. static void callback(void *arg)
  41. {
  42. (void)arg;
  43. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  44. ANNOTATE_HAPPENS_BEFORE(&cnt);
  45. if (res == 0)
  46. {
  47. ANNOTATE_HAPPENS_AFTER(&cnt);
  48. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  49. finished = 1;
  50. STARPU_PTHREAD_COND_SIGNAL(&cond);
  51. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  52. }
  53. }
  54. void codelet_sleep(void *descr[], void *_args)
  55. {
  56. (void)descr;
  57. (void)_args;
  58. STARPU_SKIP_IF_VALGRIND;
  59. usleep(TASKDURATION);
  60. }
  61. static struct starpu_perfmodel model =
  62. {
  63. .type = STARPU_HISTORY_BASED,
  64. .symbol = NULL /* to be defined later */
  65. };
  66. static struct starpu_codelet cl =
  67. {
  68. .cpu_funcs = {codelet_sleep},
  69. .cuda_funcs = {codelet_sleep},
  70. .opencl_funcs = {codelet_sleep},
  71. .cpu_funcs_name = {"codelet_sleep"},
  72. .nbuffers = 1,
  73. .modes = {STARPU_R},
  74. .model = &model
  75. };
  76. static char symbolname[128];
  77. int main(int argc, char **argv)
  78. {
  79. int ret;
  80. starpu_data_handle_t handle;
  81. float *buffer;
  82. ret = starpu_initialize(NULL, &argc, &argv);
  83. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  85. /* create data */
  86. starpu_malloc((void **)&buffer, NTASKS*VECTORSIZE*sizeof(char));
  87. /* declare data to StarPU */
  88. starpu_vector_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)buffer,
  89. NTASKS*VECTORSIZE, sizeof(char));
  90. struct starpu_data_filter f =
  91. {
  92. .filter_func = starpu_vector_filter_block,
  93. .nchildren = NTASKS
  94. };
  95. starpu_data_partition(handle, &f);
  96. snprintf(symbolname, sizeof(symbolname), "overlap_sleep_%d_%u", VECTORSIZE, TASKDURATION);
  97. model.symbol = symbolname;
  98. unsigned iter;
  99. for (iter = 0; iter < NTASKS; iter++)
  100. {
  101. struct starpu_task *task = starpu_task_create();
  102. task->cl = &cl;
  103. task->handles[0] = starpu_data_get_sub_data(handle, 1, iter);
  104. task->callback_func = callback;
  105. task->callback_arg = NULL;
  106. ret = starpu_task_submit(task);
  107. if (ret == -ENODEV) goto enodev;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  109. }
  110. starpu_do_schedule();
  111. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  112. if (!finished)
  113. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  114. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  115. starpu_data_unpartition(handle, STARPU_MAIN_RAM);
  116. starpu_data_unregister(handle);
  117. starpu_free_noflag(buffer, NTASKS*VECTORSIZE*sizeof(char));
  118. starpu_shutdown();
  119. STARPU_RETURN(EXIT_SUCCESS);
  120. enodev:
  121. starpu_data_unregister(handle);
  122. starpu_free_noflag(buffer, NTASKS*VECTORSIZE*sizeof(char));
  123. fprintf(stderr, "WARNING: No one can execute this task\n");
  124. /* yes, we do not perform the computation but we did detect that no one
  125. * could perform the kernel, so this is not an error from StarPU */
  126. starpu_shutdown();
  127. STARPU_RETURN(STARPU_TEST_SKIPPED);
  128. }