Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_initializeKokkos.cpp
1// @HEADER
2// *****************************************************************************
3// Tpetra: Templated Linear Algebra Services Package
4//
5// Copyright 2008 NTESS and the Tpetra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
11#include "Teuchos_GlobalMPISession.hpp"
12#include "Teuchos_oblackholestream.hpp"
13#include "Kokkos_Core.hpp"
14#include "Tpetra_Details_checkLaunchBlocking.hpp"
16#include <cstdlib> // std::atexit
17#include <string>
18#include <vector>
19#include "KokkosKernels_EagerInitialize.hpp"
20
21namespace Tpetra {
22namespace Details {
23
24class HideOutputExceptOnProcess0 {
25 public:
26 HideOutputExceptOnProcess0(std::ostream& stream,
27 const int myRank)
28 : stream_(stream)
29 , originalBuffer_(stream.rdbuf()) {
30 if (myRank != 0) {
31 stream.rdbuf(blackHole_.rdbuf());
32 }
33 }
34
35 ~HideOutputExceptOnProcess0() {
36 stream_.rdbuf(originalBuffer_);
37 }
38
39 private:
40 std::ostream& stream_;
41 decltype(std::cout.rdbuf()) originalBuffer_;
42 Teuchos::oblackholestream blackHole_;
43};
44
45void finalizeKokkosIfNeeded() {
46 if (!Kokkos::is_finalized()) {
47 Kokkos::finalize();
48 }
49}
50
51void initializeKokkos(int* argc, char*** argv, int myRank) {
52 if (!Kokkos::is_initialized()) {
53 HideOutputExceptOnProcess0 hideCerr(std::cerr, myRank);
54 HideOutputExceptOnProcess0 hideCout(std::cout, myRank);
55
56 if (!argc) {
57 // If there are no args, we try to find them via Teuchos
58 std::vector<std::string> args = Teuchos::GlobalMPISession::getArgv();
59 int narg = static_cast<int>(args.size()); // must be nonconst
60
61 std::vector<char*> args_c;
62 std::vector<std::unique_ptr<char[]>> args_;
63 for (auto const& x : args) {
64 args_.emplace_back(new char[x.size() + 1]);
65 char* ptr = args_.back().get();
66 strcpy(ptr, x.c_str());
67 args_c.push_back(ptr);
68 }
69 args_c.push_back(nullptr);
70
71 Kokkos::initialize(narg, narg == 0 ? nullptr : args_c.data());
72 } else {
73 // If there are args, we use them
74 Kokkos::initialize(*argc, *argv);
75 }
76
77 checkOldCudaLaunchBlocking();
78
79 std::atexit(finalizeKokkosIfNeeded);
80 }
81 // Add Kokkos calls to the TimeMonitor if the environment says so
82 Tpetra::Details::AddKokkosDeepCopyToTimeMonitor();
83 Tpetra::Details::AddKokkosFenceToTimeMonitor();
84 Tpetra::Details::AddKokkosFunctionsToTimeMonitor();
85
86 // Now that the Kokkos backend(s) are initialized,
87 // initialize all KokkosKernels TPLs.
88 KokkosKernels::eager_initialize();
89}
90
91} // namespace Details
92} // namespace Tpetra
Declaration functions that use Kokkos' profiling library to add deep copies between memory spaces,...
Declaration of Tpetra::Details::initializeKokkos.
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
void initializeKokkos(int *argc, char ***argv, int myRank)
Initialize Kokkos, using command-line arguments (if any) given to Teuchos::GlobalMPISession.
Namespace Tpetra contains the class and methods constituting the Tpetra library.