// initializing an array, sort it and check it void testFoursort() { printf("Running testFoursort ...\n"); int siz = 1024 * 1024 * 8; // int siz = 1024 * 1024; // int siz = 15; // create array struct intval *pi; void **A = myMalloc("testFoursort 1", sizeof(pi) * siz); int i; for (i = 0; i < siz; i++) { pi = myMalloc("testFoursort 2", sizeof (struct intval)); A[i] = pi; }; // fill its content fillarray(A, siz, 100); // sort it // int t0 = clock(); struct timeval tim; gettimeofday(&tim, NULL); double t0=tim.tv_sec+(tim.tv_usec/1000000.0); int compareIntVal(); foursort(A, siz, compareIntVal, NUMTHREADS); // int t1 = clock(); gettimeofday(&tim, NULL); double t1=tim.tv_sec+(tim.tv_usec/1000000.0); // and check it check(A, 0, siz-1); // printf("Sorting size: %d time: %d\n", siz, t1-t0); printf("Sorting time: siz: %d duration: %.3lf\n", siz, t1-t0); } // end testFoursort()
// Run an algorithm and report the time used void timeTest() { printf("timeTest() of foursort \n"); double algTime, T; int seed; int seedLimit = 10; int z; int siz = 1024 * 1024 * 16; // construct array struct intval *pi; void **A = myMalloc("timeTest 1", sizeof(pi) * siz); int i; for (i = 0; i < siz; i++) { pi = myMalloc("timeTest 2", sizeof (struct intval)); A[i] = pi; }; // warm up the process fillarray(A, siz, 666); int sumTimes = 0; for (z = 0; z < 3; z++) { // repeat to check stability algTime = 0; // measure the array fill time // int TFill = clock(); struct timeval tim; gettimeofday(&tim, NULL); double TFILL=tim.tv_sec+(tim.tv_usec/1000000.0); for (seed = 0; seed < seedLimit; seed++) fillarray(A, siz, seed); // here alternative ways to fill the array // for ( k = 0; k < siz; k++ ) A[k] = 0; // for ( k = 0; k < siz; k++ ) A[k] = k%5; // for ( k = 0; k < siz; k++ ) A[k] = siz-k; gettimeofday(&tim, NULL); TFILL=tim.tv_sec+(tim.tv_usec/1000000.0) - TFILL; // now we know how much time it takes to fill the array // measure the time to fill & sort the array // T = clock(); gettimeofday(&tim, NULL); T=tim.tv_sec+(tim.tv_usec/1000000.0); for (seed = 0; seed < seedLimit; seed++) { fillarray(A, siz, seed); // for ( k = 0; k < siz; k++ ) A[k] = 0; // for ( k = 0; k < siz; k++ ) A[k] = k%5; // for ( k = 0; k < siz; k++ ) A[k] = siz-k; foursort(A, siz, compareIntVal, NUMTHREADS); } // ... and subtract the fill time to obtain the sort time // algTime = clock() - T - TFill; gettimeofday(&tim, NULL); algTime=tim.tv_sec+(tim.tv_usec/1000000.0) - T - TFILL; printf("algTime: %f \n", algTime); sumTimes = sumTimes + algTime; } printf("%s %d %s", "sumTimes: ", sumTimes, "\n"); } // end timeTest()
void validateFourSortBT() { // validation on the Bentley bench test against heapsort printf("Entering validateFourSortBT Sawtooth ........\n"); // printf("Entering validateFourSortBT Rand2 ........\n"); // printf("Entering validateFourSortBT Plateau ........\n"); // printf("Entering validateFourSortBT Shuffle ........\n"); // printf("Entering validateFourSortBT Stagger ........\n"); int sortcBTime, cut2Time, T; int seed = 666; int z; int siz = 1024*1024; // int limit = 1024 * 1024 * 16 + 1; // int seedLimit = 32 * 1024; int limit = siz + 1; // int seedLimit = 32; int seedLimit = 1; float frac; while (siz <= limit) { printf("%s %d %s %d %s", "siz: ", siz, " seedLimit: ", seedLimit, "\n"); // int A[siz]; struct intval *pi; void **A = myMalloc("compareAlgorithms0 1", sizeof(pi) * siz); void **B = myMalloc("compareAlgorithms0 3", sizeof(pi) * siz); // construct array int i; for (i = 0; i < siz; i++) { pi = myMalloc("compareAlgorithms0 2", sizeof (struct intval)); A[i] = pi; pi = myMalloc("compareAlgorithms0 4", sizeof (struct intval)); B[i] = pi; }; // warm up the process fillarray(A, siz, seed); int TFill, m, tweak; int sortcBCnt, cut2Cnt; // , sortcBCntx, cut2Cntx; int sumQsortB, sumCut2; // , sumQsortBx, sumCut2x; // for (z = 0; z < 3; z++) { // repeat to check stability for (z = 0; z < 1; z++) { // repeat to check stability sortcBCnt = cut2Cnt = sumQsortB = sumCut2 = 0; // sortcBCntx = cut2Cntx = sumQsortBx = sumCut2x = 0; for (m = 1; m < 2 * siz; m = m * 2) { // m = 1024 * 1024; { // m = 1; { for (tweak = 0; tweak <= 5; tweak++ ) { // tweak = 5; { sortcBTime = 0; cut2Time = 0; TFill = clock(); for (seed = 0; seed < seedLimit; seed++) sawtooth(A, siz, m, tweak); // rand2(A, siz, m, tweak, seed); // plateau(A, siz, m, tweak); // shuffle(A, siz, m, tweak, seed); // stagger(A, siz, m, tweak); TFill = clock() - TFill; T = clock(); for (seed = 0; seed < seedLimit; seed++) { sawtooth(A, siz, m, tweak); // rand2(A, siz, m, tweak, seed); // plateau(A, siz, m, tweak); // shuffle(A, siz, m, tweak, seed); // stagger(A, siz, m, tweak); callHeapSort(A, siz, compareIntVal); } sortcBTime = sortcBTime + clock() - T - TFill; sumQsortB += sortcBTime; // if ( 4 != tweak ) sumQsortBx += sortcBTime; T = clock(); for (seed = 0; seed < seedLimit; seed++) { sawtooth(B, siz, m, tweak); // rand2(B, siz, m, tweak, seed); // plateau(B, siz, m, tweak); // shuffle(B, siz, m, tweak, seed); // stagger(B, siz, m, tweak); foursort(B, siz, compareIntVal, NUMTHREADS); } cut2Time = cut2Time + clock() - T - TFill; sumCut2 += cut2Time; // if ( 4 != tweak ) sumCut2x += cut2Time; printf("Size: %d m: %d tweak: %d ", siz, m, tweak); printf("sortcBTime: %d ", sortcBTime); printf("Cut2Time: %d ", cut2Time); frac = 0; if ( sortcBTime != 0 ) frac = cut2Time / ( 1.0 * sortcBTime ); printf("frac: %f \n", frac); if ( sortcBTime < cut2Time ) sortcBCnt++; else cut2Cnt++; for (i = 0; i < siz; i++) { if ( compareIntVal(A[i], B[i]) != 0 ) { printf("***** validateFourSortBT m: %i tweak: %i at i: %i\n", m, tweak, i); exit(0); } } } printf("sumQsortB: %i sumCut2: %i frac: %f", sumQsortB, sumCut2, (sumCut2/(1.0 * sumQsortB))); printf(" sortcBCnt: %i cut2Cnt: %i\n", sortcBCnt, cut2Cnt); } frac = 0; if ( sumQsortB != 0 ) frac = sumCut2 / ( 1.0 * sumQsortB ); printf("Measurements:\n"); printf("sumQsortB: %i sumCut2: %i frac: %f", sumQsortB, sumCut2, (sumCut2/(1.0 * sumQsortB))); printf(" sortcBCnt: %i cut2Cnt: %i\n", sortcBCnt, cut2Cnt); // printf("sumQsortBx: %i sumCut2x: %i", sumQsortBx, sumCut2x); // printf(" sortcBCntx: %i cut2Cntx: %i\n", sortcBCntx, cut2Cntx); } // free array for (i = 0; i < siz; i++) { free(A[i]); free(B[i]); }; free(A); free(B); siz = siz * 2; seedLimit = seedLimit / 2; } } // end validateFourSortBT