Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "Kokkos_Core.hpp"
13 #include "Tpetra_Details_checkLaunchBlocking.hpp"
15 #include <cstdlib> // std::atexit
16 #include <string>
17 #include <vector>
18 
19 namespace Tpetra {
20 namespace Details {
21 
22 void finalizeKokkosIfNeeded() {
23  if (!Kokkos::is_finalized()) {
24  Kokkos::finalize();
25  }
26 }
27 
29  if (!Kokkos::is_initialized()) {
30  std::vector<std::string> args = Teuchos::GlobalMPISession::getArgv();
31  int narg = static_cast<int>(args.size()); // must be nonconst
32 
33  std::vector<char*> args_c;
34  std::vector<std::unique_ptr<char[]>> args_;
35  for (auto const& x : args) {
36  args_.emplace_back(new char[x.size() + 1]);
37  char* ptr = args_.back().get();
38  strcpy(ptr, x.c_str());
39  args_c.push_back(ptr);
40  }
41  args_c.push_back(nullptr);
42 
43  Kokkos::initialize(narg, narg == 0 ? nullptr : args_c.data());
44  checkOldCudaLaunchBlocking();
45 
46  std::atexit(finalizeKokkosIfNeeded);
47  }
48  // Add Kokkos calls to the TimeMonitor if the environment says so
49  Tpetra::Details::AddKokkosDeepCopyToTimeMonitor();
50  Tpetra::Details::AddKokkosFenceToTimeMonitor();
51  Tpetra::Details::AddKokkosFunctionsToTimeMonitor();
52 }
53 
54 } // namespace Details
55 } // namespace Tpetra
Declaration functions that use Kokkos&#39; profiling library to add deep copies between memory spaces...
Declaration of Tpetra::Details::initializeKokkos.
void initializeKokkos()
Initialize Kokkos, using command-line arguments (if any) given to Teuchos::GlobalMPISession.