source: python/lib/gio.cxx @ 9796d0a

Revision 9796d0a, 4.9 KB checked in by Hal Finkel <hfinkel@…>, 7 years ago (diff)

Add support for float4 (and similar) to Python bindings

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