Zoltan 2 Version 0.5
block.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 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 Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #include <Zoltan2_BasicIdentifierInput.hpp>
00051 #include <Zoltan2_PartitioningProblem.hpp>
00052 #include <Zoltan2_PartitioningSolution.hpp>
00053 
00054 using namespace std;
00055 
00062 // Zoltan2 is templated.  What data types will we use for
00063 // scalars (coordinate values and weights), for local ids, and
00064 // for global ids?
00065 //
00066 // If Zoltan2 was compiled with explicit instantiation, we will
00067 // use the the library's data types.  These macros are defined
00068 // in Zoltan2_config.h.
00069 
00070 #ifdef HAVE_ZOLTAN2_INST_FLOAT_INT_LONG
00071 typedef float scalar_t;
00072 typedef int localId_t;
00073 typedef long globalId_t;
00074 #else
00075   #ifdef HAVE_ZOLTAN2_INST_DOUBLE_INT_LONG
00076   typedef double scalar_t;
00077   typedef int localId_t;
00078   typedef long globalId_t;
00079   #else
00080     #ifdef HAVE_ZOLTAN2_INST_FLOAT_INT_INT
00081     typedef float scalar_t;
00082     typedef int localId_t;
00083     typedef int globalId_t;
00084     #else
00085       #ifdef HAVE_ZOLTAN2_INST_DOUBLE_INT_INT
00086       typedef double scalar_t;
00087       typedef int localId_t;
00088       typedef int globalId_t;
00089       #else
00090       typedef float scalar_t;
00091       typedef int localId_t;
00092       typedef int globalId_t;
00093       #endif
00094     #endif
00095   #endif
00096 #endif
00097 
00098 int main(int argc, char *argv[])
00099 {
00100 #ifdef HAVE_ZOLTAN2_MPI
00101   MPI_Init(&argc, &argv);
00102   int rank, nprocs;
00103   MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
00104   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00105 #else
00106   int rank=0, nprocs=1;
00107 #endif
00108 
00110   // Generate some input data.
00111 
00112   size_t localCount = 40*(rank+1);
00113   globalId_t *globalIds = new globalId_t [localCount];
00114 
00115   if (rank==0)
00116     for (int i=0, num=40; i <= nprocs ; i++, num+=40)
00117       cout << "Rank " << i << " has " << num << " ids." << endl;
00118 
00119   globalId_t offset = 0;
00120   for (int i=1; i <= rank; i++)
00121     offset += 40*i;
00122 
00123   for (size_t i=0; i < localCount; i++)
00124     globalIds[i] = offset++;
00125    
00127   // Create a Zoltan2 input adapter with no weights
00128 
00129   // TODO explain
00130   typedef Zoltan2::BasicUserTypes<scalar_t, globalId_t, localId_t, globalId_t> myTypes;
00131 
00132   // TODO explain
00133   typedef Zoltan2::BasicIdentifierInput<myTypes> inputAdapter_t;
00134 
00135   std::vector<const scalar_t *> noWeights;
00136   std::vector<int> noStrides;
00137 
00138   inputAdapter_t ia(localCount, globalIds, noWeights, noStrides);
00139    
00141   // Create parameters for an Block problem
00142 
00143   Teuchos::ParameterList params("test params");
00144   params.set("debug_level", "basic_status");
00145   params.set("debug_procs", "0");
00146   params.set("error_check_level", "debug_mode_assertions");
00147 
00148   params.set("algorithm", "block");
00149   params.set("imbalance_tolerance", 1.1);
00150   params.set("num_global_parts", nprocs);
00151    
00153   // Create a Zoltan2 partitioning problem
00154 
00155 #ifdef HAVE_ZOLTAN2_MPI
00156   Zoltan2::PartitioningProblem<inputAdapter_t> *problem = 
00157            new Zoltan2::PartitioningProblem<inputAdapter_t>(&ia, &params, 
00158                                                             MPI_COMM_WORLD);
00159 #else
00160   Zoltan2::PartitioningProblem<inputAdapter_t> *problem = 
00161            new Zoltan2::PartitioningProblem<inputAdapter_t>(&ia, &params);
00162 #endif
00163    
00165   // Solve the problem
00166 
00167   problem->solve();
00168 
00170   // Check the solution.
00171 
00172   if (rank == 0)
00173     problem->printMetrics(cout);
00174 
00175   if (rank == 0)
00176     cout << "PASS" << endl;
00177 
00178   delete [] globalIds;
00179   delete problem;
00180 #ifdef HAVE_ZOLTAN2_MPI
00181   MPI_Finalize();
00182 #endif
00183 }
00184