Debian Codegraph @

src/atlas/linalg/sparse/Backend.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "Backend.h"
12
13 #include <map>
14
15 #include "atlas/library/config.h"
16 #if ATLAS_ECKIT_HAVE_ECKIT_585
17 #include "eckit/linalg/LinearAlgebraSparse.h"
18 #else
19 #include "eckit/linalg/LinearAlgebra.h"
20 #endif
21 #include "eckit/utils/Tokenizer.h"
22
23 #include "atlas/library.h"
24 #include "atlas/runtime/Exception.h"
25 #include "atlas/util/Config.h"
26
27 namespace atlas {
28 namespace linalg {
29 namespace sparse {
30
31 namespace {
32
33
34 struct backends {
35 std::map<std::string, sparse::Backend> map_;
36 std::string current_backend_;
37
38 static backends& instance() {
39 static backends x;
40 return x;
41 }
42
43 void set(const std::string& current_backend) { current_backend_ = current_backend; }
44
45 sparse::Backend& get(const std::string& type) {
46 if (map_.find(type) == map_.end()) {
47 std::vector<std::string> tokens;
48 eckit::Tokenizer{'.'}(type, tokens);
49 ATLAS_ASSERT(tokens.size() <= 2);
50 if (tokens.size() == 1) {
51 map_.emplace(type, util::Config("type", type));
52 }
53 else {
54 util::Config b;
55 b.set("type", tokens[0]);
56 b.set("backend", tokens[1]);
57 map_.emplace(type, b);
58 }
59 }
60 return map_[type];
61 }
62
63 sparse::Backend& get() { return get(current_backend_); }
64
65 private:
66 backends() {
67 auto configured = atlas::Library::instance().linalgSparseBackend();
68 current_backend_ = configured.empty() ? backend::openmp::type() : configured;
69 }
70 };
71 } // namespace
72
73 void current_backend(const std::string& backend) {
74 backends::instance().set(backend);
75 }
76 sparse::Backend& current_backend() {
77 return backends::instance().get();
78 }
79
80 sparse::Backend& default_backend(const std::string& backend) {
81 return backends::instance().get(backend);
82 }
83
84 Backend::Backend(const std::string& type): util::Config() {
85 if (not type.empty()) {
86 set(default_backend(type));
87 }
88 else {
89 set(current_backend());
90 }
91 }
92
93 Backend::Backend(const eckit::Configuration& other): util::Config(other) {
94 ATLAS_ASSERT(has("type"));
95 }
96
97 bool Backend::available() const {
98 std::string t = type();
99 if (t == backend::openmp::type()) {
100 return true;
101 }
102 if (t == backend::hicsparse::type()) {
103 #if ATLAS_HAVE_GPU
104 return true;
105 #else
106 return false;
107 #endif
108 }
109 if (t == backend::eckit_linalg::type()) {
110 if (has("backend")) {
111 #if ATLAS_ECKIT_HAVE_ECKIT_585
112 return eckit::linalg::LinearAlgebraSparse::hasBackend(getString("backend"));
113 #else
114 return eckit::linalg::LinearAlgebra::hasBackend(getString("backend"));
115 #endif
116 }
117 return true;
118 }
119 #if ATLAS_ECKIT_HAVE_ECKIT_585
120 return eckit::linalg::LinearAlgebraSparse::hasBackend(t);
121 #else
122 return eckit::linalg::LinearAlgebra::hasBackend(t);
123 #endif
124 }
125
126
127 } // namespace sparse
128 } // namespace linalg
129 } // namespace atlas