1/*
2 * Copyright (c) 2016-2019 Arm Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __BASE_STATS_HDF5_HH__
41#define __BASE_STATS_HDF5_HH__
42
43#include <H5Cpp.h>
44
45#include <memory>
46#include <stack>
47#include <string>
48#include <vector>
49
50#include "base/output.hh"
51#include "base/stats/output.hh"
52#include "base/stats/types.hh"
53
54namespace Stats {
55
56class Hdf5 : public Output
57{
58  public:
59    Hdf5(const std::string &file, unsigned chunking, bool desc, bool formulas);
60
61    ~Hdf5();
62
63    Hdf5() = delete;
64    Hdf5(const Hdf5 &other) = delete;
65
66  public: // Output interface
67    void begin() override;
68    void end() override;
69    bool valid() const override;
70
71    void beginGroup(const char *name) override;
72    void endGroup() override;
73
74    void visit(const ScalarInfo &info) override;
75    void visit(const VectorInfo &info) override;
76    void visit(const DistInfo &info) override;
77    void visit(const VectorDistInfo &info) override;
78    void visit(const Vector2dInfo &info) override;
79    void visit(const FormulaInfo &info) override;
80    void visit(const SparseHistInfo &info) override;
81
82  protected:
83    /**
84     * Helper function to append vector stats and set their metadata.
85     */
86    H5::DataSet appendVectorInfo(const VectorInfo &info);
87
88    /**
89     * Helper function to append an n-dimensional double stat to the
90     * file.
91     *
92     * This helper function assumes that all stats include a time
93     * component. I.e., a Stat::Scalar is a 1-dimensional stat.
94     *
95     * @param info Stat info structure.
96     * @param rank Stat dimensionality (including time).
97     * @param dims Size of each of the dimensions.
98     */
99    H5::DataSet appendStat(const Info &info, int rank, hsize_t *dims,
100                           const double *data);
101
102    /**
103     * Helper function to add a string vector attribute to a stat.
104     *
105     * @param loc Parent location in the file.
106     * @param name Attribute name.
107     * @param values Attribute value.
108     */
109    void addMetaData(H5::DataSet &loc, const char *name,
110                     const std::vector<const char *> &values);
111
112    /**
113     * Helper function to add a string vector attribute to a stat.
114     *
115     * @param loc Parent location in the file.
116     * @param name Attribute name.
117     * @param values Attribute value.
118     */
119    void addMetaData(H5::DataSet &loc, const char *name,
120                     const std::vector<std::string> &values);
121
122    /**
123     * Helper function to add a string attribute to a stat.
124     *
125     * @param loc Parent location in the file.
126     * @param name Attribute name.
127     * @param value Attribute value.
128     */
129    void addMetaData(H5::DataSet &loc, const char *name,
130                     const std::string &value);
131
132    /**
133     * Helper function to add a double attribute to a stat.
134     *
135     * @param loc Parent location in the file.
136     * @param name Attribute name.
137     * @param value Attribute value.
138     */
139    void addMetaData(H5::DataSet &loc, const char *name, double value);
140
141  protected:
142    const std::string fname;
143    const hsize_t timeChunk;
144    const bool enableDescriptions;
145    const bool enableFormula;
146
147    std::stack<H5::Group> path;
148
149    unsigned dumpCount;
150    H5::H5File h5File;
151};
152
153std::unique_ptr<Output> initHDF5(
154    const std::string &filename,unsigned chunking = 10,
155    bool desc = true, bool formulas = true);
156
157} // namespace Stats
158
159#endif // __BASE_STATS_HDF5_HH__
160