#include #include #include #include #include #include #include #include "nmglobal.h" #include "randnum.h" /************************* ** ASSIGNMENT ALGORITHM ** *************************/ /* ** DEFINES */ #define ASSIGNROWS 101L #define ASSIGNCOLS 101L /* ** TYPEDEFS */ typedef struct { union { long *p; long (*ap)[ASSIGNROWS][ASSIGNCOLS]; } ptrs; } longptr; /* ** PROTOTYPES */ static clock_t 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) { const char* context = "CPU:Assignment"; AssignStruct* locassignstruct = &global_assignstruct; /* Local structure ptr */ long* arraybase = NULL; unsigned long total_time = 0; int iterations = 0; /* ** See if we need to do self adjustment code. */ if (locassignstruct->adjust == FALSE) { locassignstruct->adjust = TRUE; /* ** 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; } ++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); } } do { total_time += DoAssignIteration(arraybase, locassignstruct->numarrays); ++iterations; } while (total_time < locassignstruct->request_secs * CLOCKS_PER_SEC); free(arraybase); locassignstruct->iterspersec = (double)(iterations * CLOCKS_PER_SEC *locassignstruct->numarrays) / (double)total_time; } /********************** ** DoAssignIteration ** *********************** ** This routine executes one iteration of the assignment test. ** It returns the number of ticks elapsed in the iteration. */ static clock_t DoAssignIteration(long *arraybase, unsigned long numarrays) { clock_t start, stop; longptr abase; unsigned long i; abase.ptrs.p=arraybase; LoadAssignArrayWithRand(arraybase,numarrays); start = clock(); for (i = 0; i < numarrays; i++) { /* abase.ptrs.p+=i*ASSIGNROWS*ASSIGNCOLS; */ /* Fixed by Eike Dierks */ Assignment(*abase.ptrs.ap); abase.ptrs.p += ASSIGNROWS * ASSIGNCOLS; } stop = clock(); return stop - start; } /**************************** ** LoadAssignArrayWithRand ** ***************************** ** Load the assignment arrays with random numbers. All positive. ** These numbers represent costs. */ static void LoadAssignArrayWithRand(long *arraybase, unsigned long numarrays) { longptr abase,abase1; /* Local for array pointer */ unsigned long i; /* ** Set local array pointer */ abase.ptrs.p=arraybase; abase1.ptrs.p=arraybase; /* ** Set up the first array. Then just copy it into the ** others. */ LoadAssign(*(abase.ptrs.ap)); if(numarrays>1) for(i=1;i