runner_single.cpp 2.59 KB
Newer Older
Stefan Weßel's avatar
ic  
Stefan Weßel committed
1
#include "runner_single.h"
2
#include "iodump.h"
3
#include "merger.h"
Stefan Weßel's avatar
ic  
Stefan Weßel committed
4

5
#include <fmt/format.h>
6
#include <fstream>
Lukas Weber's avatar
Lukas Weber committed
7 8
#include <iostream>
#include <sys/stat.h>
9

10 11
namespace loadl {

12
int runner_single_start(jobinfo job, const mc_factory &mccreator, int, char **) {
13 14 15
	runner_single r{std::move(job), mccreator};
	r.start();
	return 0;
16 17
}

18
runner_single::runner_single(jobinfo job, mc_factory mccreator)
19
    : job_{std::move(job)}, mccreator_{std::move(mccreator)} {}
Lukas Weber's avatar
Lukas Weber committed
20

21 22 23
int runner_single::start() {
	time_start_ = time(nullptr);
	time_last_checkpoint_ = time_start_;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
24 25

	read();
Lukas Weber's avatar
Lukas Weber committed
26
	task_id_ = get_new_task_id(task_id_);
27
	while(task_id_ != -1 && !time_is_up()) {
Lukas Weber's avatar
oops  
Lukas Weber committed
28
		sys_ = std::unique_ptr<mc>{mccreator_(job_.jobfile["tasks"][job_.task_names.at(task_id_)])};
29 30
		if(!sys_->_read(job_.rundir(task_id_, 1))) {
			sys_->_init();
Lukas Weber's avatar
Lukas Weber committed
31 32 33
			job_.log(fmt::format("* initialized {}", job_.rundir(task_id_, 1)));
		} else {
			job_.log(fmt::format("* read {}", job_.rundir(task_id_, 1)));
Stefan Weßel's avatar
ic  
Stefan Weßel committed
34
		}
35

36
		while(!tasks_[task_id_].is_done() && !time_is_up()) {
37
			sys_->_do_update();
Lukas Weber's avatar
Lukas Weber committed
38
			tasks_[task_id_].sweeps++;
39 40
			if(sys_->is_thermalized()) {
				sys_->_do_measurement();
Stefan Weßel's avatar
ic  
Stefan Weßel committed
41
			}
42

43
			if(is_checkpoint_time()) {
Stefan Weßel's avatar
ic  
Stefan Weßel committed
44 45 46
				checkpointing();
			}
		}
Lukas Weber's avatar
Lukas Weber committed
47

Stefan Weßel's avatar
ic  
Stefan Weßel committed
48 49
		checkpointing();
		merge_measurements();
Lukas Weber's avatar
Lukas Weber committed
50
		task_id_ = get_new_task_id(task_id_);
Stefan Weßel's avatar
ic  
Stefan Weßel committed
51
	}
52 53

	return 0;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
54 55
}

56 57
bool runner_single::is_checkpoint_time() const {
	return time(nullptr) - time_last_checkpoint_ > job_.checkpoint_time;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
58 59
}

60
bool runner_single::time_is_up() const {
Lukas Weber's avatar
Lukas Weber committed
61
	return time(nullptr) - time_start_ > job_.runtime;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
62 63
}

Lukas Weber's avatar
Lukas Weber committed
64 65 66 67
int runner_single::get_new_task_id(int old_id) {
	int ntasks = tasks_.size();
	int i;
	for(i = 1; i <= ntasks; i++) {
68 69
		if(!tasks_[(old_id + i) % ntasks].is_done()) {
			return (old_id + i) % ntasks;
70
		}
Stefan Weßel's avatar
ic  
Stefan Weßel committed
71 72
	}

Lukas Weber's avatar
Lukas Weber committed
73 74
	// everything done!
	return -1;
Stefan Weßel's avatar
ic  
Stefan Weßel committed
75 76 77
}

void runner_single::read() {
78 79
	for(size_t i = 0; i < job_.task_names.size(); i++) {
		auto task = job_.jobfile["tasks"][job_.task_names[i]];
80

Lukas Weber's avatar
Lukas Weber committed
81 82
		int target_sweeps = task.get<int>("sweeps");
		int target_thermalization = task.get<int>("thermalization");
Lukas Weber's avatar
Lukas Weber committed
83 84
		int sweeps = 0;

85
		sweeps = job_.read_dump_progress(i);
Lukas Weber's avatar
Lukas Weber committed
86
		tasks_.emplace_back(target_sweeps, target_thermalization, sweeps, 0);
Stefan Weßel's avatar
ic  
Stefan Weßel committed
87 88 89 90
	}
}

void runner_single::checkpointing() {
91
	time_last_checkpoint_ = time(nullptr);
92 93
	sys_->_write(job_.rundir(task_id_, 1));
	job_.log(fmt::format("* checkpointing {}", job_.rundir(task_id_, 1)));
Stefan Weßel's avatar
ic  
Stefan Weßel committed
94 95 96
}

void runner_single::merge_measurements() {
97
	std::string unique_filename = job_.taskdir(task_id_);
98
	sys_->_write_output(unique_filename);
99 100

	std::vector<evalable> evalables;
101
	sys_->register_evalables(evalables);
102

103
	job_.log(fmt::format("merging {}", job_.taskdir(task_id_)));
104
	job_.merge_task(task_id_, evalables);
Stefan Weßel's avatar
ic  
Stefan Weßel committed
105
}
106
}