prio_list.c 5.0 KB

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