compareResourcesArray.c 6.97 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
  gridDefTrunc ( gridID, 1 );
64
65
66
67
68

  cdiDefKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDUSED, 6);
  cdiDefKeyInt(gridID, CDI_GLOBAL, CDI_KEY_NUMBEROFGRIDINREFERENCE, 7);
  cdiDefKeyString(gridID, CDI_GLOBAL, CDI_KEY_REFERENCEURI, "myReference");

69
70
71
72
73
/* 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); */
74
  for ( i = 0; i < nlon*nlat; i++ )
75
76
    area_vec[i] = 0.1 * i;
  gridDefArea ( gridID, ap );
77
  for ( i = 0; i < nlon*nlat; i++ )
78
    mask_vec[i] = i;
79
80
  gridDefReducedPoints ( gridID, nlon*nlat, mp );
  gridDefComplexPacking ( gridID, 1 );
81
82
83

  return gridID;
}
84

85
static int defineZaxis ()
86
87
88
89
90
91
92
{
  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 );
93
94
95
96
97

  cdiDefKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_NAME, "myName");
  cdiDefKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_LONGNAME, "myLongname");
  cdiDefKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_UNITS, "myUnits");

98
  zaxisDefDatatype ( zaxisID, DOUBLE_PRECISION );
99
  cdiDefKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_TYPEOFFIRSTFIXEDSURFACE, 1);
100
101
102
103
104
105
106
  zaxisDefVct ( zaxisID, 3, vct );
  zaxisDefLbounds ( zaxisID, &levs[0] );
  zaxisDefUbounds ( zaxisID, &levs[0] );
  zaxisDefWeights ( zaxisID, &levs[0] );

  return zaxisID;
}
107

108
static int defineTaxis ()
109
{
110
  int taxisID = taxisCreate(TAXIS_ABSOLUTE);
111

112
113
114
115
116
117
118
119
120
121
  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 );
122

123
124
125
  return taxisID;
}

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

134
static int defineVlist ( int gridID, int zaxisID, int taxisID )
135
136
137
138
139
140
141
142
{
  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);
143
144
145
  vlistDefVarName(vlistID, varID1, "varname1");
  {
    int globfac[] = { 23, 42 };
146
    cdiDefAttInt(vlistID, varID1, "seer's globule factors", CDI_DATATYPE_INT16,
147
148
149
                   2, globfac);
  }
  vlistDefVarName(vlistID, varID2, "varname2");
150
  cdiDefAttTxt(vlistID, varID2, "txt demo", 6, "banana");
151
  vlistDefTaxis(vlistID, taxisID);
152
153
154
  return vlistID;
}

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

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

  return instID;
}

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

Thomas Jahns's avatar
Thomas Jahns committed
171
172
173
174
175
176
177
178
179
180
181
182
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 )
183
{
Thomas Jahns's avatar
Thomas Jahns committed
184
  int gridID, zaxisID, taxisID, instID, vlistID, streamID;
185
186
187
188
189

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

190
  namespaceSetActive ( 0 );
191
192
193
194
195

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

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

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

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

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

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

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

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

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

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

  modelRun ( commModel );
243

244
  xt_finalize();
245
  MPI_Finalize ();
246

247
248
  return 0;
}
249

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