-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterations_wrapper.cuh
253 lines (223 loc) · 10.5 KB
/
iterations_wrapper.cuh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifndef MINDS_CRAWL_MAIN_LOGIC_CUH
#define MINDS_CRAWL_MAIN_LOGIC_CUH
#ifdef COMPILE_FOR_CPU
#include <initializer_list>
#include <utility>
#endif //COMPILE_FOR_CPU
#include <cstdio>
#include "../simulation_objects/SimulationMap.cuh"
#include "../simulation_objects/geometric/Polyhedron.cuh"
#include "../simulation_objects/MapNode.cuh"
#include "../simulation_objects/Particle.cuh"
#include "simulation_logic.cuh"
#include "../external/random_generator.cuh"
#include "../jones_constants.hpp"
#include "../common.cuh"
namespace jc = jones_constants;
#define RUN_ITERATION_SET_SELF(self, i) { \
if(i >= simulation_map->get_n_of_nodes()) \
return; \
self = &simulation_map->nodes[i]; \
}
typedef void (*RunIterationFunc)(SimulationMap *, int *);
/**
* Initializes the simulation's objects (at the moment only a `SimulationMap`)
*
* This function is used to initialize simulation objects on device. It at the moment only initializes `SimulationMap`,
* but can be extended to initialize more objects
*
* @param simulation_map Pointer to `SimulationMap`. A constructed simulation map will be moved there
* @param polyhedron Pointer to a polyhedron to be used to initialize `SimulationMap` (constructor's parameter)
*
* @warning While `polyhedron` parameter must point to a real `Polyhedron` object, `simulation_map` might contain an
* existing map already, but it will be destructed in this case. Note that if the pointer doesn't contain an
* object the destructor will be called anyway, but it should be safe (???)
*
* @see destruct_simulation_objects
*/
__global__ void init_simulation_objects(SimulationMap *const simulation_map, Polyhedron *const polyhedron)
{
#ifndef COMPILE_FOR_CPU
STOP_ALL_THREADS_EXCEPT_FIRST;
#endif
*simulation_map = SimulationMap(polyhedron);
}
/**
* Initializes environment (food, first particles) of the `SimulationMap`
*
* Purpose of this function is to flexibly perform additional configuration of a simulation map such as placing food
* and first particles on it. However, at the moment (on the most early stages of development) it performs the most
* basic configuration you can't control from outside of the function. Later the function is of course going to take
* more parameters
*
* @param simulation_map Pointer to `SimulationMap` object to be configured
*/
__global__ void init_environment(SimulationMap *const simulation_map)
{
#ifndef COMPILE_FOR_CPU
STOP_ALL_THREADS_EXCEPT_FIRST;
#endif
int random_node_index = (int)(rand0to1() * (simulation_map->get_n_of_nodes() - 1));
MapNode *random_node = simulation_map->nodes + random_node_index;
if(!create_particle(random_node))
{
printf("%s:%d - something went REALLY wrong at ", __FILE__, __LINE__);
}
}
/**
* The opposite of `init_simulation_objects`
*
* This function destructs the objects constructed in `init_simulation_objects` (at the moment only a `SimulationMap`)
* to let you safely free memory without breaking any invariants etc
*
* @param simulation_map Pointer to the `SimulationMap` to be destructed
*
* @see init_simulation_objects
*/
__global__ void destruct_simulation_objects(SimulationMap *const simulation_map)
{
#ifndef COMPILE_FOR_CPU
STOP_ALL_THREADS_EXCEPT_FIRST;
#endif
simulation_map->~SimulationMap();
}
/**
* Performs a part of an iteration <b>for one node</b> (not self-sufficient, see note below)
*
* Projection: for each node containing food, the amount of trail being added is either `jc::suppressvalue` if
* there is at least one particle in its 3x3 node window (which is also called node neighborhood), or `jc::projectvalue`
* if there are no. This trail is added not only to the node containing food, but <b>to its neighborhood either</b>
*
* @param simulation_map Simulation map to run iteration on
* @param iteration_number Serial number of current iteration
* @param node_index Index of `SimulationMap`'s node to run iteration on. Can be out of bounds
*
* @see run_iteration_diffuse_trail, run_iteration_process_particles, run_iteration_cleanup
*
* @note Again, this function only processes just one node. To use it, you'll need
*
* @note This function is a part of run_iteration kernels set. Because at some moments while running an iteration we
* have to synchronize all the threads (including inside blocks), the "run_iteration" operation is split into
* multiple kernel functions. Other functions (hopefully, all of them) are mentioned in the "see" block above
*/
__device__ inline void run_iteration_project_nutrients(SimulationMap *const simulation_map,
const int *const iteration_number, const unsigned int node_index)
{
/// Pointer to the `MapNode` being processed by current thread
MapNode *self;
RUN_ITERATION_SET_SELF(self, node_index)
if(jc::projectnutrients && *iteration_number >= jc::startprojecttime)
{
if(self->does_contain_food())
{
double trail_value;
if(count_particles_in_node_window(self, 3) > 0)
trail_value = jc::suppressvalue;
else
trail_value = jc::projectvalue;
// Add trail to 3x3 node window
MapNode *left = self->get_left();
for(MapNode *node : {left->get_top(), left,
left->get_bottom()}) // for each leading node of rows of 3x3 square
{
for(int i = 0; i < 3; ++i)
{
atomicAdd(&node->trail, trail_value); // add trail
node = node->get_right(); // move to next node in row
}
}
}
}
}
/**
* Performs a part of an iteration <b>for one node</b> (not self-sufficient, see note below
*
* Diffusion: the diffusion algorithm (developed by Jeff Jones) is pretty simple at first sight. We calculate an average
* `trail` value in a 3x3 node window around the given one and multiply it by `(1 - jones_constants::diffdamp)`.
* The new `temp_trail` value in the given node is the value just calculated. This is a natural way to implement the
* smell spread: on each iteration smell moves more far from the source, but becomes less strong, because
* `(1 - jones_constants::diffdamp)` < 1
*
* @param simulation_map Simulation map to run iteration on
* @param iteration_number Serial number of current iteration
* @param node_index Index of `SimulationMap`'s node to run iteration on. Can be out of bounds
*
* @see run_iteration_project_nutrients, run_iteration_process_particles, run_iteration_cleanup
*
* @note This function is a part of run_iteration kernels set. Because at some moments while running an iteration we
* have to synchronize all the threads (including inside blocks), the "run_iteration" operation is split into
* multiple kernel functions. Other functions (hopefully, all of them) are mentioned in the "see" block above
*/
__device__ inline void run_iteration_diffuse_trail(SimulationMap *const simulation_map,
const int *const iteration_number, const unsigned int node_index)
{
MapNode *self;
RUN_ITERATION_SET_SELF(self, node_index)
diffuse_trail(self);
}
/**
* Performs a part of an iteration <b>for one node</b> (not self-sufficient, see note below
*
* Processing particles: For each node containing particles, the following operations are performed on the particles:
* motor behaviours, sensory behaviours, division test, random death test, death test (order saved). You can read
* details about these in the corresponding functions' docs
*
* @param simulation_map Simulation map to run iteration on
* @param iteration_number Serial number of current iteration
* @param node_index Index of `SimulationMap`'s node to run iteration on. Can be out of bounds
*
* @see run_iteration_project_nutrients, run_iteration_diffuse_trail, run_iteration_cleanup
*
* @note This function is a part of run_iteration kernels set. Because at some moments while running an iteration we
* have to synchronize all the threads (including inside blocks), the "run_iteration" operation is split into
* multiple kernel functions. Other functions (hopefully, all of them) are mentioned in the "see" block above
*/
__device__ inline void run_iteration_process_particles(SimulationMap *const simulation_map,
const int *const iteration_number, const unsigned int node_index)
{
/// Pointer to the `MapNode` being processed by current thread
MapNode *self;
RUN_ITERATION_SET_SELF(self, node_index)
if(!self->does_contain_particle() || !self->get_particle()->capture())
return;
self->get_particle()->do_motor_behaviours();
self->get_particle()->do_sensory_behaviours();
if(*iteration_number % jc::division_test_frequency == 0)
division_test(self);
if(jc::do_random_death_test && jc::random_death_probability > 0 &&
*iteration_number > jc::startprojecttime)
if(random_death_test(self)) // If the particle died
return;
if(*iteration_number % jc::death_test_frequency == 0)
if(death_test(self)) // If the particle died
return;
}
/**
* Performs a part of an iteration <b>for one node</b> (not self-sufficient, see note below
*
* Applies trail changes (sets `trail`s to `temp_trail`s), releases captured particles and increases `iteration_number`
* by 1
*
* @param simulation_map Simulation map to run iteration on
* @param iteration_number Serial number of current iteration
* @param node_index Index of `SimulationMap`'s node to run iteration on. Can be out of bounds
*
* @see run_iteration_project_nutrients, run_iteration_diffuse_trail, run_iteration_process_particles
*
* @note This function is a part of run_iteration kernels set. Because at some moments while running an iteration we
* have to synchronize all the threads (including inside blocks), the "run_iteration" operation is split into
* multiple kernel functions. Other functions (hopefully, all of them) are mentioned in the "see" block above
*/
__device__ inline void run_iteration_cleanup(SimulationMap *const simulation_map, int *const iteration_number,
const unsigned int node_index)
{
/// Pointer to the `MapNode` being processed by current thread
MapNode *self;
RUN_ITERATION_SET_SELF(self, node_index)
self->trail = self->temp_trail;
if(self->does_contain_particle())
self->get_particle()->release();
if(node_index == 0)
++*iteration_number;
}
#endif //MINDS_CRAWL_MAIN_LOGIC_CUH