multithreaded_init.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <pthread.h>
  20. #include <starpu.h>
  21. #include "../helper.h"
  22. #define NUM_THREADS 5
  23. void *launch_starpu(void *id)
  24. {
  25. int ret;
  26. ret = starpu_init(NULL);
  27. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  28. return NULL;
  29. }
  30. void *shutdown_starpu(void *unused)
  31. {
  32. (void) unused;
  33. starpu_shutdown();
  34. return NULL;
  35. }
  36. int main(int argc, char **argv)
  37. {
  38. unsigned i;
  39. double timing;
  40. struct timeval start;
  41. struct timeval end;
  42. pthread_t threads[NUM_THREADS];
  43. gettimeofday(&start, NULL);
  44. for (i = 0; i < NUM_THREADS; ++i)
  45. {
  46. int ret = pthread_create(&threads[i], NULL, launch_starpu, NULL);
  47. STARPU_ASSERT(ret == 0);
  48. }
  49. for (i = 0; i < NUM_THREADS; ++i)
  50. {
  51. int ret = pthread_join(threads[i], NULL);
  52. STARPU_ASSERT(ret == 0);
  53. }
  54. gettimeofday(&end, NULL);
  55. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  56. FPRINTF(stderr, "Success : %d threads launching simultaneously starpu_init\n", NUM_THREADS);
  57. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  58. FPRINTF(stderr, "Per task: %f usecs\n", timing/NUM_THREADS);
  59. for (i = 0; i < NUM_THREADS; i++)
  60. {
  61. int ret = pthread_create(&threads[i], NULL, shutdown_starpu, NULL);
  62. STARPU_ASSERT(ret == 0);
  63. }
  64. return EXIT_SUCCESS;
  65. }