prio_list.c 4.3 KB

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