Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Tpetra_Details_assumeMpiIsCudaAware.cpp
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 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #include "TpetraCore_config.h"
43 #include "Tpetra_Details_assumeMpiIsCudaAware.hpp"
44 #include <string>
45 #include <cctype> // toupper
46 #include <cstdlib> // getenv
47 
48 namespace Tpetra {
49 namespace Details {
50 
51 bool
52 assumeMpiIsCudaAware (std::ostream* out)
53 {
54  using std::endl;
55 #ifdef TPETRA_ASSUME_CUDA_AWARE_MPI
56  const bool configureTimeDefault = true;
57 #else
58  const bool configureTimeDefault = false;
59 #endif // TPETRA_ASSUME_CUDA_AWARE_MPI
60  const char envVarName[] = "TPETRA_ASSUME_CUDA_AWARE_MPI";
61 
62  if (out != NULL) {
63  *out << "Test whether users want Trilinos to assume that its MPI "
64  "implementation is CUDA aware." << endl;
65  }
66 
67  // Environment variable overrides default configure-time value.
68  const char* envVarVal = std::getenv (envVarName);
69  if (envVarVal == NULL) { // use configure-time default value
70  if (out != NULL) {
71  *out << " - Did not find environment variable \"" << envVarName
72  << "\"." << endl
73  << " - Return configure-time default: "
74  << (configureTimeDefault ? "true" : "false") << "." << endl;
75  }
76  return configureTimeDefault;
77  }
78  else {
79  if (out != NULL) {
80  *out << " - Found environment variable \"" << envVarName << "\"."
81  << endl << " - Its value is \"" << envVarVal << "\"." << endl;
82  }
83  const std::string varVal ([=] { // compare to Lisp' LET (yay const!)
84  std::string varVal_nc (envVarVal);
85  for (auto& c : varVal_nc) {
86  c = toupper (c);
87  }
88  return varVal_nc;
89  } ());
90  // We include "" with the "true" values rather than the "false"
91  // values, since just setting a Boolean environment variable
92  // without setting its value suggests a true value. Note that
93  // this requires actually setting the value to "". For example,
94  // in bash, one must do this:
95  //
96  // export TPETRA_ASSUME_CUDA_AWARE_MPI=""
97  //
98  // not this:
99  //
100  // export TPETRA_ASSUME_CUDA_AWARE_MPI
101  //
102  // If you do the latter, getenv will return NULL.
103  const char* falseVals[] = {"0", "NO", "OFF", "FALSE"};
104  for (auto falseVal : falseVals) {
105  if (varVal == falseVal) {
106  if (out != NULL) {
107  *out << " - Report value of environment variable as \"false\"."
108  << endl;
109  }
110  return false;
111  }
112  }
113  const char* trueVals[] = {"", "1", "YES", "ON", "TRUE"};
114  for (auto trueVal : trueVals) {
115  if (varVal == trueVal) {
116  if (out != NULL) {
117  *out << " - Report value of environment variable as \"true\"."
118  << endl;
119  }
120  return true;
121  }
122  }
123 
124  if (out != NULL) {
125  *out << " - Found environment variable, but didn't know how to "
126  << "interpret its value." << endl
127  << " - Return configure-time default: "
128  << (configureTimeDefault ? "true" : "false") << endl;
129  }
130  return configureTimeDefault;
131  }
132 }
133 
134 } // namespace Details
135 } // namespace Tpetra
bool assumeMpiIsCudaAware(std::ostream *out)
Whether to assume that MPI is CUDA aware.