1 | #include <cstdlib> |
---|
2 | #include <cstdio> |
---|
3 | #include <cstring> |
---|
4 | #include <cmath> |
---|
5 | #include <iostream> |
---|
6 | #include <string> |
---|
7 | #include <cassert> |
---|
8 | |
---|
9 | #include "GenericIO.h" |
---|
10 | |
---|
11 | #define POSVEL_T float |
---|
12 | #define ID_T int64_t |
---|
13 | #define MASK_T uint16_t |
---|
14 | |
---|
15 | using namespace std; |
---|
16 | using namespace gio; |
---|
17 | |
---|
18 | // This code is based on restart2cosmo.cxx |
---|
19 | int main(int argc, char *argv[]) { |
---|
20 | #ifndef GENERICIO_NO_MPI |
---|
21 | MPI_Init(&argc, &argv); |
---|
22 | #endif |
---|
23 | |
---|
24 | int commRank, commRanks; |
---|
25 | #ifndef GENERICIO_NO_MPI |
---|
26 | MPI_Comm_rank(MPI_COMM_WORLD, &commRank); |
---|
27 | MPI_Comm_size(MPI_COMM_WORLD, &commRanks); |
---|
28 | #else |
---|
29 | commRank = 0; |
---|
30 | commRanks = 1; |
---|
31 | #endif |
---|
32 | |
---|
33 | if(argc != 3 + commRanks) { |
---|
34 | fprintf(stderr,"USAGE: %s <mpiioName> <cosmoName> <rank0> <rank1> ...\n",argv[0]); |
---|
35 | exit(-1); |
---|
36 | } |
---|
37 | |
---|
38 | char *mpiioName = argv[1]; |
---|
39 | char *cosmoName = argv[2]; |
---|
40 | int rank = atoi(argv[3 + commRank]); |
---|
41 | |
---|
42 | vector<POSVEL_T> xx, yy, zz, vx, vy, vz, phi; |
---|
43 | vector<ID_T> id; |
---|
44 | vector<MASK_T> mask; |
---|
45 | |
---|
46 | assert(sizeof(ID_T) == 8); |
---|
47 | |
---|
48 | size_t Np = 0; |
---|
49 | { // create GIO |
---|
50 | |
---|
51 | unsigned Method = GenericIO::FileIOPOSIX; |
---|
52 | #ifndef GENERICIO_NO_MPI |
---|
53 | const char *EnvStr = getenv("GENERICIO_USE_MPIIO"); |
---|
54 | if (EnvStr && string(EnvStr) == "1") |
---|
55 | Method = GenericIO::FileIOMPI; |
---|
56 | #endif |
---|
57 | |
---|
58 | GenericIO GIO( |
---|
59 | #ifndef GENERICIO_NO_MPI |
---|
60 | MPI_COMM_WORLD, |
---|
61 | #endif |
---|
62 | mpiioName, Method); |
---|
63 | GIO.openAndReadHeader(false); |
---|
64 | |
---|
65 | int NR = GIO.readNRanks(); |
---|
66 | if (rank >= NR) { |
---|
67 | fprintf(stderr,"rank %d is invalid: file has data from %d ranks\n", rank, NR); |
---|
68 | fflush(stderr); |
---|
69 | exit(-1); |
---|
70 | } |
---|
71 | |
---|
72 | #ifndef GENERICIO_NO_MPI |
---|
73 | MPI_Barrier(MPI_COMM_WORLD); |
---|
74 | #endif |
---|
75 | |
---|
76 | Np = GIO.readNumElems(rank); |
---|
77 | |
---|
78 | xx.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
79 | yy.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
80 | zz.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
81 | vx.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
82 | vy.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
83 | vz.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
84 | phi.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
85 | id.resize(Np + GIO.requestedExtraSpace()/sizeof(ID_T)); |
---|
86 | mask.resize(Np + GIO.requestedExtraSpace()/sizeof(MASK_T)); |
---|
87 | |
---|
88 | GIO.addVariable("x", xx, true); |
---|
89 | GIO.addVariable("y", yy, true); |
---|
90 | GIO.addVariable("z", zz, true); |
---|
91 | GIO.addVariable("vx", vx, true); |
---|
92 | GIO.addVariable("vy", vy, true); |
---|
93 | GIO.addVariable("vz", vz, true); |
---|
94 | GIO.addVariable("phi", phi, true); |
---|
95 | GIO.addVariable("id", id, true); |
---|
96 | GIO.addVariable("mask", mask, true); |
---|
97 | |
---|
98 | GIO.readData(rank); |
---|
99 | } // destroy GIO |
---|
100 | |
---|
101 | FILE *cosmoFile = |
---|
102 | fopen((string(cosmoName) + "." + string(argv[3 + commRank])).c_str(), "wb"); |
---|
103 | for(size_t i=0; i<Np; i++) { |
---|
104 | fwrite(&xx[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
105 | fwrite(&vx[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
106 | fwrite(&yy[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
107 | fwrite(&vy[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
108 | fwrite(&zz[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
109 | fwrite(&vz[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
110 | fwrite(&phi[i], sizeof(POSVEL_T), 1, cosmoFile); |
---|
111 | fwrite(&id[i], sizeof(ID_T), 1, cosmoFile); |
---|
112 | } |
---|
113 | fclose(cosmoFile); |
---|
114 | |
---|
115 | #ifndef GENERICIO_NO_MPI |
---|
116 | MPI_Barrier(MPI_COMM_WORLD); |
---|
117 | MPI_Finalize(); |
---|
118 | #endif |
---|
119 | |
---|
120 | return 0; |
---|
121 | } |
---|
122 | |
---|