Array<Vector<double> > vecMaker(int nVecs, int n, int nProc, int rank, const VectorType<double>& vecType) { /* This VS will go out of scope when the function is exited, but * its vectors will remember it */ VectorSpace<double> space = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n); Rand::setLocalSeed(space.comm(), 314159); Array<Vector<double> > rtn(nVecs); for (int i=0; i<rtn.size(); i++) { rtn[i] = space.createMember(); rtn[i].randomize(); } return rtn; }
Array<Vector<double> > vecMaker(int nVecs, int nProc, int rank, const VectorType<double>& vecType) { int n1 = 3; int n2 = 4; int n3 = 2; int n4 = 5; int n5 = 6; int n6 = 4; /* This VS will go out of scope when the function is exited, but * its vectors will remember it */ VectorSpace<double> vs1 = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n1); VectorSpace<double> vs2 = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n2); VectorSpace<double> vs3 = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n3); VectorSpace<double> vs4 = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n4); VectorSpace<double> vs5 = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n5); VectorSpace<double> vs6 = vecType.createEvenlyPartitionedSpace(MPIComm::world(), n6); VectorSpace<double> vs = blockSpace(vs1, blockSpace(vs2, blockSpace(vs3, vs4)), blockSpace(vs5, vs6)); Out::root() << "space = " << vs << endl; Rand::setLocalSeed(vs.comm(), 314159); Array<Vector<double> > rtn(nVecs); for (int i=0; i<rtn.size(); i++) { rtn[i] = vs.createMember(); rtn[i].randomize(); } return rtn; }
int main(int argc, char *argv[]) { int stat = 0; try { GlobalMPISession session(&argc, &argv); /* create a distributed vector space for the multivector's vectors */ VectorType<double> rowType = new EpetraVectorType(); int nLocalRows = 2; VectorSpace<double> space = rowType.createEvenlyPartitionedSpace(MPIComm::world(), nLocalRows); /* create a replicated vector space for the small space of columns */ int nVecs = 3; VectorType<double> colType = new SerialVectorType(); VectorSpace<double> replSpace = colType.createEvenlyPartitionedSpace(MPIComm::world(), nVecs); /* create some random vectors */ Teuchos::Array<Vector<double> > vecs(nVecs); for (int i=0; i<nVecs; i++) { vecs[i] = space.createMember(); vecs[i].randomize(); } /* Test multiplication by a multivector operator. We will compute * y1 by directly summing columns, and y2 by applying the operator */ LinearOperator<double> A = multiVectorOperator<double>(vecs, replSpace); Vector<double> y1 = space.createMember(); Vector<double> y2 = space.createMember(); y1.zero(); y2.zero(); /* Sum columns, putting the weights into x */ Vector<double> x = replSpace.createMember(); Out::os() << "A=" << A << std::endl; for (int j=0; j<replSpace.numLocalElements(); j++) { x[j] = 2.0*(drand48()-0.5); y1 = y1 + x[j] * vecs[j]; Out::os() << "x[" << j << "]=" << x[j] << std::endl; Out::os() << "vecs[j]=" << vecs[j] << std::endl; } Out::os() << "y1=" << std::endl << y1 << std::endl; /* Apply the operator to the vector of weights */ y2 = A * x; Out::os() << "y2=A*x=" << std::endl << y2 << std::endl; Vector<double> y12 = y1-y2; Vector<double> y21 = y2-y1; Out::os() << "y1-y2=" << std::endl << y12 << std::endl; Out::os() << "y2-y1=" << std::endl << y21 << std::endl; double errA = (y1-y2).norm2(); Out::root() << "error in A*x = " << errA << std::endl; /* Now test z = A^T * y */ LinearOperator<double> At = A.transpose(); Vector<double> z1 = replSpace.createMember(); z1.zero(); Vector<double> z2 = replSpace.createMember(); z2.zero(); Vector<double> y = y1.copy(); /* compute by vectorwise multiplication */ for (int j=0; j<replSpace.numLocalElements(); j++) { z1[j] = vecs[j].dot(y); } /* compute with operator */ z2 = At * y; double errAt = (z1-z2).normInf(); Out::root() << "error in At*y = " << errA << std::endl; double tol = 1.0e-13; bool pass = errA + errAt < tol; pass = globalAnd(pass); if (pass) { Out::root() << "multivector op test PASSED" << std::endl; } else { stat = -1; Out::root() << "multivector op test FAILED" << std::endl; } } catch(std::exception& e) { stat = -1; std::cerr << "Caught exception: " << e.what() << std::endl; } return stat; }
int main(int argc, char *argv[]) { int stat = 0; try { GlobalMPISession session(&argc, &argv); MPIComm::world().synchronize(); Out::os() << "go!" << std::endl; VectorType<double> type = new EpetraVectorType(); Array<int> domainBlockSizes = tuple(2,3,4); Array<int> rangeBlockSizes = tuple(2,2); Array<VectorSpace<double> > domainBlocks(domainBlockSizes.size()); Array<VectorSpace<double> > rangeBlocks(rangeBlockSizes.size()); for (int i=0; i<domainBlocks.size(); i++) { domainBlocks[i] = type.createEvenlyPartitionedSpace(MPIComm::world(), domainBlockSizes[i]); } for (int i=0; i<rangeBlocks.size(); i++) { rangeBlocks[i] = type.createEvenlyPartitionedSpace(MPIComm::world(), rangeBlockSizes[i]); } VectorSpace<double> domain = blockSpace(domainBlocks); VectorSpace<double> range = blockSpace(rangeBlocks); double blockDensity = 0.75; double onProcDensity = 0.5; double offProcDensity = 0.1; RandomBlockMatrixBuilder<double> builder(domain, range, blockDensity, onProcDensity, offProcDensity, type); LinearOperator<double> A = builder.getOp(); Out::os() << "A num block rows = " << A.numBlockRows() << std::endl; Out::os() << "A num block cols = " << A.numBlockCols() << std::endl; Vector<double> x = domain.createMember(); Out::os() << "randomizing trial vector" << std::endl; x.randomize(); Array<Vector<double> > xBlock(domain.numBlocks()); for (int i=0; i<xBlock.size(); i++) { xBlock[i] = x.getBlock(i); } Vector<double> xx = x.copy(); Out::os() << "------------------------------------------------------------" << std::endl; Out::os() << "computing A*x..." << std::endl; Vector<double> y0 = A * x; for (int i=0; i<y0.space().numBlocks(); i++) { Out::os() << "y0[" << i << "] = " << std::endl << y0.getBlock(i) << std::endl; } Vector<double> y1 = range.createMember(); Out::os() << "------------------------------------------------------------" << std::endl; Out::os() << "computing A*x block-by-block..." << std::endl; Array<Vector<double> > yBlock(range.numBlocks()); for (int i=0; i<yBlock.size(); i++) { yBlock[i] = range.getBlock(i).createMember(); yBlock[i].zero(); for (int j=0; j<xBlock.size(); j++) { LinearOperator<double> Aij = A.getBlock(i,j); if (Aij.ptr().get() != 0) { Out::os() << "A(" << i << ", " << j << ") = " << std::endl << Aij << std::endl; } else { Out::os() << "A(" << i << ", " << j << ") = 0 " << std::endl; } Out::os() << "x[" << j << "] = " << std::endl << xBlock[j] << std::endl; if (Aij.ptr().get()==0) continue; yBlock[i] = yBlock[i] + Aij * xBlock[j]; } y1.setBlock(i, yBlock[i]); } for (int i=0; i<y1.space().numBlocks(); i++) { Out::os() << "y1[" << i << "] = " << std::endl << y1.getBlock(i) << std::endl; } double err = (y1 - y0).norm2(); Out::os() << "error = " << err << std::endl; double tol = 1.0e-13; if (err < tol) { Out::os() << "block op test PASSED" << std::endl; } else { stat = -1; Out::os() << "block op test FAILED" << std::endl; } } catch(std::exception& e) { stat = -1; Out::os() << "Caught exception: " << e.what() << std::endl; } return stat; }
int main(int argc, char *argv[]) { int stat = 0; try { GlobalMPISession session(&argc, &argv); VectorType<double> vecType = new SerialVectorType(); VectorSpace<double> domain = vecType.createEvenlyPartitionedSpace(MPIComm::world(), 3); VectorSpace<double> range = vecType.createEvenlyPartitionedSpace(MPIComm::world(), 5); RCP<MatrixFactory<double> > mf = vecType.createMatrixFactory(domain, range); LinearOperator<double> A = mf->createMatrix(); RCP<DenseSerialMatrix> APtr = rcp_dynamic_cast<DenseSerialMatrix>(A.ptr()); APtr->setRow(0, tuple(1.0, 2.0, 3.0)); APtr->setRow(1, tuple(4.0, 5.0, 6.0)); APtr->setRow(2, tuple(7.0, 8.0, 9.0)); APtr->setRow(3, tuple(10.0, 11.0, 12.0)); APtr->setRow(4, tuple(13.0, 14.0, 15.0)); Out::os() << "A = " << std::endl; A.setVerb(10); Out::os() << A << std::endl; LinearOperator<double> U; LinearOperator<double> Vt; Vector<double> sigma; denseSVD(A, U, sigma, Vt); Out::os() << "U = " << std::endl; U.setVerb(10); Out::os() << U << std::endl; Out::os() << "sigma = " << std::endl; Out::os() << sigma << std::endl; Out::os() << "Vt = " << std::endl; Vt.setVerb(10); Out::os() << Vt << std::endl; int nSamples = 10; bool allOK = true; double tol = 1.0e-13; for (int i=0; i<nSamples; i++) { Out::os() << "Sample #" << i << " of " << nSamples << std::endl; Vector<double> x = domain.createMember(); x.randomize(); U.setVerb(0); Vt.setVerb(0); A.setVerb(0); LinearOperator<double> Sigma = diagonalOperator(sigma); Vector<double> z = (U * Sigma * Vt)*x - A*x; double ez = z.norm2(); Out::os() << "|| (U Sigma Vt - A)*x || = " << ez << std::endl; Vector<double> y = (U.transpose() * U)*x - x; double ey = y.norm2(); Out::os() << "|| (U^T U - I)*x || = " << ey << std::endl; Vector<double> w = (Vt * Vt.transpose())*x - x; double ew = w.norm2(); Out::os() << "|| (V^T*V - I)*x || = " << ew << std::endl; if (ew > tol || ez > tol || ey > tol) allOK = false; } if (allOK) { Out::os() << "SVD test PASSED" << std::endl; } else { Out::os() << "SVD test FAILED" << std::endl; stat = -1; } } catch(std::exception& e) { stat = -1; Out::os() << "Caught exception: " << e.what() << std::endl; } return stat; }
Vector<double> Rosenbrock::getInit() const { Vector<double> rtn = vs_.createMember(); rtn.setToConstant(-1.0); return rtn; }
Vector<double> Ellipsoid::exactSoln() const { Vector<double> rtn = vs_.createMember(); rtn.setToConstant(0.0); return rtn; }
Vector<double> Ellipsoid::getInit() const { Vector<double> rtn = vs_.createMember(); rtn.setToConstant(1.0); return rtn; }
Vector<double> Rosenbrock::exactSoln() const { Vector<double> rtn = vs_.createMember(); rtn.setToConstant(1.0); return rtn; }
int main(int argc, char *argv[]) { typedef Teuchos::ScalarTraits<double> ST; try { GlobalMPISession session(&argc, &argv); MPIComm::world().synchronize(); VectorType<double> type = new EpetraVectorType(); /* create the range space */ int nLocalRows = 10; MatrixLaplacian1D builder(nLocalRows, type); LinearOperator<double> A = builder.getOp(); int nBlocks = 3; Array<Vector<double> > x(nBlocks); Array<VectorSpace<double> > space(nBlocks); for (int i=0; i<nBlocks; i++) { space[i] = A.domain(); x[i] = A.domain().createMember(); Thyra::randomize(-ST::one(),+ST::one(),x[i].ptr().ptr()); } VectorSpace<double> blockSpace = productSpace(space); LinearOperator<double> bigA = makeBlockOperator(blockSpace, blockSpace); Vector<double> bigRHS = blockSpace.createMember(); Vector<double> bigX = blockSpace.createMember(); for (int i=0; i<nBlocks; i++) { bigX.setBlock(i, x[i]); for (int j=i; j<nBlocks; j++) { MatrixLaplacian1D builder(nLocalRows, type); LinearOperator<double> Aij = builder.getOp(); bigA.setBlock(i,j,Aij); } } bigA.endBlockFill(); bigRHS = bigA * bigX; Vector<double> bigSoln = blockSpace.createMember(); #ifdef HAVE_CONFIG_H ParameterXMLFileReader reader(Sundance::searchForFile("SolverParameters/poissonParams.xml")); #else ParameterXMLFileReader reader("poissonParams.xml"); #endif ParameterList solverParams = reader.getParameters(); LinearSolver<double> solver = LinearSolverBuilder::createSolver(solverParams); LinearSolver<double> blockSolver = new BlockTriangularSolver<double>(solver); SolverState<double> state = blockSolver.solve(bigA, bigRHS, bigSoln); std::cerr << state << std::endl; double err = (bigSoln - bigX).norm2(); std::cerr << "error norm = " << err << std::endl; double tol = 1.0e-8; if (err > tol) { std::cerr << "Poisson solve test FAILED" << std::endl; return 1; } else { std::cerr << "Poisson solve test PASSED" << std::endl; return 0; } } catch(std::exception& e) { std::cerr << "Caught exception: " << e.what() << std::endl; return -1; } return 0; }