prio_list.c 5.0 KB

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