namespace.c 3.21 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
static int RAS = 0;
static int * resASent = &RAS;
16

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

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

30
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
46
47
48
49
}


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

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

60

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

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

  return tin;
}

71

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

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


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

100

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


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

  activeNamespace = nId;
}

115

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


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


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
147
148
149
150
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 ();
 
  return namespaceIdxEncode2 ( nsp, tin.idx );
}

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

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


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

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