Brainsimulation
Simulation of the human brain.
utils.h
Go to the documentation of this file.
1 #ifndef UTILS_H
2 #define UTILS_H
3 
4 #include "definitions.h"
5 
6 #ifdef _WIN32
7 #include <Windows.h>
8 #else
9 
10 #include <pthread.h>
11 #include <sys/time.h>
12 
13 #endif
14 
26 typedef struct {
30  unsigned int num_threads;
31 
36 
41 
46 }
48 
49 
57 nodeval_t **alloc_2d(const int m, const int n);
58 
59 
70 nodeval_t ****alloc_4d(const int m, const int n, const int o, const int p);
71 
78 void init_zeros_2d(nodeval_t **nodes, int number_nodes_x, int number_nodes_y);
79 
84 const unsigned int system_processor_online_count();
85 
96 
102 void init_thread_barrier(threadbarrier_t *barrier, const unsigned int number_threads);
103 
109 
114 unsigned int wait_at_barrier(threadbarrier_t *barrier);
115 
121 void join_and_close_simulation_threads(threadhandle_t **handles, const int num_threads);
122 
153 void init_partial_simulation_context(partialsimulationcontext_t *context, int num_ticks, double tick_ms,
154  int number_nodes_x, int number_nodes_y,
155  int num_obervationnodes, nodetimeseries_t *observationnodes,
156  nodeval_t **old_state,
157  nodeval_t **new_state, nodeval_t **slopes, nodeval_t ****kernels,
158  kernelfunc_t d_ptr, kernelfunc_t id_ptr,
159  int number_global_inputs, nodeinputseries_t *global_inputs,
160  int thread_start_x, int thread_end_x, threadbarrier_t *barrier);
161 
169 
177 void output_to_csv(char *filename, int length, nodeval_t *values);
178 
185 int get_daytime(struct timeval *tp);
186 
193 void parse_file(nodeinputseries_t timeseries, const char *filename);
194 
202 int get_field(char *line, int num);
203 
204 #endif
Struct to store the input for the simulation for a single node.
Definition: definitions.h:151
threadhandle_t ** handles
Pointers to thread handles provided by the operating system.
Definition: utils.h:35
void destroy_thread_barrier(threadbarrier_t *barrier)
Destroys the thread barrier.
Definition: utils.c:105
pthread_t threadhandle_t
Platform-independent thread handle.
Definition: definitions.h:105
void init_partial_simulation_context(partialsimulationcontext_t *context, int num_ticks, double tick_ms, int number_nodes_x, int number_nodes_y, int num_obervationnodes, nodetimeseries_t *observationnodes, nodeval_t **old_state, nodeval_t **new_state, nodeval_t **slopes, nodeval_t ****kernels, kernelfunc_t d_ptr, kernelfunc_t id_ptr, int number_global_inputs, nodeinputseries_t *global_inputs, int thread_start_x, int thread_end_x, threadbarrier_t *barrier)
Convenience function to initialize a partialsimulationcontext_t in a single line. ...
Definition: utils.c:125
void parse_file(nodeinputseries_t timeseries, const char *filename)
Parses the .csv file, given by the specific filename and writes its contents into the given timeserie...
Definition: utils.c:247
Struct to pass a simulation&#39;s technical execution information to the ticks.
Definition: utils.h:26
void init_executioncontext(executioncontext_t *context)
Initializes the simulation&#39;s technical execution context.
Definition: utils.c:173
pthread_barrier_t threadbarrier_t
Platform-independent thread barrier.
Definition: definitions.h:114
void init_thread_barrier(threadbarrier_t *barrier, const unsigned int number_threads)
Initializes thread barrier.
Definition: utils.c:97
double nodeval_t
Type that the nodes in the brainsimulation use to store their energy level.
Definition: definitions.h:120
unsigned int num_threads
Number of threads in use by the simulation.
Definition: utils.h:30
Struct to store the results of a simulation for a single observed node.
Definition: definitions.h:126
int get_field(char *line, int num)
Gets the n-th integer value of a comma-separated (.csv) line.
Definition: utils.c:259
partialsimulationcontext_t * contexts
Array of contexts for the different threads.
Definition: utils.h:40
nodeval_t ** alloc_2d(const int m, const int n)
Allocates a new 2d array with m pointers, pointing to a list of n elements.
Definition: utils.c:21
int get_daytime(struct timeval *tp)
Get the time of day.
Definition: utils.c:200
threadbarrier_t barrier
Synchronization barrier to be used by all threads that run in this execution.
Definition: utils.h:45
Common definitions (mostly types).
threadhandle_t * create_and_run_simulation_thread(unsigned int(*callback)(partialsimulationcontext_t *), partialsimulationcontext_t *context)
Creates a new platform-specific thread with the context and starts it.
Definition: utils.c:66
nodeval_t **** alloc_4d(const int m, const int n, const int o, const int p)
Allocates a new 4d array with m pointers, pointing to a list of n elements, pointing to a list of o e...
Definition: utils.c:30
int(* kernelfunc_t)(nodeval_t *, int, int, nodeval_t **, int, int)
Definition of the kernel-function interface.
Definition: definitions.h:190
void init_zeros_2d(nodeval_t **nodes, int number_nodes_x, int number_nodes_y)
Sets all values of the given array to zero.
Definition: utils.c:45
Struct to pass all execution information to a new thread for executing a tick (or parts thereof)...
Definition: definitions.h:196
void join_and_close_simulation_threads(threadhandle_t **handles, const int num_threads)
Joins all threads and then closes them.
Definition: utils.c:81
unsigned int wait_at_barrier(threadbarrier_t *barrier)
Waits at the barrier.
Definition: utils.c:113
const unsigned int system_processor_online_count()
Returns the number of processor cores online in the system.
Definition: utils.c:55
void output_to_csv(char *filename, int length, nodeval_t *values)
Writes the given array to a .csv with every entry in its own line.
Definition: utils.c:183