prio_list.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 CNRS
  4. * Copyright (C) 2017 Inria
  5. * Copyright (C) 2017,2019-2020 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #define _STARPU_MALLOC(p, s) do {p = malloc(s);} while (0)
  19. #define _STARPU_CALLOC(p, n, s) do {p = calloc(n, s);} while (0)
  20. #define _STARPU_MALLOC_CAST(p, s, t) do {p = (t) malloc(s);} while (0)
  21. #ifndef NOCONFIG
  22. #include <common/config.h>
  23. #else
  24. #define _GNU_SOURCE 1
  25. // Assuming recent simgrid
  26. #define STARPU_HAVE_SIMGRID_MSG_H
  27. #define STARPU_HAVE_SIMGRID_SEMAPHORE_H
  28. #define STARPU_HAVE_SIMGRID_MUTEX_H
  29. #define STARPU_HAVE_SIMGRID_COND_H
  30. #define STARPU_HAVE_SIMGRID_BARRIER_H
  31. #define STARPU_HAVE_XBT_SYNCHRO_H
  32. #define HAVE_SIMGRID_GET_CLOCK
  33. #define HAVE_SG_ACTOR_SLEEP_FOR
  34. #define HAVE_SG_CFG_SET_INT
  35. #endif
  36. #include <unistd.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <limits.h>
  40. #include <common/list.h>
  41. #include <common/prio_list.h>
  42. #ifdef STARPU_HAVE_SIMGRID_MSG_H
  43. #include <simgrid/msg.h>
  44. #else
  45. #include <msg/msg.h>
  46. #endif
  47. #include <simgrid/modelchecker.h>
  48. #ifdef STARPU_HAVE_XBT_SYNCHRO_H
  49. #include <xbt/synchro.h>
  50. #else
  51. #include <xbt/synchro_core.h>
  52. #endif
  53. #include <common/rbtree.c>
  54. #ifndef NLISTS
  55. #define NLISTS 1
  56. #endif
  57. #ifndef NITERS
  58. #define NITERS 1
  59. #endif
  60. #ifndef NTHREADS
  61. #define NTHREADS 2
  62. #endif
  63. #ifndef NELEMENTS
  64. #define NELEMENTS 4
  65. #endif
  66. // MC_ignore
  67. #ifdef STARPU_HAVE_SIMGRID_MUTEX_H
  68. sg_mutex_t mutex[NLISTS];
  69. #define mutex_lock(l) sg_mutex_lock(l)
  70. #define mutex_unlock(l) sg_mutex_unlock(l)
  71. #else
  72. xbt_mutex_t mutex[NLISTS];
  73. #define mutex_lock(l) xbt_mutex_acquire(l)
  74. #define mutex_unlock(l) xbt_mutex_release(l)
  75. #endif
  76. LIST_TYPE(foo,
  77. unsigned prio;
  78. unsigned back; /* Push at back instead of front? */
  79. );
  80. PRIO_LIST_TYPE(foo, prio);
  81. struct foo_prio_list mylist[NLISTS];
  82. void check_list_prio(struct foo_prio_list *list)
  83. {
  84. struct foo *cur;
  85. unsigned lastprio = UINT_MAX;
  86. unsigned back = 0;
  87. for (cur = foo_prio_list_begin(list);
  88. cur != foo_prio_list_end(list);
  89. cur = foo_prio_list_next(list, cur))
  90. {
  91. if (cur->prio == lastprio)
  92. /* For same prio, back elements should never get before
  93. * front elements */
  94. MC_assert(!(back && !cur->back));
  95. else
  96. MC_assert(lastprio > cur->prio);
  97. lastprio = cur->prio;
  98. back = cur->back;
  99. }
  100. }
  101. int worker(int argc, char *argv[])
  102. {
  103. unsigned myrank = atoi(argv[0]);
  104. unsigned i, n, l, iter;
  105. struct foo *elem;
  106. struct drand48_data buffer;
  107. long res;
  108. srand48_r(myrank, &buffer);
  109. l = myrank%NLISTS;
  110. for (iter = 0; iter < NITERS; iter++)
  111. {
  112. for (i = 0; i < NELEMENTS; i++)
  113. {
  114. elem = malloc(sizeof(*elem));
  115. lrand48_r(&buffer, &res);
  116. elem->prio = res%10;
  117. lrand48_r(&buffer, &res);
  118. elem->back = res%2;
  119. mutex_lock(mutex[l]);
  120. if (elem->back)
  121. foo_prio_list_push_back(&mylist[l], elem);
  122. else
  123. foo_prio_list_push_front(&mylist[l], elem);
  124. check_list_prio(&mylist[l]);
  125. mutex_unlock(mutex[l]);
  126. }
  127. for (i = 0; i < NELEMENTS; i++)
  128. {
  129. lrand48_r(&buffer, &res);
  130. n = res%(NELEMENTS-i);
  131. mutex_lock(mutex[l]);
  132. for (elem = foo_prio_list_begin(&mylist[l]);
  133. n--;
  134. elem = foo_prio_list_next(&mylist[l], elem))
  135. ;
  136. foo_prio_list_erase(&mylist[l], elem);
  137. check_list_prio(&mylist[l]);
  138. mutex_unlock(mutex[l]);
  139. }
  140. /* horrible way to wait for list getting empty */
  141. #ifdef HAVE_SG_ACTOR_SLEEP_FOR
  142. sg_actor_sleep_for(1000);
  143. #else
  144. MSG_process_sleep(1000);
  145. #endif
  146. }
  147. return 0;
  148. }
  149. int master(int argc, char *argv[])
  150. {
  151. unsigned i, l;
  152. for (l = 0; l < NLISTS; l++)
  153. {
  154. #ifdef STARPU_HAVE_SIMGRID_MUTEX_H
  155. mutex[l] = sg_mutex_init();
  156. #else
  157. mutex[l] = xbt_mutex_init();
  158. #endif
  159. foo_prio_list_init(&mylist[l]);
  160. }
  161. for (i = 0; i < NTHREADS; i++)
  162. {
  163. char *s;
  164. asprintf(&s, "%d\n", i);
  165. char **args = malloc(sizeof(char*)*2);
  166. args[0] = s;
  167. args[1] = NULL;
  168. MSG_process_create_with_arguments("test", worker, NULL, MSG_host_self(), 1, args);
  169. }
  170. return 0;
  171. }
  172. int main(int argc, char *argv[])
  173. {
  174. if (argc < 3)
  175. {
  176. fprintf(stderr,"usage: %s platform.xml host\n", argv[0]);
  177. exit(EXIT_FAILURE);
  178. }
  179. srand48(0);
  180. MSG_init(&argc, argv);
  181. #ifdef HAVE_SG_CFG_SET_INT
  182. sg_cfg_set_int("contexts/stack-size", 128);
  183. #elif SIMGRID_VERSION_MAJOR < 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR < 13)
  184. extern xbt_cfg_t _sg_cfg_set;
  185. xbt_cfg_set_int(_sg_cfg_set, "contexts/stack-size", 128);
  186. #else
  187. xbt_cfg_set_int("contexts/stack-size", 128);
  188. #endif
  189. MSG_create_environment(argv[1]);
  190. MSG_process_create("master", master, NULL, MSG_get_host_by_name(argv[2]));
  191. MSG_main();
  192. return 0;
  193. }