|
Tpetra Matrix/Vector Services
Version of the Day
|
00001 /* 00002 // @HEADER 00003 // *********************************************************************** 00004 // 00005 // Tpetra: Templated Linear Algebra Services Package 00006 // Copyright (2008) Sandia Corporation 00007 // 00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 00009 // the U.S. Government retains certain rights in this software. 00010 // 00011 // Redistribution and use in source and binary forms, with or without 00012 // modification, are permitted provided that the following conditions are 00013 // met: 00014 // 00015 // 1. Redistributions of source code must retain the above copyright 00016 // notice, this list of conditions and the following disclaimer. 00017 // 00018 // 2. Redistributions in binary form must reproduce the above copyright 00019 // notice, this list of conditions and the following disclaimer in the 00020 // documentation and/or other materials provided with the distribution. 00021 // 00022 // 3. Neither the name of the Corporation nor the names of the 00023 // contributors may be used to endorse or promote products derived from 00024 // this software without specific prior written permission. 00025 // 00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00037 // 00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00039 // 00040 // ************************************************************************ 00041 // @HEADER 00042 */ 00043 00044 #include <Teuchos_GlobalMPISession.hpp> 00045 #include <Teuchos_oblackholestream.hpp> 00046 #include <Teuchos_CommandLineProcessor.hpp> 00047 #include <Teuchos_Array.hpp> 00048 00049 #include "Tpetra_Power_Method.hpp" 00050 00051 #include "Tpetra_DefaultPlatform.hpp" 00052 #include "Tpetra_Version.hpp" 00053 #include "Tpetra_Map.hpp" 00054 #include "Tpetra_MultiVector.hpp" 00055 #include "Tpetra_Vector.hpp" 00056 #include "Tpetra_CrsMatrix.hpp" 00057 00058 #include <algorithm> 00059 #include <functional> 00060 00061 int main(int argc, char *argv[]) { 00062 Teuchos::oblackholestream blackhole; 00063 Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole); 00064 00065 // 00066 // Specify types used in this example 00067 // 00068 typedef double Scalar; 00069 typedef Teuchos::ScalarTraits<Scalar>::magnitudeType Magnitude; 00070 typedef int Ordinal; 00071 typedef Tpetra::DefaultPlatform::DefaultPlatformType Platform; 00072 typedef Tpetra::DefaultPlatform::DefaultPlatformType::NodeType Node; 00073 typedef Tpetra::Map<Ordinal,Ordinal,Node> Map; 00074 typedef Tpetra::CrsMatrix<Scalar,Ordinal,Ordinal,Node> CrsMatrix; 00075 using Teuchos::RCP; 00076 using Teuchos::tuple; 00077 00078 // 00079 // Get the default communicator and node 00080 // 00081 Platform &platform = Tpetra::DefaultPlatform::getDefaultPlatform(); 00082 RCP<const Teuchos::Comm<int> > comm = platform.getComm(); 00083 RCP<Node> node = platform.getNode(); 00084 const int myRank = comm->getRank(); 00085 00086 // 00087 // Get example parameters from command-line processor 00088 // 00089 bool printMatrix = false; 00090 bool verbose = (myRank==0); 00091 int niters = 100; 00092 int numGlobalElements = 100; 00093 Magnitude tolerance = 1.0e-2; 00094 Teuchos::CommandLineProcessor cmdp(false,true); 00095 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); 00096 cmdp.setOption("numGlobalElements",&numGlobalElements,"Global problem size."); 00097 cmdp.setOption("tolerance",&tolerance,"Relative residual tolerance used for solver."); 00098 cmdp.setOption("iterations",&niters,"Maximum number of iterations."); 00099 cmdp.setOption("printMatrix","noPrintMatrix",&printMatrix,"Print the full matrix after reading it."); 00100 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { 00101 return -1; 00102 } 00103 00104 // 00105 // Say hello, print some communicator info 00106 // 00107 if (verbose) { 00108 std::cout << "\n" << Tpetra::version() << std::endl << std::endl; 00109 } 00110 std::cout << "Comm info: " << *comm; 00111 00112 // 00113 // Construct the problem 00114 // 00115 // Construct a Map that puts approximately the same number of equations on each processor. 00116 RCP<const Map> map = Tpetra::createUniformContigMap<Ordinal,Ordinal>(numGlobalElements, comm); 00117 // Get update list and number of local equations from newly created map. 00118 const size_t numMyElements = map->getNodeNumElements(); 00119 Teuchos::ArrayView<const Ordinal> myGlobalElements = map->getNodeElementList(); 00120 // Create a CrsMatrix using the map, with a dynamic allocation of 3 entries per row 00121 RCP<CrsMatrix> A = Tpetra::createCrsMatrix<Scalar>(map,3); 00122 // Add rows one-at-a-time 00123 for (size_t i=0; i<numMyElements; i++) { 00124 if (myGlobalElements[i] == 0) { 00125 A->insertGlobalValues( myGlobalElements[i], 00126 tuple<Ordinal>( myGlobalElements[i], myGlobalElements[i]+1 ), 00127 tuple<Scalar> ( 2.0, -1.0 ) ); 00128 } 00129 else if (myGlobalElements[i] == numGlobalElements-1) { 00130 A->insertGlobalValues( myGlobalElements[i], 00131 tuple<Ordinal>( myGlobalElements[i]-1, myGlobalElements[i] ), 00132 tuple<Scalar> ( -1.0, 2.0 ) ); 00133 } 00134 else { 00135 A->insertGlobalValues( myGlobalElements[i], 00136 tuple<Ordinal>( myGlobalElements[i]-1, myGlobalElements[i], myGlobalElements[i]+1 ), 00137 tuple<Scalar> ( -1.0, 2.0, -1.0 ) ); 00138 } 00139 } 00140 // Complete the fill 00141 A->fillComplete(); 00142 if (printMatrix) { 00143 RCP<Teuchos::FancyOStream> fos = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); 00144 A->describe(*fos, Teuchos::VERB_EXTREME); 00145 } 00146 else if (verbose) { 00147 std::cout << std::endl << A->description() << std::endl << std::endl; 00148 } 00149 00150 // 00151 // Iterate 00152 // 00153 TpetraExamples::powerMethod<Scalar,Ordinal>(A, niters, tolerance, verbose); 00154 00155 // Increase diagonal dominance 00156 if (verbose) { 00157 std::cout << "\nIncreasing magnitude of first diagonal term, solving again\n" 00158 << std::endl; 00159 } 00160 00161 A->resumeFill(); 00162 if (A->getRowMap()->isNodeGlobalElement(0)) { 00163 // get a copy of the row with with global index 0 00164 // modify the diagonal entry of that row 00165 // submit the modified values to the matrix 00166 size_t numVals = A->getNumEntriesInGlobalRow(0); 00167 Teuchos::Array<Scalar> rowvals(numVals); 00168 Teuchos::Array<Ordinal> rowinds(numVals); 00169 A->getGlobalRowCopy(0, rowinds, rowvals, numVals); 00170 // replace the diagonal with 10.0 00171 std::replace_if( rowvals.begin(), rowvals.end(), 00172 std::bind2nd(std::equal_to<Ordinal>(), 0), 00173 10.0 00174 ); 00175 A->replaceGlobalValues(0, rowinds(), rowvals()); 00176 } 00177 A->fillComplete(); 00178 00179 // Iterate again 00180 TpetraExamples::powerMethod<Scalar,Ordinal>(A, niters, tolerance, verbose); 00181 00182 if (verbose) { 00183 std::cout << "\nEnd Result: TEST PASSED" << std::endl; 00184 } 00185 return 0; 00186 }
1.7.6.1