-
Notifications
You must be signed in to change notification settings - Fork 10
/
handles.c
309 lines (218 loc) · 5.16 KB
/
handles.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* 5/20/2005 RML
*
*/
#include "mpiP.h"
#include "type.h"
/*
* handles.c
*
* handle management
* based on code from mpich-1.x/ptrcvt.c
* --> simplified and store item directly in the struct
* rather than as pointer to separately allocated object.
*
* CAVEAT:
* as in mpich-1, storage will grow as needed and will
* remain at the high water mark since it is likely that
* the user code will repeat the use.
*
*/
typedef struct _Handleitem
{
int handle;
struct _Handleitem *next;
union
{
void *anything; /* At least size of void * */
Comm comm;
Req req;
Datatype* type;
} data;
} Handleitem;
/*
* These must be consistent with each other
*
*/
#define BLOCK_ITEMS (256)
#define HANDLE_TO_BLOCK(x) ( (x) >> 8)
#define HANDLE_TO_INDEX(x) ( (x) & 0xff )
#define HANDLE(block,index) ( (block << 8) | (index) )
/*
* The first block of handle items will be statically allocated.
* Subsequent ones will be added if necessary.
* blocks[0..nblocks-1] are allocated at any given time.
*
* Increase MAX_BLOCKS if you *really* need more active request
* (Although probably something is wrong if you need more than 256k !!!)
*
*/
#define MAX_BLOCKS (1024)
static Handleitem block0[BLOCK_ITEMS]; /* array of handleitems */
static Handleitem *(blocks[MAX_BLOCKS]); /* array of pointers to blocks */
static int nblocks;
static int need_to_init=1;
static Handleitem *nextfree;
/************************************************************************/
void *mpi_malloc(int size)
{
void *ret;
ret=malloc(size);
if (!ret)
{
fprintf(stderr,"mpi_malloc: failed to allocate %d bytes\n",size);
abort();
}
return(ret);
}
void mpi_free(void *ptr)
{
free(ptr);
}
/************************************************************************/
/*
* initialize a block s.t. handles are set and
* 0 -> 1 -> 2 ... -> (BLOCK_ITEMS-1) -> NULL
*
*/
static Handleitem *init_block(int block, Handleitem *b)
{
int i;
for (i=0; i<BLOCK_ITEMS-1; i++)
{
b[i].handle= HANDLE(block,i);
b[i].next = &b[i+1];
}
b[BLOCK_ITEMS-1].handle= HANDLE(block,BLOCK_ITEMS-1);
b[BLOCK_ITEMS-1].next=0;
return( &(b[0]) );
}
static void init_handles(void)
{
int i;
Handleitem *new;
/*
* item 0 will not be used (handle 0 maps to NULL)
*
*/
new=init_block(0,block0);
nextfree=new->next; /* Skip over using item 0 */
new->next=NULL;
/*
* initialize the array of blocks
*
*/
blocks[0]=block0;
nblocks=1;
for (i=1; i<MAX_BLOCKS; i++)
blocks[i]=NULL;
need_to_init=0;
}
void mpi_destroy_handles(void)
{
int i;
if (need_to_init)
return;
for (i=1; i<nblocks; i++) /* blocks[0] is statically allocated */
mpi_free(blocks[i]);
need_to_init=1;
}
/************************************************************************/
void mpi_alloc_handle(int *handle, void **data)
{
Handleitem *new;
if (need_to_init)
init_handles();
if (nextfree)
{
new= nextfree;
nextfree= nextfree->next;
new->next=NULL;
*handle= new->handle;
*data= &(new->data);
return;
}
/* there is nothing free, so allocate a new block and add it
* to blocks[]
*/
if (nblocks==MAX_BLOCKS)
{
fprintf(stderr,"mpi_allocate_handle: max %d active handles exceeded\n",
MAX_BLOCKS*BLOCK_ITEMS);
abort();
}
blocks[nblocks]= (Handleitem *)mpi_malloc(sizeof(Handleitem)* BLOCK_ITEMS);
new=init_block(nblocks,blocks[nblocks]);
nextfree= new->next;
new->next=NULL;
*handle= new->handle;
*data= &(new->data);
nblocks++; /* DON'T FORGET THIS!!!! */
#ifdef HANDLE_INFO
fflush(stdout);
fprintf(stderr,"mpi_alloc_handle: allocation %d blocks (%d handles)\n",
nblocks,nblocks*BLOCK_ITEMS);
#endif
}
#ifdef CHECKS
static void verify_handle(int handle, int block, int index)
{
if (block>=nblocks || block<0 ||
index>=BLOCK_ITEMS || index<0)
{
fprintf(stderr,"mpi_verify_handle: bad handle\n");
abort();
}
if (blocks[block][index].handle != handle)
{
fprintf(stderr,"mpi_verify_handle: handle mismatch\n");
abort();
}
}
#endif
void *mpi_handle_to_ptr(int handle)
{
int block;
int index;
if (need_to_init)
init_handles();
if (!handle) /* Handle 0 -> NULL */
return(NULL);
block=HANDLE_TO_BLOCK(handle);
index=HANDLE_TO_INDEX(handle);
#ifdef CHECKS
verify_handle(handle,block,index);
#endif
return( &(blocks[block][index].data) );
}
void mpi_free_handle(int handle)
{
int block;
int index;
Handleitem *item;
if (!handle) /* ignore null handle */
return;
if (need_to_init)
{
fprintf(stderr,"mpi_free_handle: handles not initialized\n");
abort();
}
block=HANDLE_TO_BLOCK(handle);
index=HANDLE_TO_INDEX(handle);
#ifdef CHECKS
verify_handle(handle,block,index);
#endif
item=&(blocks[block][index]);
#ifdef CHECKS
if (item->next)
{
fprintf(stderr,"mpi_free_handle: handle still in use\n");
abort();
}
#endif
/* just return it to the free list.
* space is not reclaimed.
*/
item->next=nextfree;
nextfree=item;
}