MulticoreBSP for C  Version 2.0.4
mcutil.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012
3  *
4  * File created by A. N. Yzelman, 2007-2012.
5  *
6  * This file is part of MulticoreBSP in C --
7  * a port of the original Java-based MulticoreBSP.
8  *
9  * MulticoreBSP for C is distributed as part of the original
10  * MulticoreBSP and is free software: you can redistribute
11  * it and/or modify it under the terms of the GNU Lesser
12  * General Public License as published by the Free Software
13  * Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  * MulticoreBSP is distributed in the hope that it will be
16  * useful, but WITHOUT ANY WARRANTY; without even the
17  * implied warranty of MERCHANTABILITY or FITNESS FOR A
18  * PARTICULAR PURPOSE. See the GNU Lesser General Public
19  * License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General
22  * Public License along with MulticoreBSP. If not, see
23  * <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef _H_MCUTIL
27 #define _H_MCUTIL
28 
29 #include "mcbsp-internal-macros.h"
30 #include "mcbsp-affinity.h"
31 #include "mcbsp-resiliency.h"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <assert.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <pthread.h>
43 #include <stdbool.h>
44 
45 #ifdef _WIN32
46  #include <windows.h>
47 #endif
48 
49 
54 
56  size_t start;
57 
59  size_t stride;
60 
62  size_t end;
63 
64 };
65 
70 
72  bool Tset;
73 
75  bool Aset;
76 
78  bool Cset;
79 
81  bool TpCset;
82 
84  bool TNset;
85 
87  bool Pset;
88 
90  bool Rset;
91 
93  bool UTset;
94 
96  bool CPFset;
97 
99  bool SCFset;
100 
102  size_t threads;
103 
106 
115  size_t cores;
116 
122 
125 
137 
142 
152  size_t *reserved_cores;
153 
159 
166  size_t cp_f;
167 
174  size_t safe_cp_f;
175 };
176 
179 
181  size_t cap;
182 
184  size_t size;
185 
187  void * * keys;
188 
190  size_t * values;
191 
192 };
193 
196 
198  void * address;
199 
201  size_t size;
202 
203 };
204 
207 
209  size_t cap;
210 
212  size_t top;
213 
215  size_t size;
216 
218  void * array;
219 
220 };
221 
242 
244  pthread_mutex_t mutex;
245 
247  size_t cap;
248 
250  size_t P;
251 
253 #ifdef MCBSP_ALLOW_MULTIPLE_REGS
254  struct mcbsp_util_stack ** table;
255 #else
257 #endif
258 };
259 
264 
266  size_t *pinning;
267 
270 };
271 
276 
283 void mcbsp_util_stack_initialize( struct mcbsp_util_stack * const stack, const size_t elementSize );
284 
292 void mcbsp_util_stack_grow( struct mcbsp_util_stack * const stack, const size_t target_size );
293 
299 bool mcbsp_util_stack_empty( const struct mcbsp_util_stack * const stack );
300 
314 void * mcbsp_util_stack_pop( struct mcbsp_util_stack * const stack );
315 
326 void * mcbsp_util_stack_peek( const struct mcbsp_util_stack * const stack );
327 
335 void mcbsp_util_stack_push( struct mcbsp_util_stack * const stack, const void * const item );
336 
342 void mcbsp_util_stack_destroy( struct mcbsp_util_stack * const stack );
343 
350 void mcbsp_util_varstack_grow( struct mcbsp_util_stack * const stack, const size_t requested_size );
351 
360 void * mcbsp_util_varstack_pop( struct mcbsp_util_stack * const stack, const size_t size );
361 
372 inline void * mcbsp_util_varstack_regpop( struct mcbsp_util_stack * const stack ) {
373  return mcbsp_util_varstack_pop( stack, stack->size );
374 }
375 
383 void * mcbsp_util_varstack_peek( const struct mcbsp_util_stack * const stack, const size_t size );
384 
395 inline void * mcbsp_util_varstack_regpeek( const struct mcbsp_util_stack * const stack ) {
396  return mcbsp_util_varstack_peek( stack, stack->size );
397 }
398 
406 void mcbsp_util_varstack_push( struct mcbsp_util_stack * const stack, const void * const item, const size_t size );
407 
418 inline void mcbsp_util_varstack_regpush( struct mcbsp_util_stack * const stack, const void * const item ) {
419  mcbsp_util_varstack_push( stack, item, stack->size );
420 }
421 
430 void mcbsp_util_address_table_initialize( struct mcbsp_util_address_table * const table, const size_t P );
431 
442 void mcbsp_util_address_table_grow( struct mcbsp_util_address_table * const table, const size_t target_size );
443 
452 void mcbsp_util_address_table_setsize( struct mcbsp_util_address_table * const table, const size_t target_size );
453 
462 
480  struct mcbsp_util_address_table * const table,
481  const size_t key,
482  const size_t version,
483  void * const value,
484  const size_t size
485 );
486 
500  const struct mcbsp_util_address_table * const table,
501  const size_t key,
502  const size_t version
503 );
504 
514 bool mcbsp_util_address_table_delete( struct mcbsp_util_address_table * const table, const size_t key, const size_t version );
515 
521 void mcbsp_util_address_map_initialize( struct mcbsp_util_address_map * const address_map );
522 
528 void mcbsp_util_address_map_grow( struct mcbsp_util_address_map * const address_map );
529 
535 void mcbsp_util_address_map_destroy( struct mcbsp_util_address_map * const address_map );
536 
544 size_t mcbsp_util_address_map_get( const struct mcbsp_util_address_map * const address_map, const void * const key );
545 
553 void mcbsp_util_address_map_insert( struct mcbsp_util_address_map * const address_map, void * const key, const size_t value );
554 
562 void mcbsp_util_address_map_remove( struct mcbsp_util_address_map * const address_map, void * const key );
563 
577 size_t mcbsp_util_address_map_binsearch( const struct mcbsp_util_address_map * const address_map, const void * const key, size_t lo, size_t hi );
578 
586 
593 
602 void mcbsp_util_destroyMachineInfo( void * machine_info );
603 
608 int mcbsp_util_integer_compare( const void * a, const void * b );
609 
616 size_t mcbsp_util_log2( size_t x );
617 
631 size_t mcbsp_util_sort_unique_integers( size_t * const array, const size_t length, const size_t lower_bound, const size_t upper_bound );
632 
643 bool mcbsp_util_contains( const size_t * const array, const size_t value, const size_t lo, const size_t hi );
644 
655 struct mcbsp_util_pinning_info mcbsp_util_pinning( const size_t P, struct mcbsp_util_machine_info * const machine, const struct mcbsp_util_machine_partition * const super_machine );
656 
664 void mcbsp_util_fatal( void );
665 
673 void mcbsp_util_check_machine_info( struct mcbsp_util_machine_info * const machine );
674 
682 
691 struct mcbsp_util_stack mcbsp_util_partition( const struct mcbsp_util_machine_partition subpart, const size_t P, const enum mcbsp_affinity_mode affinity );
692 
702 size_t * mcbsp_util_derive_standard_pinning( const struct mcbsp_util_machine_partition * iterator, const size_t size );
703 
713 void mcbsp_util_filter_standard_pinning( const struct mcbsp_util_machine_info * const info, size_t * const array, const size_t P );
714 
723 void mcbsp_util_translate_standard_pinning( const struct mcbsp_util_machine_info * const info, size_t * const array, const size_t P );
724 
737 size_t mcbsp_util_array_sizet_lbound( const size_t * const array, const size_t key, size_t lo, size_t hi );
738 
747 #ifdef MCBSP_CPLUSPLUS
748  void mcbsp_util_memcpy( void * const __restrict__ dest, const void * const __restrict__ src, const size_t size );
749 #else
750  void mcbsp_util_memcpy( void * const restrict dest, const void * const restrict src, const size_t size );
751 #endif
752 
767 void * mcbsp_util_malloc( const size_t size, const char * const name );
768 
769 #endif
770 
size_t size
Length of global area.
Definition: mcutil.h:201
void * mcbsp_util_stack_peek(const struct mcbsp_util_stack *const stack)
Returns the top stack item.
void mcbsp_util_address_table_setsize(struct mcbsp_util_address_table *const table, const size_t target_size)
Ensures the address table is of at least the given size.
size_t cores
The number of hardware cores available on the current system.
Definition: mcutil.h:115
size_t num_reserved_cores
Number of entries in the reserved_cores array.
Definition: mcutil.h:141
void * mcbsp_util_varstack_peek(const struct mcbsp_util_stack *const stack, const size_t size)
Peeks for a variably-sized item on the stack.
void ** keys
Keys.
Definition: mcutil.h:187
void mcbsp_util_stack_destroy(struct mcbsp_util_stack *const stack)
Frees all memory related to a given stack.
void mcbsp_util_stack_initialize(struct mcbsp_util_stack *const stack, const size_t elementSize)
Initialises the mcbsp_util_stack struct.
struct mcbsp_util_stack mcbsp_util_partition(const struct mcbsp_util_machine_partition subpart, const size_t P, const enum mcbsp_affinity_mode affinity)
Derives a P-way subpartition from a given machine partition.
bool Cset
Whether the cores field has been set manually.
Definition: mcutil.h:78
A map from pointers to unsigned long ints.
Definition: mcutil.h:178
size_t mcbsp_util_sort_unique_integers(size_t *const array, const size_t length, const size_t lower_bound, const size_t upper_bound)
Sorts an array of integers.
void mcbsp_util_address_map_insert(struct mcbsp_util_address_map *const address_map, void *const key, const size_t value)
Inserts a key-value pair in the map.
Structure representing the machine hardware information.
Definition: mcutil.h:69
size_t threads
The total number of threads available for computation.
Definition: mcutil.h:102
Self-growing stack.
Definition: mcutil.h:206
size_t cap
Capacity.
Definition: mcutil.h:247
void mcbsp_util_address_table_set(struct mcbsp_util_address_table *const table, const size_t key, const size_t version, void *const value, const size_t size)
Sets an entry in a given address table.
size_t * pinning
Pinning array.
Definition: mcutil.h:266
bool mcbsp_util_contains(const size_t *const array, const size_t value, const size_t lo, const size_t hi)
Returns whether an array contains a given value.
size_t size
Size of a single entry in bytes.
Definition: mcutil.h:215
size_t mcbsp_util_log2(size_t x)
Binary logarithm, unsigned integer version, rounding up.
size_t top
Number of entries in the stack.
Definition: mcutil.h:212
size_t safe_cp_f
The checkpointing frequency used when trouble is imminent.
Definition: mcutil.h:174
mcbsp_affinity_mode
Pre-defined strategies for pinning threads.
Definition: mcbsp-affinity.h:204
bool Tset
Whether the threads field has been set manually.
Definition: mcutil.h:72
size_t * manual_affinity
Pointer to the manually-defined affinity definition.
Definition: mcutil.h:136
void * mcbsp_util_varstack_pop(struct mcbsp_util_stack *const stack, const size_t size)
Returns the top stack item of variable size, and shortens the stack by that item size.
void mcbsp_util_varstack_regpush(struct mcbsp_util_stack *const stack, const void *const item)
Pushes a fixed-size item on the stack.
Definition: mcutil.h:418
size_t threads_per_core
The number of hardware threads per core on the system MulticoreBSP is executing on.
Definition: mcutil.h:121
void * mcbsp_util_varstack_regpeek(const struct mcbsp_util_stack *const stack)
Peeks for a fixed-sized item on the stack.
Definition: mcutil.h:395
struct mcbsp_util_stack partition
Stack of machine partitions, one for each pinning.
Definition: mcutil.h:269
struct mcbsp_util_machine_partition mcbsp_util_derive_partition(const struct mcbsp_util_machine_info *const info)
Derives an initial machine partition from a machine_info object.
size_t * reserved_cores
Pointer to the manually-defined reversed-cores list.
Definition: mcutil.h:152
enum mcbsp_thread_numbering thread_numbering
Currently active thread-numbering method.
Definition: mcutil.h:124
mcbsp_thread_numbering
Enumerates ways of hardware thread numbering.
Definition: mcbsp-affinity.h:234
struct mcbsp_util_machine_info * mcbsp_util_createMachineInfo(void)
Creates a new machine info struct.
void mcbsp_util_address_table_grow(struct mcbsp_util_address_table *const table, const size_t target_size)
Increases the capacity of a given address table to match at least a given capacity.
void mcbsp_util_memcpy(void *const restrict dest, const void *const restrict src, const size_t size)
Copies the data in [src,src+size) to [dest,dest+size).
Return type of mcbsp_util_pinning.
Definition: mcutil.h:263
size_t start
Start hardware thread index.
Definition: mcutil.h:56
void * mcbsp_util_malloc(const size_t size, const char *const name)
Memory allocation function used for internal MulticoreBSP for C memory areas.
void mcbsp_util_address_table_initialize(struct mcbsp_util_address_table *const table, const size_t P)
Initialises the mcbsp_util_address_table struct.
struct mcbsp_util_pinning_info mcbsp_util_pinning(const size_t P, struct mcbsp_util_machine_info *const machine, const struct mcbsp_util_machine_partition *const super_machine)
Function that yields a pinning of P threads according to the given machine info.
void mcbsp_util_filter_standard_pinning(const struct mcbsp_util_machine_info *const info, size_t *const array, const size_t P)
Filters reserved cores from a standard pinning list.
size_t end
What the last hardware thread is.
Definition: mcutil.h:62
void mcbsp_util_address_map_initialize(struct mcbsp_util_address_map *const address_map)
Initialises a mcbsp_util_address_map struct.
bool CPFset
Whether the checkpoint_frequency field has been overridden.
Definition: mcutil.h:96
bool Aset
Whether the affinity field as been set manually.
Definition: mcutil.h:75
size_t mcbsp_util_array_sizet_lbound(const size_t *const array, const size_t key, size_t lo, size_t hi)
Searches an array for the largest value lower or equal to a given key.
void mcbsp_util_varstack_push(struct mcbsp_util_stack *const stack, const void *const item, const size_t size)
Pushes a variably-sized item on the stack.
bool mcbsp_util_address_table_delete(struct mcbsp_util_address_table *const table, const size_t key, const size_t version)
Removes an entry from a given address table.
void mcbsp_util_translate_standard_pinning(const struct mcbsp_util_machine_info *const info, size_t *const array, const size_t P)
Translates thread numbers in the standard pinning format (consecutive), to the numbering format activ...
void mcbsp_util_destroyMachineInfo(void *machine_info)
If a current machine info instance exists, destroys it.
size_t mcbsp_util_address_map_binsearch(const struct mcbsp_util_address_map *const address_map, const void *const key, size_t lo, size_t hi)
Helper-function for mcbsp_util_address_map_insert, and mcbsp_util_address_map_remove.
void * array
Stack entries.
Definition: mcutil.h:218
void mcbsp_util_address_map_remove(struct mcbsp_util_address_map *const address_map, void *const key)
Removes a key-value pair from the map.
void mcbsp_util_fatal(void)
Handles a fatal error in a uniform fashion.
size_t cap
Stack capacity in the number of elements.
Definition: mcutil.h:209
void * mcbsp_util_stack_pop(struct mcbsp_util_stack *const stack)
Returns the newest item in the stack.
void mcbsp_util_varstack_grow(struct mcbsp_util_stack *const stack, const size_t requested_size)
Doubles the capacity of a given stack that consists of elements of variable size. ...
size_t P
Number of local versions.
Definition: mcutil.h:250
size_t * values
Values.
Definition: mcutil.h:190
int mcbsp_util_integer_compare(const void *a, const void *b)
Comparison function for use with qsort on arrays of size_t&#39;s.
void * address
Local address.
Definition: mcutil.h:198
A table of local address locations per SPMD variable.
Definition: mcutil.h:241
size_t * mcbsp_util_derive_standard_pinning(const struct mcbsp_util_machine_partition *iterator, const size_t size)
Derives a pinning from an array of machine partitions.
void mcbsp_util_address_map_destroy(struct mcbsp_util_address_map *const address_map)
Frees the memory related to a given address map.
bool TNset
Whether the thread numbering field has been set manually.
Definition: mcutil.h:84
bool TpCset
Whether the threads_per_core field has been set manually.
Definition: mcutil.h:81
size_t mcbsp_util_address_map_get(const struct mcbsp_util_address_map *const address_map, const void *const key)
Address map accessor.
pthread_mutex_t mutex
Mutex for locking-out write access to the global table.
Definition: mcutil.h:244
size_t size
Size.
Definition: mcutil.h:184
void * mcbsp_util_varstack_regpop(struct mcbsp_util_stack *const stack)
Returns the top fixed-size item of the stack.
Definition: mcutil.h:372
size_t mcbsp_util_detect_hardware_threads(void)
Attempts to detect the number of hard-ware threads supported on the current machine.
bool UTset
Whether the unused_threads_per_core field has been set.
Definition: mcutil.h:93
bool Pset
Whether the manual pinning field has been set.
Definition: mcutil.h:87
size_t unused_threads_per_core
Number of threads per core that should remain unused.
Definition: mcutil.h:158
Structure representing a part of the current machine.
Definition: mcutil.h:53
void mcbsp_util_address_map_grow(struct mcbsp_util_address_map *const address_map)
Doubles the capacity of a given address map.
void mcbsp_util_stack_grow(struct mcbsp_util_stack *const stack, const size_t target_size)
Increases the capacity of a given stack to match or exceed a given target capacity.
void mcbsp_util_stack_push(struct mcbsp_util_stack *const stack, const void *const item)
Pushes a new item on the stack.
void mcbsp_util_address_table_destroy(struct mcbsp_util_address_table *const table)
Frees the memory associated with a given address table.
bool SCFset
Whether the safe_checkpoint_frequency field has been overridden.
Definition: mcutil.h:99
bool Rset
Whether the reserved cores field has been set.
Definition: mcutil.h:90
size_t cp_f
The default checkpointing frequency.
Definition: mcutil.h:166
bool mcbsp_util_stack_empty(const struct mcbsp_util_stack *const stack)
Checks whether a given stack is empty.
struct mcbsp_util_address_table_entry * mcbsp_util_address_table_get(const struct mcbsp_util_address_table *const table, const size_t key, const size_t version)
Gets an entry from a given address table.
size_t cap
Capacity.
Definition: mcutil.h:181
struct mcbsp_util_address_table_entry ** table
Table entries.
Definition: mcutil.h:256
A single address table entry.
Definition: mcutil.h:195
struct mcbsp_util_address_map MCBSP_UTIL_EMPTY_ADDRESS_MAP
A default initialiser of type mcbsp_util_address_map.
void mcbsp_util_check_machine_info(struct mcbsp_util_machine_info *const machine)
Function that prepares a mcbsp_util_machine_info for use.
enum mcbsp_affinity_mode affinity
Currently active affinity strategy.
Definition: mcutil.h:105
size_t stride
How much farther away the next hardware thread is.
Definition: mcutil.h:59