Debian Codegraph @

src/atlas/linalg/dense/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/LinearAlgebraDense.h"
18 #else
19 #include "eckit/linalg/LinearAlgebra.h"
20 #endif
21
22 #include "eckit/utils/Tokenizer.h"
23
24 #include "atlas/library.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/util/Config.h"
27
28 #include "atlas/runtime/Log.h"
29
30 namespace atlas {
31 namespace linalg {
32 namespace dense {
33
34 namespace {
35
36 util::Config to_config(const std::string& type) {
37 util::Config b;
38 std::vector<std::string> tokens;
39 eckit::Tokenizer{'.'}(type, tokens);
40 ATLAS_ASSERT(tokens.size() <= 2);
41 ATLAS_ASSERT(tokens.size() > 0);
42 if (tokens.size() == 1) {
43 b.set("type", type);
44 }
45 else {
46 util::Config b;
47 b.set("type", tokens[0]);
48 b.set("backend", tokens[1]);
49 }
50 return b;
51 }
52
53 struct backends {
54 std::map<std::string, dense::Backend> map_;
55 std::string current_backend_;
56
57 static backends& instance() {
58 static backends x;
59 return x;
60 }
61
62 void set(const std::string& current_backend) { current_backend_ = current_backend; }
63
64 dense::Backend& get(const std::string& type) {
65 ATLAS_ASSERT(!type.empty());
66 if (map_.find(type) == map_.end()) {
67 map_.emplace(type, to_config(type));
68 }
69 return map_[type];
70 }
71
72 dense::Backend& current() { return get(current_backend_); }
73
74 private:
75 backends() {
76 auto configured = atlas::Library::instance().linalgDenseBackend();
77 if (configured.empty()) {
78 // Proposal for default backend would be "eckit_linalg":
79 // current_backend_ = configured.empty() ? backend::eckit_linalg::type() : configured;
80 //
81 // However to be identical in behaviour for TransLocal with atlas 0.26.0 and earlier before
82 // any other codes can be adapted in short notice:
83 #if ATLAS_ECKIT_HAVE_ECKIT_585
84 if (eckit::linalg::LinearAlgebraDense::hasBackend("mkl")) {
85 #else
86 if (eckit::linalg::LinearAlgebra::hasBackend("mkl")) {
87 #endif
88 current_backend_ = "mkl";
89 }
90 else {
91 current_backend_ = backend::eckit_linalg::type();
92 }
93 map_.emplace("default", util::Config("type", current_backend_));
94 }
95 else {
96 current_backend_ = configured;
97 map_.emplace("default", to_config(configured));
98 }
99 }
100 };
101 } // namespace
102
103 void current_backend(const std::string& backend) {
104 backends::instance().set(backend);
105 }
106 dense::Backend& current_backend() {
107 return backends::instance().current();
108 }
109
110 dense::Backend& default_backend(const std::string& backend) {
111 return backends::instance().get(backend);
112 }
113
114 Backend::Backend(const std::string type): util::Config() {
115 if (not type.empty()) {
116 set(default_backend(type));
117 }
118 else {
119 set(current_backend());
120 }
121 }
122
123 Backend::Backend(const eckit::Configuration& other): util::Config(other) {
124 ATLAS_ASSERT(has("type"));
125 }
126
127 Backend::operator std::string() const {
128 return type();
129 }
130
131 bool Backend::available() const {
132 std::string t = type();
133 if (t == backend::eckit_linalg::type()) {
134 if (has("backend")) {
135 #if ATLAS_ECKIT_HAVE_ECKIT_585
136 return eckit::linalg::LinearAlgebraDense::hasBackend(getString("backend"));
137 #else
138 return eckit::linalg::LinearAlgebra::hasBackend(getString("backend"));
139 #endif
140 }
141 return true;
142 }
143 #if ATLAS_ECKIT_HAVE_ECKIT_585
144 return eckit::linalg::LinearAlgebraDense::hasBackend(t);
145 #else
146 return eckit::linalg::LinearAlgebra::hasBackend(t);
147 #endif
148 }
149
150 } // namespace dense
151 } // namespace linalg
152 } // namespace atlas