Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cc03928600b7c709c77618db53165b36471ae260
[simgrid.git] / include / smpi / smpi_cocci.h
1 /* Copyright (c) 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SMPI_COCCI_H
8 #define SMPI_COCCI_H
9
10 /* Macros used by coccinelle-generated code */
11
12 #define SMPI_INITIALIZE_GLOBAL(name,type)                       \
13    NULL;                                                        \
14 void __attribute__((weak,constructor)) __preinit_##name(void) { \
15    if(!name)                                                    \
16       name = (type*)malloc(smpi_global_size() * sizeof(type));  \
17 }                                                               \
18 void __attribute__((weak,destructor)) __postfini_##name(void) { \
19    free(name);                                                  \
20    name = NULL;                                                 \
21 }
22
23 #define SMPI_INITIALIZE_AND_SET_GLOBAl(name,type,expr)          \
24    NULL;                                                        \
25 void __attribute__((weak,constructor)) __preinit_##name(void) { \
26    size_t size = smpi_global_size();                            \
27    size_t i;                                                    \
28    type value = expr;                                           \
29    if(!name) {                                                  \
30       name = (type*)malloc(size * sizeof(type));                \
31       for(i = 0; i < size; i++) {                               \
32          name[i] = value;                                       \
33       }                                                         \
34    }                                                            \
35 }                                                               \
36 void __attribute__((weak,destructor)) __postfini_##name(void) { \
37    free(name);                                                  \
38    name = NULL;                                                 \
39 }
40
41 #define SMPI_GLOBAL_VAR_LOCAL_ACCESS(name) name[__rank()]
42
43 /* This function stores the rank locally, so that a request in
44    SIMIX is not created each time */
45 int __attribute__((weak)) __rank(void) {
46    static __thread int rank = -1;
47
48    if(rank < 0) {
49       rank = smpi_global_rank();
50    }
51    return rank;
52 }
53
54 #endif