prio_list.c 4.5 KB

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