prio_list.c 4.2 KB

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