-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
159 lines (136 loc) · 4.52 KB
/
main.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <boost/mpi.hpp>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#include "timer.h"
#include "mps_op.h"
namespace mpi = boost::mpi;
#include "densitymat.h"
#include "schedule.h"
using std::cout;
using std::endl;
int main(int argc, char* argv[]){
mpi::environment env(argc, argv);
cout.setf(std::ios::fixed, std::ios::floatfield);
cout.precision(10);
if (argc <= 1) {
cout << "No input file specified" << endl;
abort();
}
mpi::communicator world;
if (world.rank() == 0) {
banner();
params.path = string(argv[1]);
// read configure file
read_config(params.path + "/config.in", params);
if (params.restart) {
// if restart, change "prefix" to the actual directory of temporarily stored MPS
params.temp = params.temp_prefix;
} else {
params.temp = mktmpdir(params.temp_prefix);
}
coefs = read_orbitals(params.path + "/orbitals.in");
}
if (!params.restart && world.size() < 3) {
cout << "This program runs with at least 3 cores" << endl;
abort();
}
broadcast(world, params, 0);
broadcast(world, coefs, 0);
if (world.rank() == 0) {
cout << "Calculation parameters" << endl;
cout << params << endl;
}
auto dm = params.bcs ?
boost::shared_ptr<DensityMatrix>(new BCSDM(coefs)) :
boost::shared_ptr<DensityMatrix>(new SlaterDM(coefs));
int nsites = dm -> get_nsites();
if (params.restart) {
if (world.rank() == 0) {
partial_compress(nsites, MPS_DIRECTION::Left, params.M, params.temp.c_str(), 0, nsites/2);
partial_compress(nsites, MPS_DIRECTION::Right, params.M, params.temp.c_str(), nsites-1, nsites/2-2);
MPS<dtype, Quantum> A(nsites);
cout << "Now normalize" << endl;
normalize_on_disk(params.temp.c_str(), nsites, nsites/2-2);
}
} else {
vector<boost::shared_ptr<SchmidtBasis>> basis_set(nsites+1, nullptr);
world.barrier();
Timer t_basis("Build Basis");
t_basis.start();
for (int i = 0; i < nsites+1; ++i) {
if (i % world.size() == world.rank()) {
//if (world.rank() == 0) {
cout << "Cut = " << i << " On processor " << world.rank() << endl;
basis_set[i] = dm -> basis(i);
}
}
t_basis.pause();
world.barrier();
if (world.rank() == 0) {
cout << endl << "Build Schmidt basis" << endl;
cout << "Processor 0 : " << t_basis.time() << " s" << endl;
for (int i = 1; i < world.size(); ++i) {
double time;
world.recv(i, i, time);
cout << "Processor " << i <<" : " << t_basis.time() << " s" << endl;
}
cout << endl;
cout << "---------------------------" << endl;
cout << " Schmidt Basis Summary" << endl;
cout << "---------------------------" << endl;
cout << endl;
} else {
world.send(0, world.rank(), t_basis.time());
}
Timer t_send("Gathering Basis");
if (world.rank() == 0) {
t_send.start();
}
if (world.rank() == 0) {
for (int i = 0; i < nsites+1; ++i) {
if (i % world.size() != 0) {
world.recv(i % world.size(), i, basis_set[i]);
}
cout << *basis_set[i] << endl;
}
} else {
for (int i = 0; i < nsites+1; ++i) {
if (i % world.size() == world.rank()) {
world.send(0, i, basis_set[i]);
basis_set[i].reset();
}
}
}
if (world.rank() == 0) {
t_send.pause();
t_send.print();
cout << endl;
}
dynamic_build(basis_set);
}
if (world.rank() == 0) {
boost::filesystem::path mps_tmp_store(params.temp);
if (!params.mem_test) {
MPS<dtype, Quantum> A(nsites);
//compress_on_disk(A, MPS_DIRECTION::Right, params.M, params.temp.c_str(), true);
if (params.calc_spectra) {
cout << "\nnow calculate entanglement spectrum\n";
auto raw_spectra = Schmidt_on_disk(nsites, -1, params.temp.c_str(), nsites/2-2, nsites/2-2);
spectra(raw_spectra);
}
if (params.savemps) {
boost::filesystem::path mps_store(params.path + "/mps.out");
boost::filesystem::create_directory(mps_store);
for (int i = 0; i < nsites; ++i) {
string filename = std::to_string(i) + ".mps";
boost::filesystem::path p1(params.temp + "/" + filename);
boost::filesystem::path p2(params.path + "/mps.out/" + filename);
boost::filesystem::copy_file(p1, p2, boost::filesystem::copy_option::overwrite_if_exists);
}
}
}
boost::filesystem::remove_all(mps_tmp_store);
}
return 0;
}