GeNN  4.9.0
GPU enhanced Neuronal Networks (GeNN)
gennUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Standard C++ includes
4 #include <array>
5 #include <iomanip>
6 #include <limits>
7 #include <sstream>
8 #include <string>
9 #include <type_traits>
10 #include <vector>
11 
12 // Standard C includes
13 #include <cstring>
14 
15 // Boost includes
16 #include <sha1.hpp>
17 
18 // GeNN includes
19 #include "gennExport.h"
20 
21 // Forward declarations
22 namespace Models
23 {
24 class VarInit;
25 }
26 
27 //--------------------------------------------------------------------------
28 // Utils
29 //--------------------------------------------------------------------------
30 namespace Utils
31 {
32 //--------------------------------------------------------------------------
34 //--------------------------------------------------------------------------
35 GENN_EXPORT bool isRNGRequired(const std::string &code);
36 
37 //--------------------------------------------------------------------------
39 //--------------------------------------------------------------------------
40 GENN_EXPORT bool isRNGRequired(const std::vector<Models::VarInit> &varInitialisers);
41 
42 //--------------------------------------------------------------------------
44 //--------------------------------------------------------------------------
45 GENN_EXPORT bool isTypePointer(const std::string &type);
46 
47 //--------------------------------------------------------------------------
49 //--------------------------------------------------------------------------
50 GENN_EXPORT bool isTypePointerToPointer(const std::string &type);
51 
52 //--------------------------------------------------------------------------
54 //--------------------------------------------------------------------------
55 GENN_EXPORT bool isTypeFloatingPoint(const std::string &type);
56 
57 //--------------------------------------------------------------------------
59 //--------------------------------------------------------------------------
60 GENN_EXPORT std::string getUnderlyingType(const std::string &type);
61 
62 //--------------------------------------------------------------------------
64 //--------------------------------------------------------------------------
65 GENN_EXPORT void validateVarName(const std::string &name, const std::string &description);
66 
67 //--------------------------------------------------------------------------
69 //--------------------------------------------------------------------------
70 GENN_EXPORT void validatePopName(const std::string &name, const std::string &description);
71 
72 //--------------------------------------------------------------------------
74 //--------------------------------------------------------------------------
75 GENN_EXPORT void validateParamNames(const std::vector<std::string> &paramNames);
76 
77 //--------------------------------------------------------------------------
79 //--------------------------------------------------------------------------
80 template<typename T>
81 void validateVecNames(const std::vector<T> &vec, const std::string &description)
82 {
83  for(const auto &v : vec) {
84  validateVarName(v.name, description);
85  }
86 }
87 
88 //--------------------------------------------------------------------------
90 //--------------------------------------------------------------------------
91 template<class T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
92 void writePreciseString(std::ostream &os, T value)
93 {
94  // Cache previous precision
95  const std::streamsize previousPrecision = os.precision();
96 
97  // Set scientific formatting
98  os << std::scientific;
99 
100  // Set precision to what is required to fully represent T
101  os << std::setprecision(std::numeric_limits<T>::max_digits10);
102 
103  // Write value to stream
104  os << value;
105 
106  // Reset to default formatting
107  // **YUCK** GCC 4.8.X doesn't seem to include std::defaultfloat
108  os.unsetf(std::ios_base::floatfield);
109  //os << std::defaultfloat;
110 
111  // Restore previous precision
112  os << std::setprecision(previousPrecision);
113 }
114 
115 //--------------------------------------------------------------------------
117 //--------------------------------------------------------------------------
118 template<class T, typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
119 inline std::string writePreciseString(T value)
120 {
121  std::stringstream s;
122  writePreciseString(s, value);
123  return s.str();
124 }
125 
127 template<typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value>::type* = nullptr>
128 inline void updateHash(const T& value, boost::uuids::detail::sha1& hash)
129 {
130  hash.process_bytes(&value, sizeof(T));
131 }
132 
134 inline void updateHash(const std::string &string, boost::uuids::detail::sha1 &hash)
135 {
136  updateHash(string.size(), hash);
137  hash.process_bytes(string.data(), string.size());
138 }
139 
141 template<typename T, size_t N>
142 inline void updateHash(const std::array<T, N> &array, boost::uuids::detail::sha1 &hash)
143 {
144  updateHash(array.size(), hash);
145  for(const auto &v : array) {
146  updateHash(v, hash);
147  }
148 }
149 
151 template<typename T>
152 inline void updateHash(const std::vector<T> &vector, boost::uuids::detail::sha1 &hash)
153 {
154  updateHash(vector.size(), hash);
155  for(const auto &v : vector) {
156  updateHash(v, hash);
157  }
158 }
159 
161 inline void updateHash(const std::vector<bool> &vector, boost::uuids::detail::sha1 &hash)
162 {
163  updateHash(vector.size(), hash);
164  for(bool v : vector) {
165  updateHash(v, hash);
166  }
167 }
168 
170 struct SHA1Hash
171 {
172  size_t operator()(const boost::uuids::detail::sha1::digest_type &digest) const
173  {
174  size_t hash;
175  memcpy(&hash, &digest[0], sizeof(size_t));
176  return hash;
177  };
178 };
179 } // namespace Utils
GENN_EXPORT bool isRNGRequired(const std::vector< Models::VarInit > &varInitialisers)
Does the model with the vectors of variable initialisers and modes require an RNG for the specified i...
Definition: gennUtils.cc:65
GENN_EXPORT void validateVarName(const std::string &name, const std::string &description)
Is the variable name valid? GeNN variable names must obey C variable naming rules.
Definition: gennUtils.cc:107
GENN_EXPORT bool isTypePointerToPointer(const std::string &type)
Function to determine whether a string containing a type is a pointer to a pointer.
Definition: gennUtils.cc:80
#define GENN_EXPORT
Definition: gennExport.h:13
size_t operator()(const boost::uuids::detail::sha1::digest_type &digest) const
Definition: gennUtils.h:172
GENN_EXPORT std::string getUnderlyingType(const std::string &type)
Assuming type is a string containing a pointer type, function to return the underlying type...
Definition: gennUtils.cc:92
Functor for generating a hash suitable for use in std::unordered_map etc (i.e. size_t size) from a SH...
Definition: gennUtils.h:170
GENN_EXPORT bool isTypePointer(const std::string &type)
Function to determine whether a string containing a type is a pointer.
Definition: gennUtils.cc:75
Definition: models.h:151
GENN_EXPORT bool isTypeFloatingPoint(const std::string &type)
Function to determine whether a string containing a type is floating point.
Definition: gennUtils.cc:86
std::string writePreciseString(T value)
This function writes a floating point value to a string - setting the precision so no digits are lost...
Definition: gennUtils.h:119
GENN_EXPORT void updateHash(const Base::Var &v, boost::uuids::detail::sha1 &hash)
Definition: models.cc:222
GENN_EXPORT void validatePopName(const std::string &name, const std::string &description)
Is the population name valid? GeNN population names obey C variable naming rules but can start with a...
Definition: gennUtils.cc:127
Base class for all models - in addition to the parameters snippets have, models can have state variab...
Definition: gennUtils.h:22
Definition: gennUtils.h:30
void validateVecNames(const std::vector< T > &vec, const std::string &description)
Are the &#39;name&#39; fields of all structs in vector valid? GeNN variables and population names must obey C...
Definition: gennUtils.h:81
GENN_EXPORT void validateParamNames(const std::vector< std::string > &paramNames)
Are all the parameter names in vector valid? GeNN variables and population names must obey C variable...
Definition: gennUtils.cc:142