root/library/bdm/base/loggers.h @ 551

Revision 551, 3.4 kB (checked in by vbarta, 15 years ago)

initializing memory used by memlog (it would probably work OK even uninitialized, but valgrind complains)

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Loggers for storing results of experiments
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef LGR_H
14#define LGR_H
15
16#include "bdmbase.h"
17#include "user_info.h"
18
19namespace bdm {
20using std::string;
21
22/*!
23 * Logging into matrices in data format in memory, optionally persisted into a file.
24 */
25class memlog : public logger {
26
27protected:
28        //! Maximum length of vectors stored in memory
29        int maxlen;
30        //! Currect record to be written
31        int ind;
32        //! Storage
33        Array<mat> vectors;
34        //! name of it file to save results
35        string itfilename;
36public:
37        memlog ( int maxlen0, string itf = "" ) : maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {}
38
39        //!Default constructor
40        memlog() : maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {}
41
42        //! Initialize storage
43        void init();
44
45        void step() {
46                if ( ind < maxlen ) ind++;
47                else it_error ( "memlog::ind is too high;" );
48        }
49        void logit ( int id, const vec &v ) {
50                it_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." );
51                if ( id >= 0 ) {
52                        vectors ( id ).set_row ( ind, v );
53                }
54        }
55        void logit ( int id, const double &d ) {
56                it_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." );
57                it_assert_debug ( vectors ( id ).cols() == 1, "Vector expected" );
58                if ( id >= 0 ) {
59                        vectors ( id ) ( ind ) = d;
60                }
61        }
62        //! Save values into an itfile named after \c fname.
63        void itsave ( const char* fname );
64        //!
65        void finalize() {
66                if ( itfilename.length() > 0 ) itsave ( itfilename.c_str() );
67        };
68
69
70        /*! \brief UI for memlog
71
72        TODO dat tam kam patri, a to celej blok
73
74        \code
75        logger = {
76                class = "memlog";
77                filename = "file_name.it"; // resulting filename with results in it format
78                maxlen = 100;          // size of memory buffer
79        }
80        \endcode
81         */
82        void from_setting ( const Setting &set );
83
84        // TODO dodelat void to_setting( Setting &set ) const;
85};
86
87UIREGISTER ( memlog );
88SHAREDPTR ( memlog );
89
90/*!
91* \brief Logging into dirfile with buffer in memory
92
93* Dirfile is a special format used by the kst program. See documentation of kst for description.
94*
95* This format is used to store scalars, hence multivariate RVs must be separated.
96*/
97class dirfilelog : public memlog {
98
99protected:
100        //!name of the directory
101        string dirname;
102        //! Automatically generated
103        Array<string> scalarnames;
104public:
105        /*!\brief Default constructor
106        @param dirname0 name of the directory in which to store the results
107        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
108        dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
109
110        dirfilelog() {}
111
112        //! Initialize storage
113        void init();
114        void step();
115        void finalize();
116        /*! \brief Write memory storage to disk.
117        @param Len length of buffer to be written, if 0 the file is truncated at 0.
118        */
119        void write_buffers ( int Len );
120
121        /*! \brief UI for dirfilelog (Kst file format)
122        \code
123        logger = {
124                class = "dirfilelog";
125                dirname = "directory_for_files"; // resulting files will be stored there
126                maxlen = 100;                    // size of memory buffer, when full results are written to disk
127        }
128        \endcode
129        */
130        void from_setting ( const Setting &set );
131
132        // TODO dodelat void to_setting( Setting &set ) const;
133};
134
135UIREGISTER ( dirfilelog );
136SHAREDPTR ( dirfilelog );
137
138};
139#endif // LGR_H
Note: See TracBrowser for help on using the browser.