forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_R_io.cpp
More file actions
143 lines (127 loc) · 4.06 KB
/
Copy pathsingle_R_io.cpp
File metadata and controls
143 lines (127 loc) · 4.06 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "single_R_io.h"
#include "source_base/parallel_reduce.h"
#include "source_io/module_parameter/parameter.h"
#include "source_base/global_function.h"
#include "source_base/global_variable.h"
inline void write_data(std::ofstream& ofs, const double& data)
{
ofs << " " << std::fixed << std::scientific << std::setprecision(8) << data;
}
inline void write_data(std::ofstream& ofs, const std::complex<double>& data)
{
ofs << " (" << std::fixed << std::scientific << std::setprecision(8) << data.real() << ","
<< std::fixed << std::scientific << std::setprecision(8) << data.imag() << ")";
}
template<typename T>
void ModuleIO::output_single_R(std::ofstream& ofs,
const std::map<size_t, std::map<size_t, T>>& XR,
const double& sparse_threshold,
const bool& binary,
const Parallel_Orbitals& pv,
const bool& reduce)
{
T* line = nullptr;
std::vector<long long> indptr;
indptr.reserve(PARAM.globalv.nlocal + 1);
indptr.push_back(0);
std::stringstream tem1;
tem1 << PARAM.globalv.global_out_dir << std::to_string(GlobalV::DRANK) + "temp_sparse_indices.dat";
std::ofstream ofs_tem1;
std::ifstream ifs_tem1;
if (!reduce || GlobalV::DRANK == 0)
{
if (binary)
{
ofs_tem1.open(tem1.str().c_str(), std::ios::binary);
}
else
{
ofs_tem1.open(tem1.str().c_str());
}
}
line = new T[PARAM.globalv.nlocal];
for(int row = 0; row < PARAM.globalv.nlocal; ++row)
{
ModuleBase::GlobalFunc::ZEROS(line, PARAM.globalv.nlocal);
if (!reduce || pv.global2local_row(row) >= 0)
{
auto iter = XR.find(row);
if (iter != XR.end())
{
for (auto &value : iter->second)
{
line[value.first] = value.second;
}
}
}
if (reduce)
{
Parallel_Reduce::reduce_all(line, PARAM.globalv.nlocal);
}
if (!reduce || GlobalV::DRANK == 0)
{
int nonzeros_count = 0;
for (int col = 0; col < PARAM.globalv.nlocal; ++col)
{
if (std::abs(line[col]) > sparse_threshold)
{
if (binary)
{
ofs.write(reinterpret_cast<char*>(&line[col]), sizeof(T));
ofs_tem1.write(reinterpret_cast<char *>(&col), sizeof(int));
}
else
{
write_data(ofs, line[col]);
ofs_tem1 << " " << col;
}
nonzeros_count++;
}
}
nonzeros_count += indptr.back();
indptr.push_back(nonzeros_count);
}
}
delete[] line;
if (!reduce || GlobalV::DRANK == 0)
{
if (binary)
{
ofs_tem1.close();
ifs_tem1.open(tem1.str().c_str(), std::ios::binary);
ofs << ifs_tem1.rdbuf();
ifs_tem1.close();
for (auto &i : indptr)
{
ofs.write(reinterpret_cast<char *>(&i), sizeof(int));
}
}
else
{
ofs << std::endl;
ofs_tem1 << std::endl;
ofs_tem1.close();
ifs_tem1.open(tem1.str().c_str());
ofs << ifs_tem1.rdbuf();
ifs_tem1.close();
for (auto &i : indptr)
{
ofs << " " << i;
}
ofs << std::endl;
}
std::remove(tem1.str().c_str());
}
}
template void ModuleIO::output_single_R<double>(std::ofstream& ofs,
const std::map<size_t, std::map<size_t, double>>& XR,
const double& sparse_threshold,
const bool& binary,
const Parallel_Orbitals& pv,
const bool& reduce);
template void ModuleIO::output_single_R<std::complex<double>>(std::ofstream& ofs,
const std::map<size_t, std::map<size_t, std::complex<double>>>& XR,
const double& sparse_threshold,
const bool& binary,
const Parallel_Orbitals& pv,
const bool& reduce);