Cypress  1.0
C++ Spiking Neural Network Simulation Framework
brainscales_lib.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2019 Christoph Ostrau
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
27 #pragma once
28 
29 #include <stdexcept>
30 #include <string>
31 #include <vector>
32 
33 #include <dlfcn.h>
34 
35 #include <cypress/core/backend.hpp>
36 #include <cypress/util/json.hpp>
37 #include <cypress/util/logger.hpp>
38 
39 namespace cypress {
45 class BS_Lib {
46 private:
47  typedef Backend *create_bs(Json setup);
48  typedef void destroy_bs(Backend *bck);
49  void *m_lib;
50  BS_Lib()
51  {
52  m_lib = dlopen("./libBS2CYPRESS.so", RTLD_LAZY);
53  if (m_lib == NULL) {
55  "cypress",
56  "Error loading ./libBS2CYPRESS.so: " + std::string(dlerror()));
57  global_logger().debug("cypress",
58  "Installed bs lib will be used before "
59  "subproject executable");
60  m_lib = dlopen("libBS2CYPRESS.so",
61  RTLD_LAZY); // if used in subproject
62  if (m_lib == NULL) {
63  global_logger().debug("cypress",
64  "Error loading libBS2CYPRESS.so: " +
65  std::string(dlerror()));
66  m_lib = dlopen(BS_LIBRARY_PATH,
67  RTLD_LAZY); // in LD_LIBRARY_PATH
68  if (m_lib == NULL) {
69  throw std::runtime_error(
70  "Error loading BrainScaleS backend: " +
71  std::string(dlerror()) +
72  "\nMake sure that you are running on a BrainScaleS "
73  "server and "
74  "bs2cypress lib is available either in this folder or "
75  "in LD_LIBRARY_PATH!");
76  }
77  }
78  }
79  };
80 
81  ~BS_Lib() { dlclose(m_lib); }
82 
83 public:
84  BS_Lib(BS_Lib const &) = delete;
85  void operator=(BS_Lib const &) = delete;
86 
93  static BS_Lib &instance()
94  {
95  static BS_Lib instance;
96  return instance;
97  }
98 
109  {
110  create_bs *mkr = (create_bs *)dlsym(m_lib, "make_brainscales_backend");
111  return mkr(setup);
112  }
113 };
114 } // namespace cypress
Logger & global_logger()
static BS_Lib & instance()
Creates a Singleton instance. Only at first time calling this method the library will be loaded...
Definition: brainscales_lib.hpp:93
Singleton class for runtime loading of the BrainScaleS Backend, which wraps all BrainScaleS libraries...
Definition: brainscales_lib.hpp:45
Definition: backend.hpp:50
nlohmann::json Json
Definition: json.hpp:27
Backend * create_bs_backend(Json &setup)
Creates a pointer to a BrainScaleS Backend. This can be executed several times, as it only creates a ...
Definition: brainscales_lib.hpp:108
void debug(const std::string &module, const std::string &message)
Definition: brainscales_lib.hpp:39
void operator=(BS_Lib const &)=delete