prio_list.c 4.9 KB

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