prio_list.c 4.2 KB

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