[ba0fbd1] | 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> |
---|
[cda87e9] | 46 | #include <algorithm> |
---|
[ba0fbd1] | 47 | #include <iostream> |
---|
| 48 | #include <string> |
---|
| 49 | #include <cassert> |
---|
| 50 | |
---|
| 51 | #include "GenericIO.h" |
---|
| 52 | |
---|
| 53 | #define POSVEL_T float |
---|
| 54 | #define ID_T int64_t |
---|
| 55 | #define MASK_T uint16_t |
---|
| 56 | |
---|
| 57 | using namespace std; |
---|
| 58 | using namespace gio; |
---|
| 59 | |
---|
[cda87e9] | 60 | template <typename T> |
---|
| 61 | struct Generator { |
---|
| 62 | Generator(T start, T inc) : value(start), inc(inc) {} |
---|
| 63 | T operator()() { |
---|
| 64 | value += inc; |
---|
| 65 | return value; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | T value; |
---|
| 69 | T inc; |
---|
| 70 | }; |
---|
| 71 | |
---|
[5d57155] | 72 | struct pos_t { |
---|
| 73 | POSVEL_T x, y, z, w; |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | template <> |
---|
| 77 | struct Generator<pos_t> { |
---|
| 78 | Generator(POSVEL_T start, POSVEL_T inc) : |
---|
| 79 | GenX(start, inc), GenY(start, inc), GenZ(start, inc), GenW(start, inc) {} |
---|
| 80 | |
---|
| 81 | pos_t operator()() { |
---|
| 82 | pos_t v; |
---|
| 83 | v.x = GenX(); |
---|
| 84 | v.y = GenY(); |
---|
| 85 | v.z = GenZ(); |
---|
| 86 | v.w = GenW(); |
---|
| 87 | |
---|
| 88 | return v; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | Generator<POSVEL_T> GenX, GenY, GenZ, GenW; |
---|
| 92 | }; |
---|
| 93 | |
---|
[ba0fbd1] | 94 | int main(int argc, char *argv[]) { |
---|
| 95 | MPI_Init(&argc, &argv); |
---|
| 96 | |
---|
| 97 | int commRank, commRanks; |
---|
| 98 | MPI_Comm_rank(MPI_COMM_WORLD, &commRank); |
---|
| 99 | MPI_Comm_size(MPI_COMM_WORLD, &commRanks); |
---|
| 100 | |
---|
[5d57155] | 101 | bool UseAOS = false; |
---|
[b060d3c] | 102 | bool UseLC = false; |
---|
[72ac664] | 103 | int a = 1; |
---|
[5d57155] | 104 | if (argc > 1 && string(argv[a]) == "-a") { |
---|
| 105 | UseAOS = true; |
---|
| 106 | --argc; |
---|
| 107 | ++a; |
---|
| 108 | } |
---|
| 109 | |
---|
[72ac664] | 110 | if (argc > 1 && string(argv[a]) == "-c") { |
---|
| 111 | GenericIO::setDefaultShouldCompress(true); |
---|
| 112 | --argc; |
---|
| 113 | ++a; |
---|
| 114 | } |
---|
| 115 | |
---|
[b060d3c] | 116 | if (argc > 1 && string(argv[a]) == "-l") { |
---|
| 117 | UseLC = true; |
---|
| 118 | --argc; |
---|
| 119 | ++a; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[ba0fbd1] | 123 | if(argc != 4) { |
---|
[b060d3c] | 124 | fprintf(stderr,"USAGE: %s [-a] [-c] [-l] <mpiioName> <NP> <seed>\n", argv[0]); |
---|
[ba0fbd1] | 125 | exit(-1); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | GenericIO::setNaturalDefaultPartition(); |
---|
| 129 | |
---|
[72ac664] | 130 | char *mpiioName = argv[a++]; |
---|
| 131 | size_t Np = atoi(argv[a++])/commRanks; |
---|
| 132 | int seed = atoi(argv[a++]); |
---|
[ba0fbd1] | 133 | |
---|
| 134 | srand48(seed + commRank); |
---|
| 135 | |
---|
| 136 | // Add a 2% variance to make things a bit more realistic. |
---|
| 137 | Np = double(Np)*(1.0 + (drand48() - 0.5)*0.02); |
---|
| 138 | |
---|
| 139 | vector<POSVEL_T> xx, yy, zz, vx, vy, vz, phi; |
---|
| 140 | vector<ID_T> id; |
---|
| 141 | vector<MASK_T> mask; |
---|
| 142 | |
---|
[5d57155] | 143 | vector<pos_t> pos, vel; |
---|
| 144 | |
---|
[ba0fbd1] | 145 | assert(sizeof(ID_T) == 8); |
---|
| 146 | |
---|
| 147 | unsigned Method = GenericIO::FileIOPOSIX; |
---|
| 148 | const char *EnvStr = getenv("GENERICIO_USE_MPIIO"); |
---|
| 149 | if (EnvStr && string(EnvStr) == "1") |
---|
| 150 | Method = GenericIO::FileIOMPI; |
---|
| 151 | |
---|
| 152 | { // scope GIO |
---|
| 153 | GenericIO GIO( |
---|
| 154 | MPI_COMM_WORLD, |
---|
| 155 | mpiioName, Method); |
---|
| 156 | GIO.setNumElems(Np); |
---|
| 157 | |
---|
| 158 | int CoordFlagsX = GenericIO::VarIsPhysCoordX; |
---|
| 159 | int CoordFlagsY = GenericIO::VarIsPhysCoordY; |
---|
| 160 | int CoordFlagsZ = GenericIO::VarIsPhysCoordZ; |
---|
| 161 | GIO.setPhysOrigin(0.0); |
---|
| 162 | GIO.setPhysScale(256.0); |
---|
| 163 | |
---|
[5d57155] | 164 | if (UseAOS) { |
---|
| 165 | pos.resize(Np + (GIO.requestedExtraSpace() + sizeof(pos_t) - 1)/sizeof(pos_t)); |
---|
| 166 | vel.resize(Np + (GIO.requestedExtraSpace() + sizeof(pos_t) - 1)/sizeof(pos_t)); |
---|
| 167 | } else { |
---|
| 168 | xx.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 169 | yy.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 170 | zz.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 171 | vx.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 172 | vy.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 173 | vz.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 174 | phi.resize(Np + GIO.requestedExtraSpace()/sizeof(POSVEL_T)); |
---|
| 175 | } |
---|
[ba0fbd1] | 176 | id.resize(Np + GIO.requestedExtraSpace()/sizeof(ID_T)); |
---|
| 177 | mask.resize(Np + GIO.requestedExtraSpace()/sizeof(MASK_T)); |
---|
| 178 | |
---|
[5d57155] | 179 | if (UseAOS) { |
---|
| 180 | std::generate(pos.begin(), pos.end(), Generator<pos_t>(25, 3)); |
---|
| 181 | std::generate(vel.begin(), vel.end(), Generator<pos_t>(25, 3)); |
---|
| 182 | } else { |
---|
| 183 | std::generate(xx.begin(), xx.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 184 | std::generate(yy.begin(), yy.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 185 | std::generate(zz.begin(), zz.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 186 | std::generate(vx.begin(), vx.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 187 | std::generate(vy.begin(), vy.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 188 | std::generate(vz.begin(), vz.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 189 | std::generate(phi.begin(), phi.end(), Generator<POSVEL_T>(25, 3)); |
---|
| 190 | } |
---|
[cda87e9] | 191 | std::generate(id.begin(), id.end(), Generator<ID_T>(25, 3)); |
---|
[ba0fbd1] | 192 | std::fill(mask.begin(), mask.end(), 25); |
---|
| 193 | |
---|
[b060d3c] | 194 | GenericIO::LossyCompressionInfo LCI; |
---|
| 195 | if (UseLC) { |
---|
| 196 | LCI.Mode = GenericIO::LossyCompressionInfo::LCModeRel; |
---|
| 197 | LCI.RelErrThreshold = 1e-3; |
---|
| 198 | } |
---|
| 199 | |
---|
[5d57155] | 200 | if (UseAOS) { |
---|
| 201 | GIO.addVariable("pos", pos, CoordFlagsX | CoordFlagsY | CoordFlagsZ | |
---|
[b060d3c] | 202 | GenericIO::VarHasExtraSpace, LCI); |
---|
[5d57155] | 203 | GIO.addVariable("vel", vel, GenericIO::VarHasExtraSpace); |
---|
| 204 | } else { |
---|
[b060d3c] | 205 | GIO.addVariable("x", xx, CoordFlagsX | GenericIO::VarHasExtraSpace, LCI); |
---|
| 206 | GIO.addVariable("y", yy, CoordFlagsY | GenericIO::VarHasExtraSpace, LCI); |
---|
| 207 | GIO.addVariable("z", zz, CoordFlagsZ | GenericIO::VarHasExtraSpace, LCI); |
---|
[5d57155] | 208 | GIO.addVariable("vx", vx, GenericIO::VarHasExtraSpace); |
---|
| 209 | GIO.addVariable("vy", vy, GenericIO::VarHasExtraSpace); |
---|
| 210 | GIO.addVariable("vz", vz, GenericIO::VarHasExtraSpace); |
---|
| 211 | GIO.addVariable("phi", phi, GenericIO::VarHasExtraSpace); |
---|
| 212 | } |
---|
[ba0fbd1] | 213 | GIO.addVariable("id", id, GenericIO::VarHasExtraSpace); |
---|
| 214 | GIO.addVariable("mask", mask, GenericIO::VarHasExtraSpace); |
---|
| 215 | |
---|
| 216 | GIO.write(); |
---|
| 217 | } // destroy GIO prior to calling MPI_Finalize |
---|
| 218 | |
---|
[5d57155] | 219 | if (UseAOS) { |
---|
| 220 | pos.resize(Np); |
---|
| 221 | vel.resize(Np); |
---|
| 222 | } else { |
---|
| 223 | xx.resize(Np); |
---|
| 224 | yy.resize(Np); |
---|
| 225 | zz.resize(Np); |
---|
| 226 | vx.resize(Np); |
---|
| 227 | vy.resize(Np); |
---|
| 228 | vz.resize(Np); |
---|
| 229 | phi.resize(Np); |
---|
| 230 | } |
---|
[ba0fbd1] | 231 | id.resize(Np); |
---|
| 232 | mask.resize(Np); |
---|
| 233 | |
---|
| 234 | MPI_Barrier(MPI_COMM_WORLD); |
---|
| 235 | MPI_Finalize(); |
---|
| 236 | |
---|
| 237 | return 0; |
---|
| 238 | } |
---|
| 239 | |
---|