GeNN  2.2.3
GPU enhanced Neuronal Networks (GeNN)
sparseUtils.h
Go to the documentation of this file.
1 
2 #ifndef SPARSE_UTILS_H
3 #define SPARSE_UTILS_H
4 
5 #include "sparseProjection.h"
6 #include "global.h"
7 
8 #include <cstdlib>
9 #include <cstdio>
10 #include <string>
11 #include <cmath>
12 
13 using namespace std;
14 
15 
16 //--------------------------------------------------------------------------
20 //--------------------------------------------------------------------------
21 
22 template <class DATATYPE>
23 unsigned int countEntriesAbove(DATATYPE *Array, int sz, double includeAbove)
24 {
25  int count = 0;
26  for (int i = 0; i < sz; i++) {
27  if (abs(Array[i]) > includeAbove) count++;
28  }
29  fprintf(stdout, "\nCounted %u nonzero entries\n\n", count);
30  return count;
31 
32 }
33 
34 
35 //--------------------------------------------------------------------------
41 //--------------------------------------------------------------------------
42 
43 template <class DATATYPE>
44 DATATYPE getG(DATATYPE *wuvar, SparseProjection *sparseStruct, int x, int y)
45 {
46  fprintf(stderr,"WARNING: This function is deprecated, and if you are still using it \n\
47  you are probably trying to use the old sparse structures containing the g array. \n\
48  Conductance structures have changed: conductance values should be defined as synapse variables now; \n\
49  the structure is renamed as \"SparseProjection\" and contains only indexing arrays. \n\n\
50  The replacement function for getG is \n\
51  getSparseVar(DATATYPE * wuvar, SparseProjection * sparseStruct, int x, int y).\n\n\
52  calling getSparseVar...");
53  getSparseVar(wuvar, &sparseStruct, x, y);
54 }
55 
56 template <class DATATYPE>
57 float getSparseVar(DATATYPE *wuvar, SparseProjection *sparseStruct, int x, int y)
58 {
59  DATATYPE g = 0.0; //default return value implies zero weighted for x,y
60  int startSynapse = sparseStruct->indInG[x];
61  int endSynapse = sparseStruct->indInG[x+1];
62  for (int syn = startSynapse; syn < endSynapse; syn++) {
63  if (sparseStruct->ind[syn]==y) {//look for index y
64  g = wuvar[syn]; //set the real g
65  break; //stop looking
66  }
67  }
68  return g;
69 }
70 
71 
72 //--------------------------------------------------------------------------
76 //--------------------------------------------------------------------------
77 
78 template <class DATATYPE>
79 void setSparseConnectivityFromDense(DATATYPE *wuvar, int preN, int postN, DATATYPE *tmp_gRNPN, SparseProjection *sparseStruct)
80 {
81  int synapse = 0;
82  sparseStruct->indInG[0] = 0; //first neuron always gets first synapse listed.
83  for (int pre = 0; pre < preN; ++pre) {
84  for (int post = 0; post < postN; ++post) {
85  DATATYPE g = tmp_gRNPN[pre * postN + post];
87  sparseStruct->ind[synapse] = post;
88  wuvar[synapse] = g;
89  synapse ++;
90  }
91  }
92  sparseStruct->indInG[pre + 1] = synapse; //write start of next group
93  }
94 }
95 
96 
97 
98 //--------------------------------------------------------------------------
102 //--------------------------------------------------------------------------
103 
104 template <class DATATYPE>
105 void createSparseConnectivityFromDense(DATATYPE *wuvar, int preN, int postN, DATATYPE *tmp_gRNPN, SparseProjection *sparseStruct, bool runTest) {
106  sparseStruct->connN = countEntriesAbove(tmp_gRNPN, preN * postN, GENN_PREFERENCES::asGoodAsZero);
107  //sorry -- this is not functional anymore
108 
109  //allocateSparseArray(sparseStruct, sparseStruct.connN, preN, false);
110  int synapse = 0;
111  sparseStruct->indInG[0] = 0; //first neuron always gets first synapse listed.
112  for (int pre = 0; pre < preN; ++pre) {
113  for (int post = 0; post < postN; ++post) {
114  DATATYPE g = tmp_gRNPN[pre * postN + post];
116  sparseStruct->ind[synapse] = post;
117  wuvar[synapse] = g;
118  synapse ++;
119  }
120  }
121  sparseStruct->indInG[pre + 1] = synapse; //write start of next group
122  }
123  if (!runTest) return;
124 
125  //test correct
126  int fails = 0;
127  for (int test = 0; test < 10; ++test) {
128  int randX = rand() % preN;
129  int randY = rand() % postN;
130  float denseResult = tmp_gRNPN[randX * postN + randY];
131  float sparseResult = getG(wuvar, sparseStruct,randX,randY);
132  if (abs(denseResult-sparseResult) > GENN_PREFERENCES::asGoodAsZero) fails++;
133  }
134  if (fails > 0 ) {
135  fprintf(stderr, "ERROR: Sparse connectivity generator failed for %u out of 10 random checks.\n", fails);
136  exit(1);
137  }
138  fprintf(stdout, "Sparse connectivity generator passed %u out of 10 random checks.\n", fails);
139 }
140 
141 
142 //---------------------------------------------------------------------
145 //---------------------------------------------------------------------
146 
147 void createPosttoPreArray(unsigned int preN, unsigned int postN, SparseProjection *C);
148 
149 
150 //--------------------------------------------------------------------------
154 //--------------------------------------------------------------------------
155 
156 void createPreIndices(unsigned int preN, unsigned int postN, SparseProjection *C);
157 
158 
159 #ifndef CPU_ONLY
160 //--------------------------------------------------------------------------
164 //--------------------------------------------------------------------------
165 
166 void initializeSparseArray(SparseProjection C, unsigned int *dInd, unsigned int *dIndInG, unsigned int preN);
167 
168 
169 //--------------------------------------------------------------------------
173 //--------------------------------------------------------------------------
174 
175 void initializeSparseArrayRev(SparseProjection C, unsigned int *dRevInd, unsigned int *dRevIndInG, unsigned int *dRemap, unsigned int postN);
176 
177 
178 //--------------------------------------------------------------------------
182 //--------------------------------------------------------------------------
183 
184 void initializeSparseArrayPreInd(SparseProjection C, unsigned int *dPreInd);
185 #endif
186 
187 #endif
DATATYPE getG(DATATYPE *wuvar, SparseProjection *sparseStruct, int x, int y)
DEPRECATED Utility to get a synapse weight from a SPARSE structure by x,y coordinates NB: as the Spar...
Definition: sparseUtils.h:44
void createPosttoPreArray(unsigned int preN, unsigned int postN, SparseProjection *C)
Utility to generate the SPARSE array structure with post-to-pre arrangement from the original pre-to-...
Definition: sparseUtils.cc:16
void createSparseConnectivityFromDense(DATATYPE *wuvar, int preN, int postN, DATATYPE *tmp_gRNPN, SparseProjection *sparseStruct, bool runTest)
Utility to generate the SPARSE connectivity structure from a simple all-to-all array.
Definition: sparseUtils.h:105
Global header file containing a few global variables. Part of the code generation section...
double asGoodAsZero
Global variable that is used when detecting close to zero values, for example when setting sparse con...
Definition: global.cc:40
unsigned int countEntriesAbove(DATATYPE *Array, int sz, double includeAbove)
Utility to count how many entries above a specified value exist in a float array. ...
Definition: sparseUtils.h:23
unsigned int connN
Definition: sparseProjection.h:25
void initializeSparseArrayRev(SparseProjection C, unsigned int *dRevInd, unsigned int *dRevIndInG, unsigned int *dRemap, unsigned int postN)
Function for initializing reversed conductance array indices for sparse matrices on the GPU (by copyi...
Definition: sparseUtils.cc:80
unsigned int * ind
Definition: sparseProjection.h:20
float getSparseVar(DATATYPE *wuvar, SparseProjection *sparseStruct, int x, int y)
Definition: sparseUtils.h:57
void setSparseConnectivityFromDense(DATATYPE *wuvar, int preN, int postN, DATATYPE *tmp_gRNPN, SparseProjection *sparseStruct)
Function for setting the values of SPARSE connectivity matrix.
Definition: sparseUtils.h:79
unsigned int * indInG
Definition: sparseProjection.h:19
void createPreIndices(unsigned int preN, unsigned int postN, SparseProjection *C)
Function to create the mapping from the normal index array "ind" to the "reverse" array revInd...
Definition: sparseUtils.cc:48
class (struct) for defining a spars connectivity projection
Definition: sparseProjection.h:18
void initializeSparseArray(SparseProjection C, unsigned int *dInd, unsigned int *dIndInG, unsigned int preN)
Function for initializing conductance array indices for sparse matrices on the GPU (by copying the va...
Definition: sparseUtils.cc:67
void initializeSparseArrayPreInd(SparseProjection C, unsigned int *dPreInd)
Function for initializing reversed conductance arrays presynaptic indices for sparse matrices on the ...
Definition: sparseUtils.cc:94