GeNN  3.3.0
GPU enhanced Neuronal Networks (GeNN)
codeGenUtils.h File Reference
#include <iomanip>
#include <limits>
#include <string>
#include <sstream>
#include <vector>
#include "variableMode.h"

Go to the source code of this file.

Classes

struct  GenericFunction
 
struct  FunctionTemplate
 
class  PairKeyConstIter< BaseIter >
 Custom iterator for iterating through the keys of containers containing pairs. More...
 

Namespaces

 NeuronModels
 
 NewModels
 

Functions

template<typename BaseIter >
PairKeyConstIter< BaseIter > GetPairKeyConstIter (BaseIter iter)
 Helper function for creating a PairKeyConstIter from an iterator. More...
 
void substitute (string &s, const string &trg, const string &rep)
 Tool for substituting strings in the neuron code strings or other templates. More...
 
bool regexVarSubstitute (string &s, const string &trg, const string &rep)
 Tool for substituting variable names in the neuron code strings or other templates using regular expressions. More...
 
bool regexFuncSubstitute (string &s, const string &trg, const string &rep)
 Tool for substituting function names in the neuron code strings or other templates using regular expressions. More...
 
bool isRNGRequired (const std::string &code)
 Does the code string contain any functions requiring random number generator. More...
 
bool isInitRNGRequired (const std::vector< NewModels::VarInit > &varInitialisers, const std::vector< VarMode > &varModes, VarInit initLocation)
 Does the model with the vectors of variable initialisers and modes require an RNG for the specified init location i.e. host or device. More...
 
void functionSubstitute (std::string &code, const std::string &funcName, unsigned int numParams, const std::string &replaceFuncTemplate)
 This function substitutes function calls in the form: More...
 
template<typename NameIter >
void name_substitutions (string &code, const string &prefix, NameIter namesBegin, NameIter namesEnd, const string &postfix="", const string &ext="")
 This function performs a list of name substitutions for variables in code snippets. More...
 
void name_substitutions (string &code, const string &prefix, const vector< string > &names, const string &postfix="", const string &ext="")
 This function performs a list of name substitutions for variables in code snippets. More...
 
template<class T , typename std::enable_if< std::is_floating_point< T >::value >::type * = nullptr>
void writePreciseString (std::ostream &os, T value)
 This function writes a floating point value to a stream -setting the precision so no digits are lost. More...
 
template<class T , typename std::enable_if< std::is_floating_point< T >::value >::type * = nullptr>
std::string writePreciseString (T value)
 This function writes a floating point value to a string - setting the precision so no digits are lost. More...
 
template<typename NameIter >
void value_substitutions (string &code, NameIter namesBegin, NameIter namesEnd, const vector< double > &values, const string &ext="")
 This function performs a list of value substitutions for parameters in code snippets. More...
 
void value_substitutions (string &code, const vector< string > &names, const vector< double > &values, const string &ext="")
 This function performs a list of value substitutions for parameters in code snippets. More...
 
void functionSubstitutions (std::string &code, const std::string &ftype, const std::vector< FunctionTemplate > functions)
 This function performs a list of function substitutions in code snipped. More...
 
string ensureFtype (const string &oldcode, const string &type)
 This function implements a parser that converts any floating point constant in a code snippet to a floating point constant with an explicit precision (by appending "f" or removing it). More...
 
void checkUnreplacedVariables (const string &code, const string &codeName)
 This function checks for unknown variable definitions and returns a gennError if any are found. More...
 
uint32_t hashString (const std::string &string)
 This function returns the 32-bit hash of a string - because these are used across MPI nodes which may have different libstdc++ it would be risky to use std::hash. More...
 
void preNeuronSubstitutionsInSynapticCode (string &wCode, const SynapseGroup *sg, const string &offset, const string &axonalDelayOffset, const string &postIdx, const string &devPrefix, const string &preVarPrefix="", const string &preVarSuffix="")
 suffix to be used for presynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX) More...
 
void postNeuronSubstitutionsInSynapticCode (string &wCode, const SynapseGroup *sg, const string &offset, const string &backPropDelayOffset, const string &preIdx, const string &devPrefix, const string &postVarPrefix="", const string &postVarSuffix="")
 suffix to be used for postsynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX) More...
 
void neuron_substitutions_in_synaptic_code (string &wCode, const SynapseGroup *sg, const string &preIdx, const string &postIdx, const string &devPrefix, double dt, const string &preVarPrefix="", const string &preVarSuffix="", const string &postVarPrefix="", const string &postVarSuffix="")
 Function for performing the code and value substitutions necessary to insert neuron related variables, parameters, and extraGlobal parameters into synaptic code. More...
 

Variables

const std::vector< FunctionTemplatecudaFunctions
 CUDA implementations of standard functions. More...
 
const std::vector< FunctionTemplatecpuFunctions
 CPU implementations of standard functions. More...
 

Function Documentation

◆ checkUnreplacedVariables()

void checkUnreplacedVariables ( const string &  code,
const string &  codeName 
)

This function checks for unknown variable definitions and returns a gennError if any are found.

◆ ensureFtype()

string ensureFtype ( const string &  oldcode,
const string &  type 
)

This function implements a parser that converts any floating point constant in a code snippet to a floating point constant with an explicit precision (by appending "f" or removing it).

◆ functionSubstitute()

void functionSubstitute ( std::string &  code,
const std::string &  funcName,
unsigned int  numParams,
const std::string &  replaceFuncTemplate 
)

This function substitutes function calls in the form:

$(functionName, parameter1, param2Function(0.12, "string"))

with replacement templates in the form:

actualFunction(CONSTANT, $(0), $(1))

◆ functionSubstitutions()

void functionSubstitutions ( std::string &  code,
const std::string &  ftype,
const std::vector< FunctionTemplate functions 
)

This function performs a list of function substitutions in code snipped.

◆ GetPairKeyConstIter()

template<typename BaseIter >
PairKeyConstIter<BaseIter> GetPairKeyConstIter ( BaseIter  iter)
inline

Helper function for creating a PairKeyConstIter from an iterator.

◆ hashString()

uint32_t hashString ( const std::string &  string)

This function returns the 32-bit hash of a string - because these are used across MPI nodes which may have different libstdc++ it would be risky to use std::hash.

https://stackoverflow.com/questions/19411742/what-is-the-default-hash-function-used-in-c-stdunordered-map suggests that libstdc++ uses MurmurHash2 so this seems as good a bet as any MurmurHash2, by Austin Appleby It has a few limitations -

  1. It will not work incrementally.
  2. It will not produce the same results on little-endian and big-endian machines.

◆ isInitRNGRequired()

bool isInitRNGRequired ( const std::vector< NewModels::VarInit > &  varInitialisers,
const std::vector< VarMode > &  varModes,
VarInit  initLocation 
)

Does the model with the vectors of variable initialisers and modes require an RNG for the specified init location i.e. host or device.

Does the model with the vectors of variable initialisers and modes require an RNG for the specified init location i.e. host or device.

◆ isRNGRequired()

bool isRNGRequired ( const std::string &  code)

Does the code string contain any functions requiring random number generator.

◆ name_substitutions() [1/2]

template<typename NameIter >
void name_substitutions ( string &  code,
const string &  prefix,
NameIter  namesBegin,
NameIter  namesEnd,
const string &  postfix = "",
const string &  ext = "" 
)
inline

This function performs a list of name substitutions for variables in code snippets.

◆ name_substitutions() [2/2]

void name_substitutions ( string &  code,
const string &  prefix,
const vector< string > &  names,
const string &  postfix = "",
const string &  ext = "" 
)
inline

This function performs a list of name substitutions for variables in code snippets.

◆ neuron_substitutions_in_synaptic_code()

void neuron_substitutions_in_synaptic_code ( string &  wCode,
const SynapseGroup sg,
const string &  preIdx,
const string &  postIdx,
const string &  devPrefix,
double  dt,
const string &  preVarPrefix = "",
const string &  preVarSuffix = "",
const string &  postVarPrefix = "",
const string &  postVarSuffix = "" 
)

Function for performing the code and value substitutions necessary to insert neuron related variables, parameters, and extraGlobal parameters into synaptic code.

suffix to be used for postsynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

Parameters
wCodethe code string to work on
sgthe synapse group connecting the pre and postsynaptic neuron populations whose parameters might need to be substituted
preIdxindex of the pre-synaptic neuron to be accessed for _pre variables; differs for different Span)
postIdxindex of the post-synaptic neuron to be accessed for _post variables; differs for different Span)
devPrefixdevice prefix, "dd_" for GPU, nothing for CPU
dtsimulation timestep (ms)
preVarPrefixprefix to be used for presynaptic variable accesses - typically combined with suffix to wrap in function call such as __ldg(&XXX)
preVarSuffixsuffix to be used for presynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)
postVarPrefixprefix to be used for postsynaptic variable accesses - typically combined with suffix to wrap in function call such as __ldg(&XXX)
postVarSuffixsuffix to be used for postsynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

◆ postNeuronSubstitutionsInSynapticCode()

void postNeuronSubstitutionsInSynapticCode ( string &  wCode,
const SynapseGroup sg,
const string &  offset,
const string &  backPropDelayOffset,
const string &  preIdx,
const string &  devPrefix,
const string &  postVarPrefix = "",
const string &  postVarSuffix = "" 
)

suffix to be used for postsynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

Parameters
wCodethe code string to work on
devPrefixdevice prefix, "dd_" for GPU, nothing for CPU
postVarPrefixprefix to be used for postsynaptic variable accesses - typically combined with suffix to wrap in function call such as __ldg(&XXX)
postVarSuffixsuffix to be used for postsynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

◆ preNeuronSubstitutionsInSynapticCode()

void preNeuronSubstitutionsInSynapticCode ( string &  wCode,
const SynapseGroup sg,
const string &  offset,
const string &  axonalDelayOffset,
const string &  preIdx,
const string &  devPrefix,
const string &  preVarPrefix,
const string &  preVarSuffix 
)

suffix to be used for presynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

suffix to be used for presynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

Parameters
wCodethe code string to work on
devPrefixdevice prefix, "dd_" for GPU, nothing for CPU
preVarPrefixprefix to be used for presynaptic variable accesses - typically combined with suffix to wrap in function call such as __ldg(&XXX)
preVarSuffixsuffix to be used for presynaptic variable accesses - typically combined with prefix to wrap in function call such as __ldg(&XXX)

◆ regexFuncSubstitute()

bool regexFuncSubstitute ( string &  s,
const string &  trg,
const string &  rep 
)

Tool for substituting function names in the neuron code strings or other templates using regular expressions.

◆ regexVarSubstitute()

bool regexVarSubstitute ( string &  s,
const string &  trg,
const string &  rep 
)

Tool for substituting variable names in the neuron code strings or other templates using regular expressions.

◆ substitute()

void substitute ( string &  s,
const string &  trg,
const string &  rep 
)

Tool for substituting strings in the neuron code strings or other templates.

◆ value_substitutions() [1/2]

template<typename NameIter >
void value_substitutions ( string &  code,
NameIter  namesBegin,
NameIter  namesEnd,
const vector< double > &  values,
const string &  ext = "" 
)
inline

This function performs a list of value substitutions for parameters in code snippets.

◆ value_substitutions() [2/2]

void value_substitutions ( string &  code,
const vector< string > &  names,
const vector< double > &  values,
const string &  ext = "" 
)
inline

This function performs a list of value substitutions for parameters in code snippets.

◆ writePreciseString() [1/2]

template<class T , typename std::enable_if< std::is_floating_point< T >::value >::type * = nullptr>
void writePreciseString ( std::ostream &  os,
value 
)

This function writes a floating point value to a stream -setting the precision so no digits are lost.

◆ writePreciseString() [2/2]

template<class T , typename std::enable_if< std::is_floating_point< T >::value >::type * = nullptr>
std::string writePreciseString ( value)

This function writes a floating point value to a string - setting the precision so no digits are lost.

Variable Documentation

◆ cpuFunctions

const std::vector<FunctionTemplate> cpuFunctions
Initial value:
= {
{"gennrand_uniform", 0, "standardUniformDistribution($(rng))", "standardUniformDistribution($(rng))"},
{"gennrand_normal", 0, "standardNormalDistribution($(rng))", "standardNormalDistribution($(rng))"},
{"gennrand_exponential", 0, "standardExponentialDistribution($(rng))", "standardExponentialDistribution($(rng))"},
{"gennrand_log_normal", 2, "std::lognormal_distribution<double>($(0), $(1))($(rng))", "std::lognormal_distribution<float>($(0), $(1))($(rng))"},
{"gennrand_gamma", 1, "std::gamma_distribution<double>($(0), 1.0)($(rng))", "std::gamma_distribution<float>($(0), 1.0f)($(rng))"}
}

CPU implementations of standard functions.

◆ cudaFunctions

const std::vector<FunctionTemplate> cudaFunctions
Initial value:
= {
{"gennrand_uniform", 0, "curand_uniform_double($(rng))", "curand_uniform($(rng))"},
{"gennrand_normal", 0, "curand_normal_double($(rng))", "curand_normal($(rng))"},
{"gennrand_exponential", 0, "exponentialDistDouble($(rng))", "exponentialDistFloat($(rng))"},
{"gennrand_log_normal", 2, "curand_log_normal_double($(rng), $(0), $(1))", "curand_log_normal_float($(rng), $(0), $(1))"},
{"gennrand_gamma", 1, "gammaDistDouble($(rng), $(0))", "gammaDistFloat($(rng), $(0))"}
}

CUDA implementations of standard functions.