prio_list.c 3.6 KB

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