dichotomy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 - 2013 INRIA
  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. #include "sc_hypervisor_lp.h"
  17. #include "sc_hypervisor_policy.h"
  18. #include <math.h>
  19. #include <sys/time.h>
  20. /* executes the function lp_estimated_distrib_func over the interval [tmin, tmax] until it finds the lowest value that
  21. still has solutions */
  22. unsigned sc_hypervisor_lp_execute_dichotomy(int ns, int nw, double w_in_s[ns][nw], unsigned solve_lp_integer, void *specific_data,
  23. double tmin, double tmax, double smallest_tmax,
  24. double (*lp_estimated_distrib_func)(int ns, int nw, double draft_w_in_s[ns][nw],
  25. unsigned is_integer, double tmax, void *specifc_data))
  26. {
  27. double res = 1.0;
  28. unsigned has_sol = 0;
  29. double tmid = tmax;
  30. unsigned found_sol = 0;
  31. struct timeval start_time;
  32. struct timeval end_time;
  33. int nd = 0;
  34. double found_tmid = tmax;
  35. double potential_tmid = tmid;
  36. double threashold = tmax*0.1;
  37. gettimeofday(&start_time, NULL);
  38. /* we fix tmax and we do not treat it as an unknown
  39. we just vary by dichotomy its values*/
  40. while(1)
  41. {
  42. /* find solution and save the values in draft tables
  43. only if there is a solution for the system we save them
  44. in the proper table */
  45. printf("solving for tmid %lf \n", tmid);
  46. res = lp_estimated_distrib_func(ns, nw, w_in_s, solve_lp_integer, tmid, specific_data);
  47. if(res < 0.0)
  48. {
  49. printf("timeouted no point in continuing\n");
  50. found_sol = 0;
  51. break;
  52. }
  53. else if(res != 0.0)
  54. {
  55. has_sol = 1;
  56. found_sol = 1;
  57. found_tmid = tmid;
  58. printf("found sol for tmid %lf \n", tmid);
  59. }
  60. else
  61. {
  62. printf("failed for tmid %lf \n", tmid);
  63. if(tmid == tmax)
  64. {
  65. printf("failed for tmid %lf from the first time\n", tmid);
  66. break;
  67. }
  68. has_sol = 0;
  69. }
  70. /* if we have a solution with this tmid try a smaller value
  71. bigger than the old one */
  72. if(has_sol)
  73. {
  74. /* if the difference between tmax and tmid is smaller than
  75. a given threashold there is no point in searching more
  76. precision */
  77. tmax = tmid;
  78. potential_tmid = tmin + ((tmax-tmin)/2.0);
  79. if((tmax - potential_tmid) < threashold)
  80. {
  81. printf("had_sol but stop doing it for tmin %lf tmax %lf and potential tmid %lf \n", tmin, tmax, potential_tmid);
  82. break;
  83. }
  84. printf("try for smaller potential tmid %lf \n", potential_tmid);
  85. }
  86. else /*else try a bigger one */
  87. {
  88. /* if we previously found a good sol and we keep failing
  89. we stop searching for a better sol */
  90. tmin = tmid;
  91. potential_tmid = tmin + ((tmax-tmin)/2.0);
  92. if((tmax - potential_tmid) < threashold)
  93. {
  94. printf("didn't have sol but stop doing it for tmin %lf tmax %lf and potential tmid %lf \n", tmin, tmax, potential_tmid);
  95. break;
  96. }
  97. printf("try for bigger potential tmid %lf \n", potential_tmid);
  98. }
  99. tmid = potential_tmid;
  100. nd++;
  101. }
  102. printf("solve againd for tmid %lf \n", found_tmid);
  103. if(found_sol)
  104. {
  105. res = lp_estimated_distrib_func(ns, nw, w_in_s, solve_lp_integer, found_tmid, specific_data);
  106. found_sol = (res != 0.0);
  107. }
  108. printf("found sol %d for tmid %lf\n", found_sol, found_tmid);
  109. gettimeofday(&end_time, NULL);
  110. long diff_s = end_time.tv_sec - start_time.tv_sec;
  111. long diff_us = end_time.tv_usec - start_time.tv_usec;
  112. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000;
  113. return found_sol;
  114. }