multithreaded_init.c 2.2 KB

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