Skip to content

Commit 10e48fd

Browse files
committed
It now compiles in a non-MPI environment (not well tested)
1 parent 3fd0bf5 commit 10e48fd

8 files changed

Lines changed: 128 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_SOURCE_DIR})
88
# export ALPSCore_DIR=/location/to/ALPSCORE/
99
find_package(ALPSCore REQUIRED)
1010
find_package(Boost 1.55.0 REQUIRED timer)
11-
find_package(MPI REQUIRED)
11+
if (ALPS_ENABLE_MPI)
12+
find_package(MPI REQUIRED)
13+
endif()
1214
find_package(Eigen3 REQUIRED)
1315

1416
# Option (use quad precision for part of calculations)

src/impurity.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
#include <alps/accumulators.hpp>
4040
#include <alps/mc/api.hpp>
4141
#include <alps/mc/mcbase.hpp>
42-
#include <alps/mc/mpiadapter.hpp>
42+
#ifdef ALPS_HAVE_MPI
43+
#include <alps/mc/mpiadapter.hpp>
44+
#endif
4345
#include <alps/mc/stop_callback.hpp>
4446
#include <alps/params/convenience_params.hpp>
4547

@@ -108,7 +110,9 @@ class HybridizationSimulation : public alps::mcbase
108110
boost::scoped_ptr<IMP_MODEL> p_model;
109111

110112
//ALPS MPI communicator
113+
#ifdef ALPS_HAVE_MPI
111114
alps::mpi::communicator comm;
115+
#endif
112116

113117
//Constant simulation parameters
114118
const long thermalization_sweeps; // sweeps to be done for equilibration

src/impurity.ipp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ HybridizationSimulation<IMP_MODEL>::HybridizationSimulation(parameters_type cons
4848
N(static_cast<int>(parameters["N_TAU"])), //time slices
4949
Np1(N+1),
5050
p_model(new IMP_MODEL(p,rank==0)),//impurity model
51+
#ifdef ALPS_HAVE_MPI
5152
comm(),
53+
#endif
5254
thermalization_sweeps(parameters["THERMALIZATION"]), //sweeps needed for thermalization
5355
total_sweeps(parameters["SWEEPS"]), //sweeps needed for total run
5456
N_meas(parameters["N_MEAS"]),
@@ -513,18 +515,24 @@ void HybridizationSimulation<IMP_MODEL>::update_MC_parameters() {
513515
1.0/std::max(static_cast<double>(sweeps)/static_cast<double>(interval_update_cutoff),1.0)
514516
);
515517

518+
#ifdef ALPS_HAVE_MPI
516519
boost::tuple<bool,double> r_pair = weight_vs_distance.update_cutoff(acc_rate_cutoff, max_distance_pair, mag, comm);
520+
#else
521+
boost::tuple<bool,double> r_pair = weight_vs_distance.update_cutoff(acc_rate_cutoff, max_distance_pair, mag);
522+
#endif
517523
max_distance_pair = boost::get<1>(r_pair);
518524
max_distance_pair = std::min(0.5*BETA, max_distance_pair);
519525

520526
//std::cout << "Done max_distance_pair rank " << comm.rank() << " sweeps " << sweeps << std::endl;
521527

528+
#ifdef ALPS_HAVE_MPI
522529
boost::tuple<bool,double> r_shift = weight_vs_distance_shift.update_cutoff(acc_rate_cutoff, max_distance_shift, mag, comm);
530+
#else
531+
boost::tuple<bool,double> r_shift = weight_vs_distance_shift.update_cutoff(acc_rate_cutoff, max_distance_shift, mag);
532+
#endif
523533
max_distance_shift = boost::get<1>(r_shift);
524534
max_distance_shift = std::min(0.5*BETA, max_distance_shift);
525535

526-
//std::cout << "Done max_distance_shift rank " << comm.rank() << " sweeps " << sweeps << std::endl;
527-
528536
const double max_distance = std::max(max_distance_pair,max_distance_shift);
529537
const std::size_t n_window_new = static_cast<std::size_t>(std::max(1,static_cast<int>(BETA/(2.0*max_distance))));
530538

@@ -544,16 +552,15 @@ void HybridizationSimulation<IMP_MODEL>::prepare_for_measurement() {
544552
max_dist_optimizer.reset();
545553
weight_vs_distance.reset();
546554
weight_vs_distance_shift.reset();
547-
//std::cout << "Call prepare_for_measurement rank " << comm.rank() << std::endl;
548-
if (comm.rank()==0) {
555+
if (global_mpi_rank==0) {
549556
std::cout << "We're done with thermalization." << std::endl << "The number of segments for sliding window update is " << sliding_window.get_n_window() << "." << std::endl << std::endl;
550557
}
551558

552559
//N_meas
553560
const int N_meas_min = std::max(10, 4*sliding_window.get_n_window());//a sweep of the window takes 4*get_n_window()
554561
if (N_meas<N_meas_min) {
555562
N_meas = N_meas_min;
556-
if (comm.rank()==0) {
563+
if (global_mpi_rank==0) {
557564
std::cout << "Warning N_MEAS is too small: using N_MEAS = " << N_meas << " instead." << std::endl;
558565
}
559566
}
@@ -562,7 +569,7 @@ void HybridizationSimulation<IMP_MODEL>::prepare_for_measurement() {
562569
const int N_meas_g_min = std::max(10, sliding_window.get_n_window());//a sweep of the window takes 4*get_n_window()
563570
if (N_meas_g<N_meas_g_min) {
564571
N_meas_g = N_meas_g_min;
565-
if (comm.rank()==0) {
572+
if (global_mpi_rank==0) {
566573
std::cout << "Warning N_MEAS_GREENS_FUNCTION is too small: using N_MEAS_GREENS_FUNCTION = " << N_meas_g << " instead." << std::endl;
567574
}
568575
}
@@ -571,7 +578,7 @@ void HybridizationSimulation<IMP_MODEL>::prepare_for_measurement() {
571578
const int N_swap_min =std::max(10, 4*sliding_window.get_n_window());//a sweep of the window takes 4*get_n_window()
572579
if (N_swap<N_swap_min && swap_vector.size()>0) {
573580
N_swap = N_swap_min;
574-
if (comm.rank()==0) {
581+
if (global_mpi_rank==0) {
575582
std::cout << "Warning N_SWAP is too small: using N_SWAP = " << N_swap << " instead." << std::endl;
576583
}
577584
}

src/impurity_init.ipp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void HybridizationSimulation<IMP_MODEL>::resize_vectors() {
9797
}
9898
swap_acc_rate.resize(swap_vector.size());
9999

100-
if (comm.rank() == 0) {
100+
if (global_mpi_rank == 0) {
101101
std::cout << "The following swap updates will be performed." << std::endl;
102102
for (int i=0; i<swap_vector.size(); ++i) {
103103
std::cout << "Update #" << i << " generated from template #" << swap_vector[i].second << std::endl;
@@ -125,7 +125,7 @@ void HybridizationSimulation<IMP_MODEL>::read_eq_time_two_particle_greens_meas()
125125
const int GF_RANK=2;
126126

127127
const std::string fname_key = "EQUAL_TIME_TWO_PARTICLE_GREENS_FUNCTION";
128-
const bool verbose = (comm.rank()==0);
128+
const bool verbose = (global_mpi_rank==0);
129129

130130
if (!par.defined(fname_key)) {
131131
return;
@@ -170,7 +170,7 @@ void HybridizationSimulation<IMP_MODEL>::read_eq_time_two_particle_greens_meas()
170170
template<typename IMP_MODEL>
171171
void HybridizationSimulation<IMP_MODEL>::read_two_time_correlation_functions() {
172172
const std::string fname_key = "TWO_TIME_CORRELATION_FUNCTIONS";
173-
const bool verbose = (comm.rank()==0);
173+
const bool verbose = (global_mpi_rank==0);
174174

175175
if (!par.defined("N_TAU_TWO_TIME_CORRELATION_FUNCTIONS")) {
176176
return;

src/main.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,71 @@
1616
#include "util.hpp"
1717

1818
#include <alps/utilities/signal.hpp>
19-
#include <alps/utilities/mpi.hpp>
2019
#include <alps/mc/api.hpp>
2120
#include <alps/mc/mcbase.hpp>
22-
#include <alps/mc/mpiadapter.hpp>
2321
#include <alps/mc/stop_callback.hpp>
24-
25-
#include "mympiadapter.hpp"
22+
#ifdef ALPS_HAVE_MPI
23+
#include <alps/utilities/mpi.hpp>
24+
#include "mc/mympiadapter.hpp"
25+
#endif
26+
#include "mc/mymcadapter.hpp"
2627
#include "postprocess.hpp"
2728

2829
#undef BUILD_PYTHON_MODULE
2930

31+
int global_mpi_rank;
32+
3033
template<class SOLVER_TYPE>
3134
int run_simulation(int argc, const char* argv[], typename alps::parameters_type<SOLVER_TYPE>::type& parameters) {
35+
#ifdef ALPS_HAVE_MPI
3236
typedef mymcmpiadapter<SOLVER_TYPE> sim_type;
37+
#else
38+
typedef mymcadapter<SOLVER_TYPE> sim_type;
39+
#endif
3340

3441
SOLVER_TYPE::define_parameters(parameters);
3542
if (parameters.help_requested(std::cout)) {
3643
exit(0);
3744
}
3845

3946
char** argv_tmp = const_cast<char**>(argv);//ugly solution
47+
#ifdef ALPS_HAVE_MPI
4048
alps::mpi::environment env(argc, argv_tmp);
4149
alps::mpi::communicator c;
4250
c.barrier();
43-
if (c.rank()==0) {
51+
global_mpi_rank = c.rank();
52+
if (global_mpi_rank == 0) {
4453
std::cout << "Creating simulation..." << std::endl;
4554
}
4655
sim_type sim(parameters, c);
47-
48-
// Run the simulation
4956
const boost::function<bool()> cb = alps::stop_callback(c,size_t(parameters["timelimit"]));
57+
#else
58+
global_mpi_rank = 0;
59+
sim_type sim(parameters);
60+
const boost::function<bool()> cb = alps::stop_callback(size_t(parameters["timelimit"]));
61+
#endif
62+
5063
sim.run(cb);
5164

5265
// Saving to the output file
66+
#ifdef ALPS_HAVE_MPI
5367
if (c.rank()==0){
68+
#endif
5469
typename alps::results_type<SOLVER_TYPE>::type results = alps::collect_results(sim);
5570
std::string output_file = parameters["outputfile"];
5671
alps::hdf5::archive ar(boost::filesystem::path(output_file), "w");
5772
ar["/parameters"] << parameters;
5873
ar["/simulation/results"] << results;
5974
compute_greens_functions<SOLVER_TYPE>(results, parameters, ar);
75+
#ifdef ALPS_HAVE_MPI
6076
} else{
6177
alps::collect_results(sim);
6278
}
79+
#endif
80+
81+
#ifdef ALPS_HAVE_MPI
6382
c.barrier();
83+
#endif
6484

6585
return 0;
6686
}

src/mc/mymcadapter.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include <boost/format.hpp>
4+
#include <boost/date_time/posix_time/posix_time_types.hpp>
5+
#include <alps/mc/check_schedule.hpp>
6+
7+
template<typename Base, typename ScheduleChecker = alps::check_schedule> class mymcadapter : public Base {
8+
public:
9+
typedef typename Base::parameters_type parameters_type;
10+
typedef boost::posix_time::ptime ptime;
11+
12+
/// Construct mcmpiadapter with alps::check_schedule with the relevant parameters Tmin and Tmax taken from the provided parameters
13+
mymcadapter(parameters_type const & parameters)
14+
: Base(parameters, 0),
15+
schedule_checker(parameters["Tmin"], parameters["Tmax"]) {}
16+
17+
bool run(boost::function<bool()> const &stop_callback) {
18+
bool done = false, stopped = false;
19+
bool was_thermalized_before = false;
20+
const ptime start_time = boost::posix_time::second_clock::local_time();
21+
ptime time_last_output = start_time;
22+
const double min_output_interval = 1; //1 sec
23+
do {
24+
const bool is_thermalized = this->is_thermalized();
25+
if (is_thermalized && !was_thermalized_before) {
26+
this->prepare_for_measurement();
27+
}
28+
29+
this->update();
30+
31+
if (is_thermalized) {
32+
this->measure();
33+
}
34+
35+
was_thermalized_before = is_thermalized;
36+
const ptime current_time = boost::posix_time::second_clock::local_time();
37+
if (!stopped && !is_thermalized) {
38+
if ((current_time-time_last_output).total_seconds()>min_output_interval) {
39+
std::cout << boost::format("Not thermalized yet: %1% sec passed.")%static_cast<int>((current_time-start_time).total_seconds()) << std::endl;
40+
time_last_output = current_time;
41+
}
42+
} else if (stopped || schedule_checker.pending()) {
43+
stopped = stop_callback();
44+
double local_fraction = stopped ? 1. : Base::fraction_completed();
45+
schedule_checker.update(local_fraction);
46+
if ((current_time-time_last_output).total_seconds()>min_output_interval) {
47+
std::cout << "Checking if the simulation is finished: " << std::min(static_cast<int>(local_fraction*100),100) << "% of Monte Carlo steps done." << std::endl;
48+
time_last_output = current_time;
49+
}
50+
done = local_fraction >= 1.;
51+
}
52+
} while (!done);
53+
return !stopped;
54+
}
55+
private:
56+
ScheduleChecker schedule_checker;
57+
};

src/update_histogram.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <algorithm>
44

5+
#ifdef ALPS_HAVE_MPI
56
#include <alps/utilities/mpi.hpp>
67

78
/// performs mpi_allreduce() for a valarray of type T.
@@ -12,6 +13,7 @@ void all_reduce(const alps::mpi::communicator& comm, const std::valarray<T>& in_
1213
out_vals.resize(in_vals.size());
1314
MPI_Allreduce((void*)&in_vals[0], (void*)&out_vals[0], in_vals.size(), alps::mpi::detail::mpi_type<T>(), alps::mpi::is_mpi_op<Op,T>::op(), comm);
1415
}
16+
#endif
1517

1618
template<class T>
1719
void rebin(std::valarray<T>& org_array, int nrebin) {
@@ -106,7 +108,11 @@ class scalar_histogram
106108
}
107109

108110
//maxdist is not updated if we do not have enough data.
111+
#ifdef ALPS_HAVE_MPI
109112
boost::tuple<bool,double> update_cutoff(double cutoff_ratio, double maxdist, double mag, const alps::mpi::communicator& alps_comm) const {
113+
#else
114+
boost::tuple<bool,double> update_cutoff(double cutoff_ratio, double maxdist, double mag) const {
115+
#endif
110116
assert(cutoff_ratio>=0.0 && cutoff_ratio<=1.0);
111117
assert(mag>=1.0);
112118
const int min_count = 10;//for stabilization
@@ -116,13 +122,18 @@ class scalar_histogram
116122
std::valarray<double> counter_gathered(0.0, num_data);
117123
std::valarray<double> sumval_gathered(0.0, num_data);
118124

125+
#ifdef ALPS_HAVE_MPI
119126
alps_comm.barrier();
120127
assert(sumval.size()==num_data);
121128
assert(counter_gathered.size()==num_data);
122129
assert(sumval_gathered.size()==num_data);
123130
all_reduce(alps_comm, counter, counter_gathered, std::plus<double>());
124131
all_reduce(alps_comm, sumval, sumval_gathered, std::plus<double>());
125132
alps_comm.barrier();
133+
#else
134+
counter_gathered = counter;
135+
sumval_gathered = sumval;
136+
#endif
126137

127138
double maxdist_new = maxdist;
128139

@@ -240,11 +251,19 @@ class scalar_histogram_flavors
240251
return sumval_flavors;
241252
}
242253

254+
#ifdef ALPS_HAVE_MPI
243255
double update_cutoff(double cutoff_ratio, double maxdist, double mag, const alps::mpi::communicator& alps_comm) const {
256+
#else
257+
double update_cutoff(double cutoff_ratio, double maxdist, double mag) const {
258+
#endif
244259
double maxdist_new = -1.0;
245260
//for (auto& elem : histograms) {
246261
for (int ielm=0; ielm<histograms.size(); ++ielm) {
262+
#ifdef ALPS_HAVE_MPI
247263
boost::tuple<bool,double> r = histograms[ielm].update_cutoff(cutoff_ratio, maxdist, mag, alps_comm);
264+
#else
265+
boost::tuple<bool,double> r = histograms[ielm].update_cutoff(cutoff_ratio, maxdist, mag);
266+
#endif
248267
maxdist_new = std::max(maxdist_new, boost::get<1>(r));
249268
}
250269
assert(maxdist_new>0);

0 commit comments

Comments
 (0)