int main(int argc, char *argv[]) { int i,j; float **m1; float **m2; float **m3; float **m4; float **m5; float *diag4; float **m1T; float **M1; float M2[5][5]; int row1, col1; // n x m int row1T,col1T; // m x n int row2, col2; // n x n int row3, col3; // n x n int row4, col4; // n x n int row5, col5; // n x n if (argc<2) { printf("Usage: %s randSeed\n",argv[0]); return -1; } // Test of functions that allocate the memory required to store the resulting matrix row1=ROW; col1=COL; mRand(&m1,row1,col1,10,atoi(argv[1])); mPrint(m1,row1,col1); // Transposition mTranspB(&m1T,&row1T,&col1T,m1,row1,col1); mPrint(m1T,row1T,col1T); // Multiplication mMultB(&m2,&row2,&col2,m1,row1,col1,m1T,row1T,col1T); mPrint(m2,row2,col2); // Inversion mInvB(&m3,&row3,&col3,m2,row2,col2); mPrint(m3,row3,col3); // Diagonal matrix row4=ROW; col4=ROW; diag4= (float *) malloc(sizeof(float)*row4); for (i=0; i<row4; i++) diag4[i]=i+1; mDiag(&m4,row4,diag4); mPrint(m4,row4,col4); mFree(m1,row1); mFree(m1T,row1T); mFree(m2,row2); mFree(m3,row3); mFree(m4,row4); // Test of functions that assume the memory required to store the // resulting matrix to be already allocated // row1=row2=row3=row4=row1T=0; // col1=col2=col3=col4=col1T=0; // Random Creation mRand(&m1,row1,col1,10,atoi(argv[1])); mPrint(m1,row1,col1); mBuild(&m1T,row1T,col1T); mBuild(&m2,row2,col2); mBuild(&m3,row3,col3); mBuild(&m4,row4,col4); // Transposition mTransp(m1T,row1T,col1T,m1,row1,col1); mPrint(m1T,row1T,col1T); // Multiplication mMult(m2,row2,col2,m1,row1,col1,m1T,row1T,col1T); mPrint(m2,row2,col2); // Inversion mInv(m3,row3,col3,m2,row2,col2); mPrint(m3,row3,col3); // Diagonal matrix row4=ROW; col4=ROW; diag4= (float *) malloc(sizeof(float)*row4); for (i=0; i<row4; i++) diag4[i]=i+1; // mDiag(&m4,row4,diag4); // mPrint(m4,row4,col4); // Mult mPrint(m1T,row1T,col1T); mFree(m2,row2); mRand(&m2,row2,col2,10,atoi(argv[1])*5); mPrint(m2,row2,col2); mFree(m4,row4); row4=row2; col4=row1T; mBuild(&m4,row4,col4); mMultTr2(m4,row4,col4,m2,row2,col2,m1T,row1T,col1T); mPrint(m4,row4,col4); // Copy row5=row4; col5=col4; printf("\ncopy\n"); mBuild(&m5,row5,col5); mPrint(m5,row5,col5); mCopy(m5,row5,col5,m4,row4,col4); mPrint(m4,row4,col4); mPrint(m5,row5,col5); mFree(m1,row1); mFree(m1T,row1T); mFree(m2,row2); mFree(m3,row3); mFree(m4,row4); // To check copy to and from float[r][c] // So far only squared matrix are copied! printf("\nLast\n"); mRand(&M1,5,5,10,atoi(argv[1])*5); mPrint(M1,5,5); mCopyM2A(5,M2,M1); for(i=0; i<5; i++) for(j=0; j<5; j++) M2[i][j]*=100; mCopyA2M(M1,5,M2); mPrint(M1,5,5); return 0; }
void SpeedTest(FunctionCall fc) { int i, min, max, step, sec=0; int sizeTimeArray, fct; char fnc[MAXSIZE_FCT]; char foutput[256]; struct sigaction action; action.sa_handler = handler; sigemptyset(&action.sa_mask); struct timeval start, end, result; double maxtime = 0; double time; double usec; double* timeArray; Matrix a=NULL, b=NULL, tmp=NULL; E s; int n; TokenizeSpeedTest(fc, fnc, &min, &max, &step, &sec); if (min>0 && max>0 && step>0) { sizeTimeArray = ((max-min)/step)+1; timeArray = (double*)malloc(sizeTimeArray*sizeof(double)); for (i=0; i<sizeTimeArray; i++) timeArray[i] = 0; } usec = (double)sec * 1000000; sprintf(foutput, "speedtest_%s_%d_%d_%d_%d", fnc, min, max, step, sec); fct = GetFunction(fnc); switch (fct) { case NMX : { printf("\t You must specify another function\n"); return; } case ADD : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = addMatricis(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case SUB : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = substractMatricis(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case MUL : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = mulMatricis(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case MSC : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); s = mRand(MIN_SCA, MAX_SCA); gettimeofday(&start, NULL); tmp = mult_scal(a, s); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case EXP : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); n = (int)mRand(MIN_EXP, MAX_EXP); gettimeofday(&start, NULL); tmp = expo(a, n); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case TRA : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = transposeMatrix(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case DET : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); s = determinant(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case DLU : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = newMatrix(i, i); tmp = identityMatrix(i); gettimeofday(&start, NULL); decomposition(a, b, tmp); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case SOL : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); b = randomMatrix(i, 1, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = gauss(a, b); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(b); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case INV : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); tmp = invert(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); deleteMatrix(tmp); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case RNK : { sigaction(SIGINT, &action, NULL); for (i=min; i<=max; i+=step) { a = randomMatrix(i, i, MIN_E, MAX_E); gettimeofday(&start, NULL); n = rank(a); gettimeofday(&end, NULL); timersub(&end, &start, &result); time = (result.tv_sec*1000000)+result.tv_usec; if (maxtime < time) maxtime = time; timeArray[(i-min)/step] = time; printf("\t* %s Size:%5d ;\tTime:%8.0f µs ;\n", fnc, i, time); deleteMatrix(a); if (usec >= 1000000) { if (time >= usec) { i++; break; } } if (CTRLC) { i++; break; } } CreateGNUPLOTFile(min, i-step, step, timeArray, maxtime, foutput); break; } case VAR : // default case NOF : // default default : { printf("\t%s : Function Not Implemented\n", fnc); fni++; break; } } if (fct!=NOF && fct !=VAR) fni = 0; free(timeArray); CTRLC = 0; sigemptyset(&action.sa_mask); }
float InputLorenz63::getValueCore(const Key::Input& iKey) const { // Implement this recursively. Cache the values at the offsets specified in the namelists. std::string localVariable; bool found = getLocalVariableName(iKey.variable, localVariable); assert(found); std::vector<float> values; values.resize(3); // Initial condition if(iKey.offset <= 0) { // Add noice to initial conditions if(localVariable == "LorenzX") values[0] = mX0 + mRand() * sqrt(mXVar); if(localVariable == "LorenzY") values[1] = mY0 + mRand() * sqrt(mYVar); if(localVariable == "LorenzZ") values[2] = mZ0 + mRand() * sqrt(mZVar); } else { // Get values at last offset int currIndex = getOffsetIndex(iKey.offset); assert(Global::isValid(currIndex)); int lastIndex = currIndex - 1; assert(lastIndex >= 0); float lastOffset = getOffsets()[lastIndex]; Key::Input key = iKey; key.offset = lastOffset; key.variable = 0; float x0 = getValue(key.date, key.init, key.offset, key.location, key.member, "LorenzX", false); key.variable = 1; float y0 = getValue(key.date, key.init, key.offset, key.location, key.member, "LorenzY", false); key.variable = 2; float z0 = getValue(key.date, key.init, key.offset, key.location, key.member, "LorenzZ", false); float offset = lastOffset; // Iterate forward in time to the offset while(offset < iKey.offset) { // Update values float x1 = x0 + mDt * mS * (y0 - x0); float y1 = y0 + mDt * (mR * x0 - y0 - (x0*z0)); float z1 = z0 + mDt * (x0*y0 - mB*z0); float x2 = x1 + mDt * mS * (y1-x1); float y2 = y1 + mDt * (mR * x1 - y1 - x1*z1); float z2 = z1 + mDt * (x1*y1 - mB*z1); float x = 0.5 * (x2 + x0); float y = 0.5 * (y2 + y0); float z = 0.5 * (z2 + z0); values[0] = x; values[1] = y; values[2] = z; offset += mDt; assert(Global::isValid(x)); assert(Global::isValid(y)); assert(Global::isValid(z)); x0 = x; y0 = y; z0 = z; } } float returnValue = Global::MV; Key::Input key = iKey; // Cache the value for(key.variable = 0; key.variable < 3; key.variable++) { Input::addToCache(key, values[key.variable]); } if(localVariable == "LorenzX") returnValue = values[0]; if(localVariable == "LorenzY") returnValue = values[1]; if(localVariable == "LorenzZ") returnValue = values[2]; return returnValue; }