Advertisement
digemall

Uniform Machine Scheduling Problem by brute force

May 1st, 2012
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1.     // Compute the minimum makespan for a Uniform Machine Sheduling Problem
  2.     // using a brute force approach
  3.     #include <stdio.h>
  4.    
  5.     // compute the integer power
  6.     int ipow(int base, int exp)
  7.     {
  8.         int result = 1;
  9.         while (exp)
  10.         {
  11.             if (exp & 1)
  12.                 result *= base;
  13.             exp >>= 1;
  14.             base *= base;
  15.         }
  16.         return result;
  17.     }
  18.    
  19.     // This function computes the digits of 'i' in base 'b'
  20.     // this corresponds basically to the permutation without repetition of 'b' types on arraySize
  21.     void fill_assignment_i(int i, int b, int* array_to_fill, int array_size)
  22.     {
  23.         // reset the array
  24.         for(int j = 0; j < array_size; j++)
  25.             array_to_fill[j] = 0;
  26.         int index = 0 ;
  27.         while ( i != 0 )
  28.         {
  29.             int remainder = i % b ;  
  30.             i = i / b ;
  31.             array_to_fill[index] = remainder;
  32.             index++;
  33.         }
  34.     }
  35.     // compute the makespan
  36.     double compute_time(double** durations, int* assignment, int  nprocessors, int ntasks)
  37.     {
  38.         double makespan = 0;
  39.         for(int p = 0; p < nprocessors; p++)
  40.         {
  41.             double time = 0;
  42.             for(int i = 0; i < ntasks; i++)
  43.             {
  44.                 if(assignment[i] == p)
  45.                 {
  46.                     time += durations[i][p];
  47.                 }
  48.             }
  49.             if(makespan < time)
  50.                 makespan = time;
  51.         }
  52.         return makespan;
  53.     }
  54.     // main
  55.     int main()
  56.     {
  57.         int nprocessors = 3;
  58.         int ntasks = 5;
  59.         double values[5][3] =
  60.         {
  61.             { 1.0, 2.0, 8.0 },
  62.             { 4.0, 5.0, 6.0 },
  63.             { 1.0, 3.0, 3.0 },
  64.             { 8.0, 5.0, 4.0 },
  65.             { 7.0, 2.0, 1.0 },
  66.         };
  67.         double** durations = new double*[ntasks];
  68.         for(int t=0; t < ntasks; t++)
  69.         {
  70.             durations[t] = new double[nprocessors];
  71.             for(int p=0; p < nprocessors; p++)
  72.                 durations[t][p] = values[t][p];
  73.         }
  74.    
  75.         // *********** schedule ************
  76.         int possibleAssignments = ipow(nprocessors,ntasks);
  77.         int min_makespan = -1;
  78.         // this loop enumerates all the possible assignment task-processor
  79.         int* assignment = new int[ntasks];
  80.         for(int i = 0; i < possibleAssignments; i++)
  81.         {
  82.             fill_assignment_i(i,nprocessors,assignment,ntasks);
  83.             // now assignment contains the current assignment task-processor
  84.             double makespan = compute_time(durations,assignment, nprocessors, ntasks);
  85.             if(min_makespan < 0 || min_makespan > makespan)
  86.                 min_makespan = makespan;
  87.         }
  88.         // *********************************
  89.         printf("Min makespan: %f",min_makespan);
  90.         return 0;
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement