namespace.c 3.42 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
static int serialHLF = 1;
Deike Kleberg's avatar
Deike Kleberg committed
12
13
static int * hasLocalFiles = &serialHLF;
static int serialRS = STAGE_DEFINITION;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
static statusCode * resStatus = (statusCode *) &serialRS;
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

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

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


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

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

61

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

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

  return tin;
}

72

Deike Kleberg's avatar
Deike Kleberg committed
73
void namespaceInit ( int nspn, int * argHasLocalFile )
74
{
Thomas Jahns's avatar
Thomas Jahns committed
75
  /* FIXME: this should not be PIO-only */
76
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
77
  int nspID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78

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

81
82
83
  nNamespaces = nspn;
  if ( nspn >= 1 )
    {
Deike Kleberg's avatar
Deike Kleberg committed
84
      hasLocalFiles = xmalloc ( nspn * sizeof ( hasLocalFiles[0] ));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
      for ( nspID = 0; nspID < nspn; nspID++ )
Deike Kleberg's avatar
Deike Kleberg committed
86
	hasLocalFiles[nspID] = argHasLocalFile[nspID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
      resStatus = xmalloc ( nspn * sizeof ( resStatus[0] ));
88
    }
89
#endif
90
91
92
}


Deike Kleberg's avatar
Deike Kleberg committed
93
void namespaceCleanup ( void )
94
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
  if ( nNamespaces > 1 )
96
    {
Deike Kleberg's avatar
Deike Kleberg committed
97
98
      free ( hasLocalFiles );
      hasLocalFiles = NULL;
99
      free ( resStatus );
100
    }
101
102
}

103

104
105
106
107
108
109
int namespaceGetNumber ()
{
  return nNamespaces;
}


110
void pioNamespaceSetActive ( int nId )
111
{
Thomas Jahns's avatar
Thomas Jahns committed
112
  /* FIXME: this should not be PIO-only */
Deike Kleberg's avatar
Deike Kleberg committed
113
#ifdef USE_MPI
114
  xassert ( nId < nNamespaces && nId >= 0 );
115
116

  activeNamespace = nId;
Deike Kleberg's avatar
Deike Kleberg committed
117
#endif
118
119
}

120

121
122
123
124
int namespaceGetActive ()
{
  return activeNamespace;
}
125
126
127
128


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

Deike Kleberg's avatar
Deike Kleberg committed
131
  return hasLocalFiles ? hasLocalFiles[nId] : 0;
132
}
133
134
135
136
137
138
139
140
141
142
143
144


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
145
  xassert ( tin.nsp == nspTarget );
146
147

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

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


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
164

Deike Kleberg's avatar
Deike Kleberg committed
165
166
167
  return namespaceIdxEncode2 ( nsp, tin.idx );
}

168

169
void namespaceDefResStatus ( statusCode argResStatus )
170
171
{
  int nsp = namespaceGetActive ();
172
  resStatus[nsp] = argResStatus;
173
174
175
}


176
statusCode namespaceInqResStatus ( void )
177
178
{
  int nsp = namespaceGetActive ();
179
  return resStatus[nsp];
180
181
}

182
183
184
185
186
187
188
189
190
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */