pio.c 6.52 KB
Newer Older
Deike Kleberg's avatar
Deike Kleberg committed
1
2
3
4
5
#ifndef NOMPI

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Deike Kleberg's avatar
Deike Kleberg committed
6
#include <string.h>
Deike Kleberg's avatar
Deike Kleberg committed
7
8
9
10
11
12
13

#include "mpi.h"

#include "cdi.h"
#include "pio.h"
#include "pio_impl.h"

14
bool ddebug = false;
Deike Kleberg's avatar
Deike Kleberg committed
15

Deike Kleberg's avatar
Deike Kleberg committed
16
17
const char *token_NODE = "p";

18
19
20
long initial_buffersize = 16 * 1024 * 1024;
/*  4 KB <= x < 256 MB */ 
/* 16 * 1024 * 1024; */
21
/* 16 * 1024; */
22
/* 4 * 1024; */
Deike Kleberg's avatar
Deike Kleberg committed
23

24
25
int tagKey = 100;

Deike Kleberg's avatar
Deike Kleberg committed
26
27
28
29
30
double startTime;
double accumProbe   = 0.0;
double accumRecv    = 0.0;
double accumSend    = 0.0;
double accumSuspend = 0.0;
31

Deike Kleberg's avatar
Deike Kleberg committed
32
33
34
35
36
double accumWait    = 0.0;
double accumWrite   = 0.0;

pioInfo *pioinfo;

37
38
39
40
MPI_Request request = MPI_REQUEST_NULL;

char *token = "%";

Deike Kleberg's avatar
Deike Kleberg committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*****************************************************************************/

void check_mpi ( int line, int iret )
{
  char error_string[MPI_MAX_ERROR_STRING+1];
  int len;

  if  ( iret != MPI_SUCCESS ) 
    {
      MPI_Error_string ( iret, error_string, &len ); 
      error_string[len] = '\0';
      fprintf ( stderr,"\nLine %8d MPI error %4d: %s\n\n", line, iret, 
		error_string ); 
    }

  return;
}

/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
61
62
int setTag ( int ID, int sc )
{
63
  return ID * tagKey + sc;
Deike Kleberg's avatar
Deike Kleberg committed
64
65
66
67
68
69
70
71
72
}

/***************************************************************/

rTag * getTag ( int tag )
{
  rTag *rtag;

  rtag = ( rTag * ) malloc ( sizeof ( rTag ));
73
74
  rtag->id = tag / tagKey;
  rtag->command = tag % tagKey;
Deike Kleberg's avatar
Deike Kleberg committed
75
76
77
78
79
80

  return rtag;
}

/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
81
82
83
84
85
86
size_t pioFileWrite ( int id, int tsId, const void *buffer, size_t len )
{
  size_t iret;

  switch ( pioinfo->type )
    {
87
88
    case PIO_MPI_NONB:
      iret = fwMPINONB ( id, tsId, buffer, len );
Deike Kleberg's avatar
Deike Kleberg committed
89
      break;
Deike Kleberg's avatar
Deike Kleberg committed
90
91
92
    case PIO_POSIX_ASYNCH:
      iret = fwPOSIXASYNCH ( id, tsId, buffer, len );
      break;
93
94
95
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fwPOSIXFPGUARDSENDRECV ( id, tsId, buffer, len );
      break;
96
97
98
99
100
101
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fwPOSIXFPGUARDTHREAD ( id, tsId, buffer, len );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fwPOSIXFPGUARDTHREADREFUSE ( id, tsId, buffer, len );
      break;
102
103
104
    case PIO_POSIX_NONB:
      iret = fwPOSIXNONB ( id, tsId, buffer, len );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
105
106
107
108
109
110
111
112
113
114
115
116
117
    }

  return iret;
}

/***************************************************************/

int pioFileClose ( int id )
{
  int iret;
  
  switch ( pioinfo->type )
    {
118
119
    case PIO_MPI_NONB:
      iret = fcMPINONB ( id );
Deike Kleberg's avatar
Deike Kleberg committed
120
      break;
Deike Kleberg's avatar
Deike Kleberg committed
121
122
123
    case PIO_POSIX_ASYNCH:
      iret = fcPOSIXASYNCH ( id );
      break;
124
125
126
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fcPOSIXFPGUARDSENDRECV ( id );
      break;
127
128
129
130
131
132
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fcPOSIXFPGUARDTHREAD ( id );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fcPOSIXFPGUARDTHREADREFUSE ( id );
      break;
133
134
135
    case PIO_POSIX_NONB:
      iret = fcPOSIXNONB ( id );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
136
137
138
139
140
141
142
143
144
145
146
147
148
    }

  return iret;
}

/***************************************************************/

int pioFileOpenW ( const char *filename )
{
  int iret;
  
  switch ( pioinfo->type )
    {
149
150
    case PIO_MPI_NONB:
      iret = fowMPINONB ( filename );
Deike Kleberg's avatar
Deike Kleberg committed
151
      break;
Deike Kleberg's avatar
Deike Kleberg committed
152
153
154
    case PIO_POSIX_ASYNCH:
      iret = fowPOSIXASYNCH ( filename );
      break;
155
156
157
    case PIO_POSIX_FPGUARD_SENDRECV:
      iret = fowPOSIXFPGUARDSENDRECV ( filename );
      break;
158
159
160
161
162
163
    case PIO_POSIX_FPGUARD_THREAD:
      iret = fowPOSIXFPGUARDTHREAD ( filename );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      iret = fowPOSIXFPGUARDTHREADREFUSE ( filename );
      break;
164
165
166
    case PIO_POSIX_NONB:
      iret = fowPOSIXNONB ( filename );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
167
168
169
170
171
172
173
    }
  
  return iret;
}

/***************************************************************/

Deike Kleberg's avatar
Deike Kleberg committed
174
int pioInit ( int ptype, int comm, int *color, int *colors, int nnodes, int *pioComm, int *ncollectors )
Deike Kleberg's avatar
Deike Kleberg committed
175
{
Deike Kleberg's avatar
Deike Kleberg committed
176
  int collectingData = 1;
Deike Kleberg's avatar
Deike Kleberg committed
177
178
179
180
181
  int key, rank, size, npes_node;
  int *allcolors;
  int i,j,k,curr;

  char *cpuName, *temp;
Deike Kleberg's avatar
Deike Kleberg committed
182
183


Deike Kleberg's avatar
Deike Kleberg committed
184
  pioinfo = ( pioInfo * ) malloc ( sizeof ( pioInfo ));
Deike Kleberg's avatar
Deike Kleberg committed
185
  pioinfo->type = ptype;
Deike Kleberg's avatar
Deike Kleberg committed
186
187
188
189
190
191
192
193
194
195
196
197

  cpuName = (char * ) malloc ( 80 * sizeof(char));
  gethostname(cpuName,80);
  temp = strtok ( cpuName, token_NODE );
  *color = atoi ( temp );
  MPI_Comm_size (( MPI_Comm ) comm, &size );
  MPI_Comm_rank (( MPI_Comm ) comm, &rank );
  npes_node = size/nnodes;
  key = rank % npes_node;
  MPI_Comm_split (( MPI_Comm ) comm, *color, key, ( MPI_Comm *) pioComm );

  pioinfo->comm  = ( MPI_Comm ) *pioComm;
198
  pioinfo->color = *color;
Deike Kleberg's avatar
Deike Kleberg committed
199
200
201
202
203
204
205
206
207
208
209
210
  MPI_Comm_rank ( pioinfo->comm, &( pioinfo->rank ));
  MPI_Comm_size ( pioinfo->comm, &( pioinfo->size ));

  if ( pioinfo->type == PIO_NONE && pioinfo->size != 1 )
    {
      fprintf ( stderr, 
		"PTYPE should be set to a parallel I/O type or npe should be 1." );
      fprintf ( stderr, "PTYPE = %d, npe = %d\n", 
		pioinfo->type , pioinfo->size );
      MPI_Abort ( pioinfo->comm, 1 );
    }

Deike Kleberg's avatar
Deike Kleberg committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  if ( ddebug )
    fprintf (stdout, 
	     "pe%d in pioDefPtype(), size=%d, nnodes=%d, cpuName=%s, color=%d,npes_node=%d, key=%d\n", 
	     rank,size, nnodes,cpuName, *color, npes_node, key);

  allcolors = (int * ) malloc ( size * sizeof(int));
  MPI_Allgather ( color, 1, MPI_INTEGER, allcolors, 1, MPI_INTEGER, ( MPI_Comm )comm );

  k = 0;
  for ( i = 0; i < size; i++ )
    {
      curr = allcolors[i] ;
      if ( curr == -1 ) 
	continue;
      else
	{
	  *( colors + k ) = allcolors[i];
	  k++;
	  if ( k == nnodes ) break;
	}
      for ( j = i + 1; j < size; j++ )
	if ( allcolors[j] == curr ) allcolors[j] = -1;
    }

Deike Kleberg's avatar
Deike Kleberg committed
235
236
  if ( ddebug && pioinfo->rank == 0 )
    fprintf ( stdout, 
237
238
	      "pe%d in pioDefPtype(), ptype=%d, initial_buffersize=%ld: init pioinfo ...\n", 
	      pioinfo->rank, pioinfo->type, initial_buffersize );
Deike Kleberg's avatar
Deike Kleberg committed
239
240
241

  switch ( pioinfo->type )
    {
242
243
    case PIO_MPI_NONB:
      collectingData = initMPINONB ( ncollectors );
Deike Kleberg's avatar
Deike Kleberg committed
244
      break;
Deike Kleberg's avatar
Deike Kleberg committed
245
246
247
    case PIO_POSIX_ASYNCH:
      collectingData = initPOSIXASYNCH ( ncollectors );
      break;
248
249
250
    case PIO_POSIX_FPGUARD_SENDRECV:
      collectingData = initPOSIXFPGUARDSENDRECV ( ncollectors );
      break;
251
252
253
254
255
256
    case PIO_POSIX_FPGUARD_THREAD:
      collectingData = initPOSIXFPGUARDTHREAD ( ncollectors );
      break;
    case PIO_POSIX_FPGUARD_THREAD_REFUSE:
      collectingData = initPOSIXFPGUARDTHREADREFUSE ( ncollectors );
      break;
257
258
259
    case PIO_POSIX_NONB:
      collectingData = initPOSIXNONB ( ncollectors );
      break;
Deike Kleberg's avatar
Deike Kleberg committed
260
261
    }

Deike Kleberg's avatar
Deike Kleberg committed
262
263
264
  free ( allcolors );
  free ( cpuName );

Deike Kleberg's avatar
Deike Kleberg committed
265
266
267
268
269
270
271
  return collectingData;
}

/***************************************************************/

void pioFinalize ()
{
272
273
274
275
276
277
278
  switch ( pioinfo->type )
    {
    case PIO_POSIX_FPGUARD_THREAD:
      finalizePOSIXFPGUARDTHREAD ();
      break;
    }

Deike Kleberg's avatar
Deike Kleberg committed
279
280
281
282
283
284
  free ( pioinfo );

  return;
}

#endif
285