overlap.c 3.7 KB

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