runner_single.cpp 2.51 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
	runner_single r{std::move(job), mccreator};
14
	return r.start();
15
16
}

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

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

	read();
Lukas Weber's avatar
Lukas Weber committed
25
	task_id_ = get_new_task_id(task_id_);
26
	while(task_id_ != -1 && !time_is_up()) {
Lukas Weber's avatar
oops    
Lukas Weber committed
27
		sys_ = std::unique_ptr<mc>{mccreator_(job_.jobfile["tasks"][job_.task_names.at(task_id_)])};
28
29
		if(!sys_->_read(job_.rundir(task_id_, 1))) {
			sys_->_init();
Lukas Weber's avatar
Lukas Weber committed
30
31
32
			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
33
		}
34

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

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

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

52
53
	bool all_done = task_id_ < 0;
	return !all_done;
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
		size_t target_sweeps = task.get<int>("sweeps");
		size_t sweeps = 0;
Lukas Weber's avatar
Lukas Weber committed
83

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

void runner_single::checkpointing() {
90
	time_last_checkpoint_ = time(nullptr);
91
	sys_->_write(job_.rundir(task_id_, 1));
92
	sys_->_write_finalize(job_.rundir(task_id_, 1));
93
	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
	job_.log(fmt::format("merging {}", job_.taskdir(task_id_)));
101
	job_.merge_task(task_id_);
Stefan Weßel's avatar
ic  
Stefan Weßel committed
102
}
103
}