namespace.c 3.49 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
static int activeNamespace = 0;
11
12
13
14
15
16
17
18

struct namespace
{
  int hasLocalFiles;
  statusCode resStage;
} initialNamespace = { .hasLocalFiles = 1, .resStage = STAGE_DEFINITION };

struct namespace *namespaces = &initialNamespace;
19

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

enum {
  NUM_NAMESPACES = 1 << nspbits,
30
  NUM_IDX = 1 << idxbits,
31
};
32

33

Thomas Jahns's avatar
Thomas Jahns committed
34
#if 0
35
36
void namespaceShowbits ( int n, char *name )
{
37
38
39
  int i;
  unsigned mask;
  char bitvalues[intbits + 1];
40

41
  mask = 1;
42
  for ( i = 0; i < intbits; i++ )
43
    {
44
45
      bitvalues[i] = ((unsigned)n & mask) ? '1':'0';
      mask <<= 1;
46
    }
47
48
  bitvalues[intbits] = '\0';
  fprintf (stdout, "%s: %s\n", name, bitvalues );
49
}
Thomas Jahns's avatar
Thomas Jahns committed
50
#endif
51
52
53
54


int namespaceIdxEncode ( namespaceTuple_t tin )
{
Deike Kleberg's avatar
Deike Kleberg committed
55
  xassert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
56
  return ( tin.nsp << idxbits ) + tin.idx;
57
}
58

59
int namespaceIdxEncode2 ( int nsp, int idx )
60
{
Deike Kleberg's avatar
Deike Kleberg committed
61
  xassert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
62
63
64
  return ( nsp << idxbits ) + idx;
}

65

Deike Kleberg's avatar
Deike Kleberg committed
66
namespaceTuple_t namespaceResHDecode ( int resH )
67
68
69
{
  namespaceTuple_t tin;

Deike Kleberg's avatar
Deike Kleberg committed
70
71
  tin.idx = resH & idxmask;
  tin.nsp = (int)(((unsigned)( resH & nspmask )) >> idxbits);
72
73
74
75

  return tin;
}

76

Deike Kleberg's avatar
Deike Kleberg committed
77
void namespaceInit ( int nspn, int * argHasLocalFile )
78
{
Thomas Jahns's avatar
Thomas Jahns committed
79
  /* FIXME: this should not be PIO-only */
80
#ifdef USE_MPI
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81

Deike Kleberg's avatar
Deike Kleberg committed
82
  xassert(nspn <= NUM_NAMESPACES && nspn >= 1 );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83

84
85
86
  nNamespaces = nspn;
  if ( nspn >= 1 )
    {
87
88
89
90
91
92
      namespaces = xmalloc(nspn * sizeof (namespaces[0]));
      for (int nspID = 0; nspID < nspn; ++nspID)
        {
          namespaces[nspID].hasLocalFiles = argHasLocalFile[nspID];
          namespaces[nspID].resStage = STAGE_DEFINITION;
        }
93
    }
94
#endif
95
96
97
}


Deike Kleberg's avatar
Deike Kleberg committed
98
void namespaceCleanup ( void )
99
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
  if ( nNamespaces > 1 )
101
    {
102
103
104
      free(namespaces);
      namespaces = &initialNamespace;
      nNamespaces = 1;
105
    }
106
107
}

108

109
110
111
112
113
114
int namespaceGetNumber ()
{
  return nNamespaces;
}


115
void pioNamespaceSetActive ( int nId )
116
{
Thomas Jahns's avatar
Thomas Jahns committed
117
  /* FIXME: this should not be PIO-only */
Deike Kleberg's avatar
Deike Kleberg committed
118
#ifdef USE_MPI
119
  xassert ( nId < nNamespaces && nId >= 0 );
120
121

  activeNamespace = nId;
Deike Kleberg's avatar
Deike Kleberg committed
122
#endif
123
124
}

125

126
127
128
129
int namespaceGetActive ()
{
  return activeNamespace;
}
130
131
132
133


int namespaceHasLocalFile ( int nId )
{
134
  xassert ( nId < nNamespaces && nId >= 0 );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135

136
  return namespaces ? namespaces[nId].hasLocalFiles : 0;
137
}
138
139
140
141
142
143
144
145
146
147
148
149


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
150
  xassert ( tin.nsp == nspTarget );
151
152

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153

154
155
  return namespaceIdxEncode2 ( nsp, tin.idx );
}
Deike Kleberg's avatar
Deike Kleberg committed
156
157
158
159
160
161
162
163
164
165
166
167
168


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 ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169

Deike Kleberg's avatar
Deike Kleberg committed
170
171
172
  return namespaceIdxEncode2 ( nsp, tin.idx );
}

173

174
void namespaceDefResStatus ( statusCode argResStatus )
175
176
{
  int nsp = namespaceGetActive ();
177
  namespaces[nsp].resStage = argResStatus;
178
179
180
}


181
statusCode namespaceInqResStatus ( void )
182
183
{
  int nsp = namespaceGetActive ();
184
  return namespaces[nsp].resStage;
185
186
}

187
188
189
190
191
192
193
194
195
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */