dichotomy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2013,2015 Inria
  4. * Copyright (C) 2016,2017 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "sc_hypervisor_lp.h"
  18. #include "sc_hypervisor_policy.h"
  19. #include <math.h>
  20. #include <sys/time.h>
  21. /* executes the function lp_estimated_distrib_func over the interval [tmin, tmax] until it finds the lowest value that
  22. still has solutions */
  23. unsigned sc_hypervisor_lp_execute_dichotomy(int ns, int nw, double w_in_s[ns][nw], unsigned solve_lp_integer, void *specific_data,
  24. double tmin, double tmax, double smallest_tmax,
  25. double (*lp_estimated_distrib_func)(int ns, int nw, double draft_w_in_s[ns][nw],
  26. unsigned is_integer, double tmax, void *specifc_data))
  27. {
  28. double res = 1.0;
  29. unsigned has_sol = 0;
  30. double tmid = tmax;
  31. unsigned found_sol = 0;
  32. struct timeval start_time;
  33. struct timeval end_time;
  34. int nd = 0;
  35. double found_tmid = tmax;
  36. double potential_tmid = tmid;
  37. double threashold = tmax*0.1;
  38. gettimeofday(&start_time, NULL);
  39. /* we fix tmax and we do not treat it as an unknown
  40. we just vary by dichotomy its values*/
  41. while(1)
  42. {
  43. /* find solution and save the values in draft tables
  44. only if there is a solution for the system we save them
  45. in the proper table */
  46. printf("solving for tmid %lf \n", tmid);
  47. res = lp_estimated_distrib_func(ns, nw, w_in_s, solve_lp_integer, tmid, specific_data);
  48. if(res < 0.0)
  49. {
  50. printf("timeouted no point in continuing\n");
  51. found_sol = 0;
  52. break;
  53. }
  54. else if(res != 0.0)
  55. {
  56. has_sol = 1;
  57. found_sol = 1;
  58. found_tmid = tmid;
  59. printf("found sol for tmid %lf \n", tmid);
  60. }
  61. else
  62. {
  63. printf("failed for tmid %lf \n", tmid);
  64. if(tmid == tmax)
  65. {
  66. printf("failed for tmid %lf from the first time\n", tmid);
  67. break;
  68. }
  69. has_sol = 0;
  70. }
  71. /* if we have a solution with this tmid try a smaller value
  72. bigger than the old one */
  73. if(has_sol)
  74. {
  75. /* if the difference between tmax and tmid is smaller than
  76. a given threashold there is no point in searching more
  77. precision */
  78. tmax = tmid;
  79. potential_tmid = tmin + ((tmax-tmin)/2.0);
  80. if((tmax - potential_tmid) < threashold)
  81. {
  82. printf("had_sol but stop doing it for tmin %lf tmax %lf and potential tmid %lf \n", tmin, tmax, potential_tmid);
  83. break;
  84. }
  85. printf("try for smaller potential tmid %lf \n", potential_tmid);
  86. }
  87. else /*else try a bigger one */
  88. {
  89. /* if we previously found a good sol and we keep failing
  90. we stop searching for a better sol */
  91. tmin = tmid;
  92. potential_tmid = tmin + ((tmax-tmin)/2.0);
  93. if((tmax - potential_tmid) < threashold)
  94. {
  95. printf("didn't have sol but stop doing it for tmin %lf tmax %lf and potential tmid %lf \n", tmin, tmax, potential_tmid);
  96. break;
  97. }
  98. printf("try for bigger potential tmid %lf \n", potential_tmid);
  99. }
  100. tmid = potential_tmid;
  101. nd++;
  102. }
  103. printf("solve againd for tmid %lf \n", found_tmid);
  104. if(found_sol)
  105. {
  106. res = lp_estimated_distrib_func(ns, nw, w_in_s, solve_lp_integer, found_tmid, specific_data);
  107. found_sol = (res != 0.0);
  108. }
  109. printf("found sol %u for tmid %lf\n", found_sol, found_tmid);
  110. gettimeofday(&end_time, NULL);
  111. long diff_s = end_time.tv_sec - start_time.tv_sec;
  112. long diff_us = end_time.tv_usec - start_time.tv_usec;
  113. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000;
  114. return found_sol;
  115. }