/* Calcul de l'activation * choice = 0 : propagation couche entree -> couche cachee * choice = 1 : propagation couche cachee -> couche sortie */ void Mlp::propagation(int choice) { unsigned long sz_1 = (choice) ? couche_sortie.size() : couche_cachee.size(); unsigned long sz_2 = (choice) ? couche_cachee.size() : couche_entree.size(); for (unsigned int i = 0; i < sz_1; i++) { double x = 0.0; for (unsigned int j = 0; j < sz_2; j++) // Calcul intensite du signal reçu par j x += (choice) ? couche_cachee[j].get_sortie() * couche_cachee[j].get_poids(i) : couche_entree[j].get_sortie() * couche_entree[j].get_poids(i); // Calcul sigmoide d'activation neurones des couches entree et cachee -> f(x) = 1 / (1 + e(-x)) (choice) ? couche_sortie[i].set_sortie(1 / (1 + exp(-x))) : couche_cachee[i].set_sortie(1 / (1 + exp(-x))); } if(!choice) propagation(++choice); }
void PatchMatch::match(const cv::Mat &src, const cv::Mat &dst, RosMat<offsetElement> &NearNei) { double err = 0, pre_err = 0; do { pre_err = err; propagation(src, dst, NearNei); err = errorComputation(dst, coordinateMapping(src, NearNei)); // cout<<err<<", "<<abs(err - pre_err)<<" : "<<HALTING_PARA*err<<endl; } while ( abs(err - pre_err) > HALTING_PARA*err); showMat(coordinateMapping(src, NearNei), "map", 0); // showMat(dst, "d", 1); // showMat(src, "s", 0); reconstErrorDisplay(dst, coordinateMapping(src, NearNei)); distanceDisplay(NearNei); }
// Reconnaissance int Mlp::test(std::vector< std::vector<double> >* tests) { int err = 0, result; double tmp; for(unsigned int i = 0; i < tests->size(); i++) { result = 10; tmp = 0.0; // Attribution des données de test à la couche d'entrée for (unsigned int j = 0; j < couche_entree.size(); j++) { couche_entree[j].set_entree(tests->at(i).at(j)); couche_entree[j].set_sortie(tests->at(i).at(j)); } // Passage dans le reseau de neurones propagation(); // Comparaison résultats attendus for(unsigned int j = 0; j < couche_sortie.size(); j++) { if(couche_sortie[j].get_sortie() > tmp) { tmp = couche_sortie[j].get_sortie(); result = j; } else if(couche_sortie[j].get_sortie() == tmp) { result = 10; } } if(result != (int)i/10) err++; } return err; }
int main(int argc, char* argv[]) { //Variable declarations //SuTrace* localTraces; //Local Traces Data float* vModelData; //Local Velocity Model Data unsigned long localTraceNumber; //Local Trace Number unsigned long traceNumber; //Global Trace Number counter unsigned int shotNumber; //Number of shots counter int sx; //Position x of shot source int sy; //Position y of shot source int sz; //Position z of shot source unsigned short ns; //Number of Samples unsigned long ntssMax; //Maximum Number of Traces of the same shot unsigned int shotLimit; //Number of shots analyzed unsigned int localShotNumberLimit; //Number of shots analyzed for each process unsigned long ntt; //Total Number of traces in SU file unsigned int TD; //Size of trace's data (in Bytes) unsigned int i; //Iterator FILE *vModelFile; //File containing velocity Model data int my_rank; //Rank of process int comm_sz; //Number of processes MPI_Comm comm; //Processes' Communicator //MPI_Status status; //Status of MPI communication int Xi; //Velocity Model length (X axis) int Yi; //Velocity Model width (Y axis) int Zi; //Velocity Model depth (Z axis) float dX; //Velocity Model length step (X axis) float dY; //Velocity Model width step (Y axis) float dZ; //Velocity Model depth step (Z axis) float dt; //Propagation wave time step (T axis) float rdt; //Propagation wave time step (T axis) restricted float sdt; //Propagation wave time step (T axis) corrected float *str; //Traces data corrected unsigned short sTi; //Number of Samples corrected unsigned int gxMin; //Distance of the first hidrophone in X axis (in meters) unsigned int gxMax; //Distance of the last hidrophone in X axis (in meters) unsigned int gyMin; //Distance of the first hidrophone in Y axis (in meters) unsigned int gyMax; //Distance of the last hidrophone in Y axis (in meters) unsigned long Xbi; //Length of Velocity Model with border (X axis) unsigned long Ybi; //Width of Velocity Model with border (Y axis) unsigned long Zbi; //Depth of Velocity Model with border (Z axis) unsigned long indb; //Index counter of Velocity Model with border unsigned long ind; //Index counter of Velocity Model int xi; //Index counter of Velocity Model length (Xi) int xbi; //Index counter of Velocity Model with border length (Xi) int yi; //Index counter of Velocity Model width (Yi) int ybi; //Index counter of Velocity Model with border width (Yi) int zi; //Index counter of Velocity Model depth (Zi) int zbi; //Index counter of Velocity Model with border depth (Xi) unsigned int bw; //Border Width applied to Velocity Model float *vel; //Velocity Model with border int curSx; //Position x of shot source of the current trace int curSy; //Position y of shot source of the current trace unsigned int curGx; //Position x of hidrophone of the current trace unsigned int curGy; //Position y of hidrophone of the current trace unsigned long curTraceNumber; //Local Trace Number Counter //unsigned int dest; //Destination process index FILE *suFile; //SU File SuTrace* traces; //SU Traces SuTrace* iTraces; //SU Traces interpolated float *tr; //Traces data int *trc; //Traces data coordinate (row, column, depth) int ti; //Index counter of propagation wave time int Ntr; //Number of Traces in Propagation unsigned int localXi; //Velocity Model length (X axis) used in the current shot unsigned int localYi; //Velocity Model width (Y axis) used in the current shot unsigned int localGxMin; //Distance of the first hidrophone in X axis (in meters) of the current shot unsigned int localGxMax; //Distance of the last hidrophone in X axis (in meters) of the current shot unsigned int localGyMin; //Distance of the first hidrophone in Y axis (in meters) of the current shot unsigned int localGyMax; //Distance of the last hidrophone in Y axis (in meters) of the current shot unsigned int localGxMinC; //Coordinate of the first hidrophone in X axis (in rows) of the current shot unsigned int localGyMinC; //Coordinate of the first hidrophone in Y axis (in columns) of the current shot char fileName [20]; //Name of a file written by the program float vMin; //Minimum velocity in velocity model float vMax; //Maximum velocity in velocity model double divisor; //Divisor applied to dt accomplish time restriction unsigned int fIndex; //Floor Index used in Interpolation unsigned int cIndex; //Ceil Index used in Interpolation float fCoef; //Coeficient of floor portion in Interpolation float cCoef; //Coeficient of ceil portion in Interpolation int rest; int localCeilShotNumber; unsigned int localFirstShotNumber; unsigned int iMax; //uint8_t* localImage; //uint8_t* image; //MPI start MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_size(comm, &comm_sz); MPI_Comm_rank(comm, &my_rank); // Check command line arguments if (argc != 16) { usage(argv[0], my_rank); } // Get arguments from function call in command line Xi = atoi(argv[2]); Yi = atoi(argv[3]); Zi = atoi(argv[4]); dX = atof(argv[5]); dY = atof(argv[6]); dZ = atof(argv[7]); dt = atof(argv[8]); gxMin = atoi(argv[9]); gxMax = atoi(argv[10]); gyMin = atoi(argv[11]); gyMax = atoi(argv[12]); ntssMax = atol(argv[14]); ntt = atol(argv[15]); printf("gxMax: %d\tgyMax: %d", gxMax, gyMax); //Variables Initialization localTraceNumber = 0; traceNumber = 0; shotNumber = 0; curSx = 0; curSy = 0; curTraceNumber = 0; divisor = 1; vMin = FLT_MAX; vMax = 0; shotLimit = 1; bw = 50; Xbi = Xi + 2*bw; Ybi = Yi + 2*bw; Zbi = Zi + 2*bw; // cria imagem local zerada //localImage = (uint8_t*) calloc(Xi*Yi*Zi, sizeof(uint8_t)); //Allocate velocity model data vModelData = (float*) malloc(Xi*Yi*Zi*sizeof(float)); if (vModelData == NULL) {fputs ("Memory error",stderr); exit (2);} //Open velocity model file vModelFile = fopen(argv[1], "rb"); if (vModelFile == NULL) { fprintf(stderr, "Erro ao abrir arquivo %s\n", argv[1]); exit(0); } // Read velocity model file and puts to vModelData if (fread(vModelData, 1, Xi*Yi*Zi*sizeof(float), vModelFile) != Xi*Yi*Zi*sizeof(float)) { fputs ("Reading error",stderr); exit (3); } printf("My_rank: %d\t Leu modelo de velocidades\n", my_rank); //Close velocity model file fclose(vModelFile); //Allocate velocity model with border vel = (float *) malloc(Xbi*Ybi*Zbi*sizeof(float)); if (vel == NULL) { printf("Memory allocation failed: vel.\n"); return 0; } //Get Max velocity for (xi = 0; xi < Xi; xi++) { for (yi = 0; yi < Yi; yi++) { for (zi = 0; zi < Zi; zi++) { ind = xi*Yi*Zi + yi*Zi + zi; if(vMin > vModelData[ind]) { vMin = vModelData[ind]; } if(vMax < vModelData[ind]) { vMax = vModelData[ind]; } } } } //Open SU file suFile = fopen(argv[13], "rb"); if (suFile == NULL) { fprintf(stderr, "Erro ao abrir arquivo %s\n", argv[9]); exit(0); } //Get number of samples (ns) fseek(suFile, 114, SEEK_CUR); if (fread(&ns, 1, sizeof(unsigned short), suFile) != sizeof(unsigned short)) { printf("getSuTrace failed!\n"); exit(0); } fseek(suFile, -116, SEEK_CUR); //Invert ns bytes (Big Endian -> Little Endian) invBytes(&ns, sizeof(unsigned short)); //printf("My_rank: %d\tRealizando expansão do modelo de velocidades\n", my_rank); //Test Time Restriction rdt = 1/(vMax*sqrt(1/pow(dX,2) + 1/pow(dY,2) + 1/pow(dZ,2))); sdt = dt; sTi = ns; while(sdt > rdt) { sdt /= 2; sTi *= 2; divisor *= 2; } printf("sdt = %.3f\tsTi = %d", sdt, sTi); // Optimization for (xi = 0; xi < Xi; xi++) { for (yi = 0; yi < Yi; yi++) { for (zi = 0; zi < Zi; zi++) { ind = xi*Yi*Zi + yi*Zi + zi; indb = (bw+xi)*Ybi*Zbi + (bw+yi)*Zbi + bw+zi; vel[indb] = sdt*sdt*vModelData[ind]*vModelData[ind]; if(vMin > vModelData[ind]) { vMin = vModelData[ind]; } if(vMax < vModelData[ind]) { vMax = vModelData[ind]; } } } } printf("vMin = %.3f\n", vMin); printf("vMax = %.3f\n", vMax); //printf("velMax = %.3f\n", vMax*vMax*dt*dt); // Expansion of the velocity model for the borders for (xbi = bw; xbi < bw+Xi; xbi++) { // copying top and bottom faces -> z for (ybi = bw; ybi < bw+Yi; ybi++) { for (zbi = 0; zbi < bw; zbi++) { vel[xbi*Ybi*Zbi + ybi*Zbi + zbi] = vel[xbi*Ybi*Zbi + ybi*Zbi + bw]; vel[xbi*Ybi*Zbi + ybi*Zbi + Zbi-1-zbi] = vel[xbi*Ybi*Zbi + ybi*Zbi + Zbi-1-bw]; } } } for (xbi = bw; xbi < bw+Xi; xbi++) { // copying left and right faces -> y for (zbi = 0; zbi < Zbi; zbi++) { for (ybi = 0; ybi < bw; ybi++) { vel[xbi*Ybi*Zbi + ybi*Zbi + zbi] = vel[xbi*Ybi*Zbi + bw*Zbi + zbi]; vel[xbi*Ybi*Zbi + (Ybi-1-ybi)*Zbi + zbi] = vel[xbi*Ybi*Zbi + (Ybi-1-bw)*Zbi + zbi]; } } } for (ybi = 0; ybi < Ybi; ybi++) { // copying front and back faces -> x for (zbi = 0; zbi < Zbi; zbi++) { for (xbi = 0; xbi < bw; xbi++) { vel[xbi*Ybi*Zbi + ybi*Zbi + zbi] = vel[bw*Ybi*Zbi + ybi*Zbi + zbi]; vel[(Xbi-1-xbi)*Ybi*Zbi + ybi*Zbi + zbi] = vel[(Xbi-1-bw)*Ybi*Zbi + ybi*Zbi + zbi]; } } } //Output Velocity Model files FILE *output_file; sprintf(fileName, "vel.ad"); output_file = fopen(fileName, "wb"); if (output_file == NULL) { fprintf(stderr, "Erro ao abrir arquivo output\n"); exit(0); } fwrite(vel, 1, Xbi*Ybi*Zbi*sizeof(float), output_file); fflush(output_file); fclose(output_file); printf("My_rank: %d\tArquivo de modelo de velocidades com bordas exportado\n", my_rank); //Free memory space allocated to vModelData free(vModelData); printf("My_rank: %d\tExpansão do modelo de velocidades feito\n", my_rank); /***** End of Get Velocity Model *****/ /***** Get Traces *****/ //Calculate Trace Data Bytes TD = ns*sizeof(float); //printf("NS: %d\t TD: %d\tSizeof(short): %lu\n", ns, TD, sizeof(short)); //Allocate Traces traces = (SuTrace*) malloc(ntssMax*sizeof(SuTrace)); for (i = 0; i < ntssMax; i++) { traces[i].data = (float *) malloc(TD); } //Allocate Image //image = (uint8_t*) calloc(Xi*Yi*Zi, sizeof(uint8_t)); //shotLimit = 11; 0, 3, 6, 9 //resto = 2; //comm_sz = 4; rest = shotLimit % comm_sz; localCeilShotNumber = (int) ceil((double)shotLimit/(double)comm_sz); localFirstShotNumber = my_rank*localCeilShotNumber; shotNumber = 0; //Read other local Shots while(traceNumber < ntt && shotNumber < localFirstShotNumber) { //Get Next Sdepth, Sx, Sy fseek(suFile, 72, SEEK_CUR); if (fread(&curSx, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } if (fread(&curSy, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } fseek(suFile, -80, SEEK_CUR); //Invert bytes (Big Endian -> Little Endian) invBytes(&curSx, sizeof(int)); invBytes(&curSy, sizeof(int)); //Initialize variables of the current shot sx = curSx; sy = curSy; while(sx == curSx && sy == curSy && traceNumber < ntt) //Get traces from the same shot { //Jump Trace fseek(suFile, TH+TD, SEEK_CUR); //Increment variables traceNumber++; //Get Next Sx and Sy if(traceNumber < ntt) { fseek(suFile, 72, SEEK_CUR); if (fread(&curSx, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } if (fread(&curSy, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } fseek(suFile, -80, SEEK_CUR); invBytes(&curSx, sizeof(int)); invBytes(&curSy, sizeof(int)); } } shotNumber++; } //Calculate local ShotNumberLimit if(rest != 0 && shotLimit >= comm_sz) { if(my_rank > rest) { localShotNumberLimit = shotNumber+rest; } else { localShotNumberLimit = shotNumber+localCeilShotNumber; } } else if(shotLimit >= comm_sz) { localShotNumberLimit = shotNumber+localCeilShotNumber; } else { if(my_rank < rest) { localShotNumberLimit = shotNumber+localCeilShotNumber; } else { localShotNumberLimit = shotNumber; } } printf("My_rank: %u\tNumberShot: %u\tLocalShotNumberLimit: %u\tTraceNumber: %lu\n", my_rank, shotNumber, localShotNumberLimit, traceNumber); //Read local shots while(traceNumber < ntt && shotNumber < localShotNumberLimit) { //Get Next Sdepth, Sx, Sy fseek(suFile, 48, SEEK_CUR); if (fread(&sz, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } fseek(suFile, 20, SEEK_CUR); if (fread(&curSx, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } if (fread(&curSy, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } fseek(suFile, -80, SEEK_CUR); //Invert bytes (Big Endian -> Little Endian) invBytes(&curSx, sizeof(int)); invBytes(&curSy, sizeof(int)); invBytes(&sz, sizeof(int)); //Compute destination process //dest = (shotNumber % (comm_sz-1)) + 1; //printf("Rank: %d\tNumberShot: %u\tenviou coordenada da fonte\n", my_rank, shotNumber); //Initialize variables of the current shot sx = curSx; sy = curSy; curTraceNumber = 0; localGxMax = localGyMax = 0; localGxMin = localGyMin = INT_MAX; while(sx == curSx && sy == curSy && traceNumber < ntt) //Get traces from the same shot { //Get Trace if (fread(&(traces[curTraceNumber]), 1, TH, suFile) != TH) { printf("getSuTrace failed!\n"); exit(0); } if (fread(traces[curTraceNumber].data, 1, TD, suFile) != TD) { printf("getSuTrace failed!\n"); exit(0); } //Get Current Gx and Gy curGx = traces[curTraceNumber].gx; curGy = traces[curTraceNumber].gy; invBytes(&curGx, sizeof(int)); invBytes(&curGy, sizeof(int)); //printf("TraceNumber = %lu\t\tgxMin = %u\tgxMax = %u\tgyMin = %u\tgyMax = %u\n", traceNumber, gxMin, gxMax, gyMin, gyMax); //Get max/min Gx and Gy if(curGx > localGxMax) { localGxMax = curGx; } if(curGx < localGxMin) { localGxMin = curGx; } if(curGy > localGyMax) { localGyMax = curGy; } if(curGy < localGyMin) { localGyMin = curGy; } //Increment variables traceNumber++; curTraceNumber++; //Get Next Sx and Sy if(traceNumber < ntt) { fseek(suFile, 72, SEEK_CUR); if (fread(&curSx, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } if (fread(&curSy, 1, sizeof(int), suFile) != sizeof(int)) { printf("getSuTrace failed!\n"); exit(0); } fseek(suFile, -80, SEEK_CUR); invBytes(&curSx, sizeof(int)); invBytes(&curSy, sizeof(int)); } } printf("gxMin = %u\tgxMax = %u\tgyMin = %u\tgyMax = %u\n", localGxMin, localGxMax, localGyMin, localGyMax); printf("My_rank: %u\tNumberShot: %u\tLocalTraceNumber: %lu\tTraceNumber: %lu\n", my_rank, shotNumber, localTraceNumber, traceNumber); printf("\nMy_rank: %d\tDistribuição dos traços concluída com sucesso!\n", my_rank); fflush(stdout); Ntr = 1; //Number of traces (sources) //Allocate traces coordinates and samples of traces trc = (int *) malloc(Ntr*3*sizeof(int)); tr = (float *) malloc(Ntr*ns*sizeof(float)); for (ti = 0; ti < ns; ti++) { tr[ti] = source(ti*dt); //printf("tr[%d] = %.3f\n", ti, tr[ti]); } //Converting sx, sy and sx to coordinates trc[0] = (sx-localGxMin)/dX; trc[1] = (sy-localGyMin)/dY; trc[2] = sz/dZ; //Update propagation filename sprintf(fileName, "direta_%d.bin", shotNumber); //Calculate local bounds and local velocity model size localGxMinC = (localGxMin-gxMin)/dX; localGyMinC = (localGyMin-gyMin)/dY; localXi = (localGxMax-localGxMin)/dX + 1; localYi = (localGyMax-localGyMin)/dY + 1; printf("My_rank: %u\tNumberShot: %u\tLocalXi: %u\tLocalYi: %u\tlocalGxMinC = %u\tlocalGyMinC = %u\n", my_rank, shotNumber, localXi, localYi, localGxMinC, localGyMinC); printf("trc[0] = %d\ttrc[1] = %d\ttrc[2] = %d\tsx = %d\tsy = %d\n", trc[0], trc[1], trc[2], sx, sy); fflush(stdout); //Test Propagation Restrictions printf("maxDs tem que ser menor que %.6f\n", vMin/(4.0*freq)); if(maxDs(dX,dY,dZ) <= vMin/(4.0*freq)) { printf("Passou no teste espacial\n"); } else { printf("Nao passou no teste espacial\n"); exit(0); } printf("dt tem que ser menor que %.6f\n", 1/(vMax*sqrt(1/pow(dX,2) + 1/pow(dY,2) + 1/pow(dZ,2)))); if(dt <= rdt) { printf("Passou no teste temporal 2\n"); propagation( &(vel[localGxMinC*Ybi*Zbi + localGyMinC*Zbi]), localXi, localYi, Yi, Zi, dX, dY, dZ, tr, ns, dt, trc, Ntr, 1, fileName, bw, my_rank, shotNumber, iMax); free(tr); } else { //Allocate corrected pointers str = (float *) malloc(Ntr*sTi*sizeof(float)); /*iTraces = (SuTrace*) malloc(curTraceNumber*sizeof(SuTrace)); for (i = 0; i < curTraceNumber; i++) { iTraces[i].data = (float *) malloc(divisor*TD); }// */ //Interpolate for (ti = 0; ti < sTi; ti++) { if(ti % (int) divisor != 0) { fIndex = (int) divisor*floor((double) ti / divisor); cIndex = (int) divisor*ceil((double) ti / divisor); fCoef = ((float) (ti-fIndex)) / divisor ; cCoef = ((float) (cIndex-ti)) / divisor ; str[ti] = fCoef*tr[fIndex]+cCoef*tr[cIndex]; /*for (i = 0; i < curTraceNumber; i++) { iTraces[i].data[ti] = fCoef*traces[i].data[fIndex]+cCoef*traces[i].data[cIndex]; }*/ if(my_rank == 0) { //printf("ti: %d\tdiv: %.2f\tfI: %d\tcI: %d\t fCoef: %.2f\t cCoef: %.2f\ttr[fI]: %.3f\ttr[cI]: %.3f\tstr[t]: %.3f\n", ti, divisor, fIndex, cIndex, fCoef, cCoef, tr[fIndex], tr[cIndex], str[ti]); } } else { str[ti] = tr[ti]; } } //Free default pointers /*for (i = 0; i < ntssMax; i++) { free(traces[i].data); } free(traces); free(tr);*/ iMax = 0; printf("Nao passou no teste temporal 2\n"); propagation( &(vel[localGxMinC*Ybi*Zbi + localGyMinC*Zbi]), localXi, localYi, Yi, Zi, dX, dY, dZ, str, sTi, sdt, trc, Ntr, 1, fileName, bw, my_rank, shotNumber, iMax); //Free corrected pointers /*for (i = 0; i < curTraceNumber; i++) { free(iTraces[i].data); } free(iTraces);*/ free(str); //exit(0); }//*/ // assinatura retropropagacao //backpropagation(vel, localXi, localYi, Yi, Zi, dx, dy, dz, traces, sTi, sdt, trc, curTraceNumber, 200, fileName, bw, my_rank, shotNumber); // assinatura correlacao cruzada //imageCondition(localImage, u_i, u_r, Xi, Yi, Zi); shotNumber++;//Update number of shot free(trc); } printf("\nMy_rank: %d\tCalculo dos traços concluído com sucesso!\n", my_rank); fclose(suFile); /***** End of Get Traces *****/ // adicionar a imagem local //MPI_Reduce(localImage, image, Xi*Yi*Zi, MPI_UINT8_T, MPI_SUM, 0, comm); //Print data to File //printFile(traces, localTraceNumber, TD, vel, Xbi*Ybi*Zbi, my_rank); //Free pointers free(traces); free(vel); /* free(localImage); free(image); free(u_i); free(u_r); */ //End of MPI Processes MPI_Finalize(); return 0; }