Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_TestHelpers.hpp
Go to the documentation of this file.
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 
49 #ifndef ZOLTAN2_TESTHELPERS_HPP
50 #define ZOLTAN2_TESTHELPERS_HPP
51 
52 #include <Teuchos_UnitTestHarness.hpp>
53 #include <Zoltan2_Util.hpp>
54 #include <iostream>
55 
56 #include <Tpetra_Map.hpp>
57  typedef Tpetra::Map<>::node_type znode_t;
58 
59 // The path to the directory of test data
60 
61 #define STR_VALUE(path) #path
62 #define PATH_NAME(path) STR_VALUE(path)
63 
64 #ifdef Z2_DATA_DIR
65 std::string testDataFilePath(PATH_NAME(Z2_DATA_DIR));
66 #else
67 std::string testDataFilePath(".");
68 #endif
69 
70 // The path to the Zoltan1 test directory. We use
71 // some of their data for testing.
72 
73 #ifdef Z1_TEST_DIR
74 std::string zoltanTestDirectory(PATH_NAME(Z1_TEST_DIR));
75 #else
76 std::string zoltanTestDirectory(".");
77 #endif
78 
80 //
81 // If Tpetra is compiled with explicit instantiation,
82 // we have to choose data types that are compiled into Tpetra.
83 //
84 // Epetra uses (scalar/lno/gno) == (double/int/int) data types. If we
85 // are using these data types, we can test Epetra user input.
86 
87 // TODO: KDD 8/13/14
88 // Global definitions of types gno_t, lno_t, zgid_t and
89 // scalar_t can cause bugs in the code. If a class fails to define these
90 // types, but this file is included before the class file, the types
91 // from Zoltan2_TestHelpers.hpp will be used in the class. Compilation in
92 // user programs (without Zoltan2_TestHelpers.hpp) would then fail. An
93 // example of this bug was in the GeometricGenerator class, which used
94 // scalar_t without defining it.
95 // In this "fix," I changed gno_t, lno_t, zgid_t, scalar_t, and node_t to
96 // zgno_t, zlno_t, zzgid_t, zscalar_t and znode_t in Zoltan2_TestHelpers.hpp.
97 // This change is not the best fix; a better fix would remove the global
98 // definitions, but that would require more work. (An even better change
99 // would use the Teuchos test framework to cycle through various options,
100 // but that would require even more work and should be delayed until we
101 // revamp the testing.)
102 
103 #include <TpetraCore_config.h>
104 
105 typedef int zpart_t; // added this for consistency but needs further discussion
106 
107 typedef Tpetra::Map<>::local_ordinal_type zlno_t;
108 typedef Tpetra::Map<>::global_ordinal_type zgno_t;
109 
110 using Teuchos::compareArrays;
111 
112 #ifdef HAVE_TPETRA_DOUBLE
113 typedef double zscalar_t;
114 #define HAVE_EPETRA_SCALAR_TYPE
115 #else
116 typedef float zscalar_t;
117 #endif
118 
119 #if defined HAVE_TPETRA_INT_INT
120 #if defined HAVE_EPETRA_SCALAR_TYPE
121 #define HAVE_EPETRA_DATA_TYPES
122 #endif
123 #endif
124 
125 #ifndef HAVE_ZOLTAN2_EPETRA
126 #undef HAVE_EPETRA_SCALAR_TYPE
127 #undef HAVE_EPETRA_DATA_TYPES
128 #endif
129 
131 
132 #define MEMORY_CHECK(iPrint, msg) \
133  if (iPrint) { \
134  long kb = Zoltan2::getProcessKilobytes(); \
135  std::cout.width(10); \
136  std::cout.fill('*'); \
137  std::cout << kb << " KB, " << msg << std::endl; \
138  std::cout.width(0); \
139  std::cout.fill(' '); \
140  }
141 
142 #define Z2_TEST(TEST) \
143  { \
144  Teuchos::RCP<Teuchos::FancyOStream> fout = \
145  Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); \
146  auto &out = *fout; \
147  bool success = true; \
148  try { \
149  TEST; \
150  } catch (...) { \
151  out << "Test failed."; \
152  } \
153  if (!success) { \
154  throw std::runtime_error(#TEST " FAIL"); \
155  } \
156  }
157 
158 #define Z2_TEST_THROW(code, ExceptType) Z2_TEST(TEST_THROW(code, ExceptType))
159 #define Z2_TEST_NOTHROW(code) Z2_TEST(TEST_NOTHROW(code))
160 #define Z2_TEST_EQUALITY(val1, val2) Z2_TEST(TEST_EQUALITY(val1, val2))
161 #define Z2_TEST_INEQUALITY(val1, val2) Z2_TEST(TEST_INEQUALITY(val1, val2))
162 #define Z2_TEST_ASSERT(expr) Z2_TEST(TEST_ASSERT(expr))
163 #define Z2_TEST_EQUALITY_CONST(val1, val2) \
164  Z2_TEST(TEST_EQUALITY_CONST(val1, val2))
165 #define Z2_TEST_INEQUALITY_CONST(val1, val2) \
166  Z2_TEST(TEST_INEQUALITY_CONST(val1, val2))
167 #define Z2_TEST_COMPARE(val1, comp, val2) \
168  Z2_TEST(TEST_COMPARE(val1, comp, val2))
169 #define Z2_TEST_COMPARE_ARRAYS(val1, val2) \
170  Z2_TEST(TEST_COMPARE_ARRAYS(val1, val2))
171 #define Z2_TEST_COMPARE_FLOATING_ARRAYS(val1, val2, tol) \
172  Z2_TEST(TEST_COMPARE_FLOATING_ARRAYS(val1, val2, tol))
173 #define Z2_TEST_FLOATING_EQUALITY(val1, val2, tol) \
174  Z2_TEST(TEST_FLOATING_EQUALITY(val1, val2, tol))
175 
176 inline void PrintFromRoot(const std::string &message) {
177  if (Tpetra::getDefaultComm()->getRank() == 0) {
178  printf("%s \n", message.c_str());
179  }
180 }
181 
182 template <typename DeviceType, typename HostType>
183 void TestDeviceHostView(const DeviceType &deviceView,
184  const HostType &hostView) {
185  // Should we test for more dimensions?
186  for (int dim = 0; dim <= 2; ++dim) {
187  Z2_TEST_EQUALITY(deviceView.extent(dim), hostView.extent(dim));
188  }
189 
190  const auto mirrorDevice = Kokkos::create_mirror_view(deviceView);
191  Kokkos::deep_copy(mirrorDevice, deviceView);
192 
193  // Compare the values element-wise
194  Z2_TEST_COMPARE_ARRAYS(hostView, mirrorDevice);
195 }
196 
197 #define Z2_TEST_DEVICE_HOST_VIEWS(deviceView, hostView) \
198  \
199  { \
200  for (int dim = 0; dim <= 2; ++dim) { \
201  Z2_TEST_EQUALITY(deviceView.extent(dim), hostView.extent(dim)); \
202  } \
203  \
204  const auto mirrorDevice = Kokkos::create_mirror_view(deviceView); \
205  Kokkos::deep_copy(mirrorDevice, deviceView); \
206  \
207  Z2_TEST_COMPARE_ARRAYS(hostView, mirrorDevice); \
208  }
209 
210 #include <ErrorHandlingForTests.hpp>
211 #include <PrintData.hpp>
212 #include <UserInputForTests.hpp>
213 
214 #endif
Generate input for testing purposes.
void PrintFromRoot(const std::string &message)
#define PATH_NAME(path)
#define Z2_TEST_COMPARE_ARRAYS(val1, val2)
void TestDeviceHostView(const DeviceType &deviceView, const HostType &hostView)
int zpart_t
#define Z2_TEST_EQUALITY(val1, val2)
Tpetra::Map::local_ordinal_type zlno_t
float zscalar_t
A gathering of useful namespace methods.
Tpetra::Map::node_type znode_t
Tpetra::Map::global_ordinal_type zgno_t
std::string zoltanTestDirectory(".")
std::string testDataFilePath(".")