Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_AlgRandom.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef _ZOLTAN2_ALGRANDOM_HPP_
11 #define _ZOLTAN2_ALGRANDOM_HPP_
12 
13 #include <Zoltan2_Algorithm.hpp>
16 
17 
18 namespace Zoltan2{
19 
24 
25 template <typename Adapter>
26 class AlgRandom : public Algorithm<Adapter>
27 {
28  private:
29 
30  const RCP<const typename Adapter::base_adapter_t> adapter;
31  const RCP<Teuchos::ParameterList> pl;
32  const RCP<const Teuchos::Comm<int> > comm;
33  const RCP<const Environment> env;
34 
35  public:
36 
37  typedef typename Adapter::lno_t lno_t;
38  typedef typename Adapter::gno_t gno_t;
39 
41  const RCP<const typename Adapter::base_adapter_t> &adapter__,
42  const RCP<Teuchos::ParameterList> &pl__,
43  const RCP<const Teuchos::Comm<int> > &comm__,
44  const RCP<const Environment> &env__
45  ) : adapter(adapter__), pl(pl__), comm(comm__), env(env__)
46  {
47  }
48 
49  int globalOrder(const RCP<GlobalOrderingSolution<gno_t> > &/* solution */)
50  {
51  throw std::logic_error("AlgRandom does not yet support global ordering.");
52  }
53 
54  int localOrder(const RCP<LocalOrderingSolution<lno_t> > &solution)
55  {
56 
57  int ierr= 0;
58 
59  HELLO;
60 
61  // This is the Fisher-Yates shuffle (aka Knuth shuffle).
62  // References:
63  // R.A. Fisher, F. Yates, (1948) [1938], Statistical tables for biological, agricultural and medical research (3rd ed.).
64  // R. Durstenfeld, "Algorithm 235: Random permutation", CACM, vol. 7, 1964.
65  // D.E. Knuth, "The Art of Computer Programming", volume 2, 1969.
66 
67  // Start with the identity permutation.
68  modelFlag_t modelFlags;
69  const auto model = rcp(new IdentifierModel<Adapter>(adapter, env, comm, modelFlags));
70  const size_t n = model->getLocalNumIdentifiers();
71  lno_t *perm;
72  perm = (lno_t *) (solution->getPermutationRCP().getRawPtr());
73  if (perm){
74  for (size_t i=0; i<n; i++){
75  perm[i] = i;
76  }
77  }
78  else
79  // throw exception?
80  ierr = -1;
81 
82  // Swap random pairs of indices in perm.
83  lno_t j, temp;
84  for (lno_t i=n-1; i>0; i--){
85  // Choose j randomly in [0,i]
86  j = rand() % (i+1);
87  // Swap (perm[i], perm[j])
88  temp = perm[i];
89  perm[i] = perm[j];
90  perm[j] = temp;
91  }
92 
93  solution->setHavePerm(true);
94  return ierr;
95 
96  }
97 
98 };
99 }
100 #endif
#define HELLO
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
Defines the OrderingSolution class.
Defines the IdentifierModel interface.
AlgRandom(const RCP< const typename Adapter::base_adapter_t > &adapter__, const RCP< Teuchos::ParameterList > &pl__, const RCP< const Teuchos::Comm< int > > &comm__, const RCP< const Environment > &env__)
Algorithm defines the base class for all algorithms.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
int localOrder(const RCP< LocalOrderingSolution< lno_t > > &solution)
Ordering method.
IdentifierModel defines the interface for all identifier models.
int globalOrder(const RCP< GlobalOrderingSolution< gno_t > > &)
Ordering method.