prio_list.c 4.0 KB

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