IndepDP3Demi.cpp 10.1 KB
Newer Older
1 2 3 4 5 6 7 8
#include "IndepDP3Demi.h"
#include <iostream>
#include <limits>
#include <new>
#include <algorithm>
#include <cmath>
#include "util.h"

9 10 11 12 13 14
#ifdef WITH_CPLEX
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
#endif


15 16 17 18
using namespace std;


IndepDP3Demi::IndepDP3Demi(const AlgOptions& opt): IndepDualGeneric(opt) {
19 20
  discretizationConstant = opt.asDouble("disc", discretizationConstant);
#ifdef WITH_CPLEX
21 22
  solveWithCplex = (opt.asString("cplex", "false") == "true") || (opt.asString("cplex", "false") == "discrete");
  cplexUseDiscretizedValues = opt.asString("cplex", "false") == "discrete";
23 24 25 26 27
#else
  if(opt.isPresent("cplex"))
    cerr << "Warning: DP3demi: pmtool compiled without cplex support, ignoring 'cplex' option." << endl;
#endif
  
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
}


// returns minimum CPU load, taking into account existing loads
double IndepDP3Demi::tryGuess(Instance& instance, std::vector<int> taskSet, vector<double>& loads,
			      double target, IndepResult &result, bool getResult) {
  // CPUload(i, g) := smallest load on CPU from the first i tasks,
  //                  with at most g load on GPU
  // So need to discretize the GPU load ? Yes. Paper says with a ratio of lambda/3n
  // For all tasks in taskSet:
  //   forall g, CPUload(i, g) = min CPUload(i-1, g-T^G_i) CPUload(i-1, g) + T^C_i

  int nbCPU = instance.nbWorkers[0]; 
  int nbGPU = instance.nbWorkers[1]; 
  
  double existingCPUload = loads[0];
  double existingGPUload = loads[1]; 
  double maxGPUload = target * nbGPU - existingGPUload; 

  if(maxGPUload < 0) maxGPUload = 1; 
48

49 50
  double ratio = target / (discretizationConstant * taskSet.size()); 
  vector<int> discreteGPUtimings(instance.nbTaskTypes);
51 52
  vector<bool> isLongOnCPU(instance.nbTaskTypes);
  vector<bool> isLongOnGPU(instance.nbTaskTypes);
53 54
  for(int i = 0; i < instance.nbTaskTypes; i++) {
    discreteGPUtimings[i] = ceil(instance.execTimes[1][i] / ratio);
55 56
    isLongOnCPU[i] = instance.execTimes[0][i] > (target/2);
    isLongOnGPU[i] = instance.execTimes[1][i] > (target/2); 
57 58 59
  }
  const int N = ceil(maxGPUload / ratio);

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#ifdef WITH_CPLEX
  if(solveWithCplex) {
    IloEnv env;
    IloModel model = IloModel(env);

    IloNumVarArray affect(env, taskSet.size(), 0.0, 1.0, ILOINT); // 0 means on CPU, 1 on GPU
    IloExpr nbTasksWithLargeCPUTime(env);
    IloExpr nbTasksWithLargeGPUTime(env);
    IloExpr totalCPULoad(env);
    IloExpr totalGPULoad(env); 
    for(int i = 0; i < taskSet.size(); ++i) {
      int t = taskSet[i];
      int taskType = instance.taskTypes[t];
      const double exec0 = instance.execTimes[0][taskType];
      const double exec1 = instance.execTimes[1][taskType]; 
75
      if(isLongOnCPU[taskType])
76
	nbTasksWithLargeCPUTime += (1-affect[i]);
77
      if(isLongOnGPU[taskType])
78 79
	nbTasksWithLargeGPUTime += affect[i];
      totalCPULoad += (1-affect[i])*exec0;
80
      totalGPULoad += affect[i]*(cplexUseDiscretizedValues ? discreteGPUtimings[taskType] : exec1);
81 82 83 84
      if(exec0 > target)
	model.add(affect[i] == 1);
      if(exec1 > target)
	model.add(affect[i] == 0); 
85 86 87 88
    }

    model.add(nbTasksWithLargeCPUTime <= nbCPU);
    model.add(nbTasksWithLargeGPUTime <= nbGPU);
89 90 91 92
    if(cplexUseDiscretizedValues)
      model.add(totalGPULoad <= N);
    else
      model.add(totalGPULoad <= maxGPUload); 
93 94 95
    model.add(IloMinimize(env, totalCPULoad)); 

    IloCplex modelCplex = IloCplex(model); 
96
    if(verbosity < 8) 
97
      modelCplex.setOut(env.getNullStream());
98
    modelCplex.setParam(IloCplex::Param::MIP::Tolerances::MIPGap, 0.000001);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    IloBool feasible = modelCplex.solve();
    if(! feasible)
      return std::numeric_limits<double>::infinity(); 

    double value = modelCplex.getObjValue();

    if(getResult) {
      result[0].clear();
      result[1].clear();
      
      IloNumArray affectValues(env);
      modelCplex.getValues(affectValues, affect);
      for(int i = 0; i < taskSet.size(); ++i)
	if(affectValues[i] > 0.5)
	  result[1].push_back(i);
	else
	  result[0].push_back(i); 
    }
    return value + existingCPUload; 
  }
#endif

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  

  int nbJobsWithLargeCPUTime = 0;
  int nbJobsWithLargeGPUTime = 0;
  for(int & t: taskSet) {
    int taskType = instance.taskTypes[t];
    if(instance.execTimes[0][taskType] > (target / 2))
      ++ nbJobsWithLargeCPUTime;
    if(instance.execTimes[1][taskType] > (target / 2))
      ++ nbJobsWithLargeGPUTime;
  }
  
  const int maxMu = min(nbCPU, nbJobsWithLargeCPUTime);
  const int maxNu = min(nbGPU, nbJobsWithLargeGPUTime);
  const int stateSpaceSize = (N+1) * (maxMu + 1) * (maxNu + 1); 
  int length = getResult ? taskSet.size() + 1 : 1; 
  double** CPUload = new double*[length];
  if(verbosity >= 7) {
    cerr << "DP3demi: N=" << N << " maxMu= " << maxMu << " maxNu = " << maxNu << endl; 
    cerr << "DP3demi: allocating " << length << " space= " << stateSpaceSize << ", total= " << length * stateSpaceSize << endl;
  }
  CPUload[0] = new double[length * stateSpaceSize]; 
  for(int i = 1; i < length; i++) 
    CPUload[i] = CPUload[i-1] + stateSpaceSize; 

147
#define getTabValue(tab, l, m, k) (tab[l + (N+1)*(m) + (N+1)*(maxMu+1)*(k)])
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

  
  if(verbosity >= 7) 
    cout << "IndepDP3Demi: maxGLoad = " << maxGPUload << ", ratio = " << ratio << " N= " << N << " gR: " << getResult << " mL " << target << endl; 

  int index = 0; 
  for(int i = 0; i < stateSpaceSize; ++i) 
    CPUload[index][i] = 0; 
  for(int t : taskSet) {
    const int taskType = instance.taskTypes[t];
    const int nextIndex = getResult ? index+1: index;

    const double exec0 = instance.execTimes[0][taskType];
    const double exec1 = instance.execTimes[1][taskType]; 
    const int discreteGPUtime = discreteGPUtimings[taskType];
    
164 165
    const int muOffset = isLongOnCPU[taskType];
    const int nuOffset = isLongOnGPU[taskType];
166

167 168 169
    if(exec0 > target && exec1 > target) {
      delete[] CPUload[0];
      delete[] CPUload; 
170
      return -1; // Problem is not feasible: task t cannot be placed on any resource
171
    }
172

173 174 175
    if(verbosity >= 8) 
      cout << "Task " << t << " dGPUTime="<< discreteGPUtime << " exec0=" << exec0 << " muOffset="<<muOffset << " nuOffset="<<nuOffset << endl; 
    
176 177 178 179 180 181 182 183 184 185 186 187 188
    if((exec0 <= target) && (exec1 <= target)) { // Task t can be placed on both resources
      for(int mu = maxMu; mu >= muOffset; --mu) {
	for(int nu = maxNu; nu >= nuOffset; --nu) {
	  for(int l = N; l >= discreteGPUtime; --l) {
	    getTabValue(CPUload[nextIndex], l, mu, nu) = min(getTabValue(CPUload[index], l, mu-muOffset, nu) + exec0,
							     getTabValue(CPUload[index], l - discreteGPUtime, mu, nu-nuOffset));
	  }
	  for(int l = discreteGPUtime - 1; l >= 0; --l) {
	    getTabValue(CPUload[nextIndex], l, mu, nu) = getTabValue(CPUload[index], l, mu-muOffset, nu) + exec0;
	  }
	}
	if(nuOffset) {
	  for(int l = N; l >= 0; --l) {
189
	    getTabValue(CPUload[nextIndex], l, mu, 0) = getTabValue(CPUload[index], l, mu-muOffset, 0) + exec0;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	  }
	}
      }

      if(muOffset) {
	for(int nu = maxNu; nu >= nuOffset; --nu) {
	  for(int l = N; l >= discreteGPUtime; --l) {
	    getTabValue(CPUload[nextIndex], l, 0, nu) = getTabValue(CPUload[index], l - discreteGPUtime, 0, nu-nuOffset);
	  }
	  for(int l = discreteGPUtime - 1; l >= 0; --l) {
	    getTabValue(CPUload[nextIndex], l, 0, nu) = std::numeric_limits<double>::infinity(); 
	  }
	}
	if(nuOffset) {
	  for(int l = N; l >= 0; --l) {
	    getTabValue(CPUload[nextIndex], l, 0, 0) = std::numeric_limits<double>::infinity(); 
	  }
	}
      }
    } else if ((exec0 <= target) && (exec1 > target)) { // Task t can only be placed on CPUs
      for(int mu = maxMu; mu >= muOffset; --mu) {
	for(int nu = maxNu; nu >= 0; --nu) {
	  for(int l = N; l >= 0; --l) {
	    getTabValue(CPUload[nextIndex], l, mu, nu) = getTabValue(CPUload[index], l, mu-muOffset, nu) + exec0; 
	  }
	}
      }
      if(muOffset) {
	for(int nu = maxNu; nu >= 0; --nu) {
	  for(int l = N; l >= 0; --l) {
	    getTabValue(CPUload[nextIndex], l, 0, nu) = std::numeric_limits<double>::infinity();
	  }
	}
      }
      
    } else /* ((exec0 > target) && (exec1 <= target)) */ { // Task t can only be placed on GPUs
      for(int mu = maxMu; mu >= 0; --mu) {
	for(int nu = maxNu; nu >= nuOffset; --nu) {
	  for(int l = N; l >= discreteGPUtime; --l) {
	    getTabValue(CPUload[nextIndex], l, mu, nu) = getTabValue(CPUload[index], l - discreteGPUtime, mu, nu-nuOffset);
	  }
	  for(int l = discreteGPUtime - 1; l >= 0; l--) {
	    getTabValue(CPUload[nextIndex], l, mu, nu) = std::numeric_limits<double>::infinity(); 
	  }
	}
	if(nuOffset) {
	  for(int l = N; l >= 0; l--) {
	    getTabValue(CPUload[nextIndex], l, mu, 0) = std::numeric_limits<double>::infinity(); 
	  }
	}
      }
    }
    
243 244 245 246 247 248 249 250 251 252 253 254
    index = nextIndex;
    if(verbosity >= 8) { 
      cout << "DP3Demi: state after task " << t << endl;
      for(int mu = 0; mu <= maxMu; ++mu)
	for(int nu = 0; nu <= maxNu; ++nu) {
	  cout << "  ";
	  for(int l = 0; l <= N; ++l)
	    cout << " " << getTabValue(CPUload[index], l, mu, nu);
	  cout << endl;
	}
    }
    
255 256 257

  }

258
  double value = getTabValue(CPUload[index], N, maxMu, maxNu); 
259 260 261 262 263 264
  if(verbosity >= 7)
    cerr << "DP3demi: final value is " << value << endl; 
  
  int gLoad = N;
  int mu = maxMu;
  int nu = maxNu; 
265
  if(getResult &&   (value != std::numeric_limits<double>::infinity())) {
266 267 268 269 270 271 272 273 274 275
    
    result[0].clear();
    result[1].clear();
    
    for(; index > 0; index--) {
      const int taskType = instance.taskTypes[taskSet[index-1]];
      const double exec0 = instance.execTimes[0][taskType];
      const double exec1 = instance.execTimes[1][taskType]; 
      const int discreteGPUtime = discreteGPUtimings[taskType];
      
276 277
      const int muOffset = isLongOnCPU[taskType];
      const int nuOffset = isLongOnGPU[taskType]; 
278
      
279 280
      if((mu >= muOffset) && 
	 (abs(getTabValue(CPUload[index], gLoad, mu, nu) - (getTabValue(CPUload[index-1], gLoad, mu-muOffset, nu) + exec0)) <= 1e-5)) {
281 282 283 284 285 286 287 288 289
	mu -= muOffset; 
	result[0].push_back(taskSet[index-1]);
      }
      else {
	gLoad -= discreteGPUtimings[taskType];
	nu -= nuOffset; 
	result[1].push_back(taskSet[index-1]);
      }
    }
290 291 292 293 294 295 296
    double actualLoad = 0;
    for(int & t: result[0])
      actualLoad += instance.execTimes[0][instance.taskTypes[t]];

    if(abs(actualLoad-value) > 0.0001)
      cerr << "DP3Demi Warning: Difference between computed load and actual load: " << value << " " << actualLoad << endl;
    
297 298 299 300 301 302
  }
  delete[] CPUload[0]; 
  delete[] CPUload; 

  return value + existingCPUload; 
}