validate_sched_util.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* To compile: gcc validate_sched_util.c sched_util.c */
  2. /* To run: ./a.out */
  3. /* Schedules some items, then runs through them timestep by timestep. */
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include "sched_util.h"
  7. double elapsed(struct timeval *t1,struct timeval *t2)
  8. {
  9. return 1000.0*(t2->tv_sec-t1->tv_sec) + 0.001*(t2->tv_usec-t1->tv_usec);
  10. }
  11. void debug_print_sh(struct schedule_helper *sh)
  12. {
  13. int i;
  14. struct abstract_element *aep;
  15. printf("dt %.2f, dt_1 %.2f, now %.2f, #%d/%d {",sh->dt,sh->dt_1,sh->now,sh->count,sh->current_count);
  16. aep = sh->current;
  17. if (aep!=NULL) printf(" ");
  18. while (aep!=NULL) { printf("%f ",aep->t); aep = aep->next; }
  19. printf("} [ ");
  20. for (i=0;i<sh->buf_len;i++)
  21. {
  22. printf("{");
  23. aep = sh->circ_buf_head[i];
  24. if (aep!=NULL) printf(" ");
  25. while (aep!=NULL) { printf("%f ",aep->t); aep = aep->next; }
  26. printf("} ");
  27. }
  28. printf("]\n");
  29. if (sh->next_scale != NULL)
  30. {
  31. printf(" -> ");
  32. debug_print_sh(sh->next_scale);
  33. }
  34. }
  35. int main()
  36. {
  37. int i,j;
  38. struct schedule_helper *sh;
  39. struct abstract_element ae[10],*aep;
  40. struct timeval tv[3];
  41. ae[0].t = 0.17;
  42. ae[1].t = 1231.9;
  43. ae[2].t = 2345119.43;
  44. ae[3].t = 85.5;
  45. ae[4].t = 0.151;
  46. ae[5].t = 2.415;
  47. ae[6].t = 16.15;
  48. ae[7].t = 2.1818;
  49. ae[8].t = 85.9;
  50. ae[9].t = 1958.2;
  51. sh = create_scheduler(1.0,7.0,7,0.0);
  52. schedule_add(sh,ae + 0);
  53. schedule_add(sh,ae + 1);
  54. schedule_add(sh,ae + 2);
  55. schedule_add(sh,ae + 3);
  56. schedule_add(sh,ae + 4);
  57. schedule_add(sh,ae + 5);
  58. schedule_add(sh,ae + 6);
  59. schedule_add(sh,ae + 7);
  60. schedule_add(sh,ae + 8);
  61. schedule_excert(sh,ae+3,ae+9,sizeof(struct abstract_element));
  62. while (sh->count + sh->current_count > 0)
  63. {
  64. /*
  65. debug_print_sh(sh);
  66. aep = schedule_next(sh);
  67. if (aep!=NULL) printf("Item scheduled at %f\n",aep->t);
  68. */
  69. i = sh->count + sh->current_count;
  70. while ((aep = schedule_next(sh)) != NULL)
  71. {
  72. printf("Item scheduled at %f (now=%f)\n",aep->t,sh->now);
  73. i--;
  74. }
  75. j=0;
  76. while (i>0 && (aep=schedule_next(sh))==NULL) j++;
  77. if (i>0 && j>0)
  78. {
  79. printf("Advanced %d timestep%s; ",j,(j==1)?"":"s");
  80. if (aep != NULL)
  81. {
  82. schedule_add(sh,aep);
  83. printf("added back @ %f.\n",aep->t);
  84. }
  85. else printf("WTF?\n");
  86. }
  87. printf("\n");
  88. }
  89. delete_scheduler(sh);
  90. ae[0].t = 0.17;
  91. ae[1].t = 1231.9;
  92. ae[2].t = 2345119.43;
  93. ae[3].t = 85.5;
  94. ae[4].t = 0.151;
  95. ae[5].t = 2.415;
  96. ae[6].t = 16.15;
  97. ae[7].t = 2.1818;
  98. ae[8].t = 85.9;
  99. ae[9].t = 1958.2;
  100. sh = create_scheduler(1.5,9.0,7,0.0);
  101. printf("Scheduling: ");
  102. for (i=0;i<10;i++) printf("%.3f ",ae[i].t);
  103. printf("\n");
  104. schedule_add(sh,ae + 0);
  105. schedule_add(sh,ae + 1);
  106. schedule_add(sh,ae + 2);
  107. schedule_add(sh,ae + 3);
  108. schedule_add(sh,ae + 4);
  109. schedule_add(sh,ae + 5);
  110. schedule_add(sh,ae + 6);
  111. schedule_add(sh,ae + 7);
  112. schedule_add(sh,ae + 8);
  113. schedule_add(sh,ae + 9);
  114. printf("Scheduled with sorting: ");
  115. schedule_sort(sh);
  116. while (sh->count + sh->current_count > 0)
  117. {
  118. i = sh->count + sh->current_count;
  119. while ((aep = schedule_next(sh)) != NULL) { printf("%.3f ",aep->t); i--; }
  120. j=0;
  121. while (i>0 && (aep=schedule_next(sh))==NULL) j++;
  122. if (i>0 && j>0)
  123. {
  124. if (aep != NULL)
  125. {
  126. schedule_add(sh,aep);
  127. schedule_sort(sh);
  128. }
  129. }
  130. }
  131. printf("\n\n");
  132. printf("Sorting 10 items 1e6 times:\n");
  133. gettimeofday(&(tv[0]),NULL);
  134. for (i=0;i<1000000;i++)
  135. {
  136. for (j=0;j<9;j++) ae[j].next = &(ae[j+1]);
  137. ae[9].next = NULL;
  138. aep = ae_list_sort(&(ae[0]));
  139. }
  140. gettimeofday(&(tv[1]),NULL);
  141. printf("Took %.3f seconds.\n",elapsed(&(tv[0]),&(tv[1]))/1000.0);
  142. }