yinspire 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/README +24 -0
  2. data/bench/pq/Makefile +5 -0
  3. data/bench/pq/bench.cc +321 -0
  4. data/bench/pq/bench.rb +125 -0
  5. data/bench/pq/bench_binaryheap.h +46 -0
  6. data/bench/pq/bench_calendarqueue.h +58 -0
  7. data/bench/pq/bench_pairingheap.h +61 -0
  8. data/bench/pq/bench_stlpq.h +46 -0
  9. data/bench/pq/benchmark.h +225 -0
  10. data/bench/pq/distribution.h +93 -0
  11. data/bench/pq/make.rb +24 -0
  12. data/bin/yinspire +186 -0
  13. data/examples/nets/gereon2005.c.json +93723 -0
  14. data/examples/nets/gereon2005.yin +232650 -0
  15. data/examples/nets/skorpion.graphml +396 -0
  16. data/examples/nets/spiketrains_angle_180.txt +8 -0
  17. data/lib/Algorithms/Array.h +52 -0
  18. data/lib/Algorithms/BinaryHeap.h +265 -0
  19. data/lib/Algorithms/CalendarQueue.h +257 -0
  20. data/lib/Algorithms/IndexedBinaryHeap.h +90 -0
  21. data/lib/Algorithms/PairingHeap.h +169 -0
  22. data/lib/Allocators/ChunkedFreelistAllocator.h +96 -0
  23. data/lib/Allocators/MemoryAllocator.h +45 -0
  24. data/lib/Allocators/RubyMemoryAllocator.h +37 -0
  25. data/lib/Yinspire.rb +69 -0
  26. data/lib/Yinspire/All.rb +10 -0
  27. data/lib/Yinspire/Core/NeuralEntity.rb +133 -0
  28. data/lib/Yinspire/Core/Neuron.rb +162 -0
  29. data/lib/Yinspire/Core/Scheduling/NeuralEntity.rb +123 -0
  30. data/lib/Yinspire/Core/Scheduling/Simulator.rb +94 -0
  31. data/lib/Yinspire/Core/Simulator.rb +36 -0
  32. data/lib/Yinspire/Core/StimuliMixin.rb +103 -0
  33. data/lib/Yinspire/Core/Stimulus.rb +25 -0
  34. data/lib/Yinspire/Core/Synapse.rb +64 -0
  35. data/lib/Yinspire/Dumpers/Dumper.rb +19 -0
  36. data/lib/Yinspire/Dumpers/Dumper_Dot.rb +28 -0
  37. data/lib/Yinspire/Loaders/GraphML.rb +84 -0
  38. data/lib/Yinspire/Loaders/Loader.rb +31 -0
  39. data/lib/Yinspire/Loaders/Loader_GraphML.rb +97 -0
  40. data/lib/Yinspire/Loaders/Loader_JSON.rb +181 -0
  41. data/lib/Yinspire/Loaders/Loader_Spike.rb +42 -0
  42. data/lib/Yinspire/Loaders/Loader_Yin.rb +62 -0
  43. data/lib/Yinspire/Loaders/YinScanner.rb +247 -0
  44. data/lib/Yinspire/Models/Neuron_Base.rb +38 -0
  45. data/lib/Yinspire/Models/Neuron_Input.rb +12 -0
  46. data/lib/Yinspire/Models/Neuron_InputOutput.rb +39 -0
  47. data/lib/Yinspire/Models/Neuron_Output.rb +15 -0
  48. data/lib/Yinspire/Models/Neuron_SRM01.rb +50 -0
  49. data/lib/Yinspire/Models/Neuron_SRM02.rb +64 -0
  50. data/lib/Yinspire/Models/Synapse_Hebb.rb +67 -0
  51. data/pure_cpp/Makefile +22 -0
  52. data/pure_cpp/README +2 -0
  53. data/pure_cpp/src/algo/binary_heap.h +277 -0
  54. data/pure_cpp/src/algo/indexed_binary_heap.h +90 -0
  55. data/pure_cpp/src/json/json.cc +542 -0
  56. data/pure_cpp/src/json/json.h +182 -0
  57. data/pure_cpp/src/json/json_parser.cc +685 -0
  58. data/pure_cpp/src/json/json_parser.h +15 -0
  59. data/pure_cpp/src/json/json_parser.rl +213 -0
  60. data/pure_cpp/src/main.cc +49 -0
  61. data/pure_cpp/src/memory_allocator.h +45 -0
  62. data/pure_cpp/src/neural_entity.cc +208 -0
  63. data/pure_cpp/src/neural_entity.h +243 -0
  64. data/pure_cpp/src/neuron.cc +136 -0
  65. data/pure_cpp/src/neuron.h +70 -0
  66. data/pure_cpp/src/neuron_srm_01.cc +77 -0
  67. data/pure_cpp/src/neuron_srm_01.h +36 -0
  68. data/pure_cpp/src/simulator.cc +151 -0
  69. data/pure_cpp/src/simulator.h +116 -0
  70. data/pure_cpp/src/synapse.cc +117 -0
  71. data/pure_cpp/src/synapse.h +60 -0
  72. data/pure_cpp/src/types.h +18 -0
  73. data/run.rb +68 -0
  74. data/tools/conv_jsonc_to_yin.rb +165 -0
  75. data/tools/converter.rb +93 -0
  76. data/tools/json_writer.rb +122 -0
  77. data/yinspire.gemspec +20 -0
  78. metadata +156 -0
@@ -0,0 +1,15 @@
1
+ #ifndef __YINSPIRE__JSON_PARSER__
2
+ #define __YINSPIRE__JSON_PARSER__
3
+
4
+ #include "json.h"
5
+
6
+ class jsonParser
7
+ {
8
+ public:
9
+
10
+ static jsonValue* parse(char* content, int size);
11
+ static jsonValue* parse_file(const char* filename);
12
+ static jsonValue* parse_file_mmap(const char* filename);
13
+ };
14
+
15
+ #endif
@@ -0,0 +1,213 @@
1
+ %%{
2
+ machine jsonParser;
3
+
4
+ jnull = "null" %{ PB(new jsonNull()); };
5
+
6
+ true = "true" %{ PB(new jsonTrue()); };
7
+
8
+ false = "false" %{ PB(new jsonFalse()); };
9
+
10
+ exp = ("e"|"E") ("+"|"-")? [0-9]+;
11
+ number = (("+" | "-")? ([0-9] | [1-9] [0-9]*) ("." [0-9]* )? exp?) >{ tstart=p; } %{
12
+ PB(json_number(tstart, p, numbuf, numbufsz));
13
+ };
14
+
15
+ pos_inf = (("+")? "Infinity") %{ PB(new jsonNumber(INFINITY)); };
16
+
17
+ neg_inf = "-Infinity" %{ PB(new jsonNumber(-INFINITY)); };
18
+
19
+ string = ('"' [^"]* '"' | "'" [^']* "'") >{ tstart=p; } %{
20
+ PB(json_string(tstart+1, p-1));
21
+ };
22
+
23
+ label = (("_" | alpha) ("_" | alnum)*) >{ tstart=p; } %{
24
+ PB(json_string(tstart, p));
25
+ };
26
+
27
+ whitespace = space | "//" [^\n\r]* [\n\r];
28
+
29
+ any_value = (jnull | true | false | number | pos_inf | neg_inf
30
+ | string
31
+ | ("{" @{ fcall hash; })
32
+ | ("[" @{ fcall array; })
33
+ );
34
+
35
+ main := whitespace* any_value whitespace*;
36
+
37
+ kv = (whitespace* (label|string) whitespace* ":" whitespace* any_value) %{
38
+ if (values.size() < 3) throw "invalid format";
39
+ jsonValue* v = values.back();
40
+ values.pop_back();
41
+ jsonString* k = (jsonString*) values.back();
42
+ values.pop_back();
43
+ jsonHash* h = (jsonHash*) values.back();
44
+ h->set(k, v);
45
+ };
46
+
47
+ hash := (
48
+ (kv whitespace* ("," kv whitespace*)*)? whitespace* "}"
49
+ ) >{ PB(new jsonHash());} @{ fret; };
50
+
51
+ array := (
52
+ whitespace* ( (any_value (whitespace* "," whitespace* any_value)*)? whitespace* "]" )
53
+ ) >{ array_i.push_back(values.size()); } @{
54
+ jsonArray* a = new jsonArray();
55
+ for (int i=array_i.back(); i<values.size(); i++) a->push(values[i]);
56
+ while (values.size() > array_i.back()) values.pop_back();
57
+ PB(a);
58
+ array_i.pop_back();
59
+ fret;
60
+ };
61
+
62
+ }%%
63
+
64
+ #include <vector>
65
+ #include <math.h>
66
+ #include <stdlib.h>
67
+ #include <string.h>
68
+ #include <iostream>
69
+ #include "json_parser.h"
70
+
71
+ #define PB(x) values.push_back(x)
72
+
73
+ inline static jsonString* json_string(char* from, char* to)
74
+ {
75
+ return new jsonString(from, to-from);
76
+ }
77
+
78
+ inline static jsonNumber* json_number(char* from, char* to, char* buf, int maxsz)
79
+ {
80
+ const int sz = to-from;
81
+
82
+ if (sz > maxsz) throw "fatal";
83
+
84
+ memcpy(buf, from, sz);
85
+ buf[sz] = '\000';
86
+
87
+ return new jsonNumber(strtod(buf, NULL));
88
+ }
89
+
90
+ %% write data;
91
+
92
+ jsonValue* jsonParser::parse(char* content, int size)
93
+ {
94
+ int cs;
95
+ int top;
96
+ int stack[20];
97
+ char numbuf[61];
98
+ const int numbufsz = 60;
99
+
100
+ char *ps = content;
101
+ char *p = ps;
102
+ char *pe = content + size;
103
+
104
+ // user defined
105
+ char* tstart = NULL;
106
+ std::vector<jsonValue*> values;
107
+ std::vector<int> array_i;
108
+
109
+ %%write init;
110
+ %%write exec;
111
+
112
+ if (p != pe)
113
+ {
114
+ int err_pos = (int)(p-ps);
115
+ std::cout << "error at: " << err_pos << std::endl;
116
+ throw "error occured while parsing";
117
+ }
118
+
119
+ if (values.size() != 1)
120
+ {
121
+ throw "error occured while parsing";
122
+ }
123
+
124
+ return values[0];
125
+ }
126
+
127
+ #include <fcntl.h>
128
+ #include <unistd.h>
129
+
130
+ jsonValue* jsonParser::parse_file(const char* filename)
131
+ {
132
+ int fh;
133
+ void *mem;
134
+ int size;
135
+ jsonValue* res;
136
+
137
+ fh = open(filename, O_RDONLY);
138
+ if (fh < 0)
139
+ {
140
+ throw "cannot open file";
141
+ }
142
+
143
+ size = lseek(fh, 0, SEEK_END);
144
+ if (size < 0)
145
+ {
146
+ throw "error";
147
+ }
148
+ lseek(fh, 0, SEEK_SET);
149
+
150
+ mem = malloc(size);
151
+
152
+ if (mem == NULL)
153
+ {
154
+ throw "malloc failed";
155
+ }
156
+
157
+ int c = read(fh, mem, size);
158
+
159
+ if (c != size)
160
+ {
161
+ throw "couldn't read entire file";
162
+ }
163
+
164
+ res = jsonParser::parse((char*)mem, size);
165
+
166
+ free(mem);
167
+ close(fh);
168
+
169
+ return res;
170
+ }
171
+
172
+ #ifdef WITHOUT_MMAP
173
+ jsonValue* jsonParser::parse_file_mmap(const char* filename)
174
+ {
175
+ return parse_file(filename);
176
+ }
177
+ #else
178
+ #include <sys/mman.h>
179
+ jsonValue* jsonParser::parse_file_mmap(const char* filename)
180
+ {
181
+ int fh;
182
+ void *mem;
183
+ int size;
184
+ jsonValue* res;
185
+
186
+ fh = open(filename, O_RDONLY);
187
+ if (fh < 0)
188
+ {
189
+ throw "cannot open file";
190
+ }
191
+
192
+ size = lseek(fh, 0, SEEK_END);
193
+ if (size < 0)
194
+ {
195
+ throw "error";
196
+ }
197
+ lseek(fh, 0, SEEK_SET);
198
+
199
+ mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fh, 0);
200
+
201
+ if (mem == MAP_FAILED)
202
+ {
203
+ throw "mmap failed";
204
+ }
205
+
206
+ res = jsonParser::parse((char*)mem, size);
207
+
208
+ munmap(mem, size);
209
+ close(fh);
210
+
211
+ return res;
212
+ }
213
+ #endif
@@ -0,0 +1,49 @@
1
+ #include "simulator.h"
2
+ #include <iostream>
3
+
4
+ #include "synapse.h"
5
+ #include "neuron_srm_01.h"
6
+
7
+ #define DEF_TYPE(t) NeuralEntity *make_##t() { return new t(); }
8
+ #define REG_TYPE(t, s) (s)->entity_register_type(#t, make_##t)
9
+
10
+ DEF_TYPE(Synapse)
11
+ DEF_TYPE(Neuron_SRM_01)
12
+
13
+ int main(int argc, char** argv)
14
+ {
15
+ Simulator sim;
16
+ simtime stop_at;
17
+ real tolerance = 0.0;
18
+ char *net;
19
+
20
+ if (argc == 3 || argc == 4)
21
+ {
22
+ net = argv[1];
23
+ stop_at = atof(argv[2]);
24
+
25
+ if (argc == 4)
26
+ {
27
+ tolerance = atof(argv[3]);
28
+ }
29
+ }
30
+ else
31
+ {
32
+ std::cout << "USAGE: yinspire net stop_at [tolerance]" << std::endl;
33
+ return 1;
34
+ }
35
+
36
+ REG_TYPE(Synapse, &sim);
37
+ REG_TYPE(Neuron_SRM_01, &sim);
38
+
39
+ std::cout << "net: " << net << std::endl;
40
+ std::cout << "stop_at: " << stop_at << std::endl;
41
+ std::cout << "tolerance: " << tolerance << std::endl;
42
+
43
+ sim.load(net);
44
+ sim.run(stop_at);
45
+
46
+ std::cout << sim.stat_event_counter << std::endl;
47
+ std::cout << sim.stat_fire_counter << std::endl;
48
+ return 0;
49
+ }
@@ -0,0 +1,45 @@
1
+ #ifndef __YINSPIRE__MEMORY_ALLOCATOR__
2
+ #define __YINSPIRE__MEMORY_ALLOCATOR__
3
+
4
+ #include <stdlib.h>
5
+
6
+ /*
7
+ * Provides some basic utility functions for allocating and releasing
8
+ * memory.
9
+ */
10
+ template <typename T>
11
+ class MemoryAllocator
12
+ {
13
+ public:
14
+
15
+ inline static T*
16
+ alloc_n(size_t n)
17
+ {
18
+ T* ptr = (T*) calloc(n, sizeof(T));
19
+ if (ptr == NULL)
20
+ {
21
+ throw "memory allocation failed";
22
+ }
23
+ return ptr;
24
+ }
25
+
26
+ inline static T*
27
+ realloc_n(T* old_ptr, size_t n)
28
+ {
29
+ T* ptr = (T*) realloc(old_ptr, sizeof(T)*n);
30
+ if (ptr == NULL)
31
+ {
32
+ throw "memory allocation failed";
33
+ }
34
+ return ptr;
35
+ }
36
+
37
+ inline static void
38
+ free(T* ptr)
39
+ {
40
+ ::free(ptr);
41
+ }
42
+
43
+ };
44
+
45
+ #endif
@@ -0,0 +1,208 @@
1
+ #include "neural_entity.h"
2
+ #include "simulator.h"
3
+ #include <math.h>
4
+
5
+ NeuralEntity::NeuralEntity()
6
+ {
7
+ this->simulator = NULL;
8
+ this->id = NULL;
9
+ this->schedule_index = 0;
10
+ this->schedule_at = INFINITY;
11
+ this->schedule_stepping_list_prev = NULL;
12
+ this->schedule_stepping_list_next = NULL;
13
+ this->schedule_stepping_list_internal_next = NULL;
14
+ }
15
+
16
+ NeuralEntity::~NeuralEntity()
17
+ {
18
+ }
19
+
20
+
21
+ void
22
+ NeuralEntity::load(jsonHash *data)
23
+ {
24
+ }
25
+
26
+ void
27
+ NeuralEntity::dump(jsonHash *into)
28
+ {
29
+ }
30
+
31
+ static void
32
+ iter_disconnect(NeuralEntity *self, NeuralEntity *conn)
33
+ {
34
+ self->disconnect(conn);
35
+ }
36
+
37
+ void
38
+ NeuralEntity::disconnect_all()
39
+ {
40
+ each_connection(iter_disconnect);
41
+ }
42
+
43
+ void
44
+ NeuralEntity::schedule(simtime at)
45
+ {
46
+ // FIXME: make sure that this->schedule_at is reset
47
+ // when the entity is removed from the pq.
48
+ if (this->schedule_at != at)
49
+ {
50
+ this->schedule_at = at;
51
+ this->simulator->schedule_update(this);
52
+ }
53
+ }
54
+
55
+ inline bool
56
+ NeuralEntity::schedule_stepping_enabled()
57
+ {
58
+ return (this->schedule_stepping_list_prev != NULL &&
59
+ this->schedule_stepping_list_next != NULL);
60
+ }
61
+
62
+ void
63
+ NeuralEntity::schedule_enable_stepping()
64
+ {
65
+ if (!schedule_stepping_enabled())
66
+ {
67
+ NeuralEntity*& root = this->simulator->schedule_stepping_list_root;
68
+ if (root != NULL)
69
+ {
70
+ this->schedule_stepping_list_prev = root;
71
+ this->schedule_stepping_list_next = root->schedule_stepping_list_next;
72
+ root->schedule_stepping_list_next = this;
73
+ this->schedule_stepping_list_next->schedule_stepping_list_prev = this;
74
+ }
75
+ else
76
+ {
77
+ root = this;
78
+ this->schedule_stepping_list_prev = this;
79
+ this->schedule_stepping_list_next = this;
80
+ }
81
+ }
82
+ }
83
+
84
+ void
85
+ NeuralEntity::schedule_disable_stepping()
86
+ {
87
+ if (schedule_stepping_enabled())
88
+ {
89
+ if (this->schedule_stepping_list_prev != this->schedule_stepping_list_next)
90
+ {
91
+ this->schedule_stepping_list_prev->schedule_stepping_list_next = this->schedule_stepping_list_next;
92
+ this->schedule_stepping_list_next->schedule_stepping_list_prev = this->schedule_stepping_list_prev;
93
+ }
94
+ else
95
+ {
96
+ /*
97
+ * We are the last entity in the stepping list.
98
+ */
99
+ this->simulator->schedule_stepping_list_root = NULL;
100
+ this->schedule_stepping_list_prev = NULL;
101
+ this->schedule_stepping_list_next = NULL;
102
+ }
103
+ }
104
+ }
105
+
106
+ static bool
107
+ stimuli_accum(Stimulus &parent, const Stimulus &element, void *tolerance)
108
+ {
109
+ if ((element.at - parent.at) > *((real*)tolerance)) return false;
110
+
111
+ if (isinf(element.weight))
112
+ {
113
+ /*
114
+ * We only accumulate two infinitive values!
115
+ */
116
+ return (isinf(parent.weight) ? true : false);
117
+ }
118
+
119
+ parent.weight += element.weight;
120
+ return true;
121
+ }
122
+
123
+ void
124
+ NeuralEntity::stimuli_add(simtime at, real weight)
125
+ {
126
+ Stimulus s; s.at = at; s.weight = weight;
127
+ if (this->simulator->stimuli_tolerance >= 0.0)
128
+ {
129
+ //find_parent
130
+ if (this->stimuli_pq.accumulate(s, stimuli_accum, &this->simulator->stimuli_tolerance)) return;
131
+ }
132
+ this->stimuli_pq.push(s);
133
+ schedule(this->stimuli_pq.top().at);
134
+ }
135
+
136
+ real
137
+ NeuralEntity::stimuli_sum(simtime until)
138
+ {
139
+ real weight = 0.0;
140
+
141
+ while (!this->stimuli_pq.empty() && this->stimuli_pq.top().at <= until)
142
+ {
143
+ weight += this->stimuli_pq.top().weight;
144
+ this->stimuli_pq.pop();
145
+ }
146
+
147
+ /*
148
+ * NOTE: we don't have to remove the entity from the schedule if the
149
+ * pq is empty.
150
+ */
151
+ if (!this->stimuli_pq.empty())
152
+ {
153
+ schedule(this->stimuli_pq.top().at);
154
+ }
155
+
156
+ return weight;
157
+ }
158
+
159
+ real
160
+ NeuralEntity::stimuli_sum_inf(simtime until, bool &is_inf)
161
+ {
162
+ real weight = 0.0;
163
+ is_inf = false;
164
+
165
+ while (!this->stimuli_pq.empty() && this->stimuli_pq.top().at <= until)
166
+ {
167
+ if (isinf(this->stimuli_pq.top().weight))
168
+ {
169
+ is_inf = true;
170
+ }
171
+ else
172
+ {
173
+ weight += this->stimuli_pq.top().weight;
174
+ }
175
+ this->stimuli_pq.pop();
176
+ }
177
+
178
+ if (!this->stimuli_pq.empty())
179
+ {
180
+ schedule(this->stimuli_pq.top().at);
181
+ }
182
+
183
+ return weight;
184
+ }
185
+
186
+ void
187
+ NeuralEntity::set_simulator(Simulator *simulator)
188
+ {
189
+ this->simulator = simulator;
190
+ }
191
+
192
+ Simulator *
193
+ NeuralEntity::get_simulator() const
194
+ {
195
+ return this->simulator;
196
+ }
197
+
198
+ void
199
+ NeuralEntity::set_id(const char *id)
200
+ {
201
+ this->id = id;
202
+ }
203
+
204
+ const char *
205
+ NeuralEntity::get_id() const
206
+ {
207
+ return this->id;
208
+ }