Tpetra Matrix/Vector Services Version of the Day
RTITutorialSimple.cpp
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_XMLParameterListReader.hpp>
00046 #include <Teuchos_XMLParameterListHelpers.hpp>
00047 #include <Teuchos_DefaultMpiComm.hpp>
00048 
00049 #include <Tpetra_ConfigDefs.hpp>
00050 
00051 #undef HAVE_KOKKOSCLASSIC_THRUST
00052 
00053 #include <Tpetra_HybridPlatform.hpp>
00054 #include <Tpetra_Map.hpp>
00055 #include <Tpetra_Vector.hpp>
00056 #include <Tpetra_RTI.hpp>
00057 
00058 #include <functional>
00059 
00060 // Function object that takes two arguments and returns the second
00061 template <class S>
00062 class AssignSecond {
00063   public:
00064   AssignSecond() {}
00065   S operator()(const S &s1, const S &s2) {return s2;}
00066 };
00067 
00068 // A few examples of RTI capability
00069 template <class Node>
00070 void simple_rti_examples(const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node)
00071 {
00072   using Teuchos::RCP;
00073   using Tpetra::RTI::reduce;
00074   using Tpetra::RTI::ZeroOp;
00075   using Tpetra::RTI::reductionGlob;
00076   using Tpetra::RTI::unary_transform;
00077   using Tpetra::RTI::binary_transform;
00078   const Tpetra::global_size_t INVALID = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
00079   // Create a simple map with 1000 local entries per node
00080   const size_t numLocal   = 1000*comm->getSize();
00081   auto map = Tpetra::createContigMapWithNode<int,int>(INVALID,numLocal,comm,node);
00082   auto   dx = Tpetra::createVector<double>(map),
00083          dy = Tpetra::createVector<double>(map);
00084 
00085   // Set dx to random
00086   dx->randomize();
00087 
00088   // Assign dy = dx, multiple ways!
00089   // Via functor
00090   binary_transform( *dy, *dx, AssignSecond<double>() );
00091   // Via C++11 lambda expression
00092   binary_transform( *dy, *dx, [](double, double xx){return xx;} );
00093   // Via convenient macro
00094   TPETRA_BINARY_TRANSFORM( dy, dx,   dx ); 
00095 
00096   // Perform multi-precision inner product...
00097   // floating point inner product with double precision accumulator
00098   float fresult; double dresult;
00099   auto fx = Tpetra::createVector<float>(map),
00100        fy = Tpetra::createVector<float>(map);
00101   TPETRA_BINARY_TRANSFORM( fx, dx, (float)dx );
00102   TPETRA_BINARY_TRANSFORM( fy, dy, (float)dy );
00103   // ... using a composite adaptor and standard functors
00104   fresult = reduce(*fx, *fy, reductionGlob<ZeroOp<double>>(std::multiplies<float>(), std::plus<double>()) );
00105   // ... using a convenience macro to generate all of that
00106   fresult = TPETRA_REDUCE2( fx, fy,   fx*fy, ZeroOp<double>, std::plus<double>() );
00107 
00108   // compare against double precision approach
00109   dresult = TPETRA_REDUCE2( dx, dy,   dx*dy, ZeroOp<double>, std::plus<double>() );
00110   if (comm->getRank() == 0) {
00111     std::cout << " Float product/double accumulator: " << fresult << std::endl;
00112     std::cout << "Double product/double accumulator: " << dresult << std::endl;
00113   }
00114 }
00115 
00116 // User-specified run object; think of this as main(), but templated on the node type
00117 template <class Node>
00118 class runTest {
00119   public:
00120     // HybridNode requires a method with this signature
00121     static void run(Teuchos::ParameterList &myMachPL, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node) 
00122     {
00123       std::cout << "Running test with Node==" << Teuchos::typeName(*node) << " on rank " << comm->getRank() << "/" << comm->getSize() << std::endl;
00124       simple_rti_examples<Node>(comm,node);
00125     }
00126 };
00127 
00128 // Actual main(): configure the HybridPlatform
00129 int main(int argc, char **argv) {
00130   Teuchos::GlobalMPISession mpisess(&argc,&argv,&std::cout);
00131   Teuchos::RCP<const Teuchos::Comm<int> > comm = Teuchos::createMpiComm<int>(Teuchos::opaqueWrapper<MPI_Comm>(MPI_COMM_WORLD));
00132 
00133   // 
00134   // read machine file and initialize platform
00135   // 
00136   std::string machine_list(
00137     "<ParameterList>                                                               "
00138     "  <ParameterList name='%1=0'>                                                 "
00139     "    <Parameter name='NodeType' type='string' value='KokkosClassic::SerialNode'/>     "
00140     "  </ParameterList>                                                            "
00141     "  <ParameterList name='=-1'>                                                  "
00142     "    <Parameter name='NodeType' type='string' value='KokkosClassic::OpenMPNode'/>     "
00143     "    <Parameter name='Verbose' type='int' value='0'/>                          "
00144     "    <Parameter name='Num Threads' type='int' value='-1'/>                     "
00145     "  </ParameterList>                                                            "
00146     "  <ParameterList name='=-2'>                                                  "
00147     "    <Parameter name='NodeType' type='string' value='KokkosClassic::TBBNode'/>        "
00148     "    <Parameter name='Verbose' type='int' value='0'/>                          "
00149     "    <Parameter name='Num Threads' type='int' value='-1'/>                     "
00150     "  </ParameterList>                                                            "
00151     "  <ParameterList name='=-3'>                                                  "
00152     "    <Parameter name='NodeType' type='string' value='KokkosClassic::TPINode'/>        "
00153     "    <Parameter name='Verbose' type='int' value='0'/>                          "
00154     "    <Parameter name='Num Threads' type='int' value='0'/>                      "
00155     "  </ParameterList>                                                            "
00156     "  <ParameterList name='=-4'>                                                  "
00157     "    <Parameter name='NodeType' type='string' value='KokkosClassic::ThrustGPUNode'/>  "
00158     "    <Parameter name='Verbose' type='int' value='0'/>                          "
00159     "    <Parameter name='Device Number' type='int' value='0'/>                    "
00160     "  </ParameterList>                                                            "
00161     "</ParameterList>                                                              "
00162   );
00163 
00164   // Load the machine ParameterList from the string above; this will typically 
00165   // be loaded from an XML file
00166   Teuchos::ParameterList machPL;
00167   Teuchos::updateParametersFromXmlString(machine_list, inOutArg(machPL));
00168   Tpetra::HybridPlatform platform(comm,machPL);
00169   platform.runUserCode<runTest>();
00170 
00171   if (comm->getRank() == 0) std::cout << "End Result: TEST PASSED" << std::endl;
00172   return 0;
00173 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines