prio_list.c 4.4 KB

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