ATLAS Offline Software
test-half-precision.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "HDF5Utils/Writer.h"
6 
7 #include <random>
8 
9 //-------------------------------------------------------------------------
10 // output data structure
11 struct out_t
12 {
13  double dtype;
14  float ftype;
15  bool btype;
16 };
18 
19 #define ADD(NAME) consumers.add(#NAME, [](const out_t& o){ return o.NAME;}, 0)
20 #define HADD(NAME) consumers.add(#NAME, [](const out_t& o){ return o.NAME;}, 0, h)
21 
22 
24  consumer_t consumers;
25  ADD(ftype);
26  ADD(dtype);
27  ADD(btype);
28  return consumers;
29 }
31  consumer_t consumers;
33  HADD(ftype);
34  HADD(dtype);
35  ADD(btype);
36  return consumers;
37 }
38 
39 //-------------------------------------------------------------------------
40 // outputs
41 
42 using mt_t = decltype(std::mt19937());
43 
44 std::vector<out_t> getOutputs(size_t length, mt_t& rand ) {
45  std::vector<out_t> outvec;
46  std::uniform_int_distribution<int> exponent(-10, 2);
47  std::uniform_real_distribution<float> man(-0.0001, 0.0001);
48  std::uniform_int_distribution<short> booldist(0, 1);
49  for (size_t n = 0; n < length; n++) {
50  out_t out;
51  double value = std::exp2(exponent(rand)) * (1 + man(rand));
52  out.dtype = value;
53  out.ftype = value;
54  out.btype = booldist(rand);
55  outvec.push_back(out);
56  }
57  return outvec;
58 }
59 
60 
61 //-------------------------------------------------------------------------
62 // main routine
63 
64 void fill(H5::Group& out_file, size_t iterations) {
65 
66  const int deflate = 7;
67  const int max_width = 10;
68  std::mt19937 random(42);
69 
70  // compare outputs
72  writer_t::configuration_type config;
73  config.name = "full";
74  config.extent = {max_width};
75  config.deflate = deflate;
76  writer_t full(out_file, getFullConsumers(), config);
77  config.name = "half";
78  writer_t half(out_file, getHalfConsumers(), config);
79  std::uniform_int_distribution<int> width(0, max_width+1);
80  for (size_t n = 0; n < iterations; n++) {
81  auto out = getOutputs(width(random), random);
82  full.fill(out);
83  half.fill(out);
84  }
85 }
86 
87 int main(int nargs, char* argv[]) {
88  H5::H5File out_file("output.h5", H5F_ACC_TRUNC);
89  size_t iterations = 1;
90  if (nargs > 2) {
91  return 1;
92  }
93  if (nargs > 1) iterations = std::atoi(argv[1]);
94  fill(out_file, iterations);
95  return 0;
96 }
main
int main(int nargs, char *argv[])
Definition: test-half-precision.cxx:87
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
fill
void fill(H5::Group &out_file, size_t iterations)
Definition: test-half-precision.cxx:64
getHalfConsumers
consumer_t getHalfConsumers()
Definition: test-half-precision.cxx:30
CaloLCW_tf.nargs
nargs
Definition: CaloLCW_tf.py:26
PlotCalibFromCool.dtype
dtype
Definition: PlotCalibFromCool.py:495
python.base_data.config
config
Definition: base_data.py:20
getFullConsumers
consumer_t getFullConsumers()
Definition: test-half-precision.cxx:23
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:70
athena.value
value
Definition: athena.py:124
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
out_t::btype
bool btype
Definition: test-half-precision.cxx:15
Writer.h
H5Utils::Compression::HALF_PRECISION
@ HALF_PRECISION
getOutputs
std::vector< out_t > getOutputs(size_t length, mt_t &rand)
Definition: test-half-precision.cxx:44
H5Utils::Writer
Writer.
Definition: Writer.h:349
LArG4FSStartPointFilter.rand
rand
Definition: LArG4FSStartPointFilter.py:80
out_t::dtype
double dtype
Definition: test-half-precision.cxx:13
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
beamspotman.n
n
Definition: beamspotman.py:729
mt_t
decltype(std::mt19937()) mt_t
Definition: test-half-precision.cxx:42
out_t
Definition: test-half-precision.cxx:12
MuonEfficiencyCorrectionsCfg.out_file
out_file
Definition: MuonEfficiencyCorrectionsCfg.py:69
HADD
#define HADD(NAME)
Definition: test-half-precision.cxx:20
find_data.full
full
Definition: find_data.py:26
H5Utils::Consumers
Definition: Writer.h:129
h
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
out_t::ftype
float ftype
Definition: test-half-precision.cxx:14
ADD
#define ADD(NAME)
Definition: test-half-precision.cxx:19
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
H5Utils::defaults::deflate
const int deflate
Definition: defaults.h:10