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