Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_checkView.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Tpetra: Templated Linear Algebra Services Package
5// Copyright (2008) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// ************************************************************************
38// @HEADER
39
40#ifndef TPETRA_DETAILS_CHECKVIEW_HPP
41#define TPETRA_DETAILS_CHECKVIEW_HPP
42
49
51#include "Tpetra_Details_WrappedDualView.hpp"
52#include "Kokkos_DualView.hpp"
53#include "Teuchos_TypeNameTraits.hpp"
54#include "Teuchos_Comm.hpp"
55#include "Teuchos_CommHelpers.hpp"
56#include <sstream>
57
58namespace Tpetra {
59namespace Details {
60
61std::string memorySpaceName (const void* ptr);
62
83template<class DataType, class ... Properties>
84bool
86 (std::ostream* lclErrStrm,
87 const int myMpiProcessRank,
88 const Kokkos::View<DataType, Properties...>& view)
89{
90 using Teuchos::TypeNameTraits;
91 using std::endl;
92 using view_type = Kokkos::View<DataType, Properties...>;
93
94 if (view.size () == 0) {
95 // Kokkos::View can be zero size with a nonnull pointer.
96 // Even std::vector can have this behavior.
97 return true;
98 }
99 else { // nonzero size
100 auto ptr = view.data ();
101
102 if (ptr == nullptr) {
103 if (lclErrStrm != nullptr) {
104 const std::string viewName = TypeNameTraits<view_type>::name ();
105 *lclErrStrm << "Proc " << myMpiProcessRank << ": Kokkos::View "
106 "of type " << viewName << " has nonzero size " << view.size ()
107 << " but a null pointer." << endl;
108 }
109 return false;
110 }
111 else
112 return true;
113 }
114}
115
119template<class DataType ,
120 class Arg1Type = void ,
121 class Arg2Type = void ,
122 class Arg3Type = void>
123bool
125 (std::ostream* const lclErrStrm,
126 const int myMpiProcessRank,
127 const Kokkos::DualView<DataType, Arg1Type, Arg2Type, Arg3Type>& dv)
128{
129 const bool dev_good =
131 dv.view_device ());
132 const bool host_good =
134 dv.view_host ());
135 const bool good = dev_good && host_good;
136 if (! good && lclErrStrm != nullptr) {
137 using Teuchos::TypeNameTraits;
138 using std::endl;
139 using dv_type =
140 Kokkos::DualView<DataType, Arg1Type, Arg2Type, Arg3Type>;
141
142 const std::string dvName = TypeNameTraits<dv_type>::name ();
143 *lclErrStrm << "Proc " << myMpiProcessRank << ": Kokkos::DualView "
144 "of type " << dvName << " has one or more invalid Views. See "
145 "above error messages from this MPI process for details." << endl;
146 }
147 return good;
148}
149
150template<class DataType ,
151 class Arg1Type = void ,
152 class Arg2Type = void ,
153 class Arg3Type = void>
154bool
155checkGlobalDualViewValidity
156(std::ostream* const gblErrStrm,
157 const Kokkos::DualView<DataType, Arg1Type, Arg2Type, Arg3Type>& dv,
158 const bool verbose,
159 const Teuchos::Comm<int>* const comm)
160{
161 using std::endl;
162 const int myRank = comm == nullptr ? 0 : comm->getRank ();
163 std::ostringstream lclErrStrm;
164 int lclSuccess = 1;
165
166 try {
167 const bool lclValid =
169 lclSuccess = lclValid ? 1 : 0;
170 }
171 catch (std::exception& e) {
172 lclErrStrm << "Proc " << myRank << ": checkLocalDualViewValidity "
173 "threw an exception: " << e.what () << endl;
174 lclSuccess = 0;
175 }
176 catch (...) {
177 lclErrStrm << "Proc " << myRank << ": checkLocalDualViewValidity "
178 "threw an exception not a subclass of std::exception." << endl;
179 lclSuccess = 0;
180 }
181
182 int gblSuccess = 0; // output argument
183 if (comm == nullptr) {
184 gblSuccess = lclSuccess;
185 }
186 else {
187 using Teuchos::outArg;
188 using Teuchos::REDUCE_MIN;
189 using Teuchos::reduceAll;
190 reduceAll (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
191 }
192
193 if (gblSuccess != 1 && gblErrStrm != nullptr) {
194 *gblErrStrm << "On at least one (MPI) process, the "
195 "Kokkos::DualView has "
196 "either the device or host pointer in the "
197 "DualView equal to null, but the DualView has a nonzero number of "
198 "rows. For more detailed information, please rerun with the "
199 "TPETRA_VERBOSE environment variable set to 1. ";
200 if (verbose) {
201 *gblErrStrm << " Here are error messages from all "
202 "processes:" << endl;
203 if (comm == nullptr) {
204 *gblErrStrm << lclErrStrm.str ();
205 }
206 else {
208 gathervPrint (*gblErrStrm, lclErrStrm.str (), *comm);
209 }
210 }
211 *gblErrStrm << endl;
212 }
213 return gblSuccess == 1;
214}
215
216
220template<class DataType ,
221 class Arg1Type = void ,
222 class Arg2Type = void ,
223 class Arg3Type = void>
224bool
226 (std::ostream* const lclErrStrm,
227 const int myMpiProcessRank,
228 const Tpetra::Details::WrappedDualView<Kokkos::DualView<DataType, Arg1Type, Arg2Type, Arg3Type> >& dv)
229{
230 const bool dev_good = dv.is_valid_device();
231 const bool host_good = dv. is_valid_host();
232 const bool good = dev_good && host_good;
233 if (! good && lclErrStrm != nullptr) {
234 using Teuchos::TypeNameTraits;
235 using std::endl;
236 using dv_type =
237 Tpetra::Details::WrappedDualView<Kokkos::DualView<DataType, Arg1Type, Arg2Type, Arg3Type> >;
238
239 const std::string dvName = TypeNameTraits<dv_type>::name ();
240 *lclErrStrm << "Proc " << myMpiProcessRank << ": Tpetra::WrappedDualView "
241 "of type " << dvName << " has one or more invalid Views. See "
242 "above error messages from this MPI process for details." << endl;
243 }
244 return good;
245}
246
247template<class DataType ,
248 class Arg1Type = void ,
249 class Arg2Type = void ,
250 class Arg3Type = void>
251bool
252checkGlobalWrappedDualViewValidity
253(std::ostream* const gblErrStrm,
254 const Tpetra::Details::WrappedDualView<Kokkos::DualView<DataType, Arg1Type, Arg2Type, Arg3Type> >& dv,
255 const bool verbose,
256 const Teuchos::Comm<int>* const comm)
257{
258 using std::endl;
259 const int myRank = comm == nullptr ? 0 : comm->getRank ();
260 std::ostringstream lclErrStrm;
261 int lclSuccess = 1;
262
263 try {
264 const bool lclValid =
266 lclSuccess = lclValid ? 1 : 0;
267 }
268 catch (std::exception& e) {
269 lclErrStrm << "Proc " << myRank << ": checkLocalDualViewValidity "
270 "threw an exception: " << e.what () << endl;
271 lclSuccess = 0;
272 }
273 catch (...) {
274 lclErrStrm << "Proc " << myRank << ": checkLocalDualViewValidity "
275 "threw an exception not a subclass of std::exception." << endl;
276 lclSuccess = 0;
277 }
278
279 int gblSuccess = 0; // output argument
280 if (comm == nullptr) {
281 gblSuccess = lclSuccess;
282 }
283 else {
284 using Teuchos::outArg;
285 using Teuchos::REDUCE_MIN;
286 using Teuchos::reduceAll;
287 reduceAll (*comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
288 }
289
290 if (gblSuccess != 1 && gblErrStrm != nullptr) {
291 *gblErrStrm << "On at least one (MPI) process, the "
292 "Kokkos::DualView has "
293 "either the device or host pointer in the "
294 "DualView equal to null, but the DualView has a nonzero number of "
295 "rows. For more detailed information, please rerun with the "
296 "TPETRA_VERBOSE environment variable set to 1. ";
297 if (verbose) {
298 *gblErrStrm << " Here are error messages from all "
299 "processes:" << endl;
300 if (comm == nullptr) {
301 *gblErrStrm << lclErrStrm.str ();
302 }
303 else {
305 gathervPrint (*gblErrStrm, lclErrStrm.str (), *comm);
306 }
307 }
308 *gblErrStrm << endl;
309 }
310 return gblSuccess == 1;
311}
312
313
314} // namespace Details
315} // namespace Tpetra
316
317#endif // TPETRA_DETAILS_CHECKVIEW_HPP
Declaration of a function that prints strings from each process.
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
bool checkLocalDualViewValidity(std::ostream *const lclErrStrm, const int myMpiProcessRank, const Kokkos::DualView< DataType, Arg1Type, Arg2Type, Arg3Type > &dv)
Is the given Kokkos::DualView valid?
bool checkLocalWrappedDualViewValidity(std::ostream *const lclErrStrm, const int myMpiProcessRank, const Tpetra::Details::WrappedDualView< Kokkos::DualView< DataType, Arg1Type, Arg2Type, Arg3Type > > &dv)
Is the given Tpetra::WrappedDualView valid?
bool checkLocalViewValidity(std::ostream *lclErrStrm, const int myMpiProcessRank, const Kokkos::View< DataType, Properties... > &view)
Is the given View valid?
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator,...
Namespace Tpetra contains the class and methods constituting the Tpetra library.