gpu_concurrency.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2016 Université de Bordeaux
  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. #include <config.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include "../helper.h"
  23. #include <common/thread.h>
  24. /*
  25. * Check that concurrency does happen when using multi-stream CUDA.
  26. */
  27. #ifdef STARPU_QUICK_CHECK
  28. #define NITERS 100000
  29. #else
  30. #define NITERS 1000000
  31. #endif
  32. #define NTASKS 64
  33. #define SYNC 16
  34. #ifdef STARPU_USE_CUDA
  35. extern void long_kernel_cuda(unsigned long niters);
  36. void codelet_long_kernel_async(void *descr[], void *arg)
  37. {
  38. (void)descr;
  39. (void)arg;
  40. long_kernel_cuda(NITERS);
  41. }
  42. void codelet_long_kernel_sync(void *descr[], void *arg)
  43. {
  44. (void)descr;
  45. (void)arg;
  46. long_kernel_cuda(NITERS);
  47. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  48. }
  49. static struct starpu_perfmodel model_async =
  50. {
  51. .type = STARPU_HISTORY_BASED,
  52. .symbol = "long_kernel_async",
  53. };
  54. static struct starpu_perfmodel model_sync =
  55. {
  56. .type = STARPU_HISTORY_BASED,
  57. .symbol = "long_kernel_sync",
  58. };
  59. static struct starpu_codelet cl_async =
  60. {
  61. .cuda_funcs = {codelet_long_kernel_async},
  62. .cuda_flags = {STARPU_CUDA_ASYNC},
  63. .nbuffers = 0,
  64. .model = &model_async,
  65. };
  66. static struct starpu_codelet cl =
  67. {
  68. .cuda_funcs = {codelet_long_kernel_sync},
  69. .nbuffers = 0,
  70. .model = &model_sync,
  71. };
  72. #endif
  73. int main(int argc, char **argv)
  74. {
  75. #ifndef STARPU_USE_CUDA
  76. return STARPU_TEST_SKIPPED;
  77. #else
  78. setenv("STARPU_NWORKER_PER_CUDA", "4", 1);
  79. int ret = starpu_initialize(NULL, &argc, &argv);
  80. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  82. if (starpu_cuda_worker_get_count() == 0)
  83. {
  84. starpu_shutdown();
  85. return STARPU_TEST_SKIPPED;
  86. }
  87. unsigned iter;
  88. for (iter = 0; iter < NTASKS; iter++)
  89. {
  90. struct starpu_task *task = starpu_task_create();
  91. if (!(iter % SYNC))
  92. /* Insert a synchronous task, just for fun */
  93. task->cl = &cl;
  94. else
  95. task->cl = &cl_async;
  96. ret = starpu_task_submit(task);
  97. if (ret == -ENODEV) goto enodev;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  99. }
  100. starpu_shutdown();
  101. STARPU_RETURN(EXIT_SUCCESS);
  102. enodev:
  103. fprintf(stderr, "WARNING: No one can execute this task\n");
  104. /* yes, we do not perform the computation but we did detect that no one
  105. * could perform the kernel, so this is not an error from StarPU */
  106. starpu_shutdown();
  107. STARPU_RETURN(STARPU_TEST_SKIPPED);
  108. #endif
  109. }