starpu_barrier.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #define __COMMON_UTILS_H__
  17. #define _STARPU_MALLOC(p, s) do {p = malloc(s);} while (0)
  18. #define _STARPU_CALLOC(p, n, s) do {p = calloc(n, s);} while (0)
  19. #define _STARPU_REALLOC(p, s) do {p = realloc(p, s);} while (0)
  20. #define STARPU_HG_DISABLE_CHECKING(v) ((void) 0)
  21. #define STARPU_HG_ENABLE_CHECKING(v) ((void) 0)
  22. #define ANNOTATE_HAPPENS_AFTER(v) ((void) 0)
  23. #define ANNOTATE_HAPPENS_BEFORE(v) ((void) 0)
  24. #define STARPU_DEBUG_PREFIX "[starpu]"
  25. #ifdef STARPU_VERBOSE
  26. # define _STARPU_DEBUG(fmt, ...) do { if (!_starpu_silent) {fprintf(stderr, STARPU_DEBUG_PREFIX"[%s] " fmt ,__starpu_func__ ,## __VA_ARGS__); fflush(stderr); }} while(0)
  27. #else
  28. # define _STARPU_DEBUG(fmt, ...) do { } while (0)
  29. #endif
  30. #define STARPU_UYIELD() ((void)0)
  31. #ifndef NOCONFIG
  32. #include <common/config.h>
  33. #else
  34. #ifndef _GNU_SOURCE
  35. #define _GNU_SOURCE 1
  36. #endif
  37. // Assuming recent simgrid
  38. #define STARPU_HAVE_SIMGRID_MSG_H
  39. #define STARPU_HAVE_SIMGRID_SEMAPHORE_H
  40. #define STARPU_HAVE_SIMGRID_MUTEX_H
  41. #define STARPU_HAVE_SIMGRID_COND_H
  42. #define STARPU_HAVE_SIMGRID_BARRIER_H
  43. #define STARPU_HAVE_XBT_SYNCHRO_H
  44. #define HAVE_SIMGRID_GET_CLOCK
  45. #define HAVE_SG_ACTOR_SLEEP_FOR
  46. #define HAVE_SG_CFG_SET_INT
  47. #endif
  48. #include <unistd.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <limits.h>
  52. #include <math.h>
  53. #include <common/barrier.h>
  54. #ifdef STARPU_HAVE_SIMGRID_MSG_H
  55. #include <simgrid/msg.h>
  56. #else
  57. #include <msg/msg.h>
  58. #endif
  59. #include <simgrid/modelchecker.h>
  60. #ifdef STARPU_HAVE_XBT_SYNCHRO_H
  61. #include <xbt/synchro.h>
  62. #else
  63. #include <xbt/synchro_core.h>
  64. #endif
  65. int
  66. _starpu_simgrid_thread_start(int argc, char *argv[])
  67. {
  68. return 0;
  69. }
  70. static void _starpu_clock_gettime(struct timespec *ts)
  71. {
  72. #ifdef HAVE_SIMGRID_GET_CLOCK
  73. double now = simgrid_get_clock();
  74. #else
  75. double now = MSG_get_clock();
  76. #endif
  77. ts->tv_sec = floor(now);
  78. ts->tv_nsec = floor((now - ts->tv_sec) * 1000000000);
  79. }
  80. void starpu_sleep(float nb_sec)
  81. {
  82. #ifdef HAVE_SG_ACTOR_SLEEP_FOR
  83. sg_actor_sleep_for(nb_sec);
  84. #else
  85. MSG_process_sleep(nb_sec);
  86. #endif
  87. }
  88. #include <common/barrier.c>
  89. #undef STARPU_DEBUG
  90. int starpu_worker_get_id(void) { return 0; }
  91. static inline unsigned _starpu_worker_mutex_is_sched_mutex(int workerid, starpu_pthread_mutex_t *mutex) { return 0; }
  92. #include <common/thread.c>
  93. #ifndef NTHREADS
  94. #define NTHREADS 2
  95. #endif
  96. #ifndef NITERS
  97. #define NITERS 1
  98. #endif
  99. struct _starpu_barrier barrier;
  100. int worker(int argc, char *argv[])
  101. {
  102. unsigned iter;
  103. for (iter = 0; iter < NITERS; iter++)
  104. {
  105. MC_assert(barrier.count <= NTHREADS);
  106. _starpu_barrier_wait(&barrier);
  107. }
  108. return 0;
  109. }
  110. int master(int argc, char *argv[])
  111. {
  112. unsigned i;
  113. _starpu_barrier_init(&barrier, NTHREADS);
  114. for (i = 0; i < NTHREADS; i++)
  115. {
  116. char *s;
  117. asprintf(&s, "%d\n", i);
  118. char **args = malloc(sizeof(char*)*2);
  119. args[0] = s;
  120. args[1] = NULL;
  121. MSG_process_create_with_arguments("test", worker, NULL, MSG_host_self(), 1, args);
  122. }
  123. return 0;
  124. }
  125. #undef main
  126. int main(int argc, char *argv[])
  127. {
  128. if (argc < 3)
  129. {
  130. fprintf(stderr,"usage: %s platform.xml host\n", argv[0]);
  131. exit(EXIT_FAILURE);
  132. }
  133. srand48(0);
  134. MSG_init(&argc, argv);
  135. #ifdef HAVE_SG_CFG_SET_INT
  136. sg_cfg_set_int("contexts/stack-size", 128);
  137. #elif SIMGRID_VERSION_MAJOR < 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR < 13)
  138. extern xbt_cfg_t _sg_cfg_set;
  139. xbt_cfg_set_int(_sg_cfg_set, "contexts/stack-size", 128);
  140. #else
  141. xbt_cfg_set_int("contexts/stack-size", 128);
  142. #endif
  143. MSG_create_environment(argv[1]);
  144. MSG_process_create("master", master, NULL, MSG_get_host_by_name(argv[2]));
  145. MSG_main();
  146. return 0;
  147. }