-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileManager.h
More file actions
115 lines (110 loc) · 3.25 KB
/
FileManager.h
File metadata and controls
115 lines (110 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef _FILEMANAGER_H
#define _FILEMANAGER_H
#include <hdf5.h>
#include <iostream>
#include <fcntl.h>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <vector>
#ifdef USEMPI
#include <mpi.h>
#endif
class MatStream;
/*! \brief class to keep track and perform output.
*
*/
class FileManager
{
private:
int m_num;
std::string m_num_str;
std::string m_suffix;
std::string m_dir;
double m_compl;
int m_verbose;
int m_total;
std::map<std::string,MatStream> m_streams;
std::map<std::string,double> m_fileattr;
std::map<std::string,std::string> m_file_str_attr;
std::map<std::string,double> m_dataattr;
int m_stat_per_sample;
#ifdef USEMPI
MPI_Comm m_calc_comm;
void init_mpi_comm();
#endif
public:
FileManager(const std::string& dir="", const int& num=-1);
FileManager(const std::string& dir, const std::string& prefix);
~FileManager();
enum {message_comm=0,message_monitor=1,message_save=2,message_loop=3};
MatStream& FileStream(std::string basename);
void FileAttribute(std::string attr, double val);
void FileAttribute(std::string attr, std::string val);
void DataAttribute(std::string attr, double val);
int& StatPerSample();
int& Verbose() {return m_verbose;}
int Prefix() {return m_num;}
#ifdef USEMPI
void MainLoop();
MPI_Comm GetCalcComm() const {return m_calc_comm;}
#endif
void Monitor(int rank,double percents=0, double total_time=0);
double& MonitorCompletion() {return m_compl;}
int& MonitorTotal() {return m_total;}
void Write(int isready=0);
static void EmergencyClose(int signum);
hid_t WriteSimple(std::string filename);
};
class MatStream
{
public:
int m_nrow;
int m_ncol;
double *m_mat;
int m_stat;
MatStream()
:m_nrow(0), m_ncol(0), m_mat(0),m_stat(0) {}
~MatStream() {delete [] m_mat;}
void Write(const int& m, const int& n, const double* mat)
{
if(m_mat) {
if(!m_stat){
delete [] m_mat;
m_mat=new double[m*n];
m_nrow=m;
m_ncol=n;
std::memcpy(m_mat,mat,m*n*sizeof(double));
m_stat=1;
} else {
if(m!=m_nrow || n!=m_ncol){
#ifdef EXCEPT
throw std::logic_error("MatStream::Write: sizes must match.");
#else
std::cerr<<"MatStream::Write: sizes must match."<<std::endl;
abort();
#endif
}
for(int a=0;a<m_nrow*m_ncol;++a)
m_mat[a]=(m_stat*m_mat[a]+mat[a])/(m_stat+1);
++m_stat;
}
} else {
m_mat=new double[m*n];
m_nrow=m;
m_ncol=n;
std::memcpy(m_mat,mat,m*n*sizeof(double));
m_stat=1;
}
}
int& Stat()
{
return m_stat;
}
};
#endif//_FILEMANAGER_H