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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*@@
@file MaskUtils.c
@date October 2002
@author Denis Pollney
@desc
Utilities for registering/setting/checking the mask.
@desc
@version $Header$
@@*/
#include <stdio.h>
#include "cctk.h"
#include "cctk_FortranString.h"
#include "SpaceMask.h"
static const char* rcsid = "$Header$";
CCTK_FILEVERSION(CACTUSEINSTEIN_SPACEMASK_MaskUtils_c);
SpaceMask_Registry* spacemask_registry = NULL;
/*@@
@routine SpaceMask_get_bit_nbr
@author Denis Pollney
@date 15 October 2002
@desc
Return the number of bits required to represent a given number.
Cheesy divide-by-two method, maybe something else is quicker.
@enddesc
@@*/
int
SpaceMask_get_bit_nbr(int nstates)
{
int bstates;
int bit_nbr;
bstates = nstates-1;
for (bit_nbr=0; bstates>0; ++bit_nbr)
bstates /= 2;
return bit_nbr;
}
/*@@
@routine SpaceMask_get_free_bits
@author Denis Pollney
@date 15 October 2002
@desc
Determine the mask bits which have not yet been allocated.
The return value is a bitmask with the requested number of
free bits set to 1. If there are not enough free bits left
to satisfy the request, stop with an error.
@enddesc
@@*/
CCTK_INT8
SpaceMask_get_free_bits(int nbits)
{
CCTK_INT8 used_bits;
CCTK_INT8 new_bits;
int i;
int j;
int n;
used_bits = 0;
if (spacemask_registry != NULL)
{
for (i=0; i<spacemask_registry->ntypes; ++i)
used_bits |= spacemask_registry->type_list[i]->bitmask;
}
n=1;
new_bits = 0;
j = 0;
for (i=0; i<sizeof(CCTK_INT8)*8 & j<nbits; ++i)
{
if (!(n & used_bits))
{
++j;
new_bits |= n;
}
n *= 2;
}
if (j<nbits)
CCTK_WARN (0, "Cannot allocate mask: Not enough free bits.");
return new_bits;
}
/*@@
@routine SpaceMask_determine_state_mask
@author Denis Pollney
@date 15 October 2002
@desc
Determine appropriate bitmasks to represent a number of
states, using the allocated bit mask.
That is, if allocated_bits is set to 00111000, then
the returned list of states are permutations of the three
non-zero (active) bits.
@enddesc
@@*/
CCTK_INT8*
SpaceMask_determine_state_mask(CCTK_INT8 allocated_bits, int nstates)
{
CCTK_INT8* state_mask;
int n;
int bit;
int i;
int j;
state_mask =
(CCTK_INT8*) malloc (nstates*sizeof(CCTK_INT8));
for (j=0; j<nstates; ++j)
state_mask[j] = 0;
n=1;
bit=1;
for (i=0; i<sizeof(CCTK_INT8)*8; ++i)
{
if (n & allocated_bits)
{
for (j=0; j<nstates; ++j)
{
if (bit & j)
state_mask[j] += n;
}
bit *= 2;
}
n *= 2;
}
return state_mask;
}
/*@@
@routine SpaceMask_setup_new_state
@author Denis Pollney
@date 15 October 2002
@desc
Allocate a new SpaceMask_State, giving it a name and
a bit mask.
@enddesc
@@*/
SpaceMask_State*
SpaceMask_setup_new_state(CCTK_INT8 state_mask, char* name)
{
SpaceMask_State* new_state;
new_state = (SpaceMask_State*) malloc (sizeof(SpaceMask_State));
new_state->name = name;
new_state->bitmask = state_mask;
return new_state;
}
/*@@
@routine SpaceMask_setup_new_type
@author Denis Pollney
@date 15 October 2002
@desc
Allocate a new SpaceMask_Type. This involves allocating
a set of bits within the mask to the new type, and creating
appropriate bitmasks (using the allocated bits) for each of
the requested states.
@enddesc
@@*/
SpaceMask_Type*
SpaceMask_setup_new_type(CCTK_INT8 new_bits, char* type_name,
int nstates, char** state_list,
CCTK_INT8* state_mask)
{
SpaceMask_Type* new_type;
int j;
new_type = (SpaceMask_Type*) malloc (sizeof(SpaceMask_Type));
new_type->bitmask = new_bits;
new_type->nstates = nstates;
new_type->name = type_name;
new_type->state_list =
(SpaceMask_State**) malloc (nstates*sizeof(SpaceMask_State*));
for (j=0; j<nstates; ++j)
new_type->state_list[j] = SpaceMask_setup_new_state(state_mask[j],
state_list[j]);
return new_type;
}
/*@@
@routine SpaceMask_append_type_to_registry
@author Denis Pollney
@date 15 October 2002
@desc
Adds a new type to the spacemask_registry.
@enddesc
@@*/
void
SpaceMask_append_type_to_registry(SpaceMask_Type* new_type)
{
SpaceMask_Type** new_type_list;
int ntypes;
int i;
if (spacemask_registry == NULL)
{
spacemask_registry =
(SpaceMask_Registry*) malloc (sizeof(SpaceMask_Registry));
ntypes = 1;
new_type_list = (SpaceMask_Type**) malloc (sizeof(SpaceMask_Type*));
new_type_list[0] = new_type;
}
else
{
ntypes = spacemask_registry->ntypes + 1;
new_type_list =
(SpaceMask_Type**) malloc (ntypes*sizeof(SpaceMask_Type*));
for (i=0; i<ntypes-1; ++i)
new_type_list[i] = spacemask_registry->type_list[i];
new_type_list[ntypes-1] = new_type;
free(spacemask_registry->type_list);
}
spacemask_registry->ntypes = ntypes;
spacemask_registry->type_list = new_type_list;
}
/*@@
@routine SpaceMask_RegisterType
@author Denis Pollney
@date 15 October 2002
@desc
Allocates a set of bits of the SpaceMask to represent
a set of states of the named type.
@enddesc
@@*/
int
SpaceMask_RegisterType(char* type_name, int nstates, char** state_list)
{
SpaceMask_Type* new_type;
CCTK_INT8 new_bits;
CCTK_INT8* state_mask;
int bit_nbr;
bit_nbr = SpaceMask_get_bit_nbr(nstates);
new_bits = SpaceMask_get_free_bits(bit_nbr);
state_mask = SpaceMask_determine_state_mask(new_bits, nstates);
new_type = SpaceMask_setup_new_type(new_bits, type_name, nstates,
state_list, state_mask);
SpaceMask_append_type_to_registry (new_type);
return 0;
}
/*@@
@routine SpaceMask_AppendStatesToType
@author Denis Pollney
@date 15 October 2002
@desc
Adds a new set of possible states to an already existing
type. If required, new bits of the mask are allocated to the
given state. If bits are not available for this, then an
error is returned.
@enddesc
@@*/
int
SpaceMask_AppendStatesToType(char* type_name, int nstates, char** state_list)
{
CCTK_INT8 new_bits;
CCTK_INT8 allocated_bits;
CCTK_INT8* state_mask;
SpaceMask_Type* old_type;
SpaceMask_Type* new_type;
char** new_state_list;
int i;
int j;
int old_type_idx;
int new_bit_nbr;
int total_nstates;
old_type = NULL;
for (i=0; i<spacemask_registry->ntypes; ++i)
{
if (!strcmp(spacemask_registry->type_list[i]->name, type_name))
{
old_type = spacemask_registry->type_list[i];
old_type_idx = i;
}
}
if (old_type == NULL)
{
CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
"Mask type \"%s\" has not been registered", type_name);
return -1;
}
total_nstates = old_type->nstates + nstates;
new_bit_nbr = SpaceMask_get_bit_nbr(total_nstates) -
SpaceMask_get_bit_nbr(old_type->nstates);
new_bits = SpaceMask_get_free_bits(new_bit_nbr);
allocated_bits = old_type->bitmask | new_bits;
state_mask = SpaceMask_determine_state_mask(allocated_bits, total_nstates);
new_state_list = (char**) malloc (total_nstates*sizeof(char*));
i = 0;
for (j=0; j<old_type->nstates; ++j, ++i)
new_state_list[i] = old_type->state_list[j]->name;
for (j=0; j<nstates; ++j, ++i)
new_state_list[i] = state_list[j];
new_type = SpaceMask_setup_new_type(allocated_bits, type_name, total_nstates,
new_state_list, state_mask);
free(old_type);
spacemask_registry->type_list[old_type_idx] = new_type;
return 0;
}
/*@@
@routine SpaceMask_GetTypeBits
@author Denis Pollney
@date 15 October 2002
@desc
Returns the bitmask corresponding to the given type. Bits
which are allocated to the given type are set to 1, all other
bits are 0.
@enddesc
@@*/
CCTK_INT8
SpaceMask_GetTypeBits(char* type_name)
{
int i;
for (i=0; i<spacemask_registry->ntypes; ++i)
{
if (!strcmp(spacemask_registry->type_list[i]->name, type_name))
return spacemask_registry->type_list[i]->bitmask;
}
CCTK_VInfo (CCTK_THORNSTRING, "Type \"%s\" has not been registered.\n",
type_name);
return 0;
}
/*@@
@routine SpaceMask_GetStateBits
@author Denis Pollney
@date 15 October 2002
@desc
Returns the bitmask corresponding to the given state.
@enddesc
@@*/
CCTK_INT8
SpaceMask_GetStateBits(char* type_name, char* state_name)
{
SpaceMask_Type* type;
int i, j;
for (i=0; i<spacemask_registry->ntypes; ++i)
{
if (!strcmp(spacemask_registry->type_list[i]->name, type_name))
{
type = spacemask_registry->type_list[i];
for (j=0; j<type->nstates; ++j)
{
if (!strcmp(type->state_list[j]->name, state_name))
return type->state_list[j]->bitmask;
}
}
}
CCTK_VInfo (CCTK_THORNSTRING,
"Requested state \"%s\" could not be found in type \"%s\"\n",
state_name, type_name);
return 0;
}
/*@@
@routine SpaceMask_SetState
@author Denis Pollney
@date 15 October 2002
@desc
Sets the mask at a point to the given state.
@enddesc
@@*/
void
SpaceMask_SetState(CCTK_INT8* mask, int point, char* type_name, char* state)
{
CCTK_INT8 type_bits;
CCTK_INT8 state_bits;
type_bits = SpaceMask_GetTypeBits(type_name);
state_bits = SpaceMask_GetStateBits(type_name, state);
SpaceMask_SetStateBits(mask, point, type_bits, state_bits);
}
/*@@
@routine SpaceMask_CheckState
@author Denis Pollney
@date 15 October 2002
@desc
Checks that the mask at a point has the given state, in which
case return 1, otherwise return 0.
@enddesc
@@*/
int
SpaceMask_CheckState(CCTK_INT8* mask, int point, char* type_name, char* state)
{
CCTK_INT8 type_bits;
CCTK_INT8 state_bits;
type_bits = SpaceMask_GetTypeBits(type_name);
state_bits = SpaceMask_GetStateBits(type_name, state);
return SpaceMask_CheckStateBits(mask, point, type_bits, state_bits);
}
/********************************************************************
********************* Fortran Wrappers ***************************
********************************************************************/
/* How do you pass the char** state list from fortran???
void
CCTK_FCALL CCTK_FNAME(SpaceMask_RegisterType)(int* ierr, char* type_name,
int* nstates, char** state_list)
{
*ierr = SpaceMask_RegisterType(type_name, *nstates, state_list);
}
*/
void
CCTK_FCALL CCTK_FNAME(SpaceMask_GetTypeBits)(CCTK_INT8* type_bits,
ONE_FORTSTRING_ARG)
{
ONE_FORTSTRING_CREATE(type_name)
*type_bits = SpaceMask_GetTypeBits(type_name);
free(type_name);
}
void
CCTK_FCALL CCTK_FNAME(SpaceMask_GetStateBits)(CCTK_INT8* state_bits,
TWO_FORTSTRING_ARG)
{
TWO_FORTSTRING_CREATE(type_name, state_name)
*state_bits = SpaceMask_GetStateBits(type_name, state_name);
}
void
CCTK_FCALL CCTK_FNAME(SpaceMask_SetState)(CCTK_INT8* mask,
CCTK_INT* point, TWO_FORTSTRING_ARG)
{
TWO_FORTSTRING_CREATE(type_name, state)
SpaceMask_SetState(mask, *point, type_name, state);
}
void
CCTK_FCALL CCTK_FNAME(SpaceMask_CheckState)(int* retval,
CCTK_INT8* mask,
CCTK_INT* point,
TWO_FORTSTRING_ARG)
{
TWO_FORTSTRING_CREATE(type_name, state)
*retval = SpaceMask_CheckState(mask, *point, type_name, state);
}
|