Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compute the minimum makespan for a Uniform Machine Sheduling Problem
- // using a brute force approach
- #include <stdio.h>
- // compute the integer power
- int ipow(int base, int exp)
- {
- int result = 1;
- while (exp)
- {
- if (exp & 1)
- result *= base;
- exp >>= 1;
- base *= base;
- }
- return result;
- }
- // This function computes the digits of 'i' in base 'b'
- // this corresponds basically to the permutation without repetition of 'b' types on arraySize
- void fill_assignment_i(int i, int b, int* array_to_fill, int array_size)
- {
- // reset the array
- for(int j = 0; j < array_size; j++)
- array_to_fill[j] = 0;
- int index = 0 ;
- while ( i != 0 )
- {
- int remainder = i % b ;
- i = i / b ;
- array_to_fill[index] = remainder;
- index++;
- }
- }
- // compute the makespan
- double compute_time(double** durations, int* assignment, int nprocessors, int ntasks)
- {
- double makespan = 0;
- for(int p = 0; p < nprocessors; p++)
- {
- double time = 0;
- for(int i = 0; i < ntasks; i++)
- {
- if(assignment[i] == p)
- {
- time += durations[i][p];
- }
- }
- if(makespan < time)
- makespan = time;
- }
- return makespan;
- }
- // main
- int main()
- {
- int nprocessors = 3;
- int ntasks = 5;
- double values[5][3] =
- {
- { 1.0, 2.0, 8.0 },
- { 4.0, 5.0, 6.0 },
- { 1.0, 3.0, 3.0 },
- { 8.0, 5.0, 4.0 },
- { 7.0, 2.0, 1.0 },
- };
- double** durations = new double*[ntasks];
- for(int t=0; t < ntasks; t++)
- {
- durations[t] = new double[nprocessors];
- for(int p=0; p < nprocessors; p++)
- durations[t][p] = values[t][p];
- }
- // *********** schedule ************
- int possibleAssignments = ipow(nprocessors,ntasks);
- int min_makespan = -1;
- // this loop enumerates all the possible assignment task-processor
- int* assignment = new int[ntasks];
- for(int i = 0; i < possibleAssignments; i++)
- {
- fill_assignment_i(i,nprocessors,assignment,ntasks);
- // now assignment contains the current assignment task-processor
- double makespan = compute_time(durations,assignment, nprocessors, ntasks);
- if(min_makespan < 0 || min_makespan > makespan)
- min_makespan = makespan;
- }
- // *********************************
- printf("Min makespan: %f",min_makespan);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement