multithreaded_init.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <stdio.h>
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. /*
  20. * Try calling starpu_initialize from different threads in parallel
  21. */
  22. #define NUM_THREADS 5
  23. int *glob_argc;
  24. char ***glob_argv;
  25. static
  26. void *launch_starpu(void *unused)
  27. {
  28. int ret;
  29. (void) unused;
  30. ret = starpu_initialize(NULL, glob_argc, glob_argv);
  31. if (ret == -ENODEV)
  32. exit(STARPU_TEST_SKIPPED);
  33. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  34. return NULL;
  35. }
  36. static
  37. void *shutdown_starpu(void *unused)
  38. {
  39. (void) unused;
  40. starpu_shutdown();
  41. return NULL;
  42. }
  43. int main(int argc, char **argv)
  44. {
  45. unsigned i;
  46. double timing;
  47. double start;
  48. double end;
  49. glob_argc = &argc;
  50. glob_argv = &argv;
  51. starpu_pthread_t threads[NUM_THREADS];
  52. start = starpu_timing_now();
  53. for (i = 0; i < NUM_THREADS; ++i)
  54. {
  55. STARPU_PTHREAD_CREATE(&threads[i], NULL, launch_starpu, NULL);
  56. }
  57. for (i = 0; i < NUM_THREADS; ++i)
  58. {
  59. STARPU_PTHREAD_JOIN(threads[i], NULL);
  60. }
  61. end = starpu_timing_now();
  62. timing = end - start;
  63. FPRINTF(stderr, "Success : %d threads launching simultaneously starpu_init\n", NUM_THREADS);
  64. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  65. FPRINTF(stderr, "Per task: %f usecs\n", timing/NUM_THREADS);
  66. for (i = 0; i < NUM_THREADS; i++)
  67. {
  68. STARPU_PTHREAD_CREATE(&threads[i], NULL, shutdown_starpu, NULL);
  69. }
  70. for (i = 0; i < NUM_THREADS; i++)
  71. {
  72. STARPU_PTHREAD_JOIN(threads[i], NULL);
  73. }
  74. return EXIT_SUCCESS;
  75. }