static void fillarray(Symbol* basetype, Dimset* dimset, int index, Datalist* arraylist) { int i; Symbol* dim = dimset->dimsyms[index]; unsigned int size = dim->dim.declsize; int isunlimited = (size == 0); int lastdim = (index == (dimset->ndims - 1)); int firstdim = (index == 0); Datalist* sublist; sublist = (firstdim?builddatalist(0):arraylist); if(isunlimited) { /* do a single entry to satisfy*/ if(lastdim) { filllist(basetype,sublist); } else { fillarray(basetype->typ.basetype,dimset,index+1,sublist); } } else { /* bounded*/ if(lastdim) { for(i=0;i<size;i++) filllist(basetype,sublist); } else { for(i=0;i<size;i++) { fillarray(basetype->typ.basetype,dimset,index+1,sublist); } } } }
// Run an algorithm and report the time used void timeTest() { double algTime, T; int seed; int seedLimit = 3; int z; // int siz = 1024 * 1024 * 16; int siz = 1024 * 1024 * 4; printf("timeTest() on size: %d \n", siz); // 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); float 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 // int k; // 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; // TFill = clock() - TFill; 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); callCut2(A, siz, compareIntVal); } // ... 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 %f %s", "sumTimes: ", sumTimes, "\n"); } // end timeTest()
static void fill(Symbol* tsym, Datalist* filler) { int i; NCConstant con; Datalist* sublist; con.filled = 0; ASSERT(tsym->objectclass == NC_TYPE); switch (tsym->subclass) { case NC_ENUM: case NC_OPAQUE: case NC_PRIM: con.nctype = tsym->typ.typecode; nc_getfill(&con); break; case NC_COMPOUND: sublist = builddatalist(listlength(tsym->subnodes)); for(i=0;i<listlength(tsym->subnodes);i++) { Symbol* field = (Symbol*)listget(tsym->subnodes,i); if(field->typ.dimset.ndims > 0) { fillarray(field->typ.basetype,&field->typ.dimset,0,filler); } else filllist(field->typ.basetype,sublist); } con = builddatasublist(sublist); break; case NC_VLEN: sublist = builddatalist(0); filllist(tsym->typ.basetype,sublist); /* generate a single instance*/ con = builddatasublist(sublist); break; default: PANIC1("fill: unexpected subclass %d",tsym->subclass); } dlappend(filler,&con); }
// 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()
int main() { int bestnum=0; fillarray(); for(int remainder=0,curchain=1,maxchain=0,primendx=3;prime[primendx]<1000;primendx++,remainder=9,curchain=1) { do { remainder=remainder*10+9;//add extra digit of 9 when not divisible remainder=remainder%prime[primendx];//only care about remainder curchain++; }while(remainder!=0); if(curchain>maxchain) { maxchain=curchain; bestnum=prime[primendx]; } } printf("%d",bestnum); scanf("%d",&bestnum); return 0; }
int main(){ int a[LENGTH]; // COMMENT THIS OUT TO ALWAYS GET THE SAME VALUES srand(time(NULL)); // set seed for rand with unix time, //otherwise same sequence each run printf("First random number is %d\n", rand() ); fillarray (a, LENGTH); // fill the arrray randomly printarray(a, LENGTH); // show initial array sort (a, LENGTH); // sort it printarray(a, LENGTH); // show final array return 0;}
int main() { double vals[NUM]; fillarray(vals, NUM); puts("Random list: "); showarray(vals, NUM); qsort(vals, NUM, sizeof(double), mycomp); puts("\nSorted list: "); showarray(vals, NUM); return 0; }
void main(void) { int array[MAX]; int x; printf("wuwedi x\n"); scanf("%d\n", &x); fillarray(array); sortarray(array); printf("sortiraniyat masiw:"); printarray(array); }
// validateAlgorithm0 is used to check algorithm alg1 against a // trusted algorithm alg2 // The check consists of making sure that starting from identical // inputs they produce identical outputs // alg1 is a parallel one, alg2 is sequential void validateAlgorithm0(char* label, int siz, void (*alg1)(), void (*alg2)() ) { printf("%s on size: %d\n", label, siz); // create the input for alg1 ... struct intval *pi; void **A = myMalloc("validateAlgorithm0 1", sizeof(pi) * siz); int i; for (i = 0; i < siz; i++) { pi = myMalloc("validateAlgorithm0 2", sizeof (struct intval)); A[i] = pi; }; fillarray(A, siz, 100); // ... sort it int compareIntVal(); (*alg1)(A, siz, compareIntVal, NUMTHREADS); // create the input for alg2 ... void **B = myMalloc("validateAlgorithm0 3", sizeof(pi) * siz); // int i; // struct intval *pi; for (i = 0; i < siz; i++) { pi = myMalloc("validateAlgorithm0 4", sizeof (struct intval)); B[i] = pi; }; fillarray(B, siz, 100); // ... sort it // int compareIntVal(); (*alg2)(B, siz, compareIntVal); // check that the two outputs are the same int foundError = 0; for (i = 0; i < siz; i++) // if ( A[i] != B[i] ) { if ( compareIntVal(A[i], B[i]) != 0 ) { printf("validate error i: %d\n", i); foundError = 1; } if ( !foundError ) printf("NO error found ...\n"); } // end validateAlgorithm0
static Datalist* buildfill(Symbol* tvasym) { Datalist* filler = builddatalist(0); ASSERT(tvasym->objectclass == NC_VAR || tvasym->objectclass == NC_TYPE || tvasym->objectclass == NC_ATT); if(tvasym->objectclass == NC_VAR) { if(tvasym->typ.dimset.ndims > 0) { fillarray(tvasym->typ.basetype,&tvasym->typ.dimset,0,filler); } else fill(tvasym->typ.basetype,filler); } else if(tvasym->objectclass == NC_ATT) { fill(tvasym->typ.basetype,filler); } else /*NC_TYPE*/ fill(tvasym,filler); return filler; }
// alg1 is a sequential algorithm void testAlgorithm0S(char* label, int siz, void (*alg1)() ) { printf("%s on size: %d\n", label, siz); // create array struct intval *pi; void **A = myMalloc("testAlgorithm0 1", sizeof(pi) * siz); int i; for (i = 0; i < siz; i++) { pi = myMalloc("testAlgorithm0 2", sizeof (struct intval)); A[i] = pi; }; // fill its content fillarray(A, siz, 100); // sort it int compareIntVal(); (*alg1)(A, siz, compareIntVal); // and check it check(A, 0, siz-1); } // end testAlgorithm0S
/* libcall(const libname[], const funcname[], const typestring[], ...) * * Loads the DLL or shared library if not yet loaded (the name comparison is * case sensitive). * * typestring format: * Whitespace is permitted between the types, but not inside the type * specification. The string "ii[4]&u16s" is equivalent to "i i[4] &u16 s", * but the latter is easier on the eye. * * types: * i = signed integer, 16-bit in Windows 3.x, else 32-bit in Win32 and Linux * u = unsigned integer, 16-bit in Windows 3.x, else 32-bit in Win32 and Linux * f = IEEE floating point, 32-bit * p = packed string * s = unpacked string * The difference between packed and unpacked strings is only relevant when * the parameter is passed by reference (see below). * * pass-by-value and pass-by-reference: * By default, parameters are passed by value. To pass a parameter by * reference, prefix the type letter with an "&": * &i = signed integer passed by reference * i = signed integer passed by value * Same for '&u' versus 'u' and '&f' versus 'f'. * * Arrays are passed by "copy & copy-back". That is, libcall() allocates a * block of dynamic memory to copy the array into. On return from the foreign * function, libcall() copies the array back to the abstract machine. The * net effect is similar to pass by reference, but the foreign function does * not work in the AMX stack directly. During the copy and the copy-back * operations, libcall() may also transform the array elements, for example * between 16-bit and 32-bit elements. This is done because Pawn only * supports a single cell size, which may not fit the required integer size * of the foreign function. * * See "element ranges" for the syntax of passing an array. * * Strings may either be passed by copy, or by "copy & copy-back". When the * string is an output parameter (for the foreign function), the size of the * array that will hold the return string must be indicated between square * brackets behind the type letter (see "element ranges"). When the string * is "input only", this is not needed --libcall() will determine the length * of the input string itself. * * The tokens 'p' and 's' are equivalent, but 'p[10]' and 's[10]' are not * equivalent: the latter syntaxes determine whether the output from the * foreign function will be stored as a packed or an unpacked string. * * element sizes: * Add an integer behind the type letter; for example, 'i16' refers to a * 16-bit signed integer. Note that the value behind the type letter must * be either 8, 16 or 32. * * You should only use element size specifiers on the 'i' and 'u' types. That * is, do not use these specifiers on 'f', 's' and 'p'. * * element ranges: * For passing arrays, the size of the array may be given behind the type * letter and optional element size. The token 'u[4]' indicates an array of * four unsigned integers, which are typically 32-bit. The token 'i16[8]' * is an array of 8 signed 16-bit integers. Arrays are always passed by * "copy & copy-back" * * When compiled as Unicode, this library converts all strings to Unicode * strings. * * The calling convention for the foreign functions is assumed: * - "__stdcall" for Win32, * - "far pascal" for Win16 * - and the GCC default for Unix/Linux (_cdecl) * * C++ name mangling of the called function is not handled (there is no standard * convention for name mangling, so there is no portable way to convert C++ * function names to mangled names). Win32 name mangling (used by default by * Microsoft compilers on functions declared as __stdcall) is also not handled. * * Returns the value of the called function. */ static cell AMX_NATIVE_CALL n_libcall(AMX *amx, const cell *params) { const TCHAR *libname, *funcname, *typestring; MODLIST *item; int paramidx, typeidx, idx; PARAM ps[MAXPARAMS]; cell *cptr,result; LIBFUNC LibFunc; amx_StrParam(amx, params[1], libname); item = findlib(&ModRoot, amx, libname); if (item == NULL) item = addlib(&ModRoot, amx, libname); if (item == NULL) { amx_RaiseError(amx, AMX_ERR_NATIVE); return 0; } /* if */ /* library is loaded, get the function */ amx_StrParam(amx, params[2], funcname); LibFunc=(LIBFUNC)SearchProcAddress(item->inst, funcname); if (LibFunc==NULL) { amx_RaiseError(amx, AMX_ERR_NATIVE); return 0; } /* if */ #if defined HAVE_DYNCALL_H /* (re-)initialize the dyncall library */ if (dcVM==NULL) { dcVM=dcNewCallVM(4096); dcMode(dcVM,DC_CALL_C_X86_WIN32_STD); } /* if */ dcReset(dcVM); #endif /* decode the parameters */ paramidx=typeidx=0; amx_StrParam(amx, params[3], typestring); while (paramidx < MAXPARAMS && typestring[typeidx]!=__T('\0')) { /* skip white space */ while (typestring[typeidx]!=__T('\0') && typestring[typeidx]<=__T(' ')) typeidx++; if (typestring[typeidx]==__T('\0')) break; /* save "pass-by-reference" token */ ps[paramidx].type=0; if (typestring[typeidx]==__T('&')) { ps[paramidx].type=BYREF; typeidx++; } /* if */ /* store type character */ ps[paramidx].type |= (unsigned char)typestring[typeidx]; typeidx++; /* set default size, then check for an explicit size */ #if defined __WIN32__ || defined _WIN32 || defined WIN32 ps[paramidx].size=32; #elif defined _Windows ps[paramidx].size=16; #endif if (_istdigit(typestring[typeidx])) { ps[paramidx].size=(unsigned char)_tcstol(&typestring[typeidx],NULL,10); while (_istdigit(typestring[typeidx])) typeidx++; } /* if */ /* set default range, then check for an explicit range */ ps[paramidx].range=1; if (typestring[typeidx]=='[') { ps[paramidx].range=_tcstol(&typestring[typeidx+1],NULL,10); while (typestring[typeidx]!=']' && typestring[typeidx]!='\0') typeidx++; ps[paramidx].type |= BYREF; /* arrays are always passed by reference */ typeidx++; /* skip closing ']' too */ } /* if */ /* get pointer to parameter */ cptr=amx_Address(amx,params[paramidx+4]); switch (ps[paramidx].type) { case 'i': /* signed integer */ case 'u': /* unsigned integer */ case 'f': /* floating point */ assert(ps[paramidx].range==1); ps[paramidx].v.val=(int)*cptr; break; case 'i' | BYREF: case 'u' | BYREF: case 'f' | BYREF: ps[paramidx].v.ptr=cptr; if (ps[paramidx].range>1) { /* convert array and pass by address */ ps[paramidx].v.ptr = fillarray(amx, &ps[paramidx], cptr); } /* if */ break; case 'p': case 's': case 'p' | BYREF: case 's' | BYREF: if (ps[paramidx].type=='s' || ps[paramidx].type=='p') { int len; /* get length of input string */ amx_StrLen(cptr,&len); len++; /* include '\0' */ /* check max. size */ if (len<ps[paramidx].range) len=ps[paramidx].range; ps[paramidx].range=len; } /* if */ ps[paramidx].v.ptr=malloc(ps[paramidx].range*sizeof(TCHAR)); if (ps[paramidx].v.ptr==NULL) return amx_RaiseError(amx, AMX_ERR_NATIVE); amx_GetString((char *)ps[paramidx].v.ptr,cptr,sizeof(TCHAR)>1,UNLIMITED); break; default: /* invalid parameter type */ return amx_RaiseError(amx, AMX_ERR_NATIVE); } /* switch */ paramidx++; } /* while */ if ((params[0]/sizeof(cell)) - 3 != (size_t)paramidx) return amx_RaiseError(amx, AMX_ERR_NATIVE); /* format string does not match number of parameters */ #if defined HAVE_DYNCALL_H for (idx = 0; idx < paramidx; idx++) { if ((ps[idx].type=='i' || ps[idx].type=='u' || ps[idx].type=='f') && ps[idx].range==1) { switch (ps[idx].size) { case 8: dcArgChar(dcVM,(unsigned char)(ps[idx].v.val & 0xff)); break; case 16: dcArgShort(dcVM,(unsigned short)(ps[idx].v.val & 0xffff)); break; default: dcArgLong(dcVM,ps[idx].v.val); } /* switch */ } else { dcArgPointer(dcVM,ps[idx].v.ptr); } /* if */ } /* for */ result=(cell)dcCallPointer(dcVM,(void*)LibFunc); #else /* HAVE_DYNCALL_H */ /* push the parameters to the stack (left-to-right in 16-bit; right-to-left * in 32-bit) */ #if defined __WIN32__ || defined _WIN32 || defined WIN32 for (idx=paramidx-1; idx>=0; idx--) { #else for (idx=0; idx<paramidx; idx++) { #endif if ((ps[idx].type=='i' || ps[idx].type=='u' || ps[idx].type=='f') && ps[idx].range==1) { switch (ps[idx].size) { case 8: push((unsigned char)(ps[idx].v.val & 0xff)); break; case 16: push((unsigned short)(ps[idx].v.val & 0xffff)); break; default: push(ps[idx].v.val); } /* switch */ } else { push(ps[idx].v.ptr); } /* if */ } /* for */ /* call the function; all parameters are already pushed to the stack (the * function should remove the parameters from the stack) */ result=LibFunc(); #endif /* HAVE_DYNCALL_H */ /* store return values and free allocated memory */ for (idx=0; idx<paramidx; idx++) { switch (ps[idx].type) { case 'p': case 's': free(ps[idx].v.ptr); break; case 'p' | BYREF: case 's' | BYREF: cptr=amx_Address(amx,params[idx+4]); amx_SetString(cptr,(char *)ps[idx].v.ptr,ps[idx].type==('p'|BYREF),sizeof(TCHAR)>1,UNLIMITED); free(ps[idx].v.ptr); break; case 'i': case 'u': case 'f': assert(ps[idx].range==1); break; case 'i' | BYREF: case 'u' | BYREF: case 'f' | BYREF: cptr=amx_Address(amx,params[idx+4]); if (ps[idx].range==1) { /* modify directly in the AMX (no memory block was allocated */ switch (ps[idx].size) { case 8: *cptr= (ps[idx].type==('i' | BYREF)) ? (long)((signed char)*cptr) : (*cptr & 0xff); break; case 16: *cptr= (ps[idx].type==('i' | BYREF)) ? (long)((short)*cptr) : (*cptr & 0xffff); break; } /* switch */ } else { int i; for (i=0; i<ps[idx].range; i++) { switch (ps[idx].size) { case 8: *cptr= (ps[idx].type==('i' | BYREF)) ? ((signed char*)ps[idx].v.ptr)[i] : ((unsigned char*)ps[idx].v.ptr)[i]; break; case 16: *cptr= (ps[idx].type==('i' | BYREF)) ? ((short*)ps[idx].v.ptr)[i] : ((unsigned short*)ps[idx].v.ptr)[i]; break; default: *cptr= (ps[idx].type==('i' | BYREF)) ? ((long*)ps[idx].v.ptr)[i] : ((unsigned long*)ps[idx].v.ptr)[i]; } /* switch */ } /* for */ free((char *)ps[idx].v.ptr); } /* if */ break; default: assert(0); } /* switch */ } /* for */ return result; } /* bool: libfree(const libname[]="") * When the name is an empty string, this function frees all libraries (for this * abstract machine). The name comparison is case sensitive. * Returns true if one or more libraries were freed. */ static cell AMX_NATIVE_CALL n_libfree(AMX *amx, const cell *params) { const TCHAR *libname; amx_StrParam(amx,params[1],libname); return freelib(&ModRoot,amx,libname) > 0; } #else /* HAVE_DYNCALL_H || WIN32_FFI */ static cell AMX_NATIVE_CALL n_libcall(AMX *amx, const cell *params) { (void)amx; (void)params; return 0; }
// Report the speed fraction of two algorithms on a range of array sizes // alg1 is parallel, alg2 is sequential void compareAlgorithms0(char *label, int siz, int seedLimit, void (*alg1)(), void (*alg2)() ) { printf("%s on size: %d seedLimit: %d #treads: %d\n", label, siz, seedLimit, NUMTHREADS); double alg1Time, alg2Time, T; int seed; int z; int limit = 1024 * 1024 * 16 + 1; while (siz <= limit) { printf("%s %d %s %d %s", "siz: ", siz, " seedLimit: ", seedLimit, "\n"); struct intval *pi; void **A = myMalloc("compareAlgorithms0 1", sizeof(pi) * siz); // construct array int i; for (i = 0; i < siz; i++) { pi = myMalloc("compareAlgorithms0 2", sizeof (struct intval)); A[i] = pi; }; // warm up the process for (seed = 0; seed < seedLimit; seed++) fillarray(A, siz, seed); for (z = 0; z < 4; z++) { // repeat to check stability alg1Time = 0; alg2Time = 0; // 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); // TFill = clock() - TFill; gettimeofday(&tim, NULL); TFILL=tim.tv_sec+(tim.tv_usec/1000000.0) - TFILL; // T = clock(); gettimeofday(&tim, NULL); T=tim.tv_sec+(tim.tv_usec/1000000.0); for (seed = 0; seed < seedLimit; seed++) { fillarray(A, siz, seed); (*alg1)(A, siz, compareIntVal, NUMTHREADS); } // alg1Time = clock() - T - TFill; gettimeofday(&tim, NULL); alg1Time=tim.tv_sec+(tim.tv_usec/1000000.0) - T - TFILL; // T = clock(); gettimeofday(&tim, NULL); T=tim.tv_sec+(tim.tv_usec/1000000.0); for (seed = 0; seed < seedLimit; seed++) { fillarray(A, siz, seed); (*alg2)(A, siz, compareIntVal); } // alg2Time = clock() - T - TFill; gettimeofday(&tim, NULL); alg2Time=tim.tv_sec+(tim.tv_usec/1000000.0) - T - TFILL; printf("%s %d %s", "siz: ", siz, " "); printf("%s %f %s", "alg1Time: ", alg1Time, " "); printf("%s %f %s", "alg2Time: ", alg2Time, " "); float frac = 0; float frac2 = 0; if ( alg1Time != 0 && alg2Time != 0) { frac = alg2Time / ( 1.0 * alg1Time ); frac2 = 1.0/frac; } printf("%s %f %s %f %s", "frac: ", frac, "frac2: ", frac2, "\n"); } // free array for (i = 0; i < siz; i++) { free(A[i]); }; free(A); siz = siz * 2; seedLimit = seedLimit / 2; } } // end compareAlgorithms0
//Get the central point between the two parallel lines CvPoint getcentralpoint(IplImage *image, CvSeq *res) { CvPoint *pnt1, *pnt2, *pnt3, *pnt4, *pnt5, *pnt6; double dif1, dif2, dif3; int dif; CvPoint tmpPnt1, tmpPnt2, tmpPnt3; fillarray(res); sortarray(); if(ORIENT_ARRAY_SIZE == 8){ //get the points pnt1 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-3][0],0); pnt2 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-3][1],0); pnt3 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-2][0],0); pnt4 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-2][1],0); pnt5 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-1][0],0); pnt6 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-1][1],0); //get directions of the lines dif1 = difference(pnt1, pnt2, pnt3, pnt4, 'x') + difference(pnt1, pnt2, pnt3, pnt4, 'y'); dif2 = difference(pnt1, pnt2, pnt5, pnt6, 'x') + difference(pnt1, pnt2, pnt5, pnt6, 'y'); dif3 = difference(pnt3, pnt4, pnt5, pnt6, 'x') + difference(pnt3, pnt4, pnt5, pnt6, 'y'); //get lines who run parallel to each other //dir3 = line 1 and 2 //dir4 = line 1 and 3 //dir5 = line 2 and 3 if ((dif1 <= dif2) && (dif1 <= dif3)) dif = 3; if ((dif2 <= dif1) && (dif2 <= dif3)) dif = 4; if ((dif3 <= dif1) && (dif3 <= dif2)) dif = 5; //estimate middle point between two parallel lines if (dif == 3) { tmpPnt1 = centralpoint(*pnt3, *pnt4); tmpPnt2 = centralpoint(*pnt1, *pnt2); tmpPnt3 = centralpoint(tmpPnt1, tmpPnt2); } if (dif == 4) { tmpPnt1 = centralpoint(*pnt5, *pnt6); tmpPnt2 = centralpoint(*pnt1, *pnt2); tmpPnt3 = centralpoint(tmpPnt1, tmpPnt2); } if (dif == 5) { tmpPnt1 = centralpoint(*pnt3, *pnt4); tmpPnt2 = centralpoint(*pnt5, *pnt6); tmpPnt3 = centralpoint(tmpPnt1, tmpPnt2); } } if(ORIENT_ARRAY_SIZE == 12){ pnt3 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-2][0],0); pnt4 = (CvPoint*)cvGetSeqElem(res,points[ORIENT_ARRAY_SIZE-2][1],0); tmpPnt3 = centralpoint(*pnt3, *pnt4); } //print center point in image //cvCircle(image, tmpPnt1, 10, CV_RGB(100,100,100), 2); //return the central point return tmpPnt3; }
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