compareResourcesArray.c 6.82 KB
Newer Older
1
2
3
4
#if defined (HAVE_CONFIG_H)
#include "config.h"
#endif

5
6
#include <stdio.h>

7
#include <mpi.h>
8
#include <yaxt.h>
9
#include "cdi.h"
10
#include "cdipio.h"
11
#include "dmemory.h"
12
#include "pio_util.h"
13
14
#include "resource_handle.h"
#include "resource_unpack.h"
15

16
extern int    reshListCompare      ( int, int );
17

18
19
20
21
22
23
24
25
enum {
  IOMode           = PIO_NONE,
  nProcsIO         = 1,
  DOUBLE_PRECISION = 8,
  nlon             = 12,
  nlat             = 6,
  nlev             = 5,
  ntsteps          = 3 };
26

27
28
29
static double lons[nlon] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
static double lats[nlat] = {-75, -45, -15, 15, 45, 75};
static double levs[nlev] = {101300, 92500, 85000, 50000, 20000};
30

31
static int defineGrid()
32
{
33
34
35
36
37
38
  int gridID = CDI_UNDEFID;
  int mask_vec[nlon*nlat];
  const int * mp = &mask_vec[0];
  double area_vec[nlon*nlat];
  const double * ap = &area_vec[0];
  int i;
39

40
41
42
43
44
  gridID = gridCreate(GRID_LONLAT, nlon*nlat);
  gridDefXsize(gridID, nlon);
  gridDefYsize(gridID, nlat);
  gridDefXvals(gridID, lons);
  gridDefYvals(gridID, lats);
Deike Kleberg's avatar
Deike Kleberg committed
45
  gridDefNvertex ( gridID, 1 );
Deike Kleberg's avatar
Deike Kleberg committed
46
47
  gridDefXbounds ( gridID, lons );
  gridDefYbounds ( gridID, lats );
48
  for ( i = 0; i < nlon*nlat; i++ )
49
50
    mask_vec[i] = i % 2 ;
  gridDefMaskGME ( gridID, mp );
51
  for ( i = 0; i < nlon*nlat; i++ )
52
53
    mask_vec[i] = 1;
  gridDefMask ( gridID, mp );
54
55
56
57
58
59
60
61

  cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_NAME, "myXname");
  cdiDefKeyString(gridID, CDI_YAXIS, CDI_KEY_NAME, "myYname");
  cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_LONGNAME, "myXlongname");
  cdiDefKeyString(gridID, CDI_YAXIS, CDI_KEY_LONGNAME, "myYlongname");
  cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_UNITS, "myXunits");
  cdiDefKeyString(gridID, CDI_YAXIS, CDI_KEY_UNITS, "myYunits");

62
  gridDefDatatype ( gridID, DOUBLE_PRECISION );
63
64
65
66
67
68
69
70
71
  gridDefTrunc ( gridID, 1 );
  gridDefNumber ( gridID, 6 );
  gridDefPosition ( gridID, 7 );
  gridDefReference ( gridID, "myReference" );
/* gridDefLCC ( gridID, double originLon, double originLat,  */
/* 	   double lonParY, double lat1, double lat2, double xinc, double yinc, int projflag, int scanflag); */
/* gridDefLcc2 ( gridID, double earth_radius, double lon_0,  */
/* 	    double lat_0, double lat_1,double lat_2);*/
/* gridDefLaea ( gridID, double earth_radius, double lon_0, double lat_0); */
72
  for ( i = 0; i < nlon*nlat; i++ )
73
74
    area_vec[i] = 0.1 * i;
  gridDefArea ( gridID, ap );
75
  for ( i = 0; i < nlon*nlat; i++ )
76
    mask_vec[i] = i;
77
78
  gridDefReducedPoints ( gridID, nlon*nlat, mp );
  gridDefComplexPacking ( gridID, 1 );
79
80
81

  return gridID;
}
82

83
static int defineZaxis ()
84
85
86
87
88
89
90
91
92
93
{
  int zaxisID = CDI_UNDEFID;
  double vct[3] = { 3.0, 3.3, 3.6 };

  zaxisID = zaxisCreate(ZAXIS_PRESSURE, nlev);
  zaxisDefLevels(zaxisID, levs);
  zaxisDefLevel ( zaxisID, 2, 8507.3 );
  zaxisDefName ( zaxisID, "myName" );
  zaxisDefLongname ( zaxisID, "myLongname" );
  zaxisDefUnits ( zaxisID, "myUnits" );
94
  zaxisDefDatatype ( zaxisID, DOUBLE_PRECISION );
95
  cdiDefKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_TYPEOFFIRSTFIXEDSURFACE, 1);
96
97
98
99
100
101
102
  zaxisDefVct ( zaxisID, 3, vct );
  zaxisDefLbounds ( zaxisID, &levs[0] );
  zaxisDefUbounds ( zaxisID, &levs[0] );
  zaxisDefWeights ( zaxisID, &levs[0] );

  return zaxisID;
}
103

104
static int defineTaxis ()
105
106
{
  int taxisID = CDI_UNDEFID;
107

108
  taxisID = taxisCreate(TAXIS_ABSOLUTE);
109
110
111
112
113
114
115
116
117
118
119
  
  taxisDefType  ( taxisID, 0 );
  taxisDefVdate ( taxisID, 1 );
  taxisDefVtime ( taxisID, 2 );
  taxisDefRdate ( taxisID, 3 );
  taxisDefRtime ( taxisID, 4 );
  taxisDefVdateBounds ( taxisID, 5, 6 );
  taxisDefVtimeBounds ( taxisID, 7, 8 );
  taxisDefCalendar ( taxisID, 1 );
  taxisDefTunit ( taxisID, 1 );
  taxisDefNumavg ( taxisID, 1 );
120

121
122
123
  return taxisID;
}

124
static void defineStream ( int streamID, int vlistID )
125
{
Deike Kleberg's avatar
Deike Kleberg committed
126
127
128
129
  streamDefByteorder ( streamID, 1 );
  streamDefCompType  ( streamID, 2 );
  streamDefCompLevel ( streamID, 3 );
  streamDefVlist(streamID, vlistID);
130
131
}

132
static int defineVlist ( int gridID, int zaxisID, int taxisID )
133
134
135
136
137
138
139
140
{
  int vlistID = CDI_UNDEFID;
  int zaxisID2 = zaxisCreate(ZAXIS_SURFACE, 1);
  int varID1, varID2;

  vlistID = vlistCreate();
  varID1 = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);
  varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE);
141
142
143
  vlistDefVarName(vlistID, varID1, "varname1");
  {
    int globfac[] = { 23, 42 };
144
    cdiDefAttInt(vlistID, varID1, "seer's globule factors", CDI_DATATYPE_INT16,
145
146
147
                   2, globfac);
  }
  vlistDefVarName(vlistID, varID2, "varname2");
148
  cdiDefAttTxt(vlistID, varID2, "txt demo", 6, "banana");
149
  vlistDefTaxis(vlistID, taxisID);
150
151
152
  return vlistID;
}

153
154
static int
defineInstitute ()
155
156
157
158
159
160
161
162
{
  int instID = CDI_UNDEFID;

  instID = institutDef( 0, 0,"MYINSTITUTE", "myInstitute");

  return instID;
}

Thomas Jahns's avatar
Thomas Jahns committed
163
164
static void
defineModel(int instID)
165
{
Thomas Jahns's avatar
Thomas Jahns committed
166
  modelDef(instID, 0, "myModel");
167
168
}

Thomas Jahns's avatar
Thomas Jahns committed
169
170
171
172
173
174
175
176
177
178
179
180
static void
printResources()
{
  FILE *fp = fopen("reshArrayModel.txt", "w");
  if (!fp)
    xabort("%s", "could not open file");
  reshListPrint(fp);
  fclose(fp);
}


static void modelRun ( MPI_Comm comm )
181
{
Thomas Jahns's avatar
Thomas Jahns committed
182
  int gridID, zaxisID, taxisID, instID, vlistID, streamID;
183
184
185
186
187

  char * recvBuffer, * sendBuffer;
  int bufferSize, differ;
  MPI_Status status;

188
  namespaceSetActive ( 0 );
189
190
191
192
193

  gridID  = defineGrid      ();
  zaxisID = defineZaxis     ();
  taxisID = defineTaxis     ();
  instID  = defineInstitute ();
Thomas Jahns's avatar
Thomas Jahns committed
194
  defineModel(instID);
195
  vlistID = defineVlist     ( gridID, zaxisID, taxisID);
196
  streamID = streamOpenWrite("example.grb", CDI_FILETYPE_GRB);
197
  if ( streamID < 0 ) xabort ( "Could not open file" );
Deike Kleberg's avatar
Deike Kleberg committed
198
  defineStream ( streamID, vlistID );
199

200
201
  reshPackBufferCreate(&sendBuffer, &bufferSize, &comm);
  xmpi ( MPI_Send ( sendBuffer, bufferSize, MPI_PACKED, 0, 0, comm ));
202
  recvBuffer = Malloc((size_t)bufferSize);
203
  xmpi ( MPI_Recv ( recvBuffer, bufferSize, MPI_PACKED, 0,
204
		    0, comm, &status ));
205

206
  namespaceSetActive ( 1 );
207
  reshUnpackResources(recvBuffer, bufferSize, &comm);
208
  Free ( recvBuffer );
209
  reshPackBufferDestroy ( &sendBuffer );
210

Deike Kleberg's avatar
Deike Kleberg committed
211
  differ = reshListCompare ( 0, 1 );
Deike Kleberg's avatar
Deike Kleberg committed
212
  printf ( "The resource arrays %s.\n", differ ? "differ" : "are equal" );
Thomas Jahns's avatar
Thomas Jahns committed
213
  printResources();
214

215
  namespaceSetActive ( 0 );
216
217
218
219
  streamClose(streamID);
  return;
}

220
int main (int argc, char *argv[])
221
{
Thomas Jahns's avatar
Thomas Jahns committed
222
  int sizeGlob, pioNamespace;
223
  MPI_Comm commGlob, commModel;
224

225
  MPI_Init(&argc, &argv);
226
227
  commGlob = MPI_COMM_WORLD;
  xt_initialize(commGlob);
228
  xmpi ( MPI_Comm_set_errhandler ( commGlob, MPI_ERRORS_RETURN ));
229
  xmpi ( MPI_Comm_size ( commGlob, &sizeGlob ));
230

231
  if ( sizeGlob != 1 )
232
      xabort ( "test transition of resource array only with 1 PE." );
233

234
235
  if ( nProcsIO != 1 )
    xabort ( "bad distribution of tasks on PEs" );
236

237
238
  commModel = pioInit(commGlob, nProcsIO, IOMode, &pioNamespace, 1.0f,
                      cdiPioNoPostCommSetup);
239
  namespaceSetActive(pioNamespace);
240
241

  modelRun ( commModel );
242

243
  xt_finalize();
244
  MPI_Finalize ();
245

246
247
  return 0;
}
248

249
250
251
252
253
254
255
256
257
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */