gpu_concurrency.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(STARPU_ATTRIBUTE_UNUSED void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  37. {
  38. long_kernel_cuda(NITERS);
  39. }
  40. void codelet_long_kernel_sync(STARPU_ATTRIBUTE_UNUSED void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  41. {
  42. long_kernel_cuda(NITERS);
  43. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  44. }
  45. static struct starpu_perfmodel model_async =
  46. {
  47. .type = STARPU_HISTORY_BASED,
  48. .symbol = "long_kernel_async",
  49. };
  50. static struct starpu_perfmodel model_sync =
  51. {
  52. .type = STARPU_HISTORY_BASED,
  53. .symbol = "long_kernel_sync",
  54. };
  55. static struct starpu_codelet cl_async =
  56. {
  57. .cuda_funcs = {codelet_long_kernel_async},
  58. .cuda_flags = {STARPU_CUDA_ASYNC},
  59. .nbuffers = 0,
  60. .model = &model_async,
  61. };
  62. static struct starpu_codelet cl =
  63. {
  64. .cuda_funcs = {codelet_long_kernel_sync},
  65. .nbuffers = 0,
  66. .model = &model_sync,
  67. };
  68. #endif
  69. int main(int argc, char **argv)
  70. {
  71. #ifndef STARPU_USE_CUDA
  72. return STARPU_TEST_SKIPPED;
  73. #else
  74. setenv("STARPU_NWORKER_PER_CUDA", "4", 1);
  75. int ret = starpu_initialize(NULL, &argc, &argv);
  76. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  78. if (starpu_cuda_worker_get_count() == 0)
  79. {
  80. starpu_shutdown();
  81. return STARPU_TEST_SKIPPED;
  82. }
  83. unsigned iter;
  84. for (iter = 0; iter < NTASKS; iter++)
  85. {
  86. struct starpu_task *task = starpu_task_create();
  87. if (!(iter % SYNC))
  88. /* Insert a synchronous task, just for fun */
  89. task->cl = &cl;
  90. else
  91. task->cl = &cl_async;
  92. ret = starpu_task_submit(task);
  93. if (ret == -ENODEV) goto enodev;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  95. }
  96. starpu_shutdown();
  97. STARPU_RETURN(EXIT_SUCCESS);
  98. enodev:
  99. fprintf(stderr, "WARNING: No one can execute this task\n");
  100. /* yes, we do not perform the computation but we did detect that no one
  101. * could perform the kernel, so this is not an error from StarPU */
  102. starpu_shutdown();
  103. STARPU_RETURN(STARPU_TEST_SKIPPED);
  104. #endif
  105. }