[bd84570] | 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 "gio.h" |
---|
| 43 | #include <iostream> |
---|
| 44 | |
---|
[d409b8c] | 45 | void read_gio_float(const MPI_Comm &C, char* file_name, char* var_name, float* data, int field_count){ |
---|
| 46 | read_gio<float>(C, file_name,var_name,data,field_count); |
---|
[bd84570] | 47 | } |
---|
[d409b8c] | 48 | void read_gio_double(const MPI_Comm &C, char* file_name, char* var_name, double* data, int field_count){ |
---|
| 49 | read_gio<double>(C, file_name,var_name,data,field_count); |
---|
[bd84570] | 50 | } |
---|
[d409b8c] | 51 | void read_gio_uint16(const MPI_Comm &C, char* file_name, char* var_name, uint16_t* data, int field_count){ |
---|
| 52 | read_gio<uint16_t>(C, file_name,var_name,data,field_count); |
---|
[dd56945] | 53 | } |
---|
[d409b8c] | 54 | void read_gio_int32(const MPI_Comm &C, char* file_name, char* var_name, int* data, int field_count){ |
---|
| 55 | read_gio<int>(C, file_name,var_name,data,field_count); |
---|
[bd84570] | 56 | } |
---|
[d409b8c] | 57 | void read_gio_int64(const MPI_Comm &C, char* file_name, char* var_name, int64_t* data, int field_count){ |
---|
| 58 | read_gio<int64_t>(C, file_name,var_name,data,field_count); |
---|
[bd84570] | 59 | } |
---|
| 60 | |
---|
[d409b8c] | 61 | int64_t get_elem_num(const MPI_Comm &C, char* file_name){ |
---|
| 62 | gio::GenericIO reader(C, file_name); |
---|
[bd84570] | 63 | reader.openAndReadHeader(gio::GenericIO::MismatchAllowed); |
---|
| 64 | int num_ranks = reader.readNRanks(); |
---|
| 65 | uint64_t size = 0; |
---|
| 66 | for(int i =0;i<num_ranks;++i) |
---|
| 67 | size +=reader.readNumElems(i); |
---|
| 68 | reader.close(); |
---|
| 69 | return size; |
---|
| 70 | } |
---|
| 71 | |
---|
[d409b8c] | 72 | var_type get_variable_type(const MPI_Comm &C, char* file_name,char* var_name){ |
---|
| 73 | gio::GenericIO reader(C, file_name); |
---|
[bd84570] | 74 | std::vector<gio::GenericIO::VariableInfo> VI; |
---|
| 75 | reader.openAndReadHeader(gio::GenericIO::MismatchAllowed); |
---|
| 76 | reader.getVariableInfo(VI); |
---|
| 77 | |
---|
| 78 | int num =VI.size(); |
---|
| 79 | for(int i =0;i<num;++i){ |
---|
| 80 | gio::GenericIO::VariableInfo vinfo = VI[i]; |
---|
| 81 | if(vinfo.Name == var_name){ |
---|
[9796d0a] | 82 | if(vinfo.IsFloat && vinfo.ElementSize == 4) |
---|
[bd84570] | 83 | return float_type; |
---|
[9796d0a] | 84 | else if(vinfo.IsFloat && vinfo.ElementSize == 8) |
---|
[bd84570] | 85 | return double_type; |
---|
[dd56945] | 86 | else if(!vinfo.IsFloat && vinfo.ElementSize == 2) |
---|
| 87 | return uint16_type; |
---|
[9796d0a] | 88 | else if(!vinfo.IsFloat && vinfo.ElementSize == 4) |
---|
[bd84570] | 89 | return int32_type; |
---|
[9796d0a] | 90 | else if(!vinfo.IsFloat && vinfo.ElementSize == 8) |
---|
[bd84570] | 91 | return int64_type; |
---|
| 92 | else |
---|
| 93 | return type_not_found; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | return var_not_found; |
---|
| 97 | |
---|
| 98 | } |
---|
| 99 | |
---|
[d409b8c] | 100 | int get_variable_field_count(const MPI_Comm &C, char* file_name,char* var_name){ |
---|
| 101 | gio::GenericIO reader(C, file_name); |
---|
[9796d0a] | 102 | std::vector<gio::GenericIO::VariableInfo> VI; |
---|
| 103 | reader.openAndReadHeader(gio::GenericIO::MismatchAllowed); |
---|
| 104 | reader.getVariableInfo(VI); |
---|
| 105 | |
---|
| 106 | int num =VI.size(); |
---|
| 107 | for(int i =0;i<num;++i){ |
---|
| 108 | gio::GenericIO::VariableInfo vinfo = VI[i]; |
---|
| 109 | if(vinfo.Name == var_name) { |
---|
| 110 | return vinfo.Size/vinfo.ElementSize; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | return 0; |
---|
| 114 | } |
---|
| 115 | |
---|
[d409b8c] | 116 | #ifndef GENERICIO_NO_MPI |
---|
| 117 | extern "C" void inspect_gio(const MPI_Comm &C, char* file_name){ |
---|
| 118 | gio::GenericIO reader(C, file_name); |
---|
| 119 | #else |
---|
| 120 | extern "C" void inspect_gio(const MPI_Comm &C, char* file_name){ |
---|
| 121 | gio::GenericIO reader(C, file_name); |
---|
| 122 | #endif |
---|
| 123 | int64_t size = get_elem_num(C,file_name); |
---|
[bd84570] | 124 | std::vector<gio::GenericIO::VariableInfo> VI; |
---|
| 125 | reader.openAndReadHeader(gio::GenericIO::MismatchAllowed); |
---|
| 126 | reader.getVariableInfo(VI); |
---|
| 127 | std::cout<<"Number of Elements: "<<size<<std::endl; |
---|
| 128 | int num =VI.size(); |
---|
| 129 | std::cout<<"[data type] Variable name"<<std::endl; |
---|
| 130 | std::cout<<"---------------------------------------------"<<std::endl; |
---|
| 131 | for(int i =0;i<num;++i){ |
---|
| 132 | gio::GenericIO::VariableInfo vinfo = VI[i]; |
---|
| 133 | |
---|
| 134 | if(vinfo.IsFloat) |
---|
| 135 | std::cout<<"[f"; |
---|
| 136 | else |
---|
| 137 | std::cout<<"[i"; |
---|
[9796d0a] | 138 | int NumElements = vinfo.Size/vinfo.ElementSize; |
---|
| 139 | std::cout<<" "<<vinfo.ElementSize*8; |
---|
| 140 | if (NumElements > 1) |
---|
| 141 | std::cout<<"x"<<NumElements; |
---|
| 142 | std::cout<<"] "; |
---|
[bd84570] | 143 | std::cout<<vinfo.Name<<std::endl; |
---|
| 144 | } |
---|
| 145 | std::cout<<"\n(i=integer,f=floating point, number bits size)"<<std::endl; |
---|
| 146 | } |
---|
| 147 | |
---|