Cypress  1.0
C++ Spiking Neural Network Simulation Framework
genn_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 GENN_Lib {
46 private:
47  typedef Backend *create_bs(Json setup);
48  typedef void destroy_bs(Backend *bck);
49  void *m_lib;
50  GENN_Lib()
51  {
52  m_lib = dlopen("./libgennbck.so", RTLD_LAZY);
53  if (m_lib == NULL) {
54  m_lib = dlopen("libgennbck.so",
55  RTLD_LAZY); // if used in subproject
56  if (m_lib == NULL) {
57  m_lib = dlopen(GENN_LIBRARY_PATH,
58  RTLD_LAZY); // in LD_LIBRARY_PATH
59  if (m_lib == NULL) {
60  throw std::runtime_error(
61  "Error loading GeNN backend: " +
62  std::string(dlerror()) +
63  "\nMake sure that "
64  "libgennbck lib is available either in this folder or "
65  "in LD_LIBRARY_PATH!");
66  }
67  global_logger().debug("cypress", "Using " GENN_LIBRARY_PATH);
68  }
69  else {
71  "cypress",
72  "Using libgennbck.so, might include LD_LIBRARY_PATH");
73  }
74  }
75  else {
76  global_logger().debug("cypress", "Using ./libgennbck.so");
77  }
78  };
79 
80  ~GENN_Lib() { dlclose(m_lib); }
81 
82 public:
83  GENN_Lib(GENN_Lib const &) = delete;
84  void operator=(GENN_Lib const &) = delete;
85 
92  static GENN_Lib &instance()
93  {
94  static GENN_Lib instance;
95  return instance;
96  }
97 
108  {
109  create_bs *mkr = (create_bs *)dlsym(m_lib, "make_genn_backend");
110  return mkr(setup);
111  }
112 };
113 } // namespace cypress
Logger & global_logger()
Definition: backend.hpp:50
Singleton class for runtime loading of the GeNN Backend, which wraps all GeNN/GPU libraries...
Definition: genn_lib.hpp:45
nlohmann::json Json
Definition: json.hpp:27
static GENN_Lib & instance()
Creates a Singleton instance. Only at first time calling this method the library will be loaded...
Definition: genn_lib.hpp:92
void debug(const std::string &module, const std::string &message)
void operator=(GENN_Lib const &)=delete
Definition: brainscales_lib.hpp:39
Backend * create_genn_backend(Json &setup)
Creates a pointer to a GeNN Backend. This can be executed several times, as it only creates a Backend...
Definition: genn_lib.hpp:107