overlap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #define NTASKS 10000
  22. #define VECTORSIZE 1024
  23. #define TASKDURATION 24U
  24. #define SYMBOL "sleep"
  25. static pthread_mutex_t mutex;
  26. static pthread_cond_t cond;
  27. static unsigned finished = 0;
  28. static unsigned cnt = NTASKS;
  29. starpu_data_handle handle;
  30. static float *buffer;
  31. static void callback(void *arg)
  32. {
  33. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  34. if (res == 0)
  35. {
  36. pthread_mutex_lock(&mutex);
  37. finished = 1;
  38. pthread_cond_signal(&cond);
  39. pthread_mutex_unlock(&mutex);
  40. }
  41. }
  42. static void codelet_sleep(void *descr[], __attribute__ ((unused)) void *_args)
  43. {
  44. usleep(TASKDURATION);
  45. }
  46. static struct starpu_perfmodel_t model = {
  47. .type = STARPU_HISTORY_BASED,
  48. .symbol = NULL /* to be defined later */
  49. };
  50. static starpu_codelet cl = {
  51. .where = STARPU_CPU|STARPU_CUDA,
  52. .cpu_func = codelet_sleep,
  53. .cuda_func = codelet_sleep,
  54. .nbuffers = 1,
  55. .model = &model
  56. };
  57. static char symbolname[128];
  58. int main(int argc, char **argv)
  59. {
  60. unsigned i;
  61. starpu_init(NULL);
  62. /* create data */
  63. starpu_malloc_pinned_if_possible((void **)&buffer, NTASKS*VECTORSIZE*sizeof(char));
  64. /* declare data to StarPU */
  65. starpu_register_vector_data(&handle, 0, (uintptr_t)buffer,
  66. NTASKS*VECTORSIZE, sizeof(char));
  67. starpu_filter f =
  68. {
  69. .filter_func = starpu_block_filter_func_vector,
  70. .filter_arg = NTASKS
  71. };
  72. starpu_partition_data(handle, &f);
  73. snprintf(symbolname, 128, "overlap_sleep_%d_%d", VECTORSIZE, TASKDURATION);
  74. model.symbol = symbolname;
  75. unsigned iter;
  76. for (iter = 0; iter < NTASKS; iter++)
  77. {
  78. struct starpu_task *task = starpu_task_create();
  79. task->cl = &cl;
  80. task->buffers[0].handle = starpu_get_sub_data(handle, 1, iter);
  81. task->buffers[0].mode = STARPU_R;
  82. task->callback_func = callback;
  83. task->callback_arg = NULL;
  84. int ret = starpu_submit_task(task);
  85. if (ret == -ENODEV)
  86. goto enodev;
  87. }
  88. pthread_mutex_lock(&mutex);
  89. if (!finished)
  90. pthread_cond_wait(&cond, &mutex);
  91. pthread_mutex_unlock(&mutex);
  92. starpu_shutdown();
  93. return 0;
  94. enodev:
  95. fprintf(stderr, "WARNING: No one can execute this task\n");
  96. /* yes, we do not perform the computation but we did detect that no one
  97. * could perform the kernel, so this is not an error from StarPU */
  98. return 0;
  99. }