multithreaded_init.c 2.3 KB

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