overlap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011 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 <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include <pthread.h>
  24. #include "../common/helper.h"
  25. #define NTASKS 10000
  26. #define VECTORSIZE 1024
  27. #define TASKDURATION 24U
  28. #define SYMBOL "sleep"
  29. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  30. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  31. static unsigned finished = 0;
  32. static unsigned cnt = NTASKS;
  33. static void callback(void *arg)
  34. {
  35. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  36. if (res == 0)
  37. {
  38. PTHREAD_MUTEX_LOCK(&mutex);
  39. finished = 1;
  40. PTHREAD_COND_SIGNAL(&cond);
  41. PTHREAD_MUTEX_UNLOCK(&mutex);
  42. }
  43. }
  44. static void codelet_sleep(void *descr[], __attribute__ ((unused)) void *_args)
  45. {
  46. usleep(TASKDURATION);
  47. }
  48. static struct starpu_perfmodel_t model = {
  49. .type = STARPU_HISTORY_BASED,
  50. .symbol = NULL /* to be defined later */
  51. };
  52. static starpu_codelet cl = {
  53. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  54. .cpu_func = codelet_sleep,
  55. .cuda_func = codelet_sleep,
  56. .opencl_func = codelet_sleep,
  57. .nbuffers = 1,
  58. .model = &model
  59. };
  60. static char symbolname[128];
  61. int main(int argc, char **argv)
  62. {
  63. int ret;
  64. starpu_data_handle handle;
  65. float *buffer;
  66. ret = starpu_init(NULL);
  67. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  68. /* create data */
  69. starpu_malloc((void **)&buffer, NTASKS*VECTORSIZE*sizeof(char));
  70. /* declare data to StarPU */
  71. starpu_vector_data_register(&handle, 0, (uintptr_t)buffer,
  72. NTASKS*VECTORSIZE, sizeof(char));
  73. struct starpu_data_filter f =
  74. {
  75. .filter_func = starpu_block_filter_func_vector,
  76. .nchildren = NTASKS
  77. };
  78. starpu_data_partition(handle, &f);
  79. snprintf(symbolname, 128, "overlap_sleep_%d_%u", VECTORSIZE, TASKDURATION);
  80. model.symbol = symbolname;
  81. unsigned iter;
  82. for (iter = 0; iter < NTASKS; iter++)
  83. {
  84. struct starpu_task *task = starpu_task_create();
  85. task->cl = &cl;
  86. task->buffers[0].handle = starpu_data_get_sub_data(handle, 1, iter);
  87. task->buffers[0].mode = STARPU_R;
  88. task->callback_func = callback;
  89. task->callback_arg = NULL;
  90. int ret = starpu_task_submit(task);
  91. if (ret == -ENODEV) goto enodev;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  93. }
  94. PTHREAD_MUTEX_LOCK(&mutex);
  95. if (!finished)
  96. PTHREAD_COND_WAIT(&cond, &mutex);
  97. PTHREAD_MUTEX_UNLOCK(&mutex);
  98. starpu_data_unregister(handle);
  99. starpu_free(buffer);
  100. starpu_shutdown();
  101. return EXIT_SUCCESS;
  102. enodev:
  103. starpu_data_unregister(handle);
  104. starpu_free(buffer);
  105. fprintf(stderr, "WARNING: No one can execute this task\n");
  106. /* yes, we do not perform the computation but we did detect that no one
  107. * could perform the kernel, so this is not an error from StarPU */
  108. starpu_shutdown();
  109. return 77;
  110. }