Brainsimulation
Simulation of the human brain.
Data Structures | Functions
utils.h File Reference

Common helper functions. More...

#include "definitions.h"
#include <pthread.h>
#include <sys/time.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  executioncontext_t
 Struct to pass a simulation's technical execution information to the ticks. More...
 

Functions

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. More...
 
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 elements, pointing to a list of p elements. More...
 
void init_zeros_2d (nodeval_t **nodes, int number_nodes_x, int number_nodes_y)
 Sets all values of the given array to zero. More...
 
const unsigned int system_processor_online_count ()
 Returns the number of processor cores online in the system. More...
 
threadhandle_tcreate_and_run_simulation_thread (unsigned int(*callback)(partialsimulationcontext_t *), partialsimulationcontext_t *context)
 Creates a new platform-specific thread with the context and starts it. More...
 
void init_thread_barrier (threadbarrier_t *barrier, const unsigned int number_threads)
 Initializes thread barrier. More...
 
void destroy_thread_barrier (threadbarrier_t *barrier)
 Destroys the thread barrier. More...
 
unsigned int wait_at_barrier (threadbarrier_t *barrier)
 Waits at the barrier. More...
 
void join_and_close_simulation_threads (threadhandle_t **handles, const int num_threads)
 Joins all threads and then closes them. More...
 
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. More...
 
void init_executioncontext (executioncontext_t *context)
 Initializes the simulation's technical execution context. More...
 
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. More...
 
int get_daytime (struct timeval *tp)
 Get the time of day. More...
 
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 timeseries of the nodeinputseries_t. More...
 
int get_field (char *line, int num)
 Gets the n-th integer value of a comma-separated (.csv) line. More...
 

Detailed Description

Common helper functions.

Function Documentation

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.

Parameters
mThe number of nodes in the first dimension (x-axis).
nThe number of nodes in the second dimension (y-axis).
Returns
A two-dimensional array of size m*n.
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 elements, pointing to a list of p elements.

Parameters
mThe number of nodes in the first dimension.
nThe number of nodes in the second dimension.
oThe number of nodes in the third dimension.
pThe number of nodes in the fourth dimension.
Returns
A four-dimensional array of size m*n*o*p.
threadhandle_t* create_and_run_simulation_thread ( unsigned int(*)(partialsimulationcontext_t *)  callback,
partialsimulationcontext_t context 
)

Creates a new platform-specific thread with the context and starts it.

Parameters
callbackThe callback function to run. The function must take a threadcontext struct and returns 0 or an error code.
contextThe threadcontext struct to pass.
Returns
A handle for the running thread.
void destroy_thread_barrier ( threadbarrier_t barrier)

Destroys the thread barrier.

Parameters
barrierBarrier to destroy.
int get_daytime ( struct timeval *  tp)

Get the time of day.

Platform-independent abstraction of gettimeofday in sys/time.h.

Parameters
tpThe timeval to write back to.
Returns
Error codes.
int get_field ( char *  line,
int  num 
)

Gets the n-th integer value of a comma-separated (.csv) line.

Parameters
lineThe line to parse.
numThe index of the column to get, i.e. n.
Returns
The parsed integer.
void init_executioncontext ( executioncontext_t context)

Initializes the simulation's technical execution context.

Derives the number of threads for execution and writes the result to context->num_threads. Allocates memory for the arrays in the context.

Parameters
contextThe context to initialize.
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.

Passes most members and automatically derives partialsimulationcontext_t::number_partial_inputs and partialsimulationcontext_t::partial_inputs from the global inputs.

Parameters
contextPointer to the context.
num_ticksThe number of ticks in the simulation.
tick_msMilliseconds in between each simulation tick.
number_nodes_xThe number of total nodes in the first dimension of nodes.
number_nodes_yThe number of total nodes in the second dimension of nodes.
num_obervationnodesThe number of nodes to observe.
observationnodesPointers to the timeseries for the nodes to observe. Observations are to be written here.
old_state2D array of nodes with their current energy level.Size number_nodes_x * number_nodes_y.
new_state2D array of nodes with the new energy level.Values will be overwritten. Size number_nodes_x * number_nodes_y.
slopes2D array of nodes with their slope from the last tick iteration level. Size number_nodes_x * number_nodes_y.
kernels2D array containing the kernels of each node at each index.Each index node points to an array containing(currently) two kernels, each(currently) containing 4 neighbouring noides.Dimensions: number_nodes_x * number_nodes_y * 2 * 4.
d_ptrFunction pointer pointing to the kernel function for the direct neighborhood.
id_ptrFunction pointer pointing to the kernel function for the indirect neighborhood.
number_global_inputsNumber of all inputs on the entire node-grid inputs to be processed.
global_inputsInputs on the entire node-grid inputs to be processed. Length: number_partial_inputs. Partial inputs in the sub-grid (defined by thread_start_x and thread_end_x) are automatically derived from this global list.
thread_start_xNode x index at which to start working in this thread (inclusive).
thread_end_xNode x index at which to stop working in this thread (exclusive).
barrierThe barrier for threads to wait at. May be uninitialized in if MULTITHREADING is disabled.
void init_thread_barrier ( threadbarrier_t barrier,
const unsigned int  number_threads 
)

Initializes thread barrier.

Parameters
barrierBarrier to initialize.
number_threadsThe number of threads that the barrier should be configured to block.
void init_zeros_2d ( nodeval_t **  nodes,
int  number_nodes_x,
int  number_nodes_y 
)

Sets all values of the given array to zero.

Parameters
nodes2D array to be modified. Dimension x * y.
number_nodes_xNumber of nodes in the first dimension.
number_nodes_yNumber of nodes in the second dimension.
void join_and_close_simulation_threads ( threadhandle_t **  handles,
const int  num_threads 
)

Joins all threads and then closes them.

Frees all thread handles.

Parameters
handlesArray of thread handle pointers.
num_threadslength of handles array.
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.

Parameters
filenameName of the file to write to.
lengthNumber of entries to write.
valuesThe values to write. Length length.
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 timeseries of the nodeinputseries_t.

Parameters
timeseriesThe nodeinputseries_t struct to write the parsed file into.
filenameThe filename of the .csv file.
const unsigned int system_processor_online_count ( )

Returns the number of processor cores online in the system.

Returns
The number of processors online in the system.
unsigned int wait_at_barrier ( threadbarrier_t barrier)

Waits at the barrier.

Returns 1 for the management thread and 0 for all other threads.

Parameters
barrierBarrier to wait at.