multithreaded_init.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 INRIA
  4. * Copyright (C) 2010-2011, 2014, 2016 Université de Bordeaux
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdio.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Try calling starpu_initialize from different threads in parallel
  22. */
  23. #define NUM_THREADS 5
  24. int *glob_argc;
  25. char ***glob_argv;
  26. static
  27. void *launch_starpu(void *unused)
  28. {
  29. int ret;
  30. (void) unused;
  31. ret = starpu_initialize(NULL, glob_argc, glob_argv);
  32. if (ret == -ENODEV)
  33. exit(STARPU_TEST_SKIPPED);
  34. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  35. return NULL;
  36. }
  37. static
  38. void *shutdown_starpu(void *unused)
  39. {
  40. (void) unused;
  41. starpu_shutdown();
  42. return NULL;
  43. }
  44. int main(int argc, char **argv)
  45. {
  46. unsigned i;
  47. double timing;
  48. double start;
  49. double end;
  50. glob_argc = &argc;
  51. glob_argv = &argv;
  52. starpu_pthread_t threads[NUM_THREADS];
  53. start = starpu_timing_now();
  54. for (i = 0; i < NUM_THREADS; ++i)
  55. {
  56. STARPU_PTHREAD_CREATE(&threads[i], NULL, launch_starpu, NULL);
  57. }
  58. for (i = 0; i < NUM_THREADS; ++i)
  59. {
  60. STARPU_PTHREAD_JOIN(threads[i], NULL);
  61. }
  62. end = starpu_timing_now();
  63. timing = end - start;
  64. FPRINTF(stderr, "Success : %d threads launching simultaneously starpu_init\n", NUM_THREADS);
  65. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  66. FPRINTF(stderr, "Per task: %f usecs\n", timing/NUM_THREADS);
  67. for (i = 0; i < NUM_THREADS; i++)
  68. {
  69. STARPU_PTHREAD_CREATE(&threads[i], NULL, shutdown_starpu, NULL);
  70. }
  71. for (i = 0; i < NUM_THREADS; i++)
  72. {
  73. STARPU_PTHREAD_JOIN(threads[i], NULL);
  74. }
  75. return EXIT_SUCCESS;
  76. }