source: GenericIO2Cosmo.cxx @ 8f0a211

Revision 8f0a211, 5.0 KB checked in by Hal Finkel <hfinkel@…>, 9 years ago (diff)

Add redistribution support

  • Property mode set to 100644
Line 
1/*
2 *                    Copyright (C) 2015, UChicago Argonne, LLC
3 *                               All Rights Reserved
4 *
5 *                               Generic IO (ANL-15-066)
6 *                     Hal Finkel, Argonne National Laboratory
7 *
8 *                              OPEN SOURCE LICENSE
9 *
10 * Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne,
11 * LLC, the U.S. Government retains certain rights in this software.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *
16 *   1. Redistributions of source code must retain the above copyright notice,
17 *      this list of conditions and the following disclaimer.
18 *
19 *   2. Redistributions in binary form must reproduce the above copyright
20 *      notice, this list of conditions and the following disclaimer in the
21 *      documentation and/or other materials provided with the distribution.
22 *
23 *   3. Neither the names of UChicago Argonne, LLC or the Department of Energy
24 *      nor the names of its contributors may be used to endorse or promote
25 *      products derived from this software without specific prior written
26 *      permission.
27 *
28 * *****************************************************************************
29 *
30 *                                  DISCLAIMER
31 * THE SOFTWARE IS SUPPLIED “AS IS” WITHOUT WARRANTY OF ANY KIND.  NEITHER THE
32 * UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
33 * UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
34 * EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
35 * ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
36 * PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
37 * PRIVATELY OWNED RIGHTS.
38 *
39 * *****************************************************************************
40 */
41
42#include <cstdlib>
43#include <cstdio>
44#include <cstring>
45#include <cmath>
46#include <iostream>
47#include <string>
48#include <cassert>
49
50#include "GenericIO.h"
51
52#define POSVEL_T float
53#define ID_T int64_t
54#define MASK_T uint16_t
55
56using namespace std;
57using namespace gio;
58
59// This code is based on restart2cosmo.cxx
60int main(int argc, char *argv[]) {
61#ifndef GENERICIO_NO_MPI
62  MPI_Init(&argc, &argv);
63#endif
64
65  int commRank, commRanks;
66#ifndef GENERICIO_NO_MPI
67  MPI_Comm_rank(MPI_COMM_WORLD, &commRank);
68  MPI_Comm_size(MPI_COMM_WORLD, &commRanks);
69#else
70  commRank = 0;
71  commRanks = 1;
72#endif
73
74  if(argc != 3 + commRanks) {
75    fprintf(stderr,"USAGE: %s <mpiioName> <cosmoName> <rank0> <rank1> ...\n",argv[0]);
76    exit(-1);
77  }
78
79  char *mpiioName = argv[1];
80  char *cosmoName = argv[2];
81  int rank = atoi(argv[3 + commRank]);
82
83  vector<POSVEL_T> xx, yy, zz, vx, vy, vz, phi;
84  vector<ID_T> id;
85  vector<MASK_T> mask;
86
87  assert(sizeof(ID_T) == 8);
88
89  size_t Np = 0;
90  { // create GIO
91
92  unsigned Method = GenericIO::FileIOPOSIX;
93#ifndef GENERICIO_NO_MPI
94  const char *EnvStr = getenv("GENERICIO_USE_MPIIO");
95  if (EnvStr && string(EnvStr) == "1")
96    Method = GenericIO::FileIOMPI;
97#endif
98
99  GenericIO GIO(
100#ifndef GENERICIO_NO_MPI
101    MPI_COMM_WORLD,
102#endif
103    mpiioName, Method);
104  GIO.openAndReadHeader(GenericIO::MismatchAllowed);
105
106  int NR = GIO.readNRanks();
107  if (rank >= NR) {
108    fprintf(stderr,"rank %d is invalid: file has data from %d ranks\n", rank, NR);
109    fflush(stderr);
110    exit(-1);
111  }
112
113#ifndef GENERICIO_NO_MPI
114  MPI_Barrier(MPI_COMM_WORLD);
115#endif
116
117  Np = GIO.readNumElems(rank);
118
119  xx.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
120  yy.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
121  zz.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
122  vx.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
123  vy.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
124  vz.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
125  phi.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T));
126  id.resize(Np + GIO.requestedExtraSpace()/sizeof(ID_T));
127  mask.resize(Np + GIO.requestedExtraSpace()/sizeof(MASK_T));
128
129  GIO.addVariable("x", xx, true);
130  GIO.addVariable("y", yy, true);
131  GIO.addVariable("z", zz, true);
132  GIO.addVariable("vx", vx, true);
133  GIO.addVariable("vy", vy, true);
134  GIO.addVariable("vz", vz, true);
135  GIO.addVariable("phi", phi, true);
136  GIO.addVariable("id", id, true);
137  GIO.addVariable("mask", mask, true);
138
139  GIO.readData(rank);
140  } // destroy GIO
141
142  FILE *cosmoFile =
143    fopen((string(cosmoName) + "." + string(argv[3 + commRank])).c_str(), "wb");
144  for(size_t i=0; i<Np; i++) {
145    fwrite(&xx[i], sizeof(POSVEL_T), 1, cosmoFile);
146    fwrite(&vx[i], sizeof(POSVEL_T), 1, cosmoFile);
147    fwrite(&yy[i], sizeof(POSVEL_T), 1, cosmoFile);
148    fwrite(&vy[i], sizeof(POSVEL_T), 1, cosmoFile);
149    fwrite(&zz[i], sizeof(POSVEL_T), 1, cosmoFile);
150    fwrite(&vz[i], sizeof(POSVEL_T), 1, cosmoFile);
151    fwrite(&phi[i], sizeof(POSVEL_T), 1, cosmoFile);
152    fwrite(&id[i], sizeof(ID_T), 1, cosmoFile);
153  }
154  fclose(cosmoFile);
155
156#ifndef GENERICIO_NO_MPI
157  MPI_Barrier(MPI_COMM_WORLD);
158  MPI_Finalize();
159#endif
160
161  return 0;
162}
163
Note: See TracBrowser for help on using the repository browser.