void TestChebyshevVsCG() throw (Exception) { unsigned num_nodes = 1331; DistributedVectorFactory factory(num_nodes); Vec parallel_layout = factory.CreateVec(2); unsigned cg_its; unsigned chebyshev_its; Timer::Reset(); { Mat system_matrix; // Note that this test deadlocks if the file's not on the disk PetscTools::ReadPetscObject(system_matrix, "linalg/test/data/matrices/cube_6000elems_half_activated.mat", parallel_layout); Vec system_rhs; // Note that this test deadlocks if the file's not on the disk PetscTools::ReadPetscObject(system_rhs, "linalg/test/data/matrices/cube_6000elems_half_activated.vec", parallel_layout); LinearSystem ls = LinearSystem(system_rhs, system_matrix); ls.SetMatrixIsSymmetric(); ls.SetAbsoluteTolerance(1e-9); ls.SetKspType("cg"); ls.SetPcType("bjacobi"); Vec solution = ls.Solve(); cg_its = ls.GetNumIterations(); PetscTools::Destroy(system_matrix); PetscTools::Destroy(system_rhs); PetscTools::Destroy(solution); } Timer::PrintAndReset("CG"); { Mat system_matrix; // Note that this test deadlocks if the file's not on the disk PetscTools::ReadPetscObject(system_matrix, "linalg/test/data/matrices/cube_6000elems_half_activated.mat", parallel_layout); Vec system_rhs; // Note that this test deadlocks if the file's not on the disk PetscTools::ReadPetscObject(system_rhs, "linalg/test/data/matrices/cube_6000elems_half_activated.vec", parallel_layout); LinearSystem ls = LinearSystem(system_rhs, system_matrix); ls.SetMatrixIsSymmetric(); ls.SetAbsoluteTolerance(1e-9); ls.SetKspType("chebychev"); ls.SetPcType("bjacobi"); Vec solution = ls.Solve(); chebyshev_its = ls.GetNumIterations(); PetscTools::Destroy(system_matrix); PetscTools::Destroy(system_rhs); PetscTools::Destroy(solution); } Timer::Print("Chebyshev"); TS_ASSERT_LESS_THAN(cg_its, 15u); // Takes 14 iterations with 16 cores TS_ASSERT_LESS_THAN(chebyshev_its, 17u); // Takes 16 iterations with 16 cores PetscTools::Destroy(parallel_layout); }
void TestChebyshevAdaptiveVsNoAdaptive() throw (Exception) { unsigned num_nodes = 1331; DistributedVectorFactory factory(num_nodes); Vec parallel_layout = factory.CreateVec(2); // Solving with zero guess for coverage Vec zero_guess = factory.CreateVec(2); double zero = 0.0; #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) VecSet(&zero, zero_guess); #else VecSet(zero_guess, zero); #endif Mat system_matrix; // Note that this test deadlocks if the file's not on the disk PetscTools::ReadPetscObject(system_matrix, "linalg/test/data/matrices/cube_6000elems_half_activated.mat", parallel_layout); Vec system_rhs; // Note that this test deadlocks if the file's not on the disk PetscTools::ReadPetscObject(system_rhs, "linalg/test/data/matrices/cube_6000elems_half_activated.vec", parallel_layout); // Make sure we are not inheriting a non-default number of iterations from previous test std::stringstream num_it_str; num_it_str << 1000; PetscOptionsSetValue("-ksp_max_it", num_it_str.str().c_str()); try { LinearSystem ls = LinearSystem(system_rhs, system_matrix); ls.SetMatrixIsSymmetric(); // Solve to relative convergence for coverage ls.SetRelativeTolerance(1e-6); ls.SetPcType("jacobi"); ls.SetKspType("chebychev"); ls.SetUseFixedNumberIterations(true, 64); // Solving with zero guess for coverage. Vec solution = ls.Solve(zero_guess); unsigned chebyshev_adaptive_its = ls.GetNumIterations(); TS_ASSERT_EQUALS(chebyshev_adaptive_its, 40u); TS_ASSERT_DELTA(ls.mEigMin, 0.0124, 1e-4); TS_ASSERT_DELTA(ls.mEigMax, 1.8810, 1e-4); PetscTools::Destroy(solution); } catch (Exception& e) { if (e.GetShortMessage() == "Chebyshev with fixed number of iterations is known to be broken in PETSc <= 2.3.2") { WARNING(e.GetShortMessage()); } else { TS_FAIL(e.GetShortMessage()); } } // Make sure we are not inheriting a non-default number of iterations from previous test PetscOptionsSetValue("-ksp_max_it", num_it_str.str().c_str()); { LinearSystem ls = LinearSystem(system_rhs, system_matrix); ls.SetMatrixIsSymmetric(); ls.SetRelativeTolerance(1e-6); ls.SetPcType("jacobi"); ls.SetKspType("chebychev"); Vec solution = ls.Solve(zero_guess); unsigned chebyshev_no_adaptive_its = ls.GetNumIterations(); TS_ASSERT_LESS_THAN(chebyshev_no_adaptive_its, 100u); // Normally 88, but 99 on maverick & natty TS_ASSERT_DELTA(ls.mEigMin, 0.0124, 1e-4); TS_ASSERT_DELTA(ls.mEigMax, 1.8841, 1e-4); PetscTools::Destroy(solution); } // Make sure we are not inheriting a non-default number of iterations from previous test PetscOptionsSetValue("-ksp_max_it", num_it_str.str().c_str()); { LinearSystem ls = LinearSystem(system_rhs, system_matrix); ls.SetMatrixIsSymmetric(); ls.SetRelativeTolerance(1e-6); ls.SetPcType("jacobi"); ls.SetKspType("cg"); Vec solution = ls.Solve(zero_guess); unsigned cg_its = ls.GetNumIterations(); TS_ASSERT_EQUALS(cg_its, 40u); TS_ASSERT_EQUALS(ls.mEigMin, DBL_MAX); TS_ASSERT_EQUALS(ls.mEigMax, DBL_MIN); PetscTools::Destroy(solution); } PetscTools::Destroy(system_matrix); PetscTools::Destroy(system_rhs); PetscTools::Destroy(parallel_layout); PetscTools::Destroy(zero_guess); }