multithreaded_init.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. static
  25. void *launch_starpu(void *unused)
  26. {
  27. int ret;
  28. (void) unused;
  29. ret = starpu_initialize(NULL, glob_argc, glob_argv);
  30. if (ret == -ENODEV)
  31. exit(STARPU_TEST_SKIPPED);
  32. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  33. return NULL;
  34. }
  35. static
  36. void *shutdown_starpu(void *unused)
  37. {
  38. (void) unused;
  39. starpu_shutdown();
  40. return NULL;
  41. }
  42. int main(int argc, char **argv)
  43. {
  44. unsigned i;
  45. double timing;
  46. struct timeval start;
  47. struct timeval end;
  48. glob_argc = &argc;
  49. glob_argv = &argv;
  50. starpu_pthread_t threads[NUM_THREADS];
  51. gettimeofday(&start, NULL);
  52. for (i = 0; i < NUM_THREADS; ++i)
  53. {
  54. int ret = starpu_pthread_create(&threads[i], NULL, launch_starpu, NULL);
  55. STARPU_ASSERT(ret == 0);
  56. }
  57. for (i = 0; i < NUM_THREADS; ++i)
  58. {
  59. int ret = starpu_pthread_join(threads[i], NULL);
  60. STARPU_ASSERT(ret == 0);
  61. }
  62. gettimeofday(&end, NULL);
  63. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  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. int ret = starpu_pthread_create(&threads[i], NULL, shutdown_starpu, NULL);
  70. STARPU_ASSERT(ret == 0);
  71. }
  72. for (i = 0; i < NUM_THREADS; i++)
  73. {
  74. int ret = starpu_pthread_join(threads[i], NULL);
  75. STARPU_ASSERT(ret == 0);
  76. }
  77. return EXIT_SUCCESS;
  78. }