PLUTO
al_szptr_.c
Go to the documentation of this file.
1 /* ///////////////////////////////////////////////////////////////////// */
2 /*!
3  \file
4  \brief Miscellaneous of internal routines to convert sz pointers
5  to integer pointers.
6 
7  \author A. Malagoli (University of Chicago)
8  \date Jul 17, 1999
9 */
10 /* ///////////////////////////////////////////////////////////////////// */
11 #include "al_hidden.h" /*I "al_hidden.h" I*/
12 
13 /* In order to make life easier with the Fortran interface,
14  we do not pass the pointers to the SZ structures directly,
15  but we rather allocate statically an array of pointers to
16  SZ structures, and pass an integer index that points to
17  an entry in this array */
19 
20 /* The array of SZ pointers is treated as a stack, and
21  the routines in this file take care of allocating
22  and deallocating pointers to this array. So, we maintain
23  an array of integers for the bookkeeping, in order to
24  define which entries are free, and which entries are
25  allocated */
27 
28 /* More bookkeeping variables */
29 static int stack_top; /* First unallocated SZ pointer */
30 static int stack_used; /* Number of used entries in the stack */
31 
32 /* ********************************************************************* */
34 /*!
35  * Initialize the stack of SZ pointers
36  * This routine is called internally by AL_Init.
37  ********************************************************************** */
38 {
39  register int i;
40  int myrank;
41 
42  stack_top = 0;
43  stack_used = 0;
44 
45  for( i=0; i<AL_MAX_ARRAYS;i++){ stack_ptr[i] = AL_STACK_FREE ;}
46 
47  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
48 
49 #ifdef DEBUG
50  printf("AL_Init_stack_: SZ stack initialized\n");
51 #endif
52 
53  return 0;
54 }
55 
56 
57 /* ********************************************************************* */
59 /*!
60  * Return an integer pointer to an SZ stack entry.
61  *********************************************************************** */
62 {
63  register int i;
64  int sz_ptr; /* The returned pointer to the SZ array */
65 
66  sz_ptr = -1;
67 
68  /*
69  First, search the stack for the first
70  available pointer
71  */
72  for( i=0; i<AL_MAX_ARRAYS; i++){
73  if( stack_ptr[i] == AL_STACK_FREE ){
74  sz_ptr = i;
76  break;
77  }
78  }
79 
80  /*
81  If the allocation did not fail, then proceed to
82  allocate a SZ structure
83  */
84  if( sz_ptr != -1 ){
85  if( !(sz_stack[i] = (SZ *)malloc(sizeof(SZ))) ){
86  printf("AL_Allocate_sz_: Failed to allocate SZ\n");
87  }
88  }
89 
90  /*
91  If we were successful, then set sz.compiled to AL_FALSE,
92  indicating the need for intialization.
93  */
94  sz_stack[sz_ptr]->compiled = AL_FALSE;
95 
96  return sz_ptr;
97 
98 }
99 
100 
101 /* ********************************************************************* */
102 int AL_Deallocate_sz_(int sz_ptr)
103 /*!
104  * Deallocate an integer pointer to an SZ stack entry.
105  *
106  * \param [in] sz_ptr Integer pointer to an entry in the SZ stack
107  *********************************************************************** */
108 {
109  register int i;
110  int ndim;
111  SZ *s;
112 
113  if(sz_ptr<0) return AL_SUCCESS;
114  if(stack_ptr[sz_ptr] == AL_STACK_FREE) return AL_SUCCESS;
115 
116  s = sz_stack[sz_ptr];
117  ndim = s->ndim;
118 
119  /*
120  Free the MPI data types
121  */
122  if( s->compiled == AL_TRUE ){
123  for( i=0; i<ndim; i++){
124  MPI_Type_free(&(s->strided[i]));
125  MPI_Type_free(&(s->type_lr[i]));
126  MPI_Type_free(&(s->type_rl[i]));
127  MPI_Comm_free(&(s->oned_comm[i]));
128  }
129 
130  MPI_Type_free(&(s->gsubarr));
131  MPI_Type_free(&(s->lsubarr));
132  MPI_Comm_free(&(s->cart_comm));
133  }
134 
135  if( (s->begs != NULL) ) free(s->begs);
136 
137  /*
138  Begin by dellocating the SZ structure
139  */
140  free(sz_stack[sz_ptr]);
141 
142  /*
143  Now we can reset the integer pointer
144  */
145  stack_ptr[sz_ptr] = AL_STACK_FREE;
146  return (int) AL_SUCCESS;
147 }
148 
149 /* ********************************************************************* */
150 int AL_Valid_ptr(int sz_ptr)
151 /*!
152  * Return AL_TRUE if the input pointer points
153  * to an allocated distributed array.
154  * Return AL_FALSE otherwise.
155  *
156  * \param [in] sz_ptr Integer pointer to an entry in the SZ stack
157  *********************************************************************** */
158 {
159  if(stack_ptr[sz_ptr]!=AL_STACK_USED ) return AL_FALSE;
160  return sz_stack[sz_ptr]->compiled;
161 }
int stack_ptr[AL_MAX_ARRAYS]
Definition: al_szptr_.c:26
#define AL_SUCCESS
Definition: al_codes.h:32
#define AL_TRUE
Definition: al_codes.h:28
#define AL_STACK_FREE
Definition: al_codes.h:24
static int stack_used
Definition: al_szptr_.c:30
int * begs
Definition: al_hidden.h:63
MPI_Datatype gsubarr
Definition: al_hidden.h:110
MPI_Comm oned_comm[AL_MAX_DIM]
Definition: al_hidden.h:50
int AL_Allocate_sz_()
Definition: al_szptr_.c:58
MPI_Comm cart_comm
Definition: al_hidden.h:49
#define AL_STACK_USED
Definition: al_codes.h:25
MPI_Datatype type_rl[AL_MAX_DIM]
Definition: al_hidden.h:106
MPI_Datatype lsubarr
Definition: al_hidden.h:111
int AL_Deallocate_sz_(int sz_ptr)
Definition: al_szptr_.c:102
SZ * sz_stack[AL_MAX_ARRAYS]
Definition: al_szptr_.c:18
int AL_Init_stack_()
Definition: al_szptr_.c:33
int AL_Valid_ptr(int sz_ptr)
Definition: al_szptr_.c:150
MPI_Datatype strided[AL_MAX_DIM]
Definition: al_hidden.h:97
#define s
static int stack_top
Definition: al_szptr_.c:29
int i
Definition: analysis.c:2
#define AL_FALSE
Definition: al_codes.h:29
Definition: al_hidden.h:38
Internal include file for the ArrayLib.
int compiled
Definition: al_hidden.h:39
MPI_Datatype type_lr[AL_MAX_DIM]
Definition: al_hidden.h:104
#define AL_MAX_ARRAYS
Definition: al_codes.h:21
int ndim
Definition: al_hidden.h:43