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

9
10

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

15
16
17
enum {
  intbits = sizeof(int) * CHAR_BIT,
  nspbits = 4,
18
19
20
  idxbits = intbits - nspbits,
  nspmask = ((1<<nspbits) - 1) << idxbits,
  idxmask = (1<<idxbits) - 1,
21
22
23
24
};

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

28
29
30

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

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


int namespaceIdxEncode ( namespaceTuple_t tin )
{
48
  assert ( tin.nsp < NUM_NAMESPACES && tin.idx < NUM_IDX);
49
  return ( tin.nsp << idxbits ) + tin.idx;
50
}
51

52
int namespaceIdxEncode2 ( int nsp, int idx )
53
54
55
56
57
{
  assert(nsp < NUM_NAMESPACES && idx < NUM_IDX);
  return ( nsp << idxbits ) + idx;
}

58

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

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

  return tin;
}

69

70
71
void pioNamespaceInit ( int nspn, int * argHasLocalFile )
{
72
#ifdef USE_MPI
73
74
75
76
77
78
  int i;
  assert(nspn <= NUM_NAMESPACES && nspn >= 1 );
  nNamespaces = nspn;

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


void pioNamespaceCleanup ( void )
88
{
89
90
91
92
93
  if ( nNamespaces > 1 ) 
      {
          free ( hasLocalFile );
          hasLocalFile = NULL;
      }
94
95
}

96

97
98
99
100
101
102
int namespaceGetNumber ()
{
  return nNamespaces;
}


103
void pioNamespaceSetActive ( int nId )
104
105
106
107
108
109
110
{
  if ( nId >= nNamespaces || nId < 0 )
    abort ();

  activeNamespace = nId;
}

111

112
113
114
115
int namespaceGetActive ()
{
  return activeNamespace;
}
116
117
118
119
120
121
122


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


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);

  assert ( tin.nsp == nspTarget );

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


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 );
}

160
161
162
163
164
165
166
167
168
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */