dichotomy.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 old_tmax = 0.0;
  30. unsigned found_sol = 0;
  31. struct timeval start_time;
  32. struct timeval end_time;
  33. int nd = 0;
  34. gettimeofday(&start_time, NULL);
  35. /* we fix tmax and we do not treat it as an unknown
  36. we just vary by dichotomy its values*/
  37. while(tmax > 1.0)
  38. {
  39. /* find solution and save the values in draft tables
  40. only if there is a solution for the system we save them
  41. in the proper table */
  42. res = lp_estimated_distrib_func(ns, nw, w_in_s, solve_lp_integer, tmax, specific_data);
  43. if(res != 0.0)
  44. {
  45. has_sol = 1;
  46. found_sol = 1;
  47. }
  48. else
  49. has_sol = 0;
  50. /* if we have a solution with this tmax try a smaller value
  51. bigger than the old min */
  52. if(has_sol)
  53. {
  54. if(old_tmax != 0.0 && (old_tmax - tmax) < 0.5)
  55. break;
  56. old_tmax = tmax;
  57. }
  58. else /*else try a bigger one but smaller than the old tmax */
  59. {
  60. tmin = tmax;
  61. if(old_tmax != 0.0)
  62. tmax = old_tmax;
  63. }
  64. if(tmin == tmax) break;
  65. tmax = sc_hypervisor_lp_find_tmax(tmin, tmax);
  66. if(tmax < smallest_tmax)
  67. {
  68. tmax = old_tmax;
  69. tmin = smallest_tmax;
  70. tmax = sc_hypervisor_lp_find_tmax(tmin, tmax);
  71. }
  72. nd++;
  73. }
  74. gettimeofday(&end_time, NULL);
  75. long diff_s = end_time.tv_sec - start_time.tv_sec;
  76. long diff_us = end_time.tv_usec - start_time.tv_usec;
  77. float timing = (float)(diff_s*1000000 + diff_us)/1000;
  78. return found_sol;
  79. }