Advertisement
Guest User

Recursive Coordinate Bisection, Zoltan2

a guest
Nov 15th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | None | 0 0
  1. // @HEADER
  2. //
  3. // ***********************************************************************
  4. //
  5. //   Zoltan2: A package of combinatorial algorithms for scientific computing
  6. //                  Copyright 2012 Sandia Corporation
  7. //
  8. // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  9. // the U.S. Government retains certain rights in this software.
  10. //
  11. // Redistribution and use in source and binary forms, with or without
  12. // modification, are permitted provided that the following conditions are
  13. // met:
  14. //
  15. // 1. Redistributions of source code must retain the above copyright
  16. // notice, this list of conditions and the following disclaimer.
  17. //
  18. // 2. Redistributions in binary form must reproduce the above copyright
  19. // notice, this list of conditions and the following disclaimer in the
  20. // documentation and/or other materials provided with the distribution.
  21. //
  22. // 3. Neither the name of the Corporation nor the names of the
  23. // contributors may be used to endorse or promote products derived from
  24. // this software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
  27. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
  30. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. // Questions? Contact Karen Devine      (kddevin@sandia.gov)
  39. //                    Erik Boman        (egboman@sandia.gov)
  40. //                    Siva Rajamanickam (srajama@sandia.gov)
  41. //
  42. // ***********************************************************************
  43. //
  44. // @HEADER
  45.  
  46. /*! \file rcb_C.cpp
  47.     \brief An example of partitioning coordinates with RCB.
  48. */
  49.  
  50. #include <Zoltan2_PartitioningSolution.hpp>
  51. #include <Zoltan2_PartitioningProblem.hpp>
  52. #include <Zoltan2_BasicVectorAdapter.hpp>
  53. #include <Zoltan2_InputTraits.hpp>
  54. #include <Tpetra_Map.hpp>
  55. #include <vector>
  56. #include <cstdlib>
  57. #include "mpi.h"
  58.  
  59. using namespace std;
  60. using std::vector;
  61. using Teuchos::RCP;
  62.  
  63. /*! \example rcb_C.cpp
  64.     An example of the use of the RCB algorithm to partition coordinate data.
  65. */
  66.  
  67. int main(int argc, char *argv[])
  68. {
  69.   MPI_Init(&argc, &argv);
  70.   int rank, nprocs;
  71.   MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  72.   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  73.  
  74.   // For convenience, we'll use the Tpetra defaults for local/global ID types
  75.   // Users can substitute their preferred local/global ID types
  76.   typedef Tpetra::Map<> Map_t;
  77.   typedef Map_t::local_ordinal_type localId_t;
  78.   typedef Map_t::global_ordinal_type globalId_t;
  79.  
  80.   typedef double scalar_t;
  81.   typedef Zoltan2::BasicUserTypes<scalar_t, localId_t, globalId_t> myTypes;
  82.  
  83.   // TODO explain
  84.   typedef Zoltan2::BasicVectorAdapter<myTypes> inputAdapter_t;
  85.   typedef Zoltan2::EvaluatePartition<inputAdapter_t> quality_t;
  86.   typedef inputAdapter_t::part_t part_t;
  87.   typedef inputAdapter_t::base_adapter_t base_adapter_t;
  88.  
  89.   ///////////////////////////////////////////////////////////////////////
  90.   // Create input data.
  91.  
  92.   size_t totalCount = 15000000;
  93.   size_t localCount = totalCount/nprocs;
  94.   int dim = 3;
  95.  
  96.   scalar_t *coords = new scalar_t [dim * localCount];
  97.  
  98.   scalar_t *x = coords;
  99.   scalar_t *y = x + localCount;
  100.   scalar_t *z = y + localCount;
  101.  
  102.   // Create coordinates that range from 0 to 10.0
  103.  
  104.   srand(rank);
  105.   scalar_t scalingFactor = 10.0 / RAND_MAX;
  106.  
  107.   for (size_t i=0; i < localCount*dim; i++){
  108.     coords[i] = scalar_t(rand()) * scalingFactor;
  109.   }
  110.  
  111.   // Create global ids for the coordinates.
  112.  
  113.   globalId_t *globalIds = new globalId_t [localCount];
  114.   globalId_t offset = rank * localCount;
  115.  
  116.   for (size_t i=0; i < localCount; i++)
  117.     globalIds[i] = offset++;
  118.    
  119.   ///////////////////////////////////////////////////////////////////////
  120.   // Create parameters for an RCB problem
  121.  
  122.   double tolerance = 1.5;
  123.  
  124.   if (rank == 0)
  125.     std::cout << "Imbalance tolerance is " << tolerance << std::endl;
  126.  
  127.   Teuchos::ParameterList params("test params");
  128.   params.set("debug_level", "basic_status");
  129.   params.set("debug_procs", "0");
  130.   params.set("error_check_level", "debug_mode_assertions");
  131.  
  132.   params.set("algorithm", "rcb");
  133.   params.set("imbalance_tolerance", tolerance );
  134.   params.set("num_global_parts", 2048);
  135.  
  136.   ///////////////////////////////////////////////////////////////////////
  137.   ///////////////////////////////////////////////////////////////////////
  138.   // A simple problem with no weights.
  139.   ///////////////////////////////////////////////////////////////////////
  140.   ///////////////////////////////////////////////////////////////////////
  141.  
  142.   // Create a Zoltan2 input adapter for this geometry. TODO explain
  143.  
  144.   inputAdapter_t *ia1 = new inputAdapter_t(localCount,globalIds,x,y,z,1,1,1);
  145.  
  146.   // Create a Zoltan2 partitioning problem
  147.  
  148.   Zoltan2::PartitioningProblem<inputAdapter_t> *problem1 =
  149.            new Zoltan2::PartitioningProblem<inputAdapter_t>(ia1, &params);
  150.    
  151.   // Solve the problem
  152.  
  153.   MPI_Barrier(MPI_COMM_WORLD);
  154.   double startTime = MPI_Wtime();
  155.  
  156.   problem1->solve();
  157.  
  158.   MPI_Barrier(MPI_COMM_WORLD);
  159.   double totalTime = startTime - MPI_Wtime();
  160.   std::cout << "Total time is " << totalTime << endl;
  161.  
  162.   // create metric object where communicator is Teuchos default
  163.  
  164.   quality_t *metricObject1 = new quality_t(ia1, &params, //problem1->getComm(),
  165.                        &problem1->getSolution());
  166.   // Check the solution.
  167.  
  168.   if (rank == 0) {
  169.     metricObject1->printMetrics(cout);
  170.   }
  171.  
  172.   if (rank == 0){
  173.     scalar_t imb = metricObject1->getObjectCountImbalance();
  174.     if (imb <= tolerance)
  175.       std::cout << "pass: " << imb << std::endl;
  176.     else
  177.       std::cout << "fail: " << imb << std::endl;
  178.     std::cout << std::endl;
  179.   }
  180.   delete metricObject1;
  181.  
  182.   if (coords)
  183.     delete [] coords;
  184.  
  185.   if (globalIds)
  186.     delete [] globalIds;
  187.  
  188.   delete problem1;
  189.   delete ia1;
  190.  
  191.   MPI_Finalize();
  192.  
  193.   if (rank == 0)
  194.     std::cout << "PASS" << std::endl;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement