int main(int argc, char **argv) { PetscInitialize(&argc, &argv, "cmame.opt", help); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); // The global domain size double gSize[3]; gSize[0] = 1.; gSize[1] = 1.; gSize[2] = 1.; // Parameters for the balancing algorithm. // Refer to manual for details ... bool incCorner = 1; // balance across corners = true unsigned int maxNumPts= 1; // maximum number of points per octant unsigned int dim=3; // spatial dimensions // unsigned int maxDepth=8; // maximum depth of the octree, has to be <= 30 int maxDepth=8; // maximum depth of the octree, has to be <= 30 int Ns = 32; unsigned int dof = 3; char problemName[PETSC_MAX_PATH_LEN]; char filename[PETSC_MAX_PATH_LEN]; double t0 = 0.0; double dt = 0.1; double t1 = 1.0; double beta = 0.000001; Vec rho; // density - elemental scalar Vec lambda; // Lame parameter - lambda - elemental scalar Vec mu; // Lame parameter - mu - elemental scalar Vec fibers; // Fiber orientations - nodal vector (3-dof) std::vector<Vec> tau; // the scalar activation - nodal scalar std::vector<ot::TreeNode> linOct, balOct, newLinOct; std::vector<double> pts; // Initial conditions Vec initialDisplacement; Vec initialVelocity; double nu, E; nu = 0.45; E = 1000; timeInfo ti; PetscTruth mf = PETSC_FALSE; bool mfree = false; PetscOptionsGetTruth(0, "-mfree", &mf, 0); if (mf == PETSC_TRUE) { mfree = true; } else mfree = false; double ctrst = 1.0; // get Ns CHKERRQ ( PetscOptionsGetInt(0,"-Ns",&Ns,0) ); CHKERRQ ( PetscOptionsGetInt(0,"-mdepth",&maxDepth,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-ctrst",&ctrst,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-t0",&t0,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-nu",&nu,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-Youngs",&E,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-t1",&t1,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-dt",&dt,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-beta",&beta,0) ); CHKERRQ ( PetscOptionsGetString(PETSC_NULL,"-pn",problemName,PETSC_MAX_PATH_LEN-1,PETSC_NULL)); // Time info for timestepping ti.start = t0; ti.stop = t1; ti.step = dt; if (!rank) { std::cout << "Grid size is " << Ns+1 << " and NT is " << (int)ceil(1.0/dt) << std::endl; std::cout << "MaxDepth is " << maxDepth << std::endl; } /* for (int i=0; i<3*Ns*Ns*Ns; i++) { double val = gaussian(); //randgauss(0., 1., 2.0, 0.); // std::cout << val << std::endl; pts.push_back(val); } MPI_Barrier(MPI_COMM_WORLD); */ /*********************************************************************** */ // CONSTRUCT: Construct the linear octree from the points ... /*********************************************************************** */ // ot::points2Octree(pts, gSize, linOct, dim, maxDepth, maxNumPts, MPI_COMM_WORLD); // The points are not needed anymore, and can be cleared to free memory. // pts.clear(); if (!rank) { ot::readNodesFromFile("test.256.oct", newLinOct); std::cout << "Finished reading" << std::endl; } // std::sort(linOct.begin(), linOct.end()); std::cout << rank << " Original octree size is " << newLinOct.size() << std::endl; /* par::Partition<ot::TreeNode>(linoct, newLinOct, MPI_COMM_WORLD); linOct.clear(); */ par::sampleSort<ot::TreeNode>(newLinOct, linOct, MPI_COMM_WORLD); newLinOct.clear(); std::cout << rank << ": after Part octree size is " << linOct.size() << std::endl; /*********************************************************************** */ // BALANCE: Balance the linear octree to enforce the 2:1 balance conditions. /*********************************************************************** */ ot::balanceOctree (linOct, balOct, dim, maxDepth, incCorner, MPI_COMM_WORLD); std::cout << "Balanced octree size is " << balOct.size() << std::endl; // The linear octree (unbalanced) can be cleared to free memory. linOct.clear(); // If desired the octree can be written to a file using the supplied routine ... ot::writeNodesToFile("filename.oct", balOct); /*********************************************************************** */ // MESH : Construct the octree-based Distruted Array. /*********************************************************************** */ ot::DA da(balOct,MPI_COMM_WORLD); balOct.clear(); MPI_Barrier(MPI_COMM_WORLD); if (!rank) std::cout <<"Finshed Meshing" << std::endl; PetscFinalize(); return 0; // create Matrices and Vectors elasMass *Mass = new elasMass(feMat::OCT); // Mass Matrix elasStiffness *Stiffness = new elasStiffness(feMat::OCT); // Stiffness matrix raleighDamping *Damping = new raleighDamping(feMat::OCT); // Damping Matrix cardiacForce *Force = new cardiacForce(feVec::OCT); // Force Vector // create vectors da.createVector(rho, false, false, 1); da.createVector(mu, false, false, 1); da.createVector(lambda, false, false, 1); da.createVector(initialDisplacement, false, false, dof); da.createVector(initialVelocity, false, false, dof); // Set initial conditions CHKERRQ( VecSet ( initialDisplacement, 0.0) ); CHKERRQ( VecSet ( initialVelocity, 0.0) ); int parFac = 2; int numParams = 120; CHKERRQ ( PetscOptionsGetInt(0,"-pFac", &parFac,0) ); numParams = parFac*parFac*parFac*5*3; if (!rank) std::cout << "Total number of unknowns is " << numParams << std::endl; // Generate the basis ... std::vector < radialBasis > spatialBasis; bSplineBasis temporalBasis(3, 5); // this creates and sets up the basis ... // temporalBasis.knot(); double fac = 1.0/parFac; double ssq = fac*fac/5.5452; // 8log(2) = 5.5452 PetscPrintf(0, "SSQ is %f\n", ssq); // Now to set up the radial bases ... for (int k=0; k<parFac; k++) { for (int j=0; j<parFac; j++) { for (int i=0; i<parFac; i++) { // std::cout << "Adding radial basis at: " << fac/2+i*fac << ", " << fac/2+j*fac << ", " << fac/2+k*fac << std::endl; radialBasis tmp(Point( fac/2+i*fac,fac/2+j*fac,fac/2+k*fac), Point(ssq,ssq,ssq)); spatialBasis.push_back(tmp); } } } /* // Homogeneous material properties ... CHKERRQ( VecSet ( rho, 1.0) ); nu = 0.45; E = 1000; double mmu = E/(2*(1+nu)); double llam = E*nu/((1+nu)*(1-2*nu)); CHKERRQ( VecSet ( mu, mmu) ); CHKERRQ( VecSet ( lambda, llam) ); */ PetscScalar *muArray, *lamArray, *rhoArray; // Read in material properties from file ... unsigned int elemSize = Ns*Ns*Ns; unsigned char *tmp_mat = new unsigned char[elemSize]; double *tmp_tau = new double[elemSize]; double *tmp_fib = new double[dof*elemSize]; // generate filenames & read in the raw arrays first ... std::ifstream fin; sprintf(filename, "%s.%d.img", problemName, Ns); fin.open(filename, std::ios::binary); fin.read((char *)tmp_mat, elemSize); fin.close(); nu = 0.35; E = 1000; double mmu = E/(2*(1+nu)); double llam = E*nu/((1+nu)*(1-2*nu)); nu = 0.45; E = 1000*ctrst; double mmu2 = E/(2*(1+nu)); double llam2 = E*nu/((1+nu)*(1-2*nu)); da.vecGetBuffer(mu, muArray,true,true,false,1); da.vecGetBuffer(lambda, lamArray,true,true,false,1); da.vecGetBuffer(rho, rhoArray,true,true,false,1); for ( da.init<ot::DA::ALL>(), da.init<ot::DA::WRITABLE>(); da.curr() < da.end<ot::DA::ALL>(); da.next<ot::DA::ALL>()) { unsigned int i = da.curr(); Point pt; pt = da.getCurrentOffset(); int indx = pt.z()*Ns*Ns + pt.y()*Ns + pt.x(); if ( tmp_mat[indx] ) { muArray[i] = mmu2; lamArray[i] = llam2; rhoArray[i] = 1.0; } else { muArray[i] = mmu; lamArray[i] = llam; rhoArray[i] = 1.0; } } da.vecRestoreBuffer(mu, muArray,true,true,false,1); da.vecRestoreBuffer(lambda, lamArray,true,true,false,1); da.vecRestoreBuffer(rho, rhoArray,true,true,false,1); delete [] tmp_mat; // Now set the activation ... unsigned int numSteps = (unsigned int)(ceil(( ti.stop - ti.start)/ti.step)); Vec tauVec, tmpTau; // Create elemental vector ... da.createVector(tmpTau, true, true, 1); da.createVector(fibers, true, true, 3); PetscScalar *tauArray; // load the fibers ... da.vecGetBuffer(fibers, tauArray, true, true, 3); sprintf(filename, "%s.%d.fibers", problemName, Ns); std::ifstream fin3(filename, std::ios::binary); fin3.read((char *)tmp_fib, dof*elemSize*sizeof(double)); fin3.close(); for ( da.init<ot::DA::ALL>(), da.init<ot::DA::WRITABLE>(); da.curr() < da.end<ot::DA::ALL>(); da.next<ot::DA::ALL>()) { unsigned int i = da.curr(); Point pt; pt = da.getCurrentOffset(); int indx = pt.z()*Ns*Ns + pt.y()*Ns + pt.x(); for (int d=0; d<dof; d++) { tauArray[dof*i+d] = tmp_tau[dof*indx+d]; } } da.vecRestoreBuffer(fibers, tauArray, true, true, 3); delete [] tmp_fib; // loop through time steps for (unsigned int t=0; t<numSteps+1; t++) { // a. Create new nodal vector ... da.createVector(tauVec, false, true, 1); VecSet( tmpTau, 0.0 ); da.vecGetBuffer(tmpTau, tauArray, true, true, 1); // b. read in the activation // std::cout << "Setting force vectors" << std::endl; sprintf(filename, "%s.%d.%.3d.fld", problemName, Ns, t); // std::cout << "Reading force file " << filename << std::endl; fin.open(filename); fin.read((char *)tmp_tau, elemSize*sizeof(double)); fin.close(); // c. set the values ... for ( da.init<ot::DA::ALL>(), da.init<ot::DA::WRITABLE>(); da.curr() < da.end<ot::DA::ALL>(); da.next<ot::DA::ALL>()) { unsigned int i = da.curr(); Point pt; pt = da.getCurrentOffset(); int indx = pt.z()*Ns*Ns + pt.y()*Ns + pt.x(); tauArray[i] = tmp_tau[indx]; } // restore da.vecRestoreBuffer(tmpTau, tauArray, true, true, 1); // d. element2node elementToNode(da, tmpTau, tauVec, 1); // store in vector tau.push_back(tauVec); } // Setup Matrices and Force Vector ... Mass->setProblemDimensions(1.0, 1.0, 1.0); Mass->setDA(&da); Mass->setDof(dof); Mass->setDensity(rho); Stiffness->setProblemDimensions(1.0, 1.0, 1.0); Stiffness->setDA(&da); Stiffness->setDof(dof); Stiffness->setLame(lambda, mu); Damping->setAlpha(0.0); Damping->setBeta(0.00075); Damping->setMassMatrix(Mass); Damping->setStiffnessMatrix(Stiffness); Damping->setDA(&da); Damping->setDof(dof); // Force Vector Force->setProblemDimensions(1.0,1.0,1.0); Force->setDA(&da); // Force->setFDynamic(tau); Force->setActivationVec(tau); Force->setFiberOrientations(fibers); Force->setTimeInfo(&ti); // Newmark time stepper ... newmark *ts = new newmark; ts->setMassMatrix(Mass); ts->setDampingMatrix(Damping); ts->setStiffnessMatrix(Stiffness); ts->damp(false); ts->setTimeFrames(1); ts->storeVec(true); ts->setForceVector(Force); ts->setInitialDisplacement(initialDisplacement); ts->setInitialVelocity(initialVelocity); ts->setTimeInfo(&ti); ts->setAdjoint(false); // set if adjoint or forward ts->useMatrixFree(mfree); //if (!rank) // std::cout << RED"Initializing Newmark"NRM << std::endl; double itime = MPI_Wtime(); ts->init(); // initialize IMPORTANT //if (!rank) // std::cout << RED"Starting Newmark Solve"NRM << std::endl; double stime = MPI_Wtime(); ts->solve();// solve double etime = MPI_Wtime(); // PetscFinalize(); Vec alpha, outvec; PetscScalar *avec; VecCreateSeq(PETSC_COMM_SELF, numParams, &alpha); VecGetArray(alpha, &avec); for (int j=0; j<numParams; j++) avec[j] = 0.0; avec[1] = 1.0; VecRestoreArray(alpha, &avec); VecDuplicate(alpha, &outvec); // Inverse solver set up parametricActivationInverse *hyperInv = new parametricActivationInverse; hyperInv->setBasis(spatialBasis, temporalBasis); hyperInv->setForwardInitialConditions(initialDisplacement, initialVelocity); hyperInv->setTimeStepper(ts); // set the timestepper hyperInv->setInitialGuess(alpha);// set the initial guess hyperInv->setRegularizationParameter(beta); // set the regularization paramter // hyperInv->setObservations(solvec); // set the data for the problem hyperInv->init(); // initialize the inverse solver hyperInv->solve(); Vec FinalSolution; hyperInv->getCurrentControl(FinalSolution); char fname[256]; sprintf(fname, "%s.soln.%d.%d.raw",problemName, Ns, parFac ); std::ofstream sol; if (!rank) { sol.open(fname, std::ios::binary); VecGetArray(FinalSolution, &avec); sol.write((char *)avec, numParams*sizeof(PetscScalar)); VecRestoreArray(outvec, &avec); sol.close(); } PetscFinalize(); }
int main(int argc,char **args) { Mat A,RHS,C,F,X,S; Vec u,x,b; Vec xschur,bschur,uschur; IS is_schur; PetscErrorCode ierr; PetscMPIInt size; PetscInt isolver=0,size_schur,m,n,nfact,nsolve,nrhs; PetscReal norm,tol=PETSC_SQRT_MACHINE_EPSILON; PetscRandom rand; PetscBool data_provided,herm,symm,use_lu; PetscReal sratio = 5.1/12.; PetscViewer fd; /* viewer */ char solver[256]; char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscInitialize(&argc,&args,(char*)0,help); ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRQ(ierr); if (size > 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor test"); /* Determine which type of solver we want to test for */ herm = PETSC_FALSE; symm = PETSC_FALSE; ierr = PetscOptionsGetBool(NULL,NULL,"-symmetric_solve",&symm,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,NULL,"-hermitian_solve",&herm,NULL);CHKERRQ(ierr); if (herm) symm = PETSC_TRUE; /* Determine file from which we read the matrix A */ ierr = PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&data_provided);CHKERRQ(ierr); if (!data_provided) { /* get matrices from PETSc distribution */ sprintf(file,PETSC_DIR); ierr = PetscStrcat(file,"/share/petsc/datafiles/matrices/");CHKERRQ(ierr); if (symm) { #if defined (PETSC_USE_COMPLEX) ierr = PetscStrcat(file,"hpd-complex-");CHKERRQ(ierr); #else ierr = PetscStrcat(file,"spd-real-");CHKERRQ(ierr); #endif } else { #if defined (PETSC_USE_COMPLEX) ierr = PetscStrcat(file,"nh-complex-");CHKERRQ(ierr); #else ierr = PetscStrcat(file,"ns-real-");CHKERRQ(ierr); #endif } #if defined(PETSC_USE_64BIT_INDICES) ierr = PetscStrcat(file,"int64-");CHKERRQ(ierr); #else ierr = PetscStrcat(file,"int32-");CHKERRQ(ierr); #endif #if defined (PETSC_USE_REAL_SINGLE) ierr = PetscStrcat(file,"float32");CHKERRQ(ierr); #else ierr = PetscStrcat(file,"float64");CHKERRQ(ierr); #endif } /* Load matrix A */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n); /* Create dense matrix C and X; C holds true solution with identical colums */ nrhs = 2; ierr = PetscOptionsGetInt(NULL,NULL,"-nrhs",&nrhs,NULL);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&C);CHKERRQ(ierr); ierr = MatSetSizes(C,m,PETSC_DECIDE,PETSC_DECIDE,nrhs);CHKERRQ(ierr); ierr = MatSetType(C,MATDENSE);CHKERRQ(ierr); ierr = MatSetFromOptions(C);CHKERRQ(ierr); ierr = MatSetUp(C);CHKERRQ(ierr); ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rand);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rand);CHKERRQ(ierr); ierr = MatSetRandom(C,rand);CHKERRQ(ierr); ierr = MatDuplicate(C,MAT_DO_NOT_COPY_VALUES,&X);CHKERRQ(ierr); /* Create vectors */ ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,n,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecDuplicate(x,&b);CHKERRQ(ierr); ierr = VecDuplicate(x,&u);CHKERRQ(ierr); /* save the true solution */ ierr = PetscOptionsGetInt(NULL,NULL,"-solver",&isolver,NULL);CHKERRQ(ierr); switch (isolver) { #if defined(PETSC_HAVE_MUMPS) case 0: ierr = PetscStrcpy(solver,MATSOLVERMUMPS);CHKERRQ(ierr); break; #endif #if defined(PETSC_HAVE_MKL_PARDISO) case 1: ierr = PetscStrcpy(solver,MATSOLVERMKL_PARDISO);CHKERRQ(ierr); break; #endif default: ierr = PetscStrcpy(solver,MATSOLVERPETSC);CHKERRQ(ierr); break; } #if defined (PETSC_USE_COMPLEX) if (isolver == 0 && symm && !data_provided) { /* MUMPS (5.0.0) does not have support for hermitian matrices, so make them symmetric */ PetscScalar im = PetscSqrtScalar((PetscScalar)-1.); PetscScalar val = -1.0; val = val + im; ierr = MatSetValue(A,1,0,val,INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); } #endif ierr = PetscOptionsGetReal(NULL,NULL,"-schur_ratio",&sratio,NULL);CHKERRQ(ierr); if (sratio < 0. || sratio > 1.) { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "Invalid ratio for schur degrees of freedom %f", sratio); } size_schur = (PetscInt)(sratio*m); ierr = PetscPrintf(PETSC_COMM_SELF,"Solving with %s: nrhs %d, sym %d, herm %d, size schur %d, size mat %d\n",solver,nrhs,symm,herm,size_schur,m);CHKERRQ(ierr); /* Test LU/Cholesky Factorization */ use_lu = PETSC_FALSE; if (!symm) use_lu = PETSC_TRUE; #if defined (PETSC_USE_COMPLEX) if (isolver == 1) use_lu = PETSC_TRUE; #endif if (herm && !use_lu) { /* test also conversion routines inside the solver packages */ ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); ierr = MatConvert(A,MATSEQSBAIJ,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); } if (use_lu) { ierr = MatGetFactor(A,solver,MAT_FACTOR_LU,&F);CHKERRQ(ierr); } else { if (herm) { ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); ierr = MatSetOption(A,MAT_SPD,PETSC_TRUE);CHKERRQ(ierr); } else { ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); ierr = MatSetOption(A,MAT_SPD,PETSC_FALSE);CHKERRQ(ierr); } ierr = MatGetFactor(A,solver,MAT_FACTOR_CHOLESKY,&F);CHKERRQ(ierr); } ierr = ISCreateStride(PETSC_COMM_SELF,size_schur,m-size_schur,1,&is_schur);CHKERRQ(ierr); ierr = MatFactorSetSchurIS(F,is_schur);CHKERRQ(ierr); ierr = ISDestroy(&is_schur);CHKERRQ(ierr); if (use_lu) { ierr = MatLUFactorSymbolic(F,A,NULL,NULL,NULL);CHKERRQ(ierr); } else { ierr = MatCholeskyFactorSymbolic(F,A,NULL,NULL);CHKERRQ(ierr); } for (nfact = 0; nfact < 3; nfact++) { Mat AD; if (!nfact) { ierr = VecSetRandom(x,rand);CHKERRQ(ierr); if (symm && herm) { ierr = VecAbs(x);CHKERRQ(ierr); } ierr = MatDiagonalSet(A,x,ADD_VALUES);CHKERRQ(ierr); } if (use_lu) { ierr = MatLUFactorNumeric(F,A,NULL);CHKERRQ(ierr); } else { ierr = MatCholeskyFactorNumeric(F,A,NULL);CHKERRQ(ierr); } ierr = MatFactorCreateSchurComplement(F,&S);CHKERRQ(ierr); ierr = MatCreateVecs(S,&xschur,&bschur);CHKERRQ(ierr); ierr = VecDuplicate(xschur,&uschur);CHKERRQ(ierr); if (nfact == 1) { ierr = MatFactorInvertSchurComplement(F);CHKERRQ(ierr); } for (nsolve = 0; nsolve < 2; nsolve++) { ierr = VecSetRandom(x,rand);CHKERRQ(ierr); ierr = VecCopy(x,u);CHKERRQ(ierr); if (nsolve) { ierr = MatMult(A,x,b);CHKERRQ(ierr); ierr = MatSolve(F,b,x);CHKERRQ(ierr); } else { ierr = MatMultTranspose(A,x,b);CHKERRQ(ierr); ierr = MatSolveTranspose(F,b,x);CHKERRQ(ierr); } /* Check the error */ ierr = VecAXPY(u,-1.0,x);CHKERRQ(ierr); /* u <- (-1.0)x + u */ ierr = VecNorm(u,NORM_2,&norm);CHKERRQ(ierr); if (norm > tol) { PetscReal resi; if (nsolve) { ierr = MatMult(A,x,u);CHKERRQ(ierr); /* u = A*x */ } else { ierr = MatMultTranspose(A,x,u);CHKERRQ(ierr); /* u = A*x */ } ierr = VecAXPY(u,-1.0,b);CHKERRQ(ierr); /* u <- (-1.0)b + u */ ierr = VecNorm(u,NORM_2,&resi);CHKERRQ(ierr); if (nsolve) { ierr = PetscPrintf(PETSC_COMM_SELF,"(f %d, s %d) MatSolve error: Norm of error %g, residual %f\n",nfact,nsolve,norm,resi);CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_SELF,"(f %d, s %d) MatSolveTranspose error: Norm of error %g, residual %f\n",nfact,nsolve,norm,resi);CHKERRQ(ierr); } } ierr = VecSetRandom(xschur,rand);CHKERRQ(ierr); ierr = VecCopy(xschur,uschur);CHKERRQ(ierr); if (nsolve) { ierr = MatMult(S,xschur,bschur);CHKERRQ(ierr); ierr = MatFactorSolveSchurComplement(F,bschur,xschur);CHKERRQ(ierr); } else { ierr = MatMultTranspose(S,xschur,bschur);CHKERRQ(ierr); ierr = MatFactorSolveSchurComplementTranspose(F,bschur,xschur);CHKERRQ(ierr); } /* Check the error */ ierr = VecAXPY(uschur,-1.0,xschur);CHKERRQ(ierr); /* u <- (-1.0)x + u */ ierr = VecNorm(uschur,NORM_2,&norm);CHKERRQ(ierr); if (norm > tol) { PetscReal resi; if (nsolve) { ierr = MatMult(S,xschur,uschur);CHKERRQ(ierr); /* u = A*x */ } else { ierr = MatMultTranspose(S,xschur,uschur);CHKERRQ(ierr); /* u = A*x */ } ierr = VecAXPY(uschur,-1.0,bschur);CHKERRQ(ierr); /* u <- (-1.0)b + u */ ierr = VecNorm(uschur,NORM_2,&resi);CHKERRQ(ierr); if (nsolve) { ierr = PetscPrintf(PETSC_COMM_SELF,"(f %d, s %d) MatFactorSolveSchurComplement error: Norm of error %g, residual %f\n",nfact,nsolve,norm,resi);CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_SELF,"(f %d, s %d) MatFactorSolveSchurComplementTranspose error: Norm of error %g, residual %f\n",nfact,nsolve,norm,resi);CHKERRQ(ierr); } } } ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&AD); if (!nfact) { ierr = MatMatMult(AD,C,MAT_INITIAL_MATRIX,2.0,&RHS);CHKERRQ(ierr); } else { ierr = MatMatMult(AD,C,MAT_REUSE_MATRIX,2.0,&RHS);CHKERRQ(ierr); } ierr = MatDestroy(&AD);CHKERRQ(ierr); for (nsolve = 0; nsolve < 2; nsolve++) { ierr = MatMatSolve(F,RHS,X);CHKERRQ(ierr); /* Check the error */ ierr = MatAXPY(X,-1.0,C,SAME_NONZERO_PATTERN);CHKERRQ(ierr); ierr = MatNorm(X,NORM_FROBENIUS,&norm);CHKERRQ(ierr); if (norm > tol) { ierr = PetscPrintf(PETSC_COMM_SELF,"(f %D, s %D) MatMatSolve: Norm of error %g\n",nfact,nsolve,norm);CHKERRQ(ierr); } } ierr = MatDestroy(&S);CHKERRQ(ierr); ierr = VecDestroy(&xschur);CHKERRQ(ierr); ierr = VecDestroy(&bschur);CHKERRQ(ierr); ierr = VecDestroy(&uschur);CHKERRQ(ierr); } /* Free data structures */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&C);CHKERRQ(ierr); ierr = MatDestroy(&F);CHKERRQ(ierr); ierr = MatDestroy(&X);CHKERRQ(ierr); ierr = MatDestroy(&RHS);CHKERRQ(ierr); ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main( int argc, char **argv ) { Mat hmat,smat; PetscInt n=0,row,col, nev; PetscScalar number; PetscViewer view; PetscBool flag; int ierr,i; char path[PETSC_MAX_PATH_LEN]="", temporary[PETSC_MAX_PATH_LEN]=""; char infilename[PETSC_MAX_PATH_LEN]="", outfilename[PETSC_MAX_PATH_LEN]=""; SlepcInitialize(&argc,&argv,(char*)0,help); ierr = PetscOptionsGetString(PETSC_NULL,"-in",infilename,PETSC_MAX_PATH_LEN-1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-out",outfilename,PETSC_MAX_PATH_LEN-1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr); ierr = PetscGetWorkingDirectory(path,PETSC_MAX_PATH_LEN); if ( strcmp(infilename, "") == 0 || strcmp(outfilename, "") == 0 || n == 0 ){ SETERRQ(PETSC_COMM_WORLD,1,"Error in parameterlist.\nExample call: ./convertToPETScBin -in matrix.hmat -out matrix.dat -n 1912"); } // number = 2.0 + 3.0 * PETSC_i; // PetscPrintf(PETSC_COMM_WORLD,"number: %g i%g\n", number); // std::cout << number PetscPrintf(PETSC_COMM_WORLD,"processing %s into %s\n", infilename, outfilename); ierr = MatCreateSeqAIJ(PETSC_COMM_WORLD,n,n,n,PETSC_NULL,&hmat); FILE *file; double real, imag; PetscScalar complex; PetscScalar myreal; strcat(temporary, path); strcat(temporary, "/"); strcat(temporary, infilename); strcpy(infilename, temporary); ierr = PetscFOpen(PETSC_COMM_SELF,infilename,"r",&file);CHKERRQ(ierr); while ( fscanf(file,"%d %d %le %le\n",&row,&col,(double*)&real, (double*)&imag) != EOF ){ row = row - 1; col = col -1; // adjustment if ( row == col ){ complex = real /*+ imag*PETSC_i*/; ierr = MatSetValues(hmat,1,&row,1,&col,&complex,INSERT_VALUES);CHKERRQ(ierr); }else{ complex = real /*+ imag*PETSC_i */; ierr = MatSetValues(hmat,1,&row,1,&col,&complex,INSERT_VALUES);CHKERRQ(ierr); complex = real /*- imag*PETSC_i */; ierr = MatSetValues(hmat,1,&col,1,&row,&complex,INSERT_VALUES);CHKERRQ(ierr); } } fclose(file); ierr = MatAssemblyBegin(hmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(hmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatIsHermitian(hmat,0.00000000000001,&flag); CHKERRQ(ierr); if ( !flag ){ SETERRQ(PETSC_COMM_WORLD,1,"Error. Output matrix is not hermitian."); } // writing strcpy(temporary,""); strcat(temporary, path); strcat(temporary, "/"); strcat(temporary, outfilename); strcpy(outfilename, temporary); ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,outfilename,FILE_MODE_WRITE,&view);CHKERRQ(ierr); ierr = MatView(hmat,view);CHKERRQ(ierr); ierr = PetscViewersDestroy(&view); ierr = SlepcFinalize();CHKERRQ(ierr); return 0; }
PetscErrorCode Metos3DBGCTracerInit(Metos3D *metos3d) { // load PetscInt nvec = metos3d->vectorLength; PetscInt nvecloc = metos3d->vectorLengthLocal; // work vars PetscInt ntracer, itracer; PetscInt nmax; PetscBool flag; char initFileNameFormat [PETSC_MAX_PATH_LEN]; PetscFunctionBegin; // tracer count Metos3DUtilOptionsGetInt(metos3d, "-Metos3DTracerCount", &ntracer); metos3d->tracerCount = ntracer; // read tracer init options // order: // 1. file name format // 2. file names // 3. constant values PetscOptionsGetString(PETSC_NULL, "-Metos3DTracerInitFileFormat", initFileNameFormat, PETSC_MAX_PATH_LEN, &flag); if (flag == PETSC_TRUE) { // work vars char tracerInputDirectory[PETSC_MAX_PATH_LEN]; char format [PETSC_MAX_PATH_LEN]; char filePath [PETSC_MAX_PATH_LEN]; // input directory, vector, format Metos3DUtilOptionsGetString(metos3d, "-Metos3DTracerInputDirectory", tracerInputDirectory); Metos3DUtilVecCreateAndSetValue(metos3d, ntracer, nvec, nvecloc, &metos3d->y0, 0.0); Metos3DUtilFormatParse(metos3d, initFileNameFormat); for (itracer = 0; itracer < ntracer; itracer++) { sprintf(format, "%s%s", tracerInputDirectory, initFileNameFormat); sprintf(filePath, format, itracer); Metos3DUtilVectorLoad(metos3d, filePath, &metos3d->y0[itracer]); } } else { // work vars char tracerInputDirectory[PETSC_MAX_PATH_LEN]; char *initFileNames [PETSC_MAX_PATH_LEN]; char filePath [PETSC_MAX_PATH_LEN]; // file name nmax = ntracer; PetscOptionsGetStringArray(PETSC_NULL, "-Metos3DTracerInitFile", initFileNames, &nmax, &flag); if (flag == PETSC_TRUE) { // input directory, vector, file name Metos3DUtilOptionsGetString(metos3d, "-Metos3DTracerInputDirectory", tracerInputDirectory); Metos3DUtilVecCreateAndSetValue(metos3d, ntracer, nvec, nvecloc, &metos3d->y0, 0.0); for (itracer = 0; itracer < ntracer; itracer++) { sprintf(filePath, "%s%s", tracerInputDirectory, initFileNames[itracer]); Metos3DUtilVectorLoad(metos3d, filePath, &metos3d->y0[itracer]); PetscFree(initFileNames[itracer]); } } else { // work vars PetscReal *y0value; // value nmax = ntracer; PetscMalloc(nmax*sizeof(PetscReal),&y0value); // array, vector Metos3DUtilOptionsGetRealArray(metos3d, "-Metos3DTracerInitValue", &nmax, y0value); Metos3DUtilVecCreateAndSetValues(metos3d, ntracer, nvec, nvecloc, &metos3d->y0, y0value); PetscFree(y0value); } } // create work vectors Metos3DUtilVecCreateAndSetValue(metos3d, 1, ntracer*nvec, ntracer*nvecloc, &metos3d->ybgcinBD, 0.0); Metos3DUtilVecCreateAndSetValue(metos3d, 1, ntracer*nvec, ntracer*nvecloc, &metos3d->ybgcoutBD, 0.0); Metos3DDebug(metos3d, kDebugLevel, "Metos3DBGCTracerInit\n"); PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat A[3],B; /* matrix */ PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscErrorCode ierr; PetscBool flg; Vec x,y,z,work; PetscReal rnorm; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; /* Determine files from which we read the two linear systems (matrix and right-hand-side vector). */ ierr = PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix; then destroy the viewer. */ ierr = MatCreate(PETSC_COMM_WORLD,&A[0]);CHKERRQ(ierr); ierr = MatLoad(A[0],fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatDuplicate(A[0],MAT_COPY_VALUES,&A[1]);CHKERRQ(ierr); ierr = MatDuplicate(A[0],MAT_COPY_VALUES,&A[2]);CHKERRQ(ierr); ierr = MatShift(A[1],1.0);CHKERRQ(ierr); ierr = MatShift(A[1],2.0);CHKERRQ(ierr); ierr = MatCreateVecs(A[0],&x,&y);CHKERRQ(ierr); ierr = VecDuplicate(y,&work);CHKERRQ(ierr); ierr = VecDuplicate(y,&z);CHKERRQ(ierr); ierr = VecSet(x,1.0);CHKERRQ(ierr); ierr = MatMult(A[0],x,z);CHKERRQ(ierr); ierr = MatMultAdd(A[1],x,z,z);CHKERRQ(ierr); ierr = MatMultAdd(A[2],x,z,z);CHKERRQ(ierr); ierr = MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);CHKERRQ(ierr); ierr = MatMult(B,x,y);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = VecAXPY(y,-1.0,z);CHKERRQ(ierr); ierr = VecNorm(y,NORM_2,&rnorm);CHKERRQ(ierr); if (rnorm > 1.e-10) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Error with composite add %g\n",(double)rnorm);CHKERRQ(ierr); } ierr = MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);CHKERRQ(ierr); ierr = MatCompositeMerge(B);CHKERRQ(ierr); ierr = MatMult(B,x,y);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = VecAXPY(y,-1.0,z);CHKERRQ(ierr); ierr = VecNorm(y,NORM_2,&rnorm);CHKERRQ(ierr); if (rnorm > 1.e-10) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Error with composite add after merge %g\n",(double)rnorm);CHKERRQ(ierr); } ierr = VecSet(x,1.0);CHKERRQ(ierr); ierr = MatMult(A[0],x,z);CHKERRQ(ierr); ierr = MatMult(A[1],z,work);CHKERRQ(ierr); ierr = MatMult(A[2],work,z);CHKERRQ(ierr); ierr = MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);CHKERRQ(ierr); ierr = MatCompositeSetType(B,MAT_COMPOSITE_MULTIPLICATIVE);CHKERRQ(ierr); ierr = MatMult(B,x,y);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = VecAXPY(y,-1.0,z);CHKERRQ(ierr); ierr = VecNorm(y,NORM_2,&rnorm);CHKERRQ(ierr); if (rnorm > 1.e-10) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Error with composite multiplicative %g\n",(double)rnorm);CHKERRQ(ierr); } ierr = MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);CHKERRQ(ierr); ierr = MatCompositeSetType(B,MAT_COMPOSITE_MULTIPLICATIVE);CHKERRQ(ierr); ierr = MatCompositeMerge(B);CHKERRQ(ierr); ierr = MatMult(B,x,y);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = VecAXPY(y,-1.0,z);CHKERRQ(ierr); ierr = VecNorm(y,NORM_2,&rnorm);CHKERRQ(ierr); if (rnorm > 1.e-10) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Error with composite multiplicative after merge %g\n",(double)rnorm);CHKERRQ(ierr); } /* Free work space. All PETSc objects should be destroyed when they are no longer needed. */ ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = VecDestroy(&work);CHKERRQ(ierr); ierr = VecDestroy(&z);CHKERRQ(ierr); ierr = MatDestroy(&A[0]);CHKERRQ(ierr); ierr = MatDestroy(&A[1]);CHKERRQ(ierr); ierr = MatDestroy(&A[2]);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode BSSCR_DRIVER_auglag( KSP ksp, Mat stokes_A, Vec stokes_x, Vec stokes_b, Mat approxS, MatStokesBlockScaling BA, PetscTruth sym, KSP_BSSCR * bsscrp_self ) { AugLagStokes_SLE * stokesSLE = (AugLagStokes_SLE*)bsscrp_self->solver->st_sle; PetscTruth uzawastyle, KisJustK=PETSC_TRUE, restorek, change_A11rhspresolve; PetscTruth usePreviousGuess, useNormInfStoppingConditions, useNormInfMonitor, found, forcecorrection; PetscTruth change_backsolve, mg_active; PetscErrorCode ierr; //PetscInt monitor_index; PetscInt max_it,min_it; KSP ksp_inner, ksp_S, ksp_new_inner; PC pc_S, pcInner; Mat K,G,D,C, S, K2;// Korig; Vec u,p,f,f2=0,f3=0,h, h_hat,t; MGContext mgCtx; double mgSetupTime, scrSolveTime, a11SingleSolveTime, penaltyNumber;// hFactor; static int been_here = 0; /* Ha Ha Ha !! */ char name[PETSC_MAX_PATH_LEN]; char matname[PETSC_MAX_PATH_LEN]; char suffix[PETSC_MAX_PATH_LEN]; char str[PETSC_MAX_PATH_LEN]; PetscTruth flg, extractMats; PetscLogDouble flopsA,flopsB; /***************************************************************************************************************/ /***************************************************************************************************************/ //if( bsscrp_self->solver->st_sle->context->loadFromCheckPoint ){ // been_here=1; //} /* get sub matrix / vector objects */ /* note that here, the matrix D should always exist. It is set up in _StokesBlockKSPInterface_Solve in StokesBlockKSPInterface.c */ /* now extract K,G etc from a MatNest object */ MatNestGetSubMat( stokes_A, 0,0, &K ); MatNestGetSubMat( stokes_A, 0,1, &G ); MatNestGetSubMat( stokes_A, 1,0, &D );if(!D){ PetscPrintf( PETSC_COMM_WORLD, "D does not exist but should!!\n"); exit(1); } MatNestGetSubMat( stokes_A, 1,1, &C ); VecNestGetSubVec( stokes_x, 0, &u ); VecNestGetSubVec( stokes_x, 1, &p ); VecNestGetSubVec( stokes_b, 0, &f ); VecNestGetSubVec( stokes_b, 1, &h ); PetscPrintf( PETSC_COMM_WORLD, "\n\n---------- AUGMENTED LAGRANGIAN K2 METHOD ---------\n\n" ); PetscPrintf( PETSC_COMM_WORLD, "----------- Penalty = %f\n\n", bsscrp_self->solver->penaltyNumber ); sprintf(suffix,"%s","x"); PetscOptionsGetString( PETSC_NULL, "-matsuffix", suffix, PETSC_MAX_PATH_LEN-1, &extractMats ); flg=0; PetscOptionsGetString( PETSC_NULL, "-matdumpdir", name, PETSC_MAX_PATH_LEN-1, &flg ); if(flg){ sprintf(str,"%s/",name); sprintf(matname,"K%s",suffix); bsscr_dirwriteMat( K, matname,str, "Writing K matrix in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"G%s",suffix); bsscr_dirwriteMat( G, matname,str, "Writing G matrix in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"D%s",suffix); bsscr_dirwriteMat( D, matname,str, "Writing D matrix in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"f%s",suffix); bsscr_dirwriteVec( f, matname,str, "Writing f vector in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"h%s",suffix); bsscr_dirwriteVec( h, matname,str, "Writing h vector in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"Shat%s",suffix); bsscr_dirwriteMat( approxS, matname,str, "Writing Shat matrix in al Solver"); if(C){ sprintf(str,"%s/",name); sprintf(matname,"C%s",suffix); bsscr_dirwriteMat( C, matname,str, "Writing C matrix in al Solver"); } } mg_active=PETSC_TRUE; PetscOptionsGetTruth( PETSC_NULL ,"-A11_mg_active", &mg_active, &found ); bsscrp_self->solver->mg_active=mg_active; penaltyNumber = bsscrp_self->solver->penaltyNumber; //hFactor = stokesSLE->hFactor; /***************************************************************************************************************/ /***************************************************************************************************************/ /****** GET K2 ****************************************************************************************/ if(penaltyNumber > 1e-10 && bsscrp_self->k2type){ flg=0; PetscOptionsGetString( PETSC_NULL, "-matdumpdir", name, PETSC_MAX_PATH_LEN-1, &flg ); if(flg){ sprintf(str,"%s/",name); sprintf(matname,"K2%s",suffix); bsscr_dirwriteMat( bsscrp_self->K2, matname,str, "Writing K2 matrix in al Solver"); } K2=bsscrp_self->K2; scrSolveTime = MPI_Wtime(); ierr=MatAXPY(K,penaltyNumber,K2,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);/* Computes K = penaltyNumber*K2 + K */ scrSolveTime = MPI_Wtime() - scrSolveTime; PetscPrintf( PETSC_COMM_WORLD, "\n\t* K+p*K2 in time: %lf seconds\n\n", scrSolveTime); KisJustK=PETSC_FALSE; forcecorrection=PETSC_TRUE; PetscOptionsGetTruth( PETSC_NULL ,"-force_correction", &forcecorrection, &found ); if(forcecorrection){ if(bsscrp_self->f2 && forcecorrection){ f2=bsscrp_self->f2; ierr=VecAXPY(f,penaltyNumber,f2);/* f <- f +a*f2 */ }else{ switch (bsscrp_self->k2type) { case (K2_GG): { VecDuplicate( u, &f3 ); MatMult( G, h, f3); ierr=VecAXPY(f,penaltyNumber,f3);/* f <- f +a*f2 */ } break; case (K2_GMG): { Mat M; Vec Mdiag; VecDuplicate( u, &f3 ); M = bsscrp_self->solver->mStiffMat->matrix; MatGetVecs( M, &Mdiag, PETSC_NULL ); MatGetDiagonal( M, Mdiag ); VecReciprocal(Mdiag); VecPointwiseMult(Mdiag,Mdiag,h); MatMult( G, Mdiag, f3); ierr=VecAXPY(f,penaltyNumber,f3);/* f <- f +a*f2 */ Stg_VecDestroy(&Mdiag); } break; case (K2_DGMGD): { Mat M; Vec Mdiag; VecDuplicate( u, &f3 ); M = bsscrp_self->solver->mStiffMat->matrix; MatGetVecs( M, &Mdiag, PETSC_NULL ); MatGetDiagonal( M, Mdiag ); VecReciprocal(Mdiag); VecPointwiseMult(Mdiag,Mdiag,h); MatMult( G, Mdiag, f3); ierr=VecAXPY(f,penaltyNumber,f3);/* f <- f +a*f2 */ Stg_VecDestroy(&Mdiag); } break; case (K2_NULL): { ; } break; case (K2_SLE): { ; } break; } } } } /* Create Schur complement matrix */ MatCreateSchurComplement(K,K,G,D,C, &S); MatSchurComplementGetKSP( S, &ksp_inner); KSPGetPC( ksp_inner, &pcInner ); /***************************************************************************************************************/ /***************************************************************************************************************/ /********* SET PREFIX FOR INNER/VELOCITY KSP *************************************************************/ KSPSetOptionsPrefix( ksp_inner, "A11_" ); KSPSetFromOptions( ksp_inner ); Stg_KSPSetOperators(ksp_inner, K, K, DIFFERENT_NONZERO_PATTERN); useNormInfStoppingConditions = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL ,"-A11_use_norm_inf_stopping_condition", &useNormInfStoppingConditions, &found ); if(useNormInfStoppingConditions) BSSCR_KSPSetNormInfConvergenceTest( ksp_inner ); useNormInfMonitor = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-A11_ksp_norm_inf_monitor", &useNormInfMonitor, &found ); if(useNormInfMonitor) KSPMonitorSet( ksp_inner, BSSCR_KSPNormInfMonitor, PETSC_NULL, PETSC_NULL ); useNormInfMonitor = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-A11_ksp_norm_inf_to_norm_2_monitor", &useNormInfMonitor, &found ); if(useNormInfMonitor) KSPMonitorSet( ksp_inner, BSSCR_KSPNormInfToNorm2Monitor, PETSC_NULL, PETSC_NULL ); /***************************************************************************************************************/ /***************************************************************************************************************/ /* If multigrid is enabled, set it now. */ change_A11rhspresolve = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-change_A11rhspresolve", &change_A11rhspresolve, &found ); if(bsscrp_self->solver->mg_active && !change_A11rhspresolve) { mgSetupTime=setupMG( bsscrp_self, ksp_inner, pcInner, K, &mgCtx ); } /***************************************************************************************************************/ /***************************************************************************************************************/ /* create right hand side */ if(change_A11rhspresolve){ //Stg_KSPDestroy(&ksp_inner ); KSPCreate(PETSC_COMM_WORLD, &ksp_new_inner); Stg_KSPSetOperators(ksp_new_inner, K, K, DIFFERENT_NONZERO_PATTERN); KSPSetOptionsPrefix(ksp_new_inner, "rhsA11_"); MatSchurComplementSetKSP( S, ksp_new_inner );/* this call destroys the ksp_inner that is already set on S */ ksp_inner=ksp_new_inner; KSPGetPC( ksp_inner, &pcInner ); KSPSetFromOptions(ksp_inner); /* make sure we are setting up our solver how we want it */ } MatGetVecs( S, PETSC_NULL, &h_hat ); Vec f_tmp; /* It may be the case that the current velocity solution might not be bad guess for f_tmp? <-- maybe not */ MatGetVecs( K, PETSC_NULL, &f_tmp ); scrSolveTime = MPI_Wtime(); KSPSolve(ksp_inner, f, f_tmp); scrSolveTime = MPI_Wtime() - scrSolveTime; PetscPrintf( PETSC_COMM_WORLD, "\n\t* KSPSolve for RHS setup Finished in time: %lf seconds\n\n", scrSolveTime); MatMult(D, f_tmp, h_hat); VecAYPX(h_hat, -1.0, h); /* Computes y = x + alpha y. h_hat -> h - Gt*K^(-1)*f*/ Stg_VecDestroy(&f_tmp); if(bsscrp_self->solver->mg_active && change_A11rhspresolve) { //Stg_KSPDestroy(&ksp_inner ); KSPCreate(PETSC_COMM_WORLD, &ksp_new_inner); Stg_KSPSetOperators(ksp_new_inner, K, K, DIFFERENT_NONZERO_PATTERN); KSPSetOptionsPrefix( ksp_new_inner, "A11_" ); MatSchurComplementSetKSP( S, ksp_new_inner ); ksp_inner=ksp_new_inner; //MatSchurSetKSP( S, ksp_inner ); KSPGetPC( ksp_inner, &pcInner ); KSPSetFromOptions( ksp_inner ); mgSetupTime=setupMG( bsscrp_self, ksp_inner, pcInner, K, &mgCtx ); } /* create solver for S p = h_hat */ KSPCreate( PETSC_COMM_WORLD, &ksp_S ); KSPSetOptionsPrefix( ksp_S, "scr_"); /* By default use the UW approxS Schur preconditioner -- same as the one used by the Uzawa solver */ /* Note that if scaling is activated then the approxS matrix has been scaled already */ /* so no need to rebuild in the case of scaling as we have been doing */ if(!approxS){ PetscPrintf( PETSC_COMM_WORLD, "WARNING approxS is NULL\n"); } Stg_KSPSetOperators( ksp_S, S, S, SAME_NONZERO_PATTERN ); KSPSetType( ksp_S, "cg" ); KSPGetPC( ksp_S, &pc_S ); BSSCR_BSSCR_StokesCreatePCSchur2( K,G,D,C,approxS, pc_S, sym, bsscrp_self ); flg=0; PetscOptionsGetString( PETSC_NULL, "-NN", name, PETSC_MAX_PATH_LEN-1, &flg ); if(flg){ Mat Smat, Pmat; //MatStructure mstruct; Stg_PCGetOperators( pc_S, &Smat, &Pmat, NULL ); sprintf(str,"%s/",name); sprintf(matname,"Pmat%s",suffix); bsscr_dirwriteMat( Pmat, matname,str, "Writing Pmat matrix in al Solver"); } uzawastyle=PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-uzawa_style", &uzawastyle, &found ); if(uzawastyle){ /* now want to set up the ksp_S->pc to be of type ksp (gmres) by default to match Uzawa */ KSP pc_ksp; KSPGetPC( ksp_S, &pc_S ); PCSetType(pc_S,PCKSP); PCKSPGetKSP( pc_S, &pc_ksp); KSPSetType(pc_ksp, "gmres" ); KSPSetOptionsPrefix( pc_ksp, "scrPCKSP_"); KSPSetFromOptions( pc_ksp ); } KSPSetFromOptions( ksp_S ); /* Set specific monitor test */ KSPGetTolerances( ksp_S, PETSC_NULL, PETSC_NULL, PETSC_NULL, &max_it ); // Weirdness with petsc 3.2 here...look at it later //BSSCR_KSPLogSetMonitor( ksp_S, max_it, &monitor_index ); /***************************************************************************************************************/ /* Pressure / Velocity Solve */ /***************************************************************************************************************/ PetscPrintf( PETSC_COMM_WORLD, "\t* Pressure / Velocity Solve \n"); /***************************************************************************************************************/ /***************************************************************************************************************/ usePreviousGuess = PETSC_FALSE; if(been_here) PetscOptionsGetTruth( PETSC_NULL, "-scr_use_previous_guess", &usePreviousGuess, &found ); if(usePreviousGuess) { /* Note this should actually look at checkpoint information */ KSPSetInitialGuessNonzero( ksp_S, PETSC_TRUE ); } else { KSPSetInitialGuessNonzero( ksp_S, PETSC_FALSE ); } /***************************************************************************************************************/ /***************************************************************************************************************/ /******* SET CONVERGENCE TESTS *************************************************************************/ useNormInfStoppingConditions = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL ,"-scr_use_norm_inf_stopping_condition", &useNormInfStoppingConditions, &found ); if(useNormInfStoppingConditions) BSSCR_KSPSetNormInfConvergenceTest(ksp_S); useNormInfMonitor = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-scr_ksp_norm_inf_monitor", &useNormInfMonitor, &found ); if(useNormInfMonitor) KSPMonitorSet( ksp_S, BSSCR_KSPNormInfToNorm2Monitor, PETSC_NULL, PETSC_NULL ); /***************************************************************************************************************/ /***************************************************************************************************************/ /******* PRESSURE SOLVE ************************************************************************************/ PetscPrintf( PETSC_COMM_WORLD, "\t* KSPSolve( ksp_S, h_hat, p )\n"); /* if h_hat needs to be fixed up ..take out any nullspace vectors here */ /* we want to check that there is no "noise" in the null-space in the h vector */ /* this causes problems when we are trying to solve a Jacobian system when the Residual is almost converged */ if(bsscrp_self->check_pressureNS){ bsscrp_self->buildPNS(ksp);/* build and set nullspace vectors on bsscr - which is on ksp (function pointer is set in KSPSetUp_BSSCR) */ } PetscScalar hnorm, gnorm; MatNorm(G,NORM_INFINITY,&gnorm); VecNorm(h_hat, NORM_2, &hnorm); hnorm=hnorm/gnorm; if((hnorm < 1e-6) && (hnorm > 1e-20)){ VecScale(h_hat,1.0/hnorm); } /* test to see if v or t are in nullspace of G and orthogonalize wrt h_hat if needed */ KSPRemovePressureNullspace_BSSCR(ksp, h_hat); /* set convergence test to use min_it */ found = PETSC_FALSE; min_it = 0; PetscOptionsGetInt( PETSC_NULL,"-scr_ksp_set_min_it_converge", &min_it, &found); if(found && min_it > 0){ BSSCR_KSPSetConvergenceMinIts(ksp_S, min_it, bsscrp_self); } /** Pressure Solve **/ PetscGetFlops(&flopsA); scrSolveTime = MPI_Wtime(); KSPSolve( ksp_S, h_hat, p ); scrSolveTime = MPI_Wtime() - scrSolveTime; PetscGetFlops(&flopsB); PetscPrintf( PETSC_COMM_WORLD, "\n\t* KSPSolve( ksp_S, h_hat, p ) Solve Finished in time: %lf seconds\n\n", scrSolveTime); bsscrp_self->solver->stats.pressure_time=scrSolveTime; bsscrp_self->solver->stats.pressure_flops=(double)(flopsB-flopsA); /***************************************/ if((hnorm < 1e-6) && (hnorm > 1e-20)){ VecScale(h_hat,hnorm); VecScale(p,hnorm); } KSPRemovePressureNullspace_BSSCR(ksp, p); /***************************************************************************************************************/ /***************************************************************************************************************/ /* restore K and f for the Velocity back solve */ found = PETSC_FALSE; restorek = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-restore_K", &restorek, &found); //PetscOptionsGetString( PETSC_NULL, "-restore_K", name, PETSC_MAX_PATH_LEN-1, &flg ); if(penaltyNumber > 1e-10 && bsscrp_self->k2type){ if(restorek){ penaltyNumber = -penaltyNumber; if(f2) { ierr=VecAXPY(f,penaltyNumber,f2); }/* f <- f +a*f2 */ if(f3) { ierr=VecAXPY(f,penaltyNumber,f3); }/* f <- f +a*f3 */ ierr=MatAXPY(K,penaltyNumber,K2,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);/* Computes K = penaltyNumber*K2 + K */ //K=Korig; Stg_KSPSetOperators(ksp_inner, K, K, DIFFERENT_NONZERO_PATTERN); KisJustK=PETSC_TRUE; } } if(f3){ Stg_VecDestroy(&f3 ); } /* always destroy this local vector if was created */ /* obtain solution for u */ VecDuplicate( u, &t ); MatMult( G, p, t); VecAYPX( t, -1.0, f ); /*** t <- -t + f = f - G*p ***/ MatSchurComplementGetKSP( S, &ksp_inner ); a11SingleSolveTime = MPI_Wtime(); /* ---------------------------------- Final V Solve */ if(usePreviousGuess) KSPSetInitialGuessNonzero( ksp_inner, PETSC_TRUE ); /***************************************************************************************************************/ /***************************************************************************************************************/ /******* VELOCITY SOLVE ************************************************************************************/ /** Easier to just create a new KSP here if we want to do backsolve diffferently. (getting petsc errors now when switching from fgmres) */ change_backsolve=PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-change_backsolve", &change_backsolve, &found ); if(change_backsolve){ //Stg_KSPDestroy(&ksp_inner ); KSPCreate(PETSC_COMM_WORLD, &ksp_new_inner); Stg_KSPSetOperators(ksp_new_inner, K, K, DIFFERENT_NONZERO_PATTERN); KSPSetOptionsPrefix(ksp_new_inner, "backsolveA11_"); KSPSetFromOptions(ksp_new_inner); /* make sure we are setting up our solver how we want it */ MatSchurComplementSetKSP( S, ksp_new_inner );/* need to give the Schur it's inner ksp back for when we destroy it at end */ ksp_inner=ksp_new_inner; } PetscGetFlops(&flopsA); KSPSolve(ksp_inner, t, u); /* Solve, then restore default tolerance and initial guess */ PetscGetFlops(&flopsB); bsscrp_self->solver->stats.velocity_backsolve_flops=(double)(flopsB-flopsA); a11SingleSolveTime = MPI_Wtime() - a11SingleSolveTime; /* ------------------ Final V Solve */ bsscrp_self->solver->stats.velocity_backsolve_time=a11SingleSolveTime; flg=0; PetscOptionsGetString( PETSC_NULL, "-solutiondumpdir", name, PETSC_MAX_PATH_LEN-1, &flg ); if(flg){ sprintf(str,"%s/",name); sprintf(matname,"p%s",suffix); bsscr_dirwriteVec( p, matname,str, "Writing p vector in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"u%s",suffix); bsscr_dirwriteVec( u, matname,str, "Writing u vector in al Solver"); sprintf(str,"%s/",name); sprintf(matname,"h_hat%s",suffix); bsscr_dirwriteVec( h_hat, matname,str, "Writing h_hat vector in al Solver"); } found = PETSC_FALSE; restorek = PETSC_FALSE; PetscOptionsGetTruth( PETSC_NULL, "-restore_K_after_solve", &restorek, &found); if(penaltyNumber > 1e-10 && bsscrp_self->k2type){ if(restorek){ penaltyNumber = -penaltyNumber; ierr=MatAXPY(K,penaltyNumber,K2,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);/* Computes K = penaltyNumber*K2 + K */ KisJustK=PETSC_TRUE; } } /***************************************************************************************************************/ /***************************************************************************************************************/ /****** SOLUTION SUMMARY ******************************************************************************/ bsscr_summary(bsscrp_self,ksp_S,ksp_inner,K,K2,D,G,C,u,p,f,h,t,penaltyNumber,KisJustK, mgSetupTime, scrSolveTime, a11SingleSolveTime); //bsscr_summary(bsscrp_self,ksp_S,ksp_inner,K,Korig,K2,D,G,C,u,p,f,h,t,penaltyNumber,KisJustK, mgSetupTime, scrSolveTime, a11SingleSolveTime); /***************************************************************************************************************/ /***************************************************************************************************************/ Stg_VecDestroy(&t ); Stg_KSPDestroy(&ksp_S ); Stg_VecDestroy(&h_hat ); Stg_MatDestroy(&S );//This will destroy ksp_inner: also.. pcInner == pc_MG and is destroyed when ksp_inner is been_here = 1; PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat A,B; PetscErrorCode ierr; char file[PETSC_MAX_PATH_LEN]; PetscBool flg; PetscViewer fd; MatType type = MATMPIBAIJ; PetscInitialize(&argc,&args,(char*)0,help); ierr = PetscOptionsHasName(NULL,"-aij",&flg);CHKERRQ(ierr); if (flg) type = MATMPIAIJ; ierr = PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix; then destroy the viewer. */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetType(A,type);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); /* Open another binary file. Note that we use FILE_MODE_WRITE to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_WRITE,&fd);CHKERRQ(ierr); ierr = PetscViewerBinarySetFlowControl(fd,3);CHKERRQ(ierr); /* Save the matrix and vector; then destroy the viewer. */ ierr = MatView(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); /* load the new matrix */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"fileoutput",FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&B);CHKERRQ(ierr); ierr = MatSetType(B,type);CHKERRQ(ierr); ierr = MatSetFromOptions(B);CHKERRQ(ierr); ierr = MatLoad(B,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatEqual(A,B,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Matrices are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD,"Matrices are not equal\n");CHKERRQ(ierr); } ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
EXTERN_C_END #endif #undef __FUNCT__ #define __FUNCT__ "DMInitializePackage" /*@C DMInitializePackage - This function initializes everything in the DM package. It is called from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to AOCreate() or DMDACreate() when using static libraries. Input Parameter: path - The dynamic library path, or PETSC_NULL Level: developer .keywords: AO, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode DMInitializePackage(const char path[]) { char logList[256]; char *className; PetscBool opt; PetscErrorCode ierr; PetscFunctionBegin; if (DMPackageInitialized) PetscFunctionReturn(0); DMPackageInitialized = PETSC_TRUE; /* Register Classes */ ierr = PetscClassIdRegister("Distributed Mesh",&DM_CLASSID);CHKERRQ(ierr); #ifdef PETSC_HAVE_SIEVE ierr = PetscClassIdRegister("SectionReal",&SECTIONREAL_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("SectionInt",&SECTIONINT_CLASSID);CHKERRQ(ierr); #endif #if defined(PETSC_HAVE_HYPRE) ierr = MatRegisterDynamic(MATHYPRESTRUCT, path,"MatCreate_HYPREStruct", MatCreate_HYPREStruct);CHKERRQ(ierr); #endif /* Register Constructors */ ierr = DMRegisterAll(path);CHKERRQ(ierr); /* Register Events */ ierr = PetscLogEventRegister("DMConvert", DM_CLASSID,&DM_Convert);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMGlobalToLocal", DM_CLASSID,&DM_GlobalToLocal);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMLocalToGlobal", DM_CLASSID,&DM_LocalToGlobal);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMDALocalADFunc", DM_CLASSID,&DMDA_LocalADFunction);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistribute", DM_CLASSID,&DMPLEX_Distribute);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexStratify", DM_CLASSID,&DMPLEX_Stratify);CHKERRQ(ierr); #ifdef PETSC_HAVE_SIEVE ierr = PetscLogEventRegister("DMMeshView", DM_CLASSID,&DMMesh_View);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMMeshGetGlobalScatter", DM_CLASSID,&DMMesh_GetGlobalScatter);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMMeshRestrictVector", DM_CLASSID,&DMMesh_restrictVector);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMMeshAssembleVector", DM_CLASSID,&DMMesh_assembleVector);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMMeshAssemVecComplete", DM_CLASSID,&DMMesh_assembleVectorComplete);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMMeshAssembleMatrix", DM_CLASSID,&DMMesh_assembleMatrix);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMMeshUpdateOperator", DM_CLASSID,&DMMesh_updateOperator);CHKERRQ(ierr); ierr = PetscLogEventRegister("SectionRealView", SECTIONREAL_CLASSID,&SectionReal_View);CHKERRQ(ierr); ierr = PetscLogEventRegister("SectionIntView", SECTIONINT_CLASSID,&SectionInt_View);CHKERRQ(ierr); #endif /* Process info exclusions */ ierr = PetscOptionsGetString(PETSC_NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscStrstr(logList, "da", &className);CHKERRQ(ierr); if (className) { ierr = PetscInfoDeactivateClass(DM_CLASSID);CHKERRQ(ierr); } #ifdef PETSC_HAVE_SIEVE ierr = PetscStrstr(logList, "sectionreal", &className);CHKERRQ(ierr); if (className) { ierr = PetscInfoDeactivateClass(SECTIONREAL_CLASSID);CHKERRQ(ierr); } ierr = PetscStrstr(logList, "sectionint", &className);CHKERRQ(ierr); if (className) { ierr = PetscInfoDeactivateClass(SECTIONINT_CLASSID);CHKERRQ(ierr); } #endif } /* Process summary exclusions */ ierr = PetscOptionsGetString(PETSC_NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscStrstr(logList, "da", &className);CHKERRQ(ierr); if (className) { ierr = PetscLogEventDeactivateClass(DM_CLASSID);CHKERRQ(ierr); } #ifdef PETSC_HAVE_SIEVE ierr = PetscStrstr(logList, "sectionreal", &className);CHKERRQ(ierr); if (className) { ierr = PetscLogEventDeactivateClass(SECTIONREAL_CLASSID);CHKERRQ(ierr); } ierr = PetscStrstr(logList, "sectionint", &className);CHKERRQ(ierr); if (className) { ierr = PetscLogEventDeactivateClass(SECTIONINT_CLASSID);CHKERRQ(ierr); } #endif } ierr = PetscRegisterFinalize(DMFinalizePackage);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat A,Asp; PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscErrorCode ierr; PetscInt m,n,rstart,rend; PetscBool flg; PetscInt row,ncols,j,nrows,nnzA=0,nnzAsp=0; const PetscInt *cols; const PetscScalar *vals; PetscReal norm,percent,val,dtol=1.e-16; PetscMPIInt rank; MatInfo matinfo; PetscInt Dnnz,Onnz; PetscInitialize(&argc,&args,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* Determine files from which we read the linear systems. */ ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix; then destroy the viewer. */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(A,"a_");CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); ierr = MatGetInfo(A,MAT_LOCAL,&matinfo);CHKERRQ(ierr); //printf("matinfo.nz_used %g\n",matinfo.nz_used); /* Get a sparse matrix Asp by dumping zero entries of A */ ierr = MatCreate(PETSC_COMM_WORLD,&Asp);CHKERRQ(ierr); ierr = MatSetSizes(Asp,m,n,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(Asp,"asp_");CHKERRQ(ierr); ierr = MatSetFromOptions(Asp);CHKERRQ(ierr); Dnnz = (PetscInt)matinfo.nz_used/m + 1; Onnz = Dnnz/2; printf("Dnnz %d %d\n",Dnnz,Onnz); ierr = MatSeqAIJSetPreallocation(Asp,Dnnz,PETSC_NULL);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(Asp,Dnnz,PETSC_NULL,Onnz,PETSC_NULL);CHKERRQ(ierr); /* Check zero rows */ ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); nrows = 0; for (row=rstart; row<rend; row++){ ierr = MatGetRow(A,row,&ncols,&cols,&vals);CHKERRQ(ierr); nnzA += ncols; norm = 0.0; for (j=0; j<ncols; j++){ val = PetscAbsScalar(vals[j]); if (norm < val) norm = norm; if (val > dtol){ ierr = MatSetValues(Asp,1,&row,1,&cols[j],&vals[j],INSERT_VALUES);CHKERRQ(ierr); nnzAsp++; } } if (!norm) nrows++; ierr = MatRestoreRow(A,row,&ncols,&cols,&vals);CHKERRQ(ierr); } ierr = MatAssemblyBegin(Asp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(Asp,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); percent=(PetscReal)nnzA*100/(m*n); ierr = PetscPrintf(PETSC_COMM_SELF," [%d] Matrix A local size %d,%d; nnzA %d, %g percent; No. of zero rows: %d\n",rank,m,n,nnzA,percent,nrows); percent=(PetscReal)nnzAsp*100/(m*n); ierr = PetscPrintf(PETSC_COMM_SELF," [%d] Matrix Asp nnzAsp %d, %g percent\n",rank,nnzAsp,percent); /* investigate matcoloring for Asp */ PetscBool Asp_coloring = PETSC_FALSE; ierr = PetscOptionsHasName(PETSC_NULL,"-Asp_color",&Asp_coloring);CHKERRQ(ierr); if (Asp_coloring){ ISColoring iscoloring; MatFDColoring matfdcoloring; ierr = PetscPrintf(PETSC_COMM_WORLD," Create coloring of Asp...\n"); ierr = MatGetColoring(Asp,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr); ierr = MatFDColoringCreate(Asp,iscoloring,&matfdcoloring);CHKERRQ(ierr); ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); //ierr = MatFDColoringView(matfdcoloring,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr); } /* Write Asp in binary for study - see ~petsc/src/mat/examples/tests/ex124.c */ PetscBool Asp_write = PETSC_FALSE; ierr = PetscOptionsHasName(PETSC_NULL,"-Asp_write",&Asp_write);CHKERRQ(ierr); if (Asp_write){ PetscViewer viewer; ierr = PetscPrintf(PETSC_COMM_SELF,"Write Asp into file Asp.dat ...\n"); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"Asp.dat",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); ierr = MatView(Asp,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&Asp);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
PetscErrorCode waitForSignal(PetscInt waitTime) { PetscErrorCode ierr; FILE *fd1; PetscViewer fd; PetscInt iDone=0; PetscInt zero=0; PetscInt one=1; PetscInt fp; PetscInt numsignalfiles = 2; PetscTruth flg; if (firstTime) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Initializing signaling module ...\n");CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-signalscript",externalSignalScriptName,PETSC_MAX_PATH_LEN-1,&useExternalSignalScript);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-signalfiles",&useSignalFiles);CHKERRQ(ierr); if ((useExternalSignalScript) && (useSignalFiles)) { SETERRQ(1,"Cannot specify both an external signal script and signal files!"); } if ((!useExternalSignalScript) && (!useSignalFiles)) { SETERRQ(1,"Must specify an external signal script OR signal files!"); } if (useExternalSignalScript) { ierr = PetscPrintf(PETSC_COMM_WORLD,"External signaling script has been specified: %s\n",externalSignalScriptName);CHKERRQ(ierr); } if (useSignalFiles) { ierr = PetscOptionsGetStringArray(PETSC_NULL,"-signalfiles",signalfiles,&numsignalfiles,&flg);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Signal files have been specified: %s, %s\n",signalfiles[0],signalfiles[1]);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-signalwaittime",&signalWaitTime,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Signal wait time of %d seconds has been specified\n",signalWaitTime);CHKERRQ(ierr); } else { signalWaitTime = -1; } } firstTime = PETSC_FALSE; } else { if (useExternalSignalScript) { ierr = PetscPOpen(PETSC_COMM_WORLD,PETSC_NULL,externalSignalScriptName,"r",&fd1);CHKERRQ(ierr); ierr = PetscPClose(PETSC_COMM_WORLD,fd1);CHKERRQ(ierr); } else { if (signalWaitTime>0) waitTime = signalWaitTime; /* overwrite with runtime option */ /* overwrite external signal file */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,signalfiles[1],FILE_MODE_WRITE,&fd);CHKERRQ(ierr); ierr = PetscViewerBinaryGetDescriptor(fd,&fp);CHKERRQ(ierr); ierr = PetscBinarySynchronizedWrite(PETSC_COMM_WORLD,fp,&zero,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); /* send "ready" signal */ ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,signalfiles[0],&fd);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(fd,"%d\n",one);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); /* wait for external signal */ while (iDone==0) { ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,signalfiles[1],FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = PetscViewerBinaryGetDescriptor(fd,&fp);CHKERRQ(ierr); ierr = PetscBinarySynchronizedRead(PETSC_COMM_WORLD,fp,&iDone,1,PETSC_INT);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); PetscSleep(waitTime); } /* send "busy" signal */ ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,signalfiles[0],&fd);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(fd,"%d\n",zero);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); } } return 0; }
int getoptions(TAO_APPS *appsPtr) { // Haven't done break or reassign options // -o only works for PVM char options[16][10] = {"cache","alpha","pattern","search","tol","active","debug","profile", "precision","inc","contract","step","min","max","nnls","eps"}; char strings[35][50]; // Don't need this many, but just being safe char buffer[50]={0}; char tempstring[16]; int i; int info; PetscTruth flg,cacheisoff; int length; // Read in all options, store as strings. Later, copy strings to argv appsPtr->argc=0; // don't really need this one, but need to start strings at argv[1] PetscStrcpy(strings[(appsPtr->argc)++],"executable"); // It's possible to use PetscStrcpy and PetscStrcat instead of snprintf info = PetscOptionsGetString(0,"-apps_i",buffer, 49, &flg); CHKERRQ(info); if (flg) { snprintf(strings[(appsPtr->argc)++],50,"-i%-s",buffer); } info = PetscOptionsGetString(0,"-apps_o",buffer, 49, &flg); CHKERRQ(info); if (flg) { snprintf(strings[(appsPtr->argc)++],50,"-o%-s",buffer); } for (i=0;i<16;i++) { snprintf(tempstring,16,"-apps_%s",options[i]); info = PetscOptionsGetString(0,tempstring,buffer,49,&flg); CHKERRQ(info); if (flg) { snprintf(strings[(appsPtr->argc)++],50,"--%s=%-s",options[i],buffer); // The TaoFevalMgr needs to know if the cache is to be used // Assume it is to be used unless "-apps_cache false" is used if (i==0) { info = PetscStrncmp(buffer,"false",5,&cacheisoff); CHKERRQ(info); if (cacheisoff) appsPtr->usecache = 0; } } } info = TaoMalloc(sizeof(char *)*appsPtr->argc, &appsPtr->argv); CHKERRQ(info); for (i=0;i<appsPtr->argc; i++) { info = PetscStrlen(strings[i],&length); CHKERRQ(info); info = TaoMalloc(sizeof(char)*length+1, &appsPtr->argv[i]); CHKERRQ(info); info = PetscStrncpy(appsPtr->argv[i],strings[i],length); CHKERRQ(info); } TaoFunctionReturn(0); }
int main(int argc,char **args) { /*PETSc Mat Object */ Mat pMat; /* Input matrix market file and output PETSc binary file */ char inputFile[128],outputFile[128],buf[128]; /* number rows, columns, non zeros etc */ int i, m,n,nnz,ierr,col,row; /*We compute no of nozeros per row for PETSc Mat object pre-allocation*/ int *nnzPtr; /*Maximum nonzero in nay row */ int maxNNZperRow=0; /*Row number containing max non zero elements */ int maxRowNum = 0; /*Just no of comments that will be ignore during successive read of file */ int numComments=0; /* This is variable of type double */ PetscScalar val; /*File handle for read and write*/ FILE* file; /*File handle for writing nonzero elements distribution per row */ FILE *fileRowDist; /*PETSc Viewer is used for writing PETSc Mat object in binary format */ PetscViewer view; /*Just record time required for conversion */ PetscLogDouble t1,t2,elapsed_time; /*Initialise PETSc lib */ PetscInitialize(&argc,&args,(char *)0,PETSC_NULL); /* Just record time */ ierr = PetscGetTime(&t1); CHKERRQ(ierr); /*Get name of matrix market file from command line options and Open file*/ ierr = PetscOptionsGetString(PETSC_NULL,"-fin",inputFile,127,PETSC_NULL); ierr = PetscFOpen(PETSC_COMM_SELF,inputFile,"r",&file); /* Just count the comment lines in the file */ while(1) { fgets(buf,128,file); /*If line starts with %, its a comment */ if(buf[0] == '%') { printf("\n IGNORING COMMENT LINE : IGNORING...."); numComments++; } else { /*Set Pointer to Start of File */ fseek(file, 0, SEEK_SET ); int num = numComments; /* and just move pointer to the entry in the file which indicates row nums, col nums and non zero elements */ while(num--) fgets(buf,128,file); break; } } /*Reads size of sparse matrix from matrix market file */ fscanf(file,"%d %d %d\n",&m,&n,&nnz); printf ("ROWS = %d, COLUMNS = %d, NO OF NON-ZEROS = %d\n",m,n,nnz); /*Now we will calculate non zero elelments distribution per row */ nnzPtr = (int *) calloc (sizeof(int), m); /*This is similar to calculate histogram or frequency of elements in the array */ for (i=0; !feof(file); i++) { fscanf(file,"%d %d %le\n",&row,&col,&val); row = row-1; col = col-1 ; nnzPtr[row]++; } printf("\n ROW DISTRIBUTION CALCULATED....WRITING TO THE FILE..!"); fflush(stdout); /*Write row distribution to the file ROW_STR.dat */ fileRowDist = fopen ("ROW_DISTR.dat", "w"); for (i=0; i< m; i++) { fprintf(fileRowDist, "%d\t %d\n", i, nnzPtr[i]); /*Find max num of of nonzero for any row of the matrix and that row number */ if( maxNNZperRow < nnzPtr[i] ) { /*store max nonzero for any row*/ maxNNZperRow = nnzPtr[i]; /*row that contains max non zero elements*/ maxRowNum = i; } } /*Close File */ fclose(fileRowDist); printf("\n MAX NONZERO FOR ANY ROW ARE : %d & ROW NUM IS : %d", maxNNZperRow, maxRowNum ); /* Again set the file pointer the fist data record in matrix market file* * Note that we can directly move ponts with fseek, but as this is text file * we are simple reading line by line */ fseek(file, 0, SEEK_SET ); numComments++; while(numComments--) fgets(buf,128,file); /* Its important to pre-allocate memory by passing max non zero for any row in the matrix */ ierr = MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,maxNNZperRow,PETSC_NULL,&pMat); /* OR we can also pass row distribution of nozero elements for every row */ /* ierr = MatCreateSeqAIJ(PETSC_COMM_WORLD,m,n,0,nnzPtr,&pMat);*/ /*Now Set matrix elements values form matrix market file */ for (i=0; i<nnz; i++) { /*Read matrix element from matrix market file*/ fscanf(file,"%d %d %le\n",&row,&col,&val); /*In matrix market format, rows and columns starts from 1 */ row = row-1; col = col-1 ; /* For every non zero element,insert that value at row,col position */ ierr = MatSetValues(pMat,1,&row,1,&col,&val,INSERT_VALUES); } fclose(file); /*Matrix Read Complete */ ierr = PetscPrintf(PETSC_COMM_SELF,"\n MATRIX READ...DONE!"); /*Now assemeble the matrix */ ierr = MatAssemblyBegin(pMat,MAT_FINAL_ASSEMBLY); ierr = MatAssemblyEnd(pMat,MAT_FINAL_ASSEMBLY); /* Now open output file for writing into PETSc Binary FOrmat*/ ierr = PetscOptionsGetString(PETSC_NULL,"-fout",outputFile,127,PETSC_NULL);CHKERRQ(ierr); /*With the PETSc Viewer write output to File*/ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,outputFile,FILE_MODE_WRITE,&view);CHKERRQ(ierr); /*Matview will dump the Mat object to binary file */ ierr = MatView(pMat,view);CHKERRQ(ierr); /* Destroy the data structure */ ierr = PetscViewerDestroy(&view);CHKERRQ(ierr); ierr = MatDestroy(&pMat);CHKERRQ(ierr); /*Just for statistics*/ ierr = PetscGetTime(&t2);CHKERRQ(ierr); elapsed_time = t2 - t1; ierr = PetscPrintf(PETSC_COMM_SELF,"ELAPSE TIME: %g\n",elapsed_time);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **args) { Mat A,RHS,C,F,X; Vec u,x,b; PetscErrorCode ierr; PetscMPIInt rank,nproc; PetscInt i,m,n,nfact,nsolve,nrhs,ipack=0; PetscScalar *array,rval; PetscReal norm,tol=1.e-12; IS perm,iperm; MatFactorInfo info; PetscRandom rand; PetscBool flg,testMatSolve=PETSC_TRUE,testMatMatSolve=PETSC_TRUE; PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscInitialize(&argc,&args,(char*)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD, &nproc);CHKERRQ(ierr); /* Determine file from which we read the matrix A */ ierr = PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Load matrix A */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n); /* Create dense matrix C and X; C holds true solution with identical colums */ nrhs = 2; ierr = PetscOptionsGetInt(NULL,"-nrhs",&nrhs,NULL);CHKERRQ(ierr); if (!rank) printf("ex125: nrhs %d\n",nrhs); ierr = MatCreate(PETSC_COMM_WORLD,&C);CHKERRQ(ierr); ierr = MatSetSizes(C,m,PETSC_DECIDE,PETSC_DECIDE,nrhs);CHKERRQ(ierr); ierr = MatSetType(C,MATDENSE);CHKERRQ(ierr); ierr = MatSetFromOptions(C);CHKERRQ(ierr); ierr = MatSetUp(C);CHKERRQ(ierr); ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rand);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rand);CHKERRQ(ierr); ierr = MatSetRandom(C,rand);CHKERRQ(ierr); ierr = MatDuplicate(C,MAT_DO_NOT_COPY_VALUES,&X);CHKERRQ(ierr); /* Create vectors */ ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,n,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecDuplicate(x,&b);CHKERRQ(ierr); ierr = VecDuplicate(x,&u);CHKERRQ(ierr); /* save the true solution */ /* Test LU Factorization */ ierr = MatGetOrdering(A,MATORDERINGND,&perm,&iperm);CHKERRQ(ierr); /*ierr = ISView(perm,PETSC_VIEWER_STDOUT_WORLD);*/ /*ierr = ISView(perm,PETSC_VIEWER_STDOUT_SELF);*/ ierr = PetscOptionsGetInt(NULL,"-mat_solver_package",&ipack,NULL);CHKERRQ(ierr); switch (ipack) { case 0: #if defined(PETSC_HAVE_SUPERLU) if (!rank) printf(" SUPERLU LU:\n"); ierr = MatGetFactor(A,MATSOLVERSUPERLU,MAT_FACTOR_LU,&F);CHKERRQ(ierr); break; #endif case 1: #if defined(PETSC_HAVE_SUPERLU_DIST) if (!rank) printf(" SUPERLU_DIST LU:\n"); ierr = MatGetFactor(A,MATSOLVERSUPERLU_DIST,MAT_FACTOR_LU,&F);CHKERRQ(ierr); break; #endif case 2: #if defined(PETSC_HAVE_MUMPS) if (!rank) printf(" MUMPS LU:\n"); ierr = MatGetFactor(A,MATSOLVERMUMPS,MAT_FACTOR_LU,&F);CHKERRQ(ierr); { /* test mumps options */ PetscInt icntl_7 = 5; ierr = MatMumpsSetIcntl(F,7,icntl_7);CHKERRQ(ierr); } break; #endif default: if (!rank) printf(" PETSC LU:\n"); ierr = MatGetFactor(A,MATSOLVERPETSC,MAT_FACTOR_LU,&F);CHKERRQ(ierr); } info.fill = 5.0; ierr = MatLUFactorSymbolic(F,A,perm,iperm,&info);CHKERRQ(ierr); for (nfact = 0; nfact < 2; nfact++) { if (!rank) printf(" %d-the LU numfactorization \n",nfact); ierr = MatLUFactorNumeric(F,A,&info);CHKERRQ(ierr); /* Test MatMatSolve() */ /* if ((ipack == 0 || ipack == 2) && testMatMatSolve) { printf(" MatMatSolve() is not implemented for this package. Skip the testing.\n"); testMatMatSolve = PETSC_FALSE; } */ if (testMatMatSolve) { if (!nfact) { ierr = MatMatMult(A,C,MAT_INITIAL_MATRIX,2.0,&RHS);CHKERRQ(ierr); } else { ierr = MatMatMult(A,C,MAT_REUSE_MATRIX,2.0,&RHS);CHKERRQ(ierr); } for (nsolve = 0; nsolve < 2; nsolve++) { if (!rank) printf(" %d-the MatMatSolve \n",nsolve); ierr = MatMatSolve(F,RHS,X);CHKERRQ(ierr); /* Check the error */ ierr = MatAXPY(X,-1.0,C,SAME_NONZERO_PATTERN);CHKERRQ(ierr); ierr = MatNorm(X,NORM_FROBENIUS,&norm);CHKERRQ(ierr); if (norm > tol) { if (!rank) { ierr = PetscPrintf(PETSC_COMM_SELF,"1st MatMatSolve: Norm of error %g, nsolve %d\n",norm,nsolve);CHKERRQ(ierr); } } } } /* Test MatSolve() */ if (testMatSolve) { for (nsolve = 0; nsolve < 2; nsolve++) { ierr = VecGetArray(x,&array);CHKERRQ(ierr); for (i=0; i<m; i++) { ierr = PetscRandomGetValue(rand,&rval);CHKERRQ(ierr); array[i] = rval; } ierr = VecRestoreArray(x,&array);CHKERRQ(ierr); ierr = VecCopy(x,u);CHKERRQ(ierr); ierr = MatMult(A,x,b);CHKERRQ(ierr); if (!rank) printf(" %d-the MatSolve \n",nsolve); ierr = MatSolve(F,b,x);CHKERRQ(ierr); /* Check the error */ ierr = VecAXPY(u,-1.0,x);CHKERRQ(ierr); /* u <- (-1.0)x + u */ ierr = VecNorm(u,NORM_2,&norm);CHKERRQ(ierr); if (norm > tol) { ierr = MatMult(A,x,u);CHKERRQ(ierr); /* u = A*x */ PetscReal resi; ierr = VecAXPY(u,-1.0,b);CHKERRQ(ierr); /* u <- (-1.0)b + u */ ierr = VecNorm(u,NORM_2,&resi);CHKERRQ(ierr); if (!rank) { ierr = PetscPrintf(PETSC_COMM_SELF,"MatSolve: Norm of error %g, resi %g, LU numfact %d\n",norm,resi,nfact);CHKERRQ(ierr); } } } } } /* Free data structures */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&C);CHKERRQ(ierr); ierr = MatDestroy(&F);CHKERRQ(ierr); ierr = MatDestroy(&X);CHKERRQ(ierr); if (testMatMatSolve) { ierr = MatDestroy(&RHS);CHKERRQ(ierr); } ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr); ierr = ISDestroy(&perm);CHKERRQ(ierr); ierr = ISDestroy(&iperm);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
/*@C DMInitializePackage - This function initializes everything in the DM package. It is called from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to AOCreate() or DMDACreate() when using static libraries. Level: developer .keywords: AO, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode DMInitializePackage(void) { char logList[256]; char *className; PetscBool opt; PetscErrorCode ierr; PetscFunctionBegin; if (DMPackageInitialized) PetscFunctionReturn(0); DMPackageInitialized = PETSC_TRUE; /* Register Classes */ ierr = PetscClassIdRegister("Distributed Mesh",&DM_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("GraphPartitioner",&PETSCPARTITIONER_CLASSID);CHKERRQ(ierr); #if defined(PETSC_HAVE_HYPRE) ierr = MatRegister(MATHYPRESTRUCT, MatCreate_HYPREStruct);CHKERRQ(ierr); #endif /* Register Constructors */ ierr = DMRegisterAll();CHKERRQ(ierr); /* Register Events */ ierr = PetscLogEventRegister("DMConvert", DM_CLASSID,&DM_Convert);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMGlobalToLocal", DM_CLASSID,&DM_GlobalToLocal);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMLocalToGlobal", DM_CLASSID,&DM_LocalToGlobal);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMDALocalADFunc", DM_CLASSID,&DMDA_LocalADFunction);CHKERRQ(ierr); ierr = PetscLogEventRegister("Mesh Partition", PETSCPARTITIONER_CLASSID,&PETSCPARTITIONER_Partition);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexInterp", DM_CLASSID,&DMPLEX_Interpolate);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistribute", DM_CLASSID,&DMPLEX_Distribute);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistCones", DM_CLASSID,&DMPLEX_DistributeCones);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistLabels", DM_CLASSID,&DMPLEX_DistributeLabels);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistribSF", DM_CLASSID,&DMPLEX_DistributeSF);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistribOL", DM_CLASSID,&DMPLEX_DistributeOverlap);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistField", DM_CLASSID,&DMPLEX_DistributeField);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexDistData", DM_CLASSID,&DMPLEX_DistributeData);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexStratify", DM_CLASSID,&DMPLEX_Stratify);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexPrealloc", DM_CLASSID,&DMPLEX_Preallocate);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexResidualFE", DM_CLASSID,&DMPLEX_ResidualFEM);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexJacobianFE", DM_CLASSID,&DMPLEX_JacobianFEM);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexInterpFE", DM_CLASSID,&DMPLEX_InterpolatorFEM);CHKERRQ(ierr); ierr = PetscLogEventRegister("DMPlexInjectorFE", DM_CLASSID,&DMPLEX_InjectorFEM);CHKERRQ(ierr); /* Process info exclusions */ ierr = PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscStrstr(logList, "da", &className);CHKERRQ(ierr); if (className) { ierr = PetscInfoDeactivateClass(DM_CLASSID);CHKERRQ(ierr); } } /* Process summary exclusions */ ierr = PetscOptionsGetString(NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscStrstr(logList, "da", &className);CHKERRQ(ierr); if (className) { ierr = PetscLogEventDeactivateClass(DM_CLASSID);CHKERRQ(ierr); } } ierr = PetscRegisterFinalize(DMFinalizePackage);CHKERRQ(ierr); PetscFunctionReturn(0); }
Example: mpiexec -n <np> ./ex130 -f <matrix binary file> -mat_solver_package 1 -mat_superlu_equil \n\n"; #include <petscmat.h> #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { Mat A,F; Vec u,x,b; PetscErrorCode ierr; PetscMPIInt rank,size; PetscInt m,n,nfact,ipack=0; PetscReal norm,tol=1.e-12,Anorm; IS perm,iperm; MatFactorInfo info; PetscBool flg,testMatSolve=PETSC_TRUE; PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscInitialize(&argc,&args,(char*)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRQ(ierr); /* Determine file from which we read the matrix A */ ierr = PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Load matrix A */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr); ierr = VecLoad(b,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n); ierr = MatNorm(A,NORM_INFINITY,&Anorm);CHKERRQ(ierr); /* Create vectors */ ierr = VecDuplicate(b,&x);CHKERRQ(ierr); ierr = VecDuplicate(x,&u);CHKERRQ(ierr); /* save the true solution */ /* Test LU Factorization */ ierr = MatGetOrdering(A,MATORDERINGNATURAL,&perm,&iperm);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,"-mat_solver_package",&ipack,NULL);CHKERRQ(ierr); switch (ipack) { case 1: #if defined(PETSC_HAVE_SUPERLU) if (!rank) printf(" SUPERLU LU:\n"); ierr = MatGetFactor(A,MATSOLVERSUPERLU,MAT_FACTOR_LU,&F);CHKERRQ(ierr); break; #endif case 2: #if defined(PETSC_HAVE_MUMPS) if (!rank) printf(" MUMPS LU:\n"); ierr = MatGetFactor(A,MATSOLVERMUMPS,MAT_FACTOR_LU,&F);CHKERRQ(ierr); { /* test mumps options */ PetscInt icntl_7 = 5; ierr = MatMumpsSetIcntl(F,7,icntl_7);CHKERRQ(ierr); } break; #endif default: if (!rank) printf(" PETSC LU:\n"); ierr = MatGetFactor(A,MATSOLVERPETSC,MAT_FACTOR_LU,&F);CHKERRQ(ierr); } info.fill = 5.0; ierr = MatLUFactorSymbolic(F,A,perm,iperm,&info);CHKERRQ(ierr); for (nfact = 0; nfact < 1; nfact++) { if (!rank) printf(" %d-the LU numfactorization \n",nfact); ierr = MatLUFactorNumeric(F,A,&info);CHKERRQ(ierr); /* Test MatSolve() */ if (testMatSolve) { ierr = MatSolve(F,b,x);CHKERRQ(ierr); /* Check the residual */ ierr = MatMult(A,x,u);CHKERRQ(ierr); ierr = VecAXPY(u,-1.0,b);CHKERRQ(ierr); ierr = VecNorm(u,NORM_INFINITY,&norm);CHKERRQ(ierr); if (norm > tol) { if (!rank) { ierr = PetscPrintf(PETSC_COMM_SELF,"MatSolve: rel residual %g/%g = %g, LU numfact %d\n",norm,Anorm,norm/Anorm,nfact);CHKERRQ(ierr); } } } } /* Free data structures */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&F);CHKERRQ(ierr); ierr = ISDestroy(&perm);CHKERRQ(ierr); ierr = ISDestroy(&iperm);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main(int argc,char **argv) { Mat A,B,C,PtAP,PtAP_copy,PtAP_squared; PetscInt i,M,N,Istart,Iend,n=7,j,J,Ii,m=8,k,o=1; PetscScalar v; PetscErrorCode ierr; PetscBool equal=PETSC_FALSE,mat_view=PETSC_FALSE; char stencil[PETSC_MAX_PATH_LEN]; #if defined(PETSC_USE_LOG) PetscLogStage fullMatMatMultStage; #endif ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-o",&o,NULL);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL,NULL,"-result_view",&mat_view);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-stencil",stencil,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr); /* Create a aij matrix A */ M = N = m*n*o; ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); ierr = MatSetType(A,MATAIJ);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); /* Consistency checks */ if (o < 1 || m < 1 || n < 1) SETERRQ(PETSC_COMM_WORLD,1,"Dimensions need to be larger than zero!"); /************ 2D stencils ***************/ ierr = PetscStrcmp(stencil, "2d5point", &equal);CHKERRQ(ierr); if (equal) { /* 5-point stencil, 2D */ ierr = MatMPIAIJSetPreallocation(A,5,NULL,5,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,5,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (i>0) {J = global_index(i-1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = global_index(i,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 4.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = PetscStrcmp(stencil, "2d9point", &equal);CHKERRQ(ierr); if (equal) { /* 9-point stencil, 2D */ ierr = MatMPIAIJSetPreallocation(A,9,NULL,9,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,9,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (i>0) {J = global_index(i-1,j, k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>0 && j>0) {J = global_index(i-1,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if ( j>0) {J = global_index(i, j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1 && j>0) {J = global_index(i+1,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j, k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1 && j<n-1) {J = global_index(i+1,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i, j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>0 && j<n-1) {J = global_index(i-1,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 8.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = PetscStrcmp(stencil, "2d9point2", &equal);CHKERRQ(ierr); if (equal) { /* 9-point Cartesian stencil (width 2 per coordinate), 2D */ ierr = MatMPIAIJSetPreallocation(A,9,NULL,9,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,9,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (i>0) {J = global_index(i-1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>1) {J = global_index(i-2,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-2) {J = global_index(i+2,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = global_index(i,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>1) {J = global_index(i,j-2,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-2) {J = global_index(i,j+2,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 8.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = PetscStrcmp(stencil, "2d13point", &equal);CHKERRQ(ierr); if (equal) { /* 13-point Cartesian stencil (width 3 per coordinate), 2D */ ierr = MatMPIAIJSetPreallocation(A,13,NULL,13,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,13,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (i>0) {J = global_index(i-1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>1) {J = global_index(i-2,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>2) {J = global_index(i-3,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-2) {J = global_index(i+2,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-3) {J = global_index(i+3,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = global_index(i,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>1) {J = global_index(i,j-2,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>2) {J = global_index(i,j-3,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-2) {J = global_index(i,j+2,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-3) {J = global_index(i,j+3,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 12.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } /************ 3D stencils ***************/ ierr = PetscStrcmp(stencil, "3d7point", &equal);CHKERRQ(ierr); if (equal) { /* 7-point stencil, 3D */ ierr = MatMPIAIJSetPreallocation(A,7,NULL,7,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,7,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (i>0) {J = global_index(i-1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = global_index(i,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k>0) {J = global_index(i,j,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k<o-1) {J = global_index(i,j,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 6.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = PetscStrcmp(stencil, "3d13point", &equal);CHKERRQ(ierr); if (equal) { /* 13-point stencil, 3D */ ierr = MatMPIAIJSetPreallocation(A,13,NULL,13,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,13,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (i>0) {J = global_index(i-1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>1) {J = global_index(i-2,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-2) {J = global_index(i+2,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = global_index(i,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>1) {J = global_index(i,j-2,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-2) {J = global_index(i,j+2,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k>0) {J = global_index(i,j,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k>1) {J = global_index(i,j,k-2,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k<o-1) {J = global_index(i,j,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k<o-2) {J = global_index(i,j,k+2,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 12.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = PetscStrcmp(stencil, "3d19point", &equal);CHKERRQ(ierr); if (equal) { /* 19-point stencil, 3D */ ierr = MatMPIAIJSetPreallocation(A,19,NULL,19,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,19,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); /* one hop */ if (i>0) {J = global_index(i-1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = global_index(i+1,j,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = global_index(i,j-1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = global_index(i,j+1,k,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k>0) {J = global_index(i,j,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (k<o-1) {J = global_index(i,j,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} /* two hops */ if (i>0 && j>0) {J = global_index(i-1,j-1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>0 && k>0) {J = global_index(i-1,j, k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>0 && j<n-1) {J = global_index(i-1,j+1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i>0 && k<o-1) {J = global_index(i-1,j, k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1 && j>0) {J = global_index(i+1,j-1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1 && k>0) {J = global_index(i+1,j, k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1 && j<n-1) {J = global_index(i+1,j+1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1 && k<o-1) {J = global_index(i+1,j, k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0 && k>0) {J = global_index(i, j-1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0 && k<o-1) {J = global_index(i, j-1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1 && k>0) {J = global_index(i, j+1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1 && k<o-1) {J = global_index(i, j+1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 18.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = PetscStrcmp(stencil, "3d27point", &equal);CHKERRQ(ierr); if (equal) { /* 27-point stencil, 3D */ ierr = MatMPIAIJSetPreallocation(A,27,NULL,27,NULL);CHKERRQ(ierr); ierr = MatSeqAIJSetPreallocation(A,27,NULL);CHKERRQ(ierr); ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr); for (Ii=Istart; Ii<Iend; Ii++) { v = -1.0; k = Ii / (m*n); j = (Ii - k * m * n) / m; i = (Ii - k * m * n - j * m); if (k>0) { if (j>0) { if (i>0) {J = global_index(i-1,j-1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j-1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j-1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } { if (i>0) {J = global_index(i-1,j, k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j, k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j, k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } if (j<n-1) { if (i>0) {J = global_index(i-1,j+1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j+1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j+1,k-1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } } { if (j>0) { if (i>0) {J = global_index(i-1,j-1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j-1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j-1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } { if (i>0) {J = global_index(i-1,j, k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j, k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j, k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } if (j<n-1) { if (i>0) {J = global_index(i-1,j+1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j+1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j+1,k ,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } } if (k<o-1) { if (j>0) { if (i>0) {J = global_index(i-1,j-1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j-1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j-1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } { if (i>0) {J = global_index(i-1,j, k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j, k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j, k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } if (j<n-1) { if (i>0) {J = global_index(i-1,j+1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} J = global_index(i, j+1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr); if (i<m-1) {J = global_index(i+1,j+1,k+1,m,n); ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} } } v = 26.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); /* Copy A into B in order to have a more representative benchmark (A*A has more cache hits than A*B) */ ierr = MatDuplicate(A,MAT_COPY_VALUES,&B);CHKERRQ(ierr); ierr = PetscLogStageRegister("Full MatMatMult",&fullMatMatMultStage);CHKERRQ(ierr); /* Test C = A*B */ ierr = PetscLogStagePush(fullMatMatMultStage);CHKERRQ(ierr); ierr = MatMatMult(A,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C);CHKERRQ(ierr); /* Test PtAP_squared = PtAP(C,C)*PtAP(C,C) */ ierr = MatPtAP(C,C,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&PtAP);CHKERRQ(ierr); ierr = MatDuplicate(PtAP,MAT_COPY_VALUES,&PtAP_copy);CHKERRQ(ierr); ierr = MatMatMult(PtAP,PtAP_copy,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&PtAP_squared);CHKERRQ(ierr); ierr = MatView(C,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = MatView(PtAP_squared,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = MatDestroy(&PtAP_squared);CHKERRQ(ierr); ierr = MatDestroy(&PtAP_copy);CHKERRQ(ierr); ierr = MatDestroy(&PtAP);CHKERRQ(ierr); ierr = MatDestroy(&C);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
Geometry CreateGeometry(int N[3], double h[3], int Npml[3], int Nc, int LowerPML, double *eps, double *epsI, double *fprof, double wa, double y){ int i; Geometry geo = (Geometry) malloc(sizeof(struct Geometry_s)); geo->Nc = Nc; geo->LowerPML = LowerPML; geo->interference = 0.0; // default no interference for(i=0; i<3; i++){ geo->h[i] = h[i]; geo->Npml[i] = Npml[i]; } CreateGrid(&geo->gN, N, geo->Nc, 2); CreateGrid(&geo->gM, N, 1, 1); // 3/3/14: set M = N as per Steven CreateVec(2*Nxyzc(geo)+2, &geo->vepspml); int manual_epspml = 0; PetscOptionsGetInt(PETSC_NULL,PETSC_NULL,"-manual_epspml", &manual_epspml, NULL); if(manual_epspml == 0){ Vecfun pml; CreateVecfun(&pml,geo->vepspml); for(i=pml.ns; i<pml.ne; i++){ Point p; CreatePoint_i(&p, i, &geo->gN); project(&p, 3); dcomp eps_geoal; eps_geoal = pmlval(xyzc(&p), N, geo->Npml, geo->h, geo->LowerPML, 0); setr(&pml, i, p.ir? cimag(eps_geoal) : creal(eps_geoal) ); } DestroyVecfun(&pml); } CreateVec(Mxyz(geo), &geo->vMscratch[0]); for(i=0; i<SCRATCHNUM; i++){ geo->vNhscratch[i] = 0; // allows checking whether vN created or not if(i>0)VecDuplicate(geo->vMscratch[0], &geo->vMscratch[i]); } double *scratch; int ms, me; VecGetOwnershipRange(geo->vMscratch[0], &ms, &me); if( !manual_epspml){ VecGetArray(geo->vMscratch[0], &scratch); for(i=ms; i<me;i++) scratch[i-ms] = eps[i-ms]; VecRestoreArray(geo->vMscratch[0], &scratch); } CreateVec(2*Nxyzc(geo)+2, &geo->vH); VecDuplicate(geo->vH, &geo->veps); VecDuplicate(geo->vH, &geo->vIeps); for(i=0; i<SCRATCHNUM; i++) VecDuplicate(geo->vH, &geo->vscratch[i]); VecSet(geo->vH, 1.0); if( !manual_epspml){ VecShift(geo->vMscratch[0], -1.0); //hack, for background dielectric InterpolateVec(geo, geo->vMscratch[0], geo->vscratch[1]); VecShift(geo->vscratch[1], 1.0); VecPointwiseMult(geo->veps, geo->vscratch[1], geo->vepspml); if(epsI != NULL){ // imaginary part of passive dielectric VecGetArray(geo->vMscratch[0], &scratch); for(i=ms; i<me; i++){ scratch[i-ms] = epsI[i-ms]; } VecRestoreArray(geo->vMscratch[0], &scratch); InterpolateVec(geo, geo->vMscratch[0], geo->vscratch[1]); VecPointwiseMult(geo->vscratch[1], geo->vscratch[1], geo->vepspml); TimesI(geo, geo->vscratch[1], geo->vscratch[2]); VecAXPY(geo->veps, 1.0, geo->vscratch[2]); } } if(manual_epspml){ char epsManualfile[PETSC_MAX_PATH_LEN]; PetscOptionsGetString(PETSC_NULL,PETSC_NULL,"-epsManualfile", epsManualfile, PETSC_MAX_PATH_LEN, NULL); FILE *fp = fopen(epsManualfile, "r"); ReadVectorC(fp, 2*Nxyzc(geo)+2, geo->veps); // 07/11/15: if manual_epspml, then directly read in the Nxyzcr+2 vector fclose(fp); } TimesI(geo, geo->veps, geo->vIeps); // vIeps for convenience only, make sure to update it later if eps ever changes! geo->D = 0.0; geo->wa = wa; geo->y = y; VecDuplicate(geo->veps, &geo->vf); VecDuplicate(geo->vMscratch[0], &geo->vfM); VecGetArray(geo->vfM, &scratch); for(i=ms; i<me;i++) scratch[i-ms] = fprof[i-ms]; VecRestoreArray(geo->vfM, &scratch); InterpolateVec(geo, geo->vfM, geo->vf); return geo; }
int main(int argc,char **args) { Mat A; /* linear system matrix */ PetscErrorCode ierr; PetscMPIInt rank=0; PetscBool flg; PetscViewer fd; /* viewer */ PetscViewer log; char file[PETSC_MAX_PATH_LEN]; char logfile[PETSC_MAX_PATH_LEN]; char lockfile[PETSC_MAX_PATH_LEN], tmpstr[PETSC_MAX_PATH_LEN], dirname[PETSC_MAX_PATH_LEN], matrix[PETSC_MAX_PATH_LEN]; char hash[20]; PetscLogDouble solveTime,endTime,startTime; PetscInt its; PetscReal norm; KSP ksp; // Linear solver context Vec b,x,u; // RHS, solution, vector for norm calculation PetscScalar one = 1.0; PetscInt m, n, i; FILE *lock; /* if (rank == 0) { printf("Command line arguments:\n"); for (i=0; i < argc; i++) printf("%d: %s\n", i, args[i]); } // Save args int argcount = argc; char **argv = (char**) malloc (argc*sizeof(char*)); for (i=0; i < argc; i++) { argv[i] = (char*) malloc(strlen(args[i]) + 1); strcpy(argv[i],args[i]); } MPI_Comm_rank(MPI_COMM_WORLD,&rank); */ PetscInitialize(&argc,&args,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-hash",hash,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) { strcpy(hash,"nohash"); } ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) { PetscPrintf(PETSC_COMM_WORLD,"Must indicate matrix file with the -f option"); } /* Create lock file */ if (rank == 0) { for (i = strlen(file); i> 0; i--) if (file[i] == '.') break; strncpy(tmpstr, file, i-1); for (i = strlen(tmpstr); i> 0; i--) if (file[i] == '/') break; strncpy(dirname, tmpstr, i); dirname[i] = '\0'; sprintf(lockfile,"%s/../timing/.%s.%s", dirname, basename(tmpstr), hash); sprintf(logfile,"%s/../timing/%s.%s.log", dirname, basename(tmpstr), hash); lock = fopen(lockfile, "w"); fprintf(lock, "%s\n", file); fclose(lock); } /* Read file */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); // Create matrix ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetType(A,MATMPIAIJ); CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); // Load matrix from file ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetSize(A, &m, &n); CHKERRQ(ierr); // Assemble matrix //ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); //ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); // Create RHS vector ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr); ierr = VecSetSizes(b,PETSC_DECIDE,n); CHKERRQ(ierr); ierr = VecSetFromOptions(b);CHKERRQ(ierr); ierr = VecSet(b,one); CHKERRQ(ierr); //ierr = VecLoad(b,fd);CHKERRQ(ierr); // Create vectors x and u ierr = VecDuplicate(b,&x);CHKERRQ(ierr); ierr = VecDuplicate(b,&u);CHKERRQ(ierr); // Create KSP ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); CHKERRQ(ierr); ierr = KSPSetInitialGuessNonzero(ksp,PETSC_FALSE);CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); // Setup KSP ierr = KSPSetUp(ksp);CHKERRQ(ierr); ierr = KSPSetUpOnBlocks(ksp);CHKERRQ(ierr); // Get start time ierr = PetscTime(&startTime);CHKERRQ(ierr); // Get KSP and PC type KSPType kt; ierr = KSPGetType(ksp,&kt); PC pc; ierr = KSPGetPC(ksp,&pc); PCType pt; ierr = PCGetType(pc,&pt); // Print method info ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, logfile, &log); CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(log, "Hash: %s\n", hash); ierr = PetscViewerASCIIPrintf(log, "%s | %s",kt,pt);CHKERRQ(ierr); // Make sure the program doesn't crash // while trying to solve the system PetscPushErrorHandler(PetscIgnoreErrorHandler,NULL); ierr = KSPSolve(ksp,b,x); PetscPopErrorHandler(); // Check if anything went wrong if(ierr == 0 || ierr == -1){ // If no error occurred or stopped by MyKSPMonitor, // compute normal and stuff ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); ierr = MatMult(A,x,u);CHKERRQ(ierr); ierr = VecAXPY(u,-1.0,b);CHKERRQ(ierr); ierr = VecNorm(u,NORM_2,&norm);CHKERRQ(ierr); ierr = PetscTime(&endTime);CHKERRQ(ierr); // Compute solve time solveTime = endTime - startTime; // Check if KSP converged KSPConvergedReason reason; KSPGetConvergedReason(ksp,&reason); // Print convergence code, solve time, preconditioned norm, iterations ierr = PetscViewerASCIIPrintf(log, " | %D | %e | %g | %D\n",reason,solveTime,norm,its);CHKERRQ(ierr); ierr = KSPView(ksp,log); ierr = PCView(pc,log); ierr = PetscLogView(log); } else{ // Disaster happened, bail out if (rank == 0) remove(lockfile); PetscFinalize(); return 0; } // Again, destroy KSP and vector ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); if (rank == 0) remove(lockfile); PetscFinalize(); return 0; }
/*@C MatInitializePackage - This function initializes everything in the Mat package. It is called from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to MatCreate() when using static libraries. Level: developer .keywords: Mat, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode MatInitializePackage(void) { char logList[256]; char *className; PetscBool opt; PetscErrorCode ierr; PetscFunctionBegin; if (MatPackageInitialized) PetscFunctionReturn(0); MatPackageInitialized = PETSC_TRUE; /* Inialize subpackage */ ierr = MatMFFDInitializePackage();CHKERRQ(ierr); /* Register Classes */ ierr = PetscClassIdRegister("Matrix",&MAT_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("Matrix FD Coloring",&MAT_FDCOLORING_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("Matrix MatTranspose Coloring",&MAT_TRANSPOSECOLORING_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("Matrix Partitioning",&MAT_PARTITIONING_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("Matrix Coarsen",&MAT_COARSEN_CLASSID);CHKERRQ(ierr); ierr = PetscClassIdRegister("Matrix Null Space",&MAT_NULLSPACE_CLASSID);CHKERRQ(ierr); /* Register Constructors */ ierr = MatRegisterAll();CHKERRQ(ierr); ierr = MatOrderingRegisterAll();CHKERRQ(ierr); ierr = MatColoringRegisterAll();CHKERRQ(ierr); ierr = MatPartitioningRegisterAll();CHKERRQ(ierr); ierr = MatCoarsenRegisterAll();CHKERRQ(ierr); /* Register Events */ ierr = PetscLogEventRegister("MatMult", MAT_CLASSID,&MAT_Mult);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMults", MAT_CLASSID,&MAT_Mults);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMultConstr", MAT_CLASSID,&MAT_MultConstrained);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMultAdd", MAT_CLASSID,&MAT_MultAdd);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMultTranspose", MAT_CLASSID,&MAT_MultTranspose);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMultTrConstr", MAT_CLASSID,&MAT_MultTransposeConstrained);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMultTrAdd", MAT_CLASSID,&MAT_MultTransposeAdd);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSolve", MAT_CLASSID,&MAT_Solve);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSolves", MAT_CLASSID,&MAT_Solves);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSolveAdd", MAT_CLASSID,&MAT_SolveAdd);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSolveTranspos", MAT_CLASSID,&MAT_SolveTranspose);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSolveTrAdd", MAT_CLASSID,&MAT_SolveTransposeAdd);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSOR", MAT_CLASSID,&MAT_SOR);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatForwardSolve", MAT_CLASSID,&MAT_ForwardSolve);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatBackwardSolve", MAT_CLASSID,&MAT_BackwardSolve);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatLUFactor", MAT_CLASSID,&MAT_LUFactor);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatLUFactorSym", MAT_CLASSID,&MAT_LUFactorSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatLUFactorNum", MAT_CLASSID,&MAT_LUFactorNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCholeskyFctr", MAT_CLASSID,&MAT_CholeskyFactor);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCholFctrSym", MAT_CLASSID,&MAT_CholeskyFactorSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCholFctrNum", MAT_CLASSID,&MAT_CholeskyFactorNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatILUFactor", MAT_CLASSID,&MAT_ILUFactor);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatILUFactorSym", MAT_CLASSID,&MAT_ILUFactorSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatICCFactorSym", MAT_CLASSID,&MAT_ICCFactorSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCopy", MAT_CLASSID,&MAT_Copy);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatConvert", MAT_CLASSID,&MAT_Convert);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatScale", MAT_CLASSID,&MAT_Scale);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatAssemblyBegin", MAT_CLASSID,&MAT_AssemblyBegin);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatAssemblyEnd", MAT_CLASSID,&MAT_AssemblyEnd);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSetValues", MAT_CLASSID,&MAT_SetValues);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetValues", MAT_CLASSID,&MAT_GetValues);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetRow", MAT_CLASSID,&MAT_GetRow);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetRowIJ", MAT_CLASSID,&MAT_GetRowIJ);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetSubMatrice", MAT_CLASSID,&MAT_GetSubMatrices);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetColoring", MAT_CLASSID,&MAT_GetColoring);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetOrdering", MAT_CLASSID,&MAT_GetOrdering);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatIncreaseOvrlp", MAT_CLASSID,&MAT_IncreaseOverlap);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatPartitioning", MAT_PARTITIONING_CLASSID,&MAT_Partitioning);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCoarsen", MAT_COARSEN_CLASSID,&MAT_Coarsen);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatZeroEntries", MAT_CLASSID,&MAT_ZeroEntries);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatLoad", MAT_CLASSID,&MAT_Load);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatView", MAT_CLASSID,&MAT_View);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatAXPY", MAT_CLASSID,&MAT_AXPY);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatFDColorCreate", MAT_FDCOLORING_CLASSID,&MAT_FDColoringCreate);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatFDColorApply", MAT_FDCOLORING_CLASSID,&MAT_FDColoringApply);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatFDColorFunc", MAT_FDCOLORING_CLASSID,&MAT_FDColoringFunction);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatTranspose", MAT_CLASSID,&MAT_Transpose);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatMult", MAT_CLASSID,&MAT_MatMult);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatSolve", MAT_CLASSID,&MAT_MatSolve);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatMultSym", MAT_CLASSID,&MAT_MatMultSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatMultNum", MAT_CLASSID,&MAT_MatMultNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatMatMult", MAT_CLASSID,&MAT_MatMatMult);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatMatMultSym", MAT_CLASSID,&MAT_MatMatMultSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatMatMultNum", MAT_CLASSID,&MAT_MatMatMultNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatPtAP", MAT_CLASSID,&MAT_PtAP);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatPtAPSymbolic", MAT_CLASSID,&MAT_PtAPSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatPtAPNumeric", MAT_CLASSID,&MAT_PtAPNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatRARt", MAT_CLASSID,&MAT_RARt);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatRARtSymbolic", MAT_CLASSID,&MAT_RARtSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatRARtNumeric", MAT_CLASSID,&MAT_RARtNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatTransMult", MAT_CLASSID,&MAT_MatTransposeMult);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatTrnMultSym", MAT_CLASSID,&MAT_MatTransposeMultSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMatTrnMultNum", MAT_CLASSID,&MAT_MatTransposeMultNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatTrnMatMult", MAT_CLASSID,&MAT_TransposeMatMult);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatTrnMatMultSym", MAT_CLASSID,&MAT_TransposeMatMultSymbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatTrnMatMultNum", MAT_CLASSID,&MAT_TransposeMatMultNumeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatTrnColorCreate", MAT_CLASSID,&MAT_TransposeColoringCreate);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetRedundant", MAT_CLASSID,&MAT_GetRedundantMatrix);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetSeqNZStrct", MAT_CLASSID,&MAT_GetSequentialNonzeroStructure);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetMultiProcBlock", MAT_CLASSID,&MAT_GetMultiProcBlock);CHKERRQ(ierr); /* these may be specific to MPIAIJ matrices */ ierr = PetscLogEventRegister("MatMPISumSeqNumeric",MAT_CLASSID,&MAT_Seqstompinum);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMPISumSeqSymbolic",MAT_CLASSID,&MAT_Seqstompisym);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMPISumSeq",MAT_CLASSID,&MAT_Seqstompi);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatMPIConcateSeq",MAT_CLASSID,&MAT_Merge);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetLocalMat",MAT_CLASSID,&MAT_Getlocalmat);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetLocalMatCondensed",MAT_CLASSID,&MAT_Getlocalmatcondensed);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetBrowsOfAcols",MAT_CLASSID,&MAT_GetBrowsOfAcols);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetBrAoCol",MAT_CLASSID,&MAT_GetBrowsOfAocols);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatApplyPAPt_Symbolic",MAT_CLASSID,&MAT_Applypapt_symbolic);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatApplyPAPt_Numeric",MAT_CLASSID,&MAT_Applypapt_numeric);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatApplyPAPt",MAT_CLASSID,&MAT_Applypapt);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetSymTrans",MAT_CLASSID,&MAT_Getsymtranspose);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatGetSymTransR",MAT_CLASSID,&MAT_Getsymtransreduced);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatTranspose_SeqAIJ_FAST",MAT_CLASSID,&MAT_Transpose_SeqAIJ);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCUSPCopyTo",MAT_CLASSID,&MAT_CUSPCopyToGPU);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatCUSPARSECopyTo",MAT_CLASSID,&MAT_CUSPARSECopyToGPU);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSetValBatch",MAT_CLASSID,&MAT_SetValuesBatch);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSetValBatch1",MAT_CLASSID,&MAT_SetValuesBatchI);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSetValBatch2",MAT_CLASSID,&MAT_SetValuesBatchII);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSetValBatch3",MAT_CLASSID,&MAT_SetValuesBatchIII);CHKERRQ(ierr); ierr = PetscLogEventRegister("MatSetValBatch4",MAT_CLASSID,&MAT_SetValuesBatchIV);CHKERRQ(ierr); /* Turn off high traffic events by default */ ierr = PetscLogEventSetActiveAll(MAT_SetValues, PETSC_FALSE);CHKERRQ(ierr); /* Process info exclusions */ ierr = PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscStrstr(logList, "mat", &className);CHKERRQ(ierr); if (className) { ierr = PetscInfoDeactivateClass(MAT_CLASSID);CHKERRQ(ierr); } } /* Process summary exclusions */ ierr = PetscOptionsGetString(NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscStrstr(logList, "mat", &className);CHKERRQ(ierr); if (className) { ierr = PetscLogEventDeactivateClass(MAT_CLASSID);CHKERRQ(ierr); } } ierr = PetscRegisterFinalize(MatFinalizePackage);CHKERRQ(ierr); PetscFunctionReturn(0); }
MAD::MAD(Grid **grid_) { ifc_polygon = NULL; river_polygon = NULL; ascii_grids = NULL; spp_polygon = NULL; PetscReal mx = 1.; PetscReal my = 1.; PetscReal mz = 1.; PetscInt nx, ny, nz; char filename[1024]; PetscBool option_found; strcpy(filename,"mdt.in"); PetscOptionsGetString(PETSC_NULL,"-mdtin",filename,1024,&option_found); FileIO *file = new FileIO(filename); file->getLine(); file->readInt(&nx); file->readInt(&ny); file->readInt(&nz); delete file; PetscReal dx; PetscReal dy; PetscReal dz;// */ PetscReal len_x = 120.; PetscReal len_y = 122; PetscReal len_z = 60.; dx = len_x/(PetscReal)nx; dy = len_y/(PetscReal)ny; dz = len_z/(PetscReal)nz; PetscInt n = nx*ny*nz; PetscPrintf(PETSC_COMM_WORLD,"nx = %d, dx = %f, lenx = %f\n",nx,dx,nx*dx); PetscPrintf(PETSC_COMM_WORLD,"ny = %d, dy = %f, leny = %f\n",ny,dy,ny*dy); PetscPrintf(PETSC_COMM_WORLD,"nz = %d, dz = %f, lenz = %f\n",nz,dz,nz*dz); *grid_ = new Grid(nx,ny,nz); Grid *grid = *grid_; // grid spacing with a bias #if 0 PetscReal sum_x = 0.; PetscReal sum_y = 0.; dx = 0.8470329472543 PetscReal *dx_array = new double[nx]; PetscReal *dy_array = new double[ny]; PetscReal *dz_array = new double[nz]; for (int i=0; i<nx; i++) dx_array[i] = 10.; for (int i=0; i<ny; i++) dy_array[i] = 10.; for (int i=0; i<nz; i++) dz_array[i] = 0.25; for (int i=11; i<19; i++) { dx_array[i] = 10.*pow(1.30242241518419,(double)(10-i)); sum_x += dx_array[i]; } for (int i=19; i<89; i++) { dx_array[i] = 1.; sum_x += dx_array[i]; } for (int i=89; i<97; i++) { dx_array[i] = 10.*pow(1.30242241518419,(double)(i-97)); sum_x += dx_array[i]; } for (int i=97; i<9; i++) { dy_array[110+i] = 10.*pow(1.353088,i+1.); dy_array[9-i] = 10.*pow(1.353088,i+1.); sum_y += dy_array[9-i]; } grid->setGridSpacing(dx_array,dy_array,dz_array); #else grid->setGridSpacing(dx,dy,dz); #endif // grid->setOrigin(593618.9,114565.1,70.); grid->setRotation(34.); // must come before ->setOrigin() grid->setOrigin(594237.2891,115984.7447,70.); // grid->computeCoordinates(); // grid->computeConnectivity(); grid->computeCellMapping(); grid->setUpCells(); grid->computeVertexMapping(); grid->setUpVertices(); grid->mapVerticesToCells(); ifc_polygon = new Polygon(); ifc_polygon->createIFCPolygon(); #if 0 char ascii_filename[1024]; strcpy(ascii_filename,"test.asc"); AsciiGrid **ascii_grid = new AsciiGrid*[2]; ascii_grid[0] = new AsciiGrid(ascii_filename); ascii_grid[0]->setMaterialId(1); ascii_grid[1] = new AsciiGrid("default",2,2,-1.e10,-1.e10,1.e10,-9999., 1.e20,0); #else AsciiGrid::nasciigrids = 6; string *grid_filenames = new string[AsciiGrid::nasciigrids]; #if 0 grid_filenames[0].append("./basalt_300area.asc"); grid_filenames[1].append("./u9_300area.asc"); grid_filenames[2].append("./u8_300area.asc"); grid_filenames[3].append("./u5gravel_300area.asc"); grid_filenames[4].append("./u5silt_300area.asc"); grid_filenames[5].append("./newbath_10mDEM_grid.ascii"); #else grid_filenames[0].append("../basalt_300area.asc"); grid_filenames[1].append("../u9_300area.asc"); grid_filenames[2].append("../u8_300area.asc"); grid_filenames[3].append("../u5gravel_300area.asc"); grid_filenames[4].append("../u5silt_300area.asc"); grid_filenames[5].append("../newbath_10mDEM_grid.ascii"); #endif ascii_grids = new AsciiGrid*[AsciiGrid::nasciigrids]; for (PetscInt i=0; i<AsciiGrid::nasciigrids; i++) { char filename[32]; strcpy(filename,grid_filenames[i].c_str()); ascii_grids[i] = new AsciiGrid(filename); } ascii_grids[0]->setMaterialId(10); ascii_grids[1]->setMaterialId(9); ascii_grids[2]->setMaterialId(8); ascii_grids[3]->setMaterialId(6); ascii_grids[4]->setMaterialId(5); ascii_grids[5]->setMaterialId(1); PetscInt mod = grid->num_cells_ghosted/10; for (PetscInt i=0; i<grid->num_cells_ghosted; i++) { PetscInt material_id = 0; PetscReal x = grid->cells[i].getX(); PetscReal y = grid->cells[i].getY(); PetscReal z = grid->cells[i].getZ(); for (PetscInt ilayer=0; ilayer<AsciiGrid::nasciigrids; ilayer++) { PetscReal zlayer = ascii_grids[ilayer]->computeElevationFromCoordinate(x,y); if (zlayer > ascii_grids[ilayer]->nodata && zlayer >= z) { material_id = ascii_grids[ilayer]->getMaterialId(); break; } } if (material_id == 0) grid->cells[i].setActive(0); grid->cells[i].setMaterialId(material_id); if (river_polygon) { if (!river_polygon->pointInPolygon(x,y)) { grid->cells[i].setActive(0); grid->cells[i].negateMaterialId(); } } if (i%mod == 0) { PetscPrintf(PETSC_COMM_WORLD,"%d of %d cells mapped with materials and activity.\n", i,grid->num_cells_ghosted); } } #endif flagGridCells(grid); #if 0 computeEastBoundary(grid,1); computeWestBoundary(grid,1); computeNorthBoundary(grid,0); computeSouthBoundary(grid,0); computeTopBoundary(grid,0); #endif computeIFCBoundary(grid,ifc_polygon); #if 0 BoundarySet *river = grid->getBoundarySet("East"); BoundarySet *west = grid->getBoundarySet("West"); BoundarySet *north = grid->getBoundarySet("North"); BoundarySet *south = grid->getBoundarySet("South"); BoundarySet *recharge = grid->getBoundarySet("Top"); #endif /* Condition *new_condition = new Condition("river.bc"); river->condition = new_condition; new_condition = new Condition("west.bc"); west->condition = new_condition; new_condition = new Condition("recharge.bc"); recharge->condition = new_condition; new_condition = NULL; */ }
int main(int argc,char **args) { MatType mtype = MATMPIAIJ; /* matrix format */ Mat A,B; /* matrix */ PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscBool flg,viewMats,viewIS,viewVecs; PetscInt ierr,*nlocal,m,n; PetscMPIInt rank,size; MatPartitioning part; IS is,isn; Vec xin, xout; VecScatter scat; PetscInitialize(&argc,&args,(char*)0,help); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL, "-view_mats", &viewMats);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL, "-view_is", &viewIS);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL, "-view_vecs", &viewVecs);CHKERRQ(ierr); /* Determine file from which we read the matrix */ ierr = PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix and vector; then destroy the viewer. */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetType(A,mtype);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&xin);CHKERRQ(ierr); ierr = VecLoad(xin,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); if (viewMats) { if (!rank) printf("Original matrix:\n"); ierr = MatView(A,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); } if (viewVecs) { if (!rank) printf("Original vector:\n"); ierr = VecView(xin,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* Partition the graph of the matrix */ ierr = MatPartitioningCreate(PETSC_COMM_WORLD,&part);CHKERRQ(ierr); ierr = MatPartitioningSetAdjacency(part,A);CHKERRQ(ierr); ierr = MatPartitioningSetFromOptions(part);CHKERRQ(ierr); /* get new processor owner number of each vertex */ ierr = MatPartitioningApply(part,&is);CHKERRQ(ierr); if (viewIS) { if (!rank) printf("IS1 - new processor ownership:\n"); ierr = ISView(is,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* get new global number of each old global number */ ierr = ISPartitioningToNumbering(is,&isn);CHKERRQ(ierr); if (viewIS) { if (!rank) printf("IS2 - new global numbering:\n"); ierr = ISView(isn,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* get number of new vertices for each processor */ ierr = PetscMalloc(size*sizeof(PetscInt),&nlocal);CHKERRQ(ierr); ierr = ISPartitioningCount(is,size,nlocal);CHKERRQ(ierr); ierr = ISDestroy(&is);CHKERRQ(ierr); /* get old global number of each new global number */ ierr = ISInvertPermutation(isn,nlocal[rank],&is);CHKERRQ(ierr); ierr = PetscFree(nlocal);CHKERRQ(ierr); ierr = ISDestroy(&isn);CHKERRQ(ierr); ierr = MatPartitioningDestroy(&part);CHKERRQ(ierr); if (viewIS) { if (!rank) printf("IS3=inv(IS2) - old global number of each new global number:\n"); ierr = ISView(is,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* move the matrix rows to the new processes they have been assigned to by the permutation */ ierr = ISSort(is);CHKERRQ(ierr); ierr = MatGetSubMatrix(A,is,is,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); /* move the vector rows to the new processes they have been assigned to */ ierr = MatGetLocalSize(B,&m,&n);CHKERRQ(ierr); ierr = VecCreateMPI(PETSC_COMM_WORLD,m,PETSC_DECIDE,&xout);CHKERRQ(ierr); ierr = VecScatterCreate(xin,is,xout,NULL,&scat);CHKERRQ(ierr); ierr = VecScatterBegin(scat,xin,xout,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(scat,xin,xout,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterDestroy(&scat);CHKERRQ(ierr); ierr = ISDestroy(&is);CHKERRQ(ierr); if (viewMats) { if (!rank) printf("Partitioned matrix:\n"); ierr = MatView(B,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); } if (viewVecs) { if (!rank) printf("Mapped vector:\n"); ierr = VecView(xout,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } { PetscInt rstart,i,*nzd,*nzo,nzl,nzmax = 0,*ncols,nrow,j; Mat J; const PetscInt *cols; const PetscScalar *vals; PetscScalar *nvals; ierr = MatGetOwnershipRange(B,&rstart,NULL);CHKERRQ(ierr); ierr = PetscMalloc(2*m*sizeof(PetscInt),&nzd);CHKERRQ(ierr); ierr = PetscMemzero(nzd,2*m*sizeof(PetscInt));CHKERRQ(ierr); ierr = PetscMalloc(2*m*sizeof(PetscInt),&nzo);CHKERRQ(ierr); ierr = PetscMemzero(nzo,2*m*sizeof(PetscInt));CHKERRQ(ierr); for (i=0; i<m; i++) { ierr = MatGetRow(B,i+rstart,&nzl,&cols,NULL);CHKERRQ(ierr); for (j=0; j<nzl; j++) { if (cols[j] >= rstart && cols[j] < rstart+n) { nzd[2*i] += 2; nzd[2*i+1] += 2; } else { nzo[2*i] += 2; nzo[2*i+1] += 2; } } nzmax = PetscMax(nzmax,nzd[2*i]+nzo[2*i]); ierr = MatRestoreRow(B,i+rstart,&nzl,&cols,NULL);CHKERRQ(ierr); } ierr = MatCreateAIJ(PETSC_COMM_WORLD,2*m,2*m,PETSC_DECIDE,PETSC_DECIDE,0,nzd,0,nzo,&J);CHKERRQ(ierr); ierr = PetscInfo(0,"Created empty Jacobian matrix\n");CHKERRQ(ierr); ierr = PetscFree(nzd);CHKERRQ(ierr); ierr = PetscFree(nzo);CHKERRQ(ierr); ierr = PetscMalloc2(nzmax,PetscInt,&ncols,nzmax,PetscScalar,&nvals);CHKERRQ(ierr); ierr = PetscMemzero(nvals,nzmax*sizeof(PetscScalar));CHKERRQ(ierr); for (i=0; i<m; i++) { ierr = MatGetRow(B,i+rstart,&nzl,&cols,&vals);CHKERRQ(ierr); for (j=0; j<nzl; j++) { ncols[2*j] = 2*cols[j]; ncols[2*j+1] = 2*cols[j]+1; } nrow = 2*(i+rstart); ierr = MatSetValues(J,1,&nrow,2*nzl,ncols,nvals,INSERT_VALUES);CHKERRQ(ierr); nrow = 2*(i+rstart) + 1; ierr = MatSetValues(J,1,&nrow,2*nzl,ncols,nvals,INSERT_VALUES);CHKERRQ(ierr); ierr = MatRestoreRow(B,i+rstart,&nzl,&cols,&vals);CHKERRQ(ierr); } ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (viewMats) { if (!rank) printf("Jacobian matrix structure:\n"); ierr = MatView(J,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); } ierr = MatDestroy(&J);CHKERRQ(ierr); ierr = PetscFree2(ncols,nvals);CHKERRQ(ierr); } /* Free work space. All PETSc objects should be destroyed when they are no longer needed. */ ierr = MatDestroy(&B);CHKERRQ(ierr); ierr = VecDestroy(&xin);CHKERRQ(ierr); ierr = VecDestroy(&xout);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
int main(int argc,char ** argv) { PetscErrorCode ierr; char pfdata_file[PETSC_MAX_PATH_LEN]="datafiles/case9.m"; PFDATA *pfdata; PetscInt numEdges=0,numVertices=0; int *edges = NULL; PetscInt i; DM networkdm; PetscInt componentkey[4]; UserCtx User; PetscLogStage stage1,stage2; PetscMPIInt size; PetscInt eStart, eEnd, vStart, vEnd,j; PetscInt genj,loadj; Vec X,F; Mat J; SNES snes; PetscInitialize(&argc,&argv,"pfoptions",help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* Create an empty network object */ ierr = DMNetworkCreate(PETSC_COMM_WORLD,&networkdm);CHKERRQ(ierr); /* Register the components in the network */ ierr = DMNetworkRegisterComponent(networkdm,"branchstruct",sizeof(struct _p_EDGEDATA),&componentkey[0]);CHKERRQ(ierr); ierr = DMNetworkRegisterComponent(networkdm,"busstruct",sizeof(struct _p_VERTEXDATA),&componentkey[1]);CHKERRQ(ierr); ierr = DMNetworkRegisterComponent(networkdm,"genstruct",sizeof(struct _p_GEN),&componentkey[2]);CHKERRQ(ierr); ierr = DMNetworkRegisterComponent(networkdm,"loadstruct",sizeof(struct _p_LOAD),&componentkey[3]);CHKERRQ(ierr); ierr = PetscLogStageRegister("Read Data",&stage1);CHKERRQ(ierr); PetscLogStagePush(stage1); /* READ THE DATA */ if (!rank) { /* READ DATA */ /* Only rank 0 reads the data */ ierr = PetscOptionsGetString(NULL,NULL,"-pfdata",pfdata_file,PETSC_MAX_PATH_LEN-1,NULL);CHKERRQ(ierr); ierr = PetscNew(&pfdata);CHKERRQ(ierr); ierr = PFReadMatPowerData(pfdata,pfdata_file);CHKERRQ(ierr); User.Sbase = pfdata->sbase; numEdges = pfdata->nbranch; numVertices = pfdata->nbus; ierr = PetscMalloc(2*numEdges*sizeof(int),&edges);CHKERRQ(ierr); ierr = GetListofEdges(pfdata->nbranch,pfdata->branch,edges);CHKERRQ(ierr); } PetscLogStagePop(); ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr); ierr = PetscLogStageRegister("Create network",&stage2);CHKERRQ(ierr); PetscLogStagePush(stage2); /* Set number of nodes/edges */ ierr = DMNetworkSetSizes(networkdm,numVertices,numEdges,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); /* Add edge connectivity */ ierr = DMNetworkSetEdgeList(networkdm,edges);CHKERRQ(ierr); /* Set up the network layout */ ierr = DMNetworkLayoutSetUp(networkdm);CHKERRQ(ierr); if (!rank) { ierr = PetscFree(edges);CHKERRQ(ierr); } /* Add network components */ genj=0; loadj=0; ierr = DMNetworkGetEdgeRange(networkdm,&eStart,&eEnd);CHKERRQ(ierr); for (i = eStart; i < eEnd; i++) { ierr = DMNetworkAddComponent(networkdm,i,componentkey[0],&pfdata->branch[i-eStart]);CHKERRQ(ierr); } ierr = DMNetworkGetVertexRange(networkdm,&vStart,&vEnd);CHKERRQ(ierr); for (i = vStart; i < vEnd; i++) { ierr = DMNetworkAddComponent(networkdm,i,componentkey[1],&pfdata->bus[i-vStart]);CHKERRQ(ierr); if (pfdata->bus[i-vStart].ngen) { for (j = 0; j < pfdata->bus[i-vStart].ngen; j++) { ierr = DMNetworkAddComponent(networkdm,i,componentkey[2],&pfdata->gen[genj++]);CHKERRQ(ierr); } } if (pfdata->bus[i-vStart].nload) { for (j=0; j < pfdata->bus[i-vStart].nload; j++) { ierr = DMNetworkAddComponent(networkdm,i,componentkey[3],&pfdata->load[loadj++]);CHKERRQ(ierr); } } /* Add number of variables */ ierr = DMNetworkAddNumVariables(networkdm,i,2);CHKERRQ(ierr); } /* Set up DM for use */ ierr = DMSetUp(networkdm);CHKERRQ(ierr); if (!rank) { ierr = PetscFree(pfdata->bus);CHKERRQ(ierr); ierr = PetscFree(pfdata->gen);CHKERRQ(ierr); ierr = PetscFree(pfdata->branch);CHKERRQ(ierr); ierr = PetscFree(pfdata->load);CHKERRQ(ierr); ierr = PetscFree(pfdata);CHKERRQ(ierr); } ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); if (size > 1) { DM distnetworkdm; /* Network partitioning and distribution of data */ ierr = DMNetworkDistribute(networkdm,0,&distnetworkdm);CHKERRQ(ierr); ierr = DMDestroy(&networkdm);CHKERRQ(ierr); networkdm = distnetworkdm; } PetscLogStagePop(); ierr = DMNetworkGetEdgeRange(networkdm,&eStart,&eEnd);CHKERRQ(ierr); ierr = DMNetworkGetVertexRange(networkdm,&vStart,&vEnd);CHKERRQ(ierr); #if 0 PetscInt numComponents; EDGEDATA edge; PetscInt offset,key,kk; DMNetworkComponentGenericDataType *arr; VERTEXDATA bus; GEN gen; LOAD load; for (i = eStart; i < eEnd; i++) { ierr = DMNetworkGetComponentDataArray(networkdm,&arr);CHKERRQ(ierr); ierr = DMNetworkGetComponentTypeOffset(networkdm,i,0,&key,&offset);CHKERRQ(ierr); edge = (EDGEDATA)(arr+offset); ierr = DMNetworkGetNumComponents(networkdm,i,&numComponents);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_SELF,"Rank %d ncomps = %d Line %d ---- %d\n",rank,numComponents,edge->internal_i,edge->internal_j);CHKERRQ(ierr); } for (i = vStart; i < vEnd; i++) { ierr = DMNetworkGetComponentDataArray(networkdm,&arr);CHKERRQ(ierr); ierr = DMNetworkGetNumComponents(networkdm,i,&numComponents);CHKERRQ(ierr); for (kk=0; kk < numComponents; kk++) { ierr = DMNetworkGetComponentTypeOffset(networkdm,i,kk,&key,&offset);CHKERRQ(ierr); if (key == 1) { bus = (VERTEXDATA)(arr+offset); ierr = PetscPrintf(PETSC_COMM_SELF,"Rank %d ncomps = %d Bus %d\n",rank,numComponents,bus->internal_i);CHKERRQ(ierr); } else if (key == 2) { gen = (GEN)(arr+offset); ierr = PetscPrintf(PETSC_COMM_SELF,"Rank %d Gen pg = %f qg = %f\n",rank,gen->pg,gen->qg);CHKERRQ(ierr); } else if (key == 3) { load = (LOAD)(arr+offset); ierr = PetscPrintf(PETSC_COMM_SELF,"Rank %d Load pl = %f ql = %f\n",rank,load->pl,load->ql);CHKERRQ(ierr); } } } #endif /* Broadcast Sbase to all processors */ ierr = MPI_Bcast(&User.Sbase,1,MPIU_SCALAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr); ierr = DMCreateGlobalVector(networkdm,&X);CHKERRQ(ierr); ierr = VecDuplicate(X,&F);CHKERRQ(ierr); ierr = DMCreateMatrix(networkdm,&J);CHKERRQ(ierr); ierr = MatSetOption(J,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); ierr = SetInitialValues(networkdm,X,&User);CHKERRQ(ierr); /* HOOK UP SOLVER */ ierr = SNESCreate(PETSC_COMM_WORLD,&snes);CHKERRQ(ierr); ierr = SNESSetDM(snes,networkdm);CHKERRQ(ierr); ierr = SNESSetFunction(snes,F,FormFunction,&User);CHKERRQ(ierr); ierr = SNESSetJacobian(snes,J,J,FormJacobian,&User);CHKERRQ(ierr); ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); ierr = SNESSolve(snes,NULL,X);CHKERRQ(ierr); ierr = VecDestroy(&X);CHKERRQ(ierr); ierr = VecDestroy(&F);CHKERRQ(ierr); ierr = MatDestroy(&J);CHKERRQ(ierr); ierr = SNESDestroy(&snes);CHKERRQ(ierr); ierr = DMDestroy(&networkdm);CHKERRQ(ierr); PetscFinalize(); return 0; }
PetscErrorCode PetscOptionsCheckInitial_Private(void) { char string[64],mname[PETSC_MAX_PATH_LEN],*f; MPI_Comm comm = PETSC_COMM_WORLD; PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flag; PetscErrorCode ierr; PetscReal si,logthreshold; int i; PetscMPIInt rank; char version[256]; #if defined(PETSC_USE_SERVER) PetscBool flgz; #endif PetscFunctionBegin; ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* Setup the memory management; support for tracing malloc() usage */ ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);CHKERRQ(ierr); logthreshold = 0.0; ierr = PetscOptionsGetReal(PETSC_NULL,"-malloc_log_threshold",&logthreshold,&flg1);CHKERRQ(ierr); if (flg1) flg3 = PETSC_TRUE; #if defined(PETSC_USE_DEBUG) ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc",&flg1,&flg2);CHKERRQ(ierr); if ((!flg2 || flg1) && !petscsetmallocvisited) { if (flg2 || !(PETSC_RUNNING_ON_VALGRIND)) { /* turn off default -malloc if valgrind is being used */ ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr); } } #else ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc",&flg2,PETSC_NULL);CHKERRQ(ierr); if (flg1 || flg2 || flg3) {ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr);} #endif if (flg3) { ierr = PetscMallocSetDumpLogThreshold((PetscLogDouble)logthreshold);CHKERRQ(ierr); } flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_debug",&flg1,PETSC_NULL);CHKERRQ(ierr); if (flg1) { ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr); ierr = PetscMallocDebug(PETSC_TRUE);CHKERRQ(ierr); } flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_test",&flg1,PETSC_NULL);CHKERRQ(ierr); #if defined(PETSC_USE_DEBUG) if (flg1 && !PETSC_RUNNING_ON_VALGRIND) { ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr); ierr = PetscMallocSetDumpLog();CHKERRQ(ierr); ierr = PetscMallocDebug(PETSC_TRUE);CHKERRQ(ierr); } #endif flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_info",&flg1,PETSC_NULL);CHKERRQ(ierr); if (!flg1) { flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-memory_info",&flg1,PETSC_NULL);CHKERRQ(ierr); } if (flg1) { ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr); } /* Set the display variable for graphics */ ierr = PetscSetDisplay();CHKERRQ(ierr); /* Print the PETSc version information */ ierr = PetscOptionsHasName(PETSC_NULL,"-v",&flg1);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-version",&flg2);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg3);CHKERRQ(ierr); if (flg1 || flg2 || flg3){ /* Print "higher-level" package version message */ if (PetscExternalVersionFunction) { ierr = (*PetscExternalVersionFunction)(comm);CHKERRQ(ierr); } ierr = PetscGetVersion(version,256);CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"--------------------------------------------\ ------------------------------\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"%s\n",version);CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"--------------------------------------------\ ------------------------------\n");CHKERRQ(ierr); } /* Print "higher-level" package help message */ if (flg3){ if (PetscExternalHelpFunction) { ierr = (*PetscExternalHelpFunction)(comm);CHKERRQ(ierr); } } /* Setup the error handling */ flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-on_error_abort",&flg1,PETSC_NULL);CHKERRQ(ierr); if (flg1) { ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_ARE_FATAL);CHKERRQ(ierr); ierr = PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr); } flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-on_error_mpiabort",&flg1,PETSC_NULL);CHKERRQ(ierr); if (flg1) { ierr = PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);CHKERRQ(ierr);} flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-mpi_return_on_error",&flg1,PETSC_NULL);CHKERRQ(ierr); if (flg1) { ierr = MPI_Errhandler_set(comm,MPI_ERRORS_RETURN);CHKERRQ(ierr); } flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr); if (!flg1) {ierr = PetscPushSignalHandler(PetscDefaultSignalHandler,(void*)0);CHKERRQ(ierr);} flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-fp_trap",&flg1,PETSC_NULL);CHKERRQ(ierr); if (flg1) {ierr = PetscSetFPTrap(PETSC_FP_TRAP_ON);CHKERRQ(ierr);} /* Setup debugger information */ ierr = PetscSetDefaultDebugger();CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-on_error_attach_debugger",string,64,&flg1);CHKERRQ(ierr); if (flg1) { MPI_Errhandler err_handler; ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr); ierr = MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_DebuggerOnError,&err_handler);CHKERRQ(ierr); ierr = MPI_Errhandler_set(comm,err_handler);CHKERRQ(ierr); ierr = PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);CHKERRQ(ierr); } ierr = PetscOptionsGetString(PETSC_NULL,"-debug_terminal",string,64,&flg1);CHKERRQ(ierr); if (flg1) { ierr = PetscSetDebugTerminal(string);CHKERRQ(ierr); } ierr = PetscOptionsGetString(PETSC_NULL,"-start_in_debugger",string,64,&flg1);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-stop_for_debugger",string,64,&flg2);CHKERRQ(ierr); if (flg1 || flg2) { PetscMPIInt size; PetscInt lsize,*nodes; MPI_Errhandler err_handler; /* we have to make sure that all processors have opened connections to all other processors, otherwise once the debugger has stated it is likely to receive a SIGUSR1 and kill the program. */ ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); if (size > 2) { PetscMPIInt dummy = 0; MPI_Status status; for (i=0; i<size; i++) { if (rank != i) { ierr = MPI_Send(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD);CHKERRQ(ierr); } } for (i=0; i<size; i++) { if (rank != i) { ierr = MPI_Recv(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD,&status);CHKERRQ(ierr); } } } /* check if this processor node should be in debugger */ ierr = PetscMalloc(size*sizeof(PetscInt),&nodes);CHKERRQ(ierr); lsize = size; ierr = PetscOptionsGetIntArray(PETSC_NULL,"-debugger_nodes",nodes,&lsize,&flag);CHKERRQ(ierr); if (flag) { for (i=0; i<lsize; i++) { if (nodes[i] == rank) { flag = PETSC_FALSE; break; } } } if (!flag) { ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr); ierr = PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr); if (flg1) { ierr = PetscAttachDebugger();CHKERRQ(ierr); } else { ierr = PetscStopForDebugger();CHKERRQ(ierr); } ierr = MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);CHKERRQ(ierr); ierr = MPI_Errhandler_set(comm,err_handler);CHKERRQ(ierr); } ierr = PetscFree(nodes);CHKERRQ(ierr); } ierr = PetscOptionsGetString(PETSC_NULL,"-on_error_emacs",emacsmachinename,128,&flg1);CHKERRQ(ierr); if (flg1 && !rank) {ierr = PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr);} #if defined(PETSC_USE_SERVER) ierr = PetscOptionsHasName(PETSC_NULL,"-server", &flgz);CHKERRQ(ierr); if (flgz){ PetscInt port = PETSC_DECIDE; ierr = PetscOptionsGetInt(PETSC_NULL,"-server",&port,PETSC_NULL);CHKERRQ(ierr); ierr = PetscWebServe(PETSC_COMM_WORLD,(int)port);CHKERRQ(ierr); } #endif /* Setup profiling and logging */ #if defined (PETSC_USE_INFO) { char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0; ierr = PetscOptionsGetString(PETSC_NULL,"-info",logname,250,&flg1);CHKERRQ(ierr); if (flg1 && logname[0]) { ierr = PetscInfoAllow(PETSC_TRUE,logname);CHKERRQ(ierr); } else if (flg1) { ierr = PetscInfoAllow(PETSC_TRUE,PETSC_NULL);CHKERRQ(ierr); } } #endif #if defined(PETSC_USE_LOG) mname[0] = 0; ierr = PetscOptionsGetString(PETSC_NULL,"-history",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); if (flg1) { if (mname[0]) { ierr = PetscOpenHistoryFile(mname,&petsc_history);CHKERRQ(ierr); } else { ierr = PetscOpenHistoryFile(0,&petsc_history);CHKERRQ(ierr); } } #if defined(PETSC_HAVE_MPE) flg1 = PETSC_FALSE; ierr = PetscOptionsHasName(PETSC_NULL,"-log_mpe",&flg1);CHKERRQ(ierr); if (flg1) PetscLogMPEBegin(); #endif flg1 = PETSC_FALSE; flg2 = PETSC_FALSE; flg3 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-log_all",&flg1,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(PETSC_NULL,"-log",&flg2,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-log_summary_python",&flg4);CHKERRQ(ierr); if (flg1) { ierr = PetscLogAllBegin();CHKERRQ(ierr); } else if (flg2 || flg3 || flg4) { ierr = PetscLogBegin();CHKERRQ(ierr);} ierr = PetscOptionsGetString(PETSC_NULL,"-log_trace",mname,250,&flg1);CHKERRQ(ierr); if (flg1) { char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN]; FILE *file; if (mname[0]) { sprintf(name,"%s.%d",mname,rank); ierr = PetscFixFilename(name,fname);CHKERRQ(ierr); file = fopen(fname,"w"); if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname); } else { file = PETSC_STDOUT; } ierr = PetscLogTraceBegin(file);CHKERRQ(ierr); } #endif /* Setup building of stack frames for all function calls */ ierr = PetscOptionsGetBool(PETSC_NULL,"-options_gui",&PetscOptionsPublish,PETSC_NULL);CHKERRQ(ierr); #if defined(PETSC_HAVE_CUDA) ierr = PetscOptionsHasName(PETSC_NULL,"-cuda_show_devices",&flg1);CHKERRQ(ierr); if (flg1) { struct cudaDeviceProp prop; int devCount; int device; ierr = cudaGetDeviceCount(&devCount);CHKERRQ(ierr); for (device = 0; device < devCount; ++device) { ierr = cudaGetDeviceProperties(&prop, device);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "CUDA device %d: %s\n", device, prop.name);CHKERRQ(ierr); } } { int size; ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); if (size>1) { int devCount, device, rank; ierr = cudaGetDeviceCount(&devCount);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); device = rank % devCount; ierr = cudaSetDevice(device);CHKERRQ(ierr); } else { int device; /* the code below works for serial GPU simulations */ ierr = PetscOptionsGetInt(PETSC_NULL,"-cuda_set_device", &device, &flg1);CHKERRQ(ierr); if (flg1) { ierr = cudaSetDevice(device);CHKERRQ(ierr); } } } #endif /* Print basic help message */ ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg1);CHKERRQ(ierr); if (flg1) { ierr = (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -help: prints help method for each option\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n ");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," only when run in the debugger\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," start the debugger in new xterm\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," unless noxterm is given\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," start all processes in the debugger\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," emacs jumps to error file\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," waits the delay for you to attach\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," note on IBM RS6000 this slows run greatly\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -malloc_log: keeps log of all memory allocations\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -server <port>: Run PETSc webserver (default port is 8080) see PetscWebServe()\n");CHKERRQ(ierr); #if defined(PETSC_USE_LOG) ierr = (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -log[_summary _summary_python]: logging objects and events\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");CHKERRQ(ierr); #if defined(PETSC_HAVE_MPE) ierr = (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");CHKERRQ(ierr); #endif ierr = (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");CHKERRQ(ierr); #endif ierr = (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");CHKERRQ(ierr); ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr); } ierr = PetscOptionsGetReal(PETSC_NULL,"-petsc_sleep",&si,&flg1);CHKERRQ(ierr); if (flg1) { ierr = PetscSleep(si);CHKERRQ(ierr); } ierr = PetscOptionsGetString(PETSC_NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); ierr = PetscStrstr(mname,"null",&f);CHKERRQ(ierr); if (f) { ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr); } #if defined(PETSC_HAVE_CUSP) ierr = PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);CHKERRQ(ierr); if (flg3) flg1 = PETSC_TRUE; else flg1 = PETSC_FALSE; ierr = PetscOptionsGetBool(PETSC_NULL,"-cusp_synchronize",&flg1,PETSC_NULL);CHKERRQ(ierr); if (flg1) PetscCUSPSynchronize = PETSC_TRUE; #endif PetscFunctionReturn(0); }
int main( int argc, char **argv ) { Mat M, C, K; /* problem matrices */ QEP qep; /* quadratic eigenproblem solver context */ const QEPType type; PetscReal error, tol, re, im; PetscScalar kr, ki; PetscErrorCode ierr; PetscInt nev, maxit, i, its, nconv; char filename[256]; PetscViewer viewer; PetscBool flg; SlepcInitialize(&argc,&argv,(char*)0,help); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Load the matrices that define the quadratic eigenproblem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic eigenproblem stored in file.\n\n");CHKERRQ(ierr); #if defined(PETSC_USE_COMPLEX) ierr = PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrices from binary files...\n");CHKERRQ(ierr); #else ierr = PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrices from binary files...\n");CHKERRQ(ierr); #endif ierr = PetscOptionsGetString(PETSC_NULL,"-M",filename,256,&flg);CHKERRQ(ierr); if (!flg) { SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix M with the -M option."); } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&M);CHKERRQ(ierr); ierr = MatSetFromOptions(M);CHKERRQ(ierr); ierr = MatLoad(M,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-C",filename,256,&flg);CHKERRQ(ierr); if (!flg) { SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix C with the -C option."); } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&C);CHKERRQ(ierr); ierr = MatSetFromOptions(C);CHKERRQ(ierr); ierr = MatLoad(C,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-K",filename,256,&flg);CHKERRQ(ierr); if (!flg) { SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix K with the -K option."); } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&K);CHKERRQ(ierr); ierr = MatSetFromOptions(K);CHKERRQ(ierr); ierr = MatLoad(K,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create the eigensolver and set various options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Create eigensolver context */ ierr = QEPCreate(PETSC_COMM_WORLD,&qep);CHKERRQ(ierr); /* Set matrices */ ierr = QEPSetOperators(qep,M,C,K);CHKERRQ(ierr); /* Set solver parameters at runtime */ ierr = QEPSetFromOptions(qep);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Solve the eigensystem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = QEPSolve(qep);CHKERRQ(ierr); ierr = QEPGetIterationNumber(qep, &its);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);CHKERRQ(ierr); /* Optional: Get some information from the solver and display it */ ierr = QEPGetType(qep,&type);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr); ierr = QEPGetDimensions(qep,&nev,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);CHKERRQ(ierr); ierr = QEPGetTolerances(qep,&tol,&maxit);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Display solution and clean up - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Get number of converged approximate eigenpairs */ ierr = QEPGetConverged(qep,&nconv);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate eigenpairs: %d\n\n",nconv); CHKERRQ(ierr); if (nconv>0) { /* Display eigenvalues and relative errors */ ierr = PetscPrintf(PETSC_COMM_WORLD, " k ||(k^2M+Ck+K)x||/||kx||\n" " ----------------- -------------------------\n" );CHKERRQ(ierr); for( i=0; i<nconv; i++ ) { /* Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and ki (imaginary part) */ ierr = QEPGetEigenpair(qep,i,&kr,&ki,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* Compute the relative error associated to each eigenpair */ ierr = QEPComputeRelativeError(qep,i,&error);CHKERRQ(ierr); #ifdef PETSC_USE_COMPLEX re = PetscRealPart(kr); im = PetscImaginaryPart(kr); #else re = kr; im = ki; #endif if (im!=0.0) { ierr = PetscPrintf(PETSC_COMM_WORLD," %9f%+9f j %12g\n",re,im,error);CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD," %12f %12g\n",re,error);CHKERRQ(ierr); } } ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr); } /* Free work space */ ierr = QEPDestroy(qep);CHKERRQ(ierr); ierr = MatDestroy(M);CHKERRQ(ierr); ierr = MatDestroy(C);CHKERRQ(ierr); ierr = MatDestroy(K);CHKERRQ(ierr); ierr = SlepcFinalize();CHKERRQ(ierr); return 0; }
PetscErrorCode PetscViewerMathematicaSetFromOptions(PetscViewer v) { PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data; char linkname[256]; char modename[256]; char hostname[256]; char type[256]; PetscInt numPorts; PetscInt *ports; PetscInt numHosts; int h; char **hosts; PetscMPIInt size, rank; PetscBool opt; PetscErrorCode ierr; PetscFunctionBegin; ierr = MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank);CHKERRQ(ierr); /* Get link name */ ierr = PetscOptionsGetString("viewer_", "-math_linkname", linkname, 256, &opt);CHKERRQ(ierr); if (opt) { ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr); } /* Get link port */ numPorts = size; ierr = PetscMalloc1(size, &ports);CHKERRQ(ierr); ierr = PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt);CHKERRQ(ierr); if (opt) { if (numPorts > rank) snprintf(linkname, 255, "%6d", ports[rank]); else snprintf(linkname, 255, "%6d", ports[0]); ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr); } ierr = PetscFree(ports);CHKERRQ(ierr); /* Get link host */ numHosts = size; ierr = PetscMalloc1(size, &hosts);CHKERRQ(ierr); ierr = PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt);CHKERRQ(ierr); if (opt) { if (numHosts > rank) { ierr = PetscStrncpy(hostname, hosts[rank], 255);CHKERRQ(ierr); } else { ierr = PetscStrncpy(hostname, hosts[0], 255);CHKERRQ(ierr); } ierr = PetscViewerMathematicaSetLinkHost(v, hostname);CHKERRQ(ierr); } for (h = 0; h < numHosts; h++) { ierr = PetscFree(hosts[h]);CHKERRQ(ierr); } ierr = PetscFree(hosts);CHKERRQ(ierr); /* Get link mode */ ierr = PetscOptionsGetString("viewer_", "-math_linkmode", modename, 256, &opt);CHKERRQ(ierr); if (opt) { LinkMode mode; ierr = PetscViewerMathematicaParseLinkMode(modename, &mode);CHKERRQ(ierr); ierr = PetscViewerMathematicaSetLinkMode(v, mode);CHKERRQ(ierr); } /* Get graphics type */ ierr = PetscOptionsGetString("viewer_", "-math_graphics", type, 256, &opt);CHKERRQ(ierr); if (opt) { PetscBool isMotif, isPS, isPSFile; ierr = PetscStrcasecmp(type, "Motif", &isMotif);CHKERRQ(ierr); ierr = PetscStrcasecmp(type, "PS", &isPS);CHKERRQ(ierr); ierr = PetscStrcasecmp(type, "PSFile", &isPSFile);CHKERRQ(ierr); if (isMotif) vmath->graphicsType = GRAPHICS_MOTIF; else if (isPS) vmath->graphicsType = GRAPHICS_PS_STDOUT; else if (isPSFile) vmath->graphicsType = GRAPHICS_PS_FILE; } /* Get plot type */ ierr = PetscOptionsGetString("viewer_", "-math_type", type, 256, &opt);CHKERRQ(ierr); if (opt) { PetscBool isTri, isVecTri, isVec, isSurface; ierr = PetscStrcasecmp(type, "Triangulation", &isTri);CHKERRQ(ierr); ierr = PetscStrcasecmp(type, "VectorTriangulation", &isVecTri);CHKERRQ(ierr); ierr = PetscStrcasecmp(type, "Vector", &isVec);CHKERRQ(ierr); ierr = PetscStrcasecmp(type, "Surface", &isSurface);CHKERRQ(ierr); if (isTri) vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT; else if (isVecTri) vmath->plotType = MATHEMATICA_VECTOR_TRIANGULATION_PLOT; else if (isVec) vmath->plotType = MATHEMATICA_VECTOR_PLOT; else if (isSurface) vmath->plotType = MATHEMATICA_SURFACE_PLOT; } PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat A,B,MA; PetscViewer fd; char file[PETSC_MAX_PATH_LEN]; PetscInt m,n,M,N,dof=1; PetscMPIInt rank,size; PetscErrorCode ierr; PetscTruth flg; PetscInitialize(&argc,&args,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); #if defined(PETSC_USE_COMPLEX) SETERRQ(1,"This example does not work with complex numbers"); #else /* Load aij matrix A */ ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_ERR_USER,"Must indicate binary file with the -f option"); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatLoad(fd,MATAIJ,&A);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); /* Get dof, then create maij matrix MA */ ierr = PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);CHKERRQ(ierr); ierr = MatCreateMAIJ(A,dof,&MA);CHKERRQ(ierr); ierr = MatGetLocalSize(MA,&m,&n);CHKERRQ(ierr); ierr = MatGetSize(MA,&M,&N);CHKERRQ(ierr); if (size == 1){ ierr = MatConvert(MA,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr); } else { ierr = MatConvert(MA,MATMPIAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr); } /* Test MatMult() */ ierr = MatMultEqual(MA,B,10,&flg);CHKERRQ(ierr); if (!flg){ SETERRQ(PETSC_ERR_CONV_FAILED,"Error: MatMul() for MAIJ matrix"); } /* Test MatMultAdd() */ ierr = MatMultAddEqual(MA,B,10,&flg);CHKERRQ(ierr); if (!flg){ SETERRQ(PETSC_ERR_CONV_FAILED,"Error: MatMulAdd() for MAIJ matrix"); } /* Test MatMultTranspose() */ ierr = MatMultTransposeEqual(MA,B,10,&flg);CHKERRQ(ierr); if (!flg){ SETERRQ(PETSC_ERR_CONV_FAILED,"Error: MatMulAdd() for MAIJ matrix"); } /* Test MatMultTransposeAdd() */ ierr = MatMultTransposeAddEqual(MA,B,10,&flg);CHKERRQ(ierr); if (!flg){ SETERRQ(PETSC_ERR_CONV_FAILED,"Error: MatMulTransposeAdd() for MAIJ matrix"); } ierr = MatDestroy(MA);CHKERRQ(ierr); ierr = MatDestroy(A);CHKERRQ(ierr); ierr = MatDestroy(B);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); #endif return 0; }
int main(int argc,char **args) { Mat U,V; /* matrix */ PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscErrorCode ierr; PetscBool flg; Vec x,y,work1,work2; PetscInt i,N,n,M,m; PetscScalar *xx; PetscInitialize(&argc,&args,(char *)0,help); /* Determine file from which we read the matrix */ ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix; then destroy the viewer. Note both U and V are stored as tall skinny matrices */ ierr = MatCreate(PETSC_COMM_WORLD,&U);CHKERRQ(ierr); ierr = MatSetType(U,MATMPIDENSE);CHKERRQ(ierr); ierr = MatLoad(U,fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&V);CHKERRQ(ierr); ierr = MatSetType(V,MATMPIDENSE);CHKERRQ(ierr); ierr = MatLoad(V,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = MatGetLocalSize(U,&N,&n);CHKERRQ(ierr); ierr = MatGetLocalSize(V,&M,&m);CHKERRQ(ierr); if (N != M) SETERRQ2(PETSC_COMM_SELF,1,"U and V matrices must have same number of local rows %D %D",N,M); if (n != m) SETERRQ2(PETSC_COMM_SELF,1,"U and V matrices must have same number of local columns %D %D",n,m); ierr = VecCreateMPI(PETSC_COMM_WORLD,N,PETSC_DETERMINE,&x);CHKERRQ(ierr); ierr = VecDuplicate(x,&y);CHKERRQ(ierr); ierr = MatGetSize(U,0,&n);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&work1);CHKERRQ(ierr); ierr = VecDuplicate(work1,&work2);CHKERRQ(ierr); /* put some initial values into x for testing */ ierr = VecGetArray(x,&xx);CHKERRQ(ierr); for (i=0; i<N; i++) xx[i] = i; ierr = VecRestoreArray(x,&xx);CHKERRQ(ierr); ierr = LowRankUpdate(U,V,x,y,work1,work2,n);CHKERRQ(ierr); ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecView(y,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); /* Free work space. All PETSc objects should be destroyed when they are no longer needed. */ ierr = MatDestroy(&U);CHKERRQ(ierr); ierr = MatDestroy(&V);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = VecDestroy(&work1);CHKERRQ(ierr); ierr = VecDestroy(&work2);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
-ov <overlap> : >=0 amount of overlap between domains\n\n"; #include <petscmat.h> int main(int argc,char **args) { PetscErrorCode ierr; PetscInt nd = 2,ov=1,i,start,m,n,end,lsize; PetscMPIInt rank; PetscBool flg; Mat A,B; char file[PETSC_MAX_PATH_LEN]; PetscViewer fd; IS *is1,*is2; PetscRandom r; PetscScalar rand; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; #if defined(PETSC_USE_COMPLEX) SETERRQ(PETSC_COMM_WORLD,1,"This example does not work with complex numbers"); #else ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must use -f filename to indicate a file containing a PETSc binary matrix"); ierr = PetscOptionsGetInt(NULL,NULL,"-nd",&nd,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-ov",&ov,NULL);CHKERRQ(ierr); /* Read matrix and RHS */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); /* Read the matrix again as a sequential matrix */ ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); ierr = MatLoad(B,fd);CHKERRQ(ierr); ierr = MatSetFromOptions(B);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); /* Create the IS corresponding to subdomains */ ierr = PetscMalloc1(nd,&is1);CHKERRQ(ierr); ierr = PetscMalloc1(nd,&is2);CHKERRQ(ierr); /* Create the random Index Sets */ ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr); ierr = PetscRandomCreate(PETSC_COMM_SELF,&r);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(r);CHKERRQ(ierr); for (i=0; i<nd; i++) { ierr = PetscRandomGetValue(r,&rand);CHKERRQ(ierr); start = (PetscInt)(rand*m); ierr = PetscRandomGetValue(r,&rand);CHKERRQ(ierr); end = (PetscInt)(rand*m); lsize = end - start; if (start > end) { start = end; lsize = -lsize;} ierr = ISCreateStride(PETSC_COMM_SELF,lsize,start,1,is1+i);CHKERRQ(ierr); ierr = ISCreateStride(PETSC_COMM_SELF,lsize,start,1,is2+i);CHKERRQ(ierr); } ierr = MatIncreaseOverlap(A,nd,is1,ov);CHKERRQ(ierr); ierr = MatIncreaseOverlap(B,nd,is2,ov);CHKERRQ(ierr); /* Now see if the serial and parallel case have the same answers */ for (i=0; i<nd; ++i) { ierr = ISEqual(is1[i],is2[i],&flg);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_SELF,"proc:[%d], i=%D, flg =%d\n",rank,i,(int)flg);CHKERRQ(ierr); } /* Free allocated memory */ for (i=0; i<nd; ++i) { ierr = ISDestroy(&is1[i]);CHKERRQ(ierr); ierr = ISDestroy(&is2[i]);CHKERRQ(ierr); } ierr = PetscFree(is1);CHKERRQ(ierr); ierr = PetscFree(is2);CHKERRQ(ierr); ierr = PetscRandomDestroy(&r);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&B);CHKERRQ(ierr); #endif ierr = PetscFinalize(); return ierr; }
int main(int argc, char **args) { PetscErrorCode ierr; ierr = SlepcInitialize(&argc, &args, (char*)0, help); CHKERRQ(ierr); MPI_Comm comm = PETSC_COMM_SELF; PetscBool find; PetscViewer v = PETSC_VIEWER_STDOUT_SELF; PetscPrintf(comm, "\n"); PetscPrintf(comm, ">>>> fit_oce1 program >>>>\n"); PetscPrintf(comm, "Fit L=0 radial function in OCE1\n"); OCE1 oce; Pot pot; KSP ksp; Vec c; char path_out[100]; PetscViewerFormat format; PetscBool set; // -- create -- PrintTimeStamp(comm, "Init", NULL); ierr = OCE1Create(comm, &oce); CHKERRQ(ierr); ierr = PotCreate(comm, &pot); CHKERRQ(ierr); ierr = KSPCreate(comm, &ksp); CHKERRQ(ierr); // -- read options -- PrintTimeStamp(comm, "Set", NULL); PetscOptionsBegin(comm, "", "fit_oce1.c options", "none"); ierr = OCE1SetFromOptions(oce); CHKERRQ(ierr); ierr = PotSetFromOptions2(pot, "v_", &find); CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL, NULL, "-out", path_out, 100, &set); CHKERRQ(ierr); CHKERRQ(ierr); ierr = PetscOptionsEnd(); CHKERRQ(ierr); // -- input error -- if(pot == NULL) { SETERRQ(comm, 1, "-v_pot option is necessary"); } // -- print in -- PrintTimeStamp(comm, "PrintIn", NULL); ierr = PetscPrintf(comm, "OCE1: "); CHKERRQ(ierr); ierr = OCE1View(oce, v); CHKERRQ(ierr); ierr = PetscPrintf(comm, "POT: "); CHKERRQ(ierr); ierr = PFView(pot, v); CHKERRQ(ierr); ierr = PetscPrintf(comm, "out: %s\n", path_out); // -- calculation -- PrintTimeStamp(comm, "Calc", NULL); ierr = OCE1CreateVec(oce, &c); CHKERRQ(ierr); ierr = OCE1Fit(oce, pot, 0, ksp, c); CHKERRQ(ierr); // -- write -- PetscViewer v_out; ierr = PetscViewerBinaryOpen(comm, path_out, FILE_MODE_WRITE, &v_out); CHKERRQ(ierr); ierr = VecView(c, v_out); CHKERRQ(ierr); PetscViewerDestroy(&v_out); PetscPrintf(comm, "<<<< fit_oce1 program <<<<\n\n"); // -- finalize -- OCE1Destroy(&oce); PFDestroy(&pot); KSPDestroy(&ksp); VecDestroy(&c); SlepcFinalize(); return 0; }
int main(int argc,char **args) { Mat A,C,Bdense,Cdense; PetscErrorCode ierr; PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscBool flg,viewmats=PETSC_FALSE; PetscMPIInt rank,size; PetscReal fill=1.0; PetscInt m,n,i,j,BN=10,rstart,rend,*rows,*cols; PetscScalar *Barray,*Carray,rval,*array; Vec x,y; PetscRandom rand; PetscInitialize(&argc,&args,(char*)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* Determine file from which we read the matrix A */ ierr = PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option"); /* Load matrix A */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); /* Print (for testing only) */ ierr = PetscOptionsHasName(NULL, "-view_mats", &viewmats);CHKERRQ(ierr); if (viewmats) { if (!rank) printf("A_aij:\n"); ierr = MatView(A,0);CHKERRQ(ierr); } /* Test MatTransposeMatMult_aij_aij() */ ierr = MatTransposeMatMult(A,A,MAT_INITIAL_MATRIX,fill,&C);CHKERRQ(ierr); if (viewmats) { if (!rank) printf("\nC = A_aij^T * A_aij:\n"); ierr = MatView(C,0);CHKERRQ(ierr); } ierr = MatDestroy(&C);CHKERRQ(ierr); ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); /* create a dense matrix Bdense */ ierr = MatCreate(PETSC_COMM_WORLD,&Bdense);CHKERRQ(ierr); ierr = MatSetSizes(Bdense,m,PETSC_DECIDE,PETSC_DECIDE,BN);CHKERRQ(ierr); ierr = MatSetType(Bdense,MATDENSE);CHKERRQ(ierr); ierr = MatSetFromOptions(Bdense);CHKERRQ(ierr); ierr = MatSetUp(Bdense);CHKERRQ(ierr); ierr = MatGetOwnershipRange(Bdense,&rstart,&rend);CHKERRQ(ierr); //printf("[%d] rstart/end %d %d; local size %d %d\n",rank,rstart,rend,m,n); ierr = PetscMalloc3(m,PetscInt,&rows,BN,PetscInt,&cols,m*BN,PetscScalar,&array);CHKERRQ(ierr); for (i=0; i<m; i++) rows[i] = rstart + i; ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rand);CHKERRQ(ierr); ierr = PetscRandomSetFromOptions(rand);CHKERRQ(ierr); for (j=0; j<BN; j++) { cols[j] = j; for (i=0; i<m; i++) { ierr = PetscRandomGetValue(rand,&rval);CHKERRQ(ierr); array[m*j+i] = rval; } } ierr = MatSetValues(Bdense,m,rows,BN,cols,array,INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(Bdense,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(Bdense,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr); ierr = PetscFree3(rows,cols,array);CHKERRQ(ierr); if (viewmats) { if (!rank) printf("\nBdense:\n"); ierr = MatView(Bdense,0);CHKERRQ(ierr); } /* Test MatTransposeMatMult_aij_dense() */ ierr = MatTransposeMatMult(A,Bdense,MAT_INITIAL_MATRIX,fill,&C);CHKERRQ(ierr); ierr = MatTransposeMatMult(A,Bdense,MAT_REUSE_MATRIX,fill,&C);CHKERRQ(ierr); if (viewmats) { if (!rank) printf("\nC=A^T*Bdense:\n"); ierr = MatView(C,0);CHKERRQ(ierr); } /* Check accuracy */ ierr = MatCreate(PETSC_COMM_WORLD,&Cdense);CHKERRQ(ierr); ierr = MatSetSizes(Cdense,n,PETSC_DECIDE,PETSC_DECIDE,BN);CHKERRQ(ierr); ierr = MatSetType(Cdense,MATDENSE);CHKERRQ(ierr); ierr = MatSetFromOptions(Cdense);CHKERRQ(ierr); ierr = MatSetUp(Cdense);CHKERRQ(ierr); ierr = MatAssemblyBegin(Cdense,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(Cdense,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); if (size == 1) { ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&x);CHKERRQ(ierr); ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,1,n,NULL,&y);CHKERRQ(ierr); } else { ierr = VecCreateMPIWithArray(PETSC_COMM_WORLD,1,m,PETSC_DECIDE,NULL,&x);CHKERRQ(ierr); ierr = VecCreateMPIWithArray(PETSC_COMM_WORLD,1,n,PETSC_DECIDE,NULL,&y);CHKERRQ(ierr); } /* Cdense[:,j] = A^T * Bdense[:,j] */ ierr = MatDenseGetArray(Bdense,&Barray);CHKERRQ(ierr); ierr = MatDenseGetArray(Cdense,&Carray);CHKERRQ(ierr); for (j=0; j<BN; j++) { ierr = VecPlaceArray(x,Barray);CHKERRQ(ierr); ierr = VecPlaceArray(y,Carray);CHKERRQ(ierr); ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); ierr = VecResetArray(x);CHKERRQ(ierr); ierr = VecResetArray(y);CHKERRQ(ierr); Barray += m; Carray += n; } ierr = MatDenseRestoreArray(Bdense,&Barray);CHKERRQ(ierr); ierr = MatDenseRestoreArray(Cdense,&Carray);CHKERRQ(ierr); if (viewmats) { if (!rank) printf("\nCdense:\n"); ierr = MatView(Cdense,0);CHKERRQ(ierr); } ierr = MatEqual(C,Cdense,&flg);CHKERRQ(ierr); if (!flg) { if (!rank) printf(" C != Cdense\n"); } /* Free data structures */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = MatDestroy(&C);CHKERRQ(ierr); ierr = MatDestroy(&Bdense);CHKERRQ(ierr); ierr = MatDestroy(&Cdense);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }