#include #include #include #include #include #include #include "nmglobal.h" #include "nbench1.h" /************************* ** ASSIGNMENT ALGORITHM ** *************************/ /* ** DEFINES */ #define ASSIGNROWS 101L #define ASSIGNCOLS 101L /* ** TYPEDEFS */ typedef struct { union { long *p; long (*ap)[ASSIGNROWS][ASSIGNCOLS]; } ptrs; } longptr; /* ** PROTOTYPES */ static unsigned long DoAssignIteration(long *arraybase, unsigned long numarrays); static void LoadAssignArrayWithRand(long *arraybase, unsigned long numarrays); static void LoadAssign(long arraybase[][ASSIGNCOLS]); static void CopyToAssign(long arrayfrom[][ASSIGNCOLS], long arrayto[][ASSIGNCOLS]); static void Assignment(long arraybase[][ASSIGNCOLS]); static void calc_minimum_costs(long tableau[][ASSIGNCOLS]); static int first_assignments(long tableau[][ASSIGNCOLS], short assignedtableau[][ASSIGNCOLS]); static void second_assignments(long tableau[][ASSIGNCOLS], short assignedtableau[][ASSIGNCOLS]); /************* ** DoAssign ** ************** ** Perform an assignment algorithm. ** The algorithm was adapted from the step by step guide found ** in "Quantitative Decision Making for Business" (Gordon, ** Pressman, and Cohn; Prentice-Hall) ** ** ** NOTES: ** 1. Even though the algorithm distinguishes between ** ASSIGNROWS and ASSIGNCOLS, as though the two might ** be different, it does presume a square matrix. ** I.E., ASSIGNROWS and ASSIGNCOLS must be the same. ** This makes for some algorithmically-correct but ** probably non-optimal constructs. ** */ void DoAssign(void) { AssignStruct *locassignstruct = &global_assignstruct; /* Local structure ptr */ long *arraybase = NULL; char *context = "CPU:Assignment"; unsigned long accumtime; double iterations; /* ** See if we need to do self adjustment code. */ if(locassignstruct->adjust==0) { /* ** Self-adjustment code. The system begins by working on 1 ** array. If it does that in no time, then two arrays ** are built. This process continues until ** enough arrays are built to handle the tolerance. */ locassignstruct->numarrays=1; while(1) { arraybase = realloc(arraybase, sizeof(long) * ASSIGNROWS * ASSIGNCOLS * locassignstruct->numarrays); if (!arraybase) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } /* ** Do an iteration of the assignment alg. If the ** elapsed time is less than or equal to the permitted ** minimum, then allocate for more arrays and ** try again. */ if(DoAssignIteration(arraybase, locassignstruct->numarrays)>global_min_ticks) break; /* We're ok...exit */ locassignstruct->numarrays++; } } else { arraybase = malloc(sizeof(long) * ASSIGNROWS * ASSIGNCOLS * locassignstruct->numarrays); if (!arraybase) { fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); exit(1); } } /* ** All's well if we get here. Do the tests. */ accumtime=0L; iterations=(double)0.0; do { accumtime+=DoAssignIteration(arraybase, locassignstruct->numarrays); iterations+=(double)1.0; } while(TicksToSecs(accumtime)request_secs); /* ** Clean up, calculate results, and go home. Be sure to ** show that we don't have to rerun adjustment code. */ free(arraybase); locassignstruct->iterspersec=iterations * (double)locassignstruct->numarrays / TicksToFracSecs(accumtime); if(locassignstruct->adjust==0) locassignstruct->adjust=1; return; } /********************** ** DoAssignIteration ** *********************** ** This routine executes one iteration of the assignment test. ** It returns the number of ticks elapsed in the iteration. */ static unsigned long DoAssignIteration(long *arraybase, unsigned long numarrays) { longptr abase; /* local pointer */ unsigned long elapsed; /* Elapsed ticks */ unsigned long i; /* ** Set up local pointer */ abase.ptrs.p=arraybase; /* ** Load up the arrays with a random table. */ LoadAssignArrayWithRand(arraybase,numarrays); /* ** Start the stopwatch */ elapsed=StartStopwatch(); /* ** Execute assignment algorithms */ for(i=0;i1) for(i=1;i