prio_list.c 3.3 KB

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