namespace.c 3.2 KB
Newer Older
1
#include <limits.h>
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include "cdi.h"
5
#include "namespace.h"
6
7
#include "pio_util.h"

8
9

static int nNamespaces = 1;
10
11
12
static int activeNamespace = 0;
static int HLF = 1;
static int * hasLocalFile = &HLF;
13
14
static int RAS = 0;
static int * resASent = &RAS;
15

16
17
18
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
19
  idxbits = intbits - nspbits,
Deike Kleberg's avatar
Deike Kleberg committed
20
21
  nspmask = (( 1 << nspbits ) - 1) << idxbits,
  idxmask = ( 1 << idxbits ) - 1,
22
23
24
25
};

enum {
  NUM_NAMESPACES = 1 << nspbits,
26
  NUM_IDX = 1 << idxbits,
27
};
28

29
30
31

void namespaceShowbits ( int n, char *name )
{
32
33
34
  int i;
  unsigned mask;
  char bitvalues[intbits + 1];
35

36
  mask = 1;
37
  for ( i = 0; i < intbits; i++ )
38
    {
39
40
      bitvalues[i] = ((unsigned)n & mask) ? '1':'0';
      mask <<= 1;
41
    }
42
43
  bitvalues[intbits] = '\0';
  fprintf (stdout, "%s: %s\n", name, bitvalues );
44
45
46
47
48
}


int namespaceIdxEncode ( namespaceTuple_t tin )
{
Deike Kleberg's avatar
Deike Kleberg committed
49
  xassert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
50
  return ( tin.nsp << idxbits ) + tin.idx;
51
}
52

53
int namespaceIdxEncode2 ( int nsp, int idx )
54
{
Deike Kleberg's avatar
Deike Kleberg committed
55
  xassert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
56
57
58
  return ( nsp << idxbits ) + idx;
}

59

Deike Kleberg's avatar
Deike Kleberg committed
60
namespaceTuple_t namespaceResHDecode ( int resH )
61
62
63
{
  namespaceTuple_t tin;

Deike Kleberg's avatar
Deike Kleberg committed
64
65
  tin.idx = resH & idxmask;
  tin.nsp = (int)(((unsigned)( resH & nspmask )) >> idxbits);
66
67
68
69

  return tin;
}

70

71
72
void pioNamespaceInit ( int nspn, int * argHasLocalFile )
{
73
#ifdef USE_MPI
74
  int i;
Deike Kleberg's avatar
Deike Kleberg committed
75
  xassert(nspn <= NUM_NAMESPACES && nspn >= 1 );
76
77
78
79
  nNamespaces = nspn;

  if ( nspn >= 1 )
    {
80
      hasLocalFile = xmalloc ( nspn * sizeof ( hasLocalFile[0] ));
81
82
      for ( i = 0; i < nspn; i++ )  
	hasLocalFile[i] = argHasLocalFile[i];
83
      resASent = xmalloc ( nspn * sizeof ( resASent[0] )); 
84
    }
85
#endif
86
87
88
89
}


void pioNamespaceCleanup ( void )
90
{
91
  if ( nNamespaces > 1 ) 
92
93
94
95
96
    {
      free ( hasLocalFile );
      hasLocalFile = NULL;
      free ( resASent );
    }
97
98
}

99

100
101
102
103
104
105
int namespaceGetNumber ()
{
  return nNamespaces;
}


106
void pioNamespaceSetActive ( int nId )
107
108
109
110
111
112
113
{
  if ( nId >= nNamespaces || nId < 0 )
    abort ();

  activeNamespace = nId;
}

114

115
116
117
118
int namespaceGetActive ()
{
  return activeNamespace;
}
119
120
121
122
123
124
125


int namespaceHasLocalFile ( int nId )
{
  if ( nId >= nNamespaces || nId < 0 )
    abort ();
  
126
  return hasLocalFile ? hasLocalFile[nId] : 0;
127
}
128
129
130
131
132
133
134
135
136
137
138
139


int namespaceAdaptKey ( int key, int nspTarget )
{
  namespaceTuple_t tin;
  int nsp;

  if ( key == CDI_UNDEFID ) return CDI_UNDEFID;

  tin.idx = key & idxmask;
  tin.nsp = (int)(((unsigned)( key & nspmask )) >> idxbits);

Deike Kleberg's avatar
Deike Kleberg committed
140
  xassert ( tin.nsp == nspTarget );
141
142
143
144
145

  nsp = namespaceGetActive ();
 
  return namespaceIdxEncode2 ( nsp, tin.idx );
}
Deike Kleberg's avatar
Deike Kleberg committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162


int namespaceAdaptKey2 ( int key )
{
  namespaceTuple_t tin;
  int nsp;

  if ( key == CDI_UNDEFID ) return CDI_UNDEFID;

  tin.idx = key & idxmask;
  tin.nsp = (int)(((unsigned)( key & nspmask )) >> idxbits);

  nsp = namespaceGetActive ();
 
  return namespaceIdxEncode2 ( nsp, tin.idx );
}

163
164
165
166
167
168
169
170
171
172
173
174
175
176

void namespaceDefResASent ( void )
{
  int nsp = namespaceGetActive ();
  resASent[nsp] = 1;
}


int namespaceInqResASent ( void )
{
  int nsp = namespaceGetActive ();
  return resASent[nsp];
}

177
178
179
180
181
182
183
184
185
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */