ATLAS Offline Software
Loading...
Searching...
No Matches
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
11struct 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
42using mt_t = decltype(std::mt19937());
43
44std::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
64void 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
87int 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}
double length(const pvec &v)
const double width
Header file for AthHistogramAlgorithm.
Writer.
Definition Writer.h:350
int main()
Definition hello.cxx:18
consumer_t getHalfConsumers()
void fill(H5::Group &out_file, size_t iterations)
#define ADD(NAME)
decltype(std::mt19937()) mt_t
std::vector< out_t > getOutputs(size_t length, mt_t &rand)
#define HADD(NAME)
consumer_t getFullConsumers()
H5Utils::Consumers< const out_t & > consumer_t