Skip to content

Commit

Permalink
fix state, end flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tlemane committed Jan 17, 2024
1 parent 01871f9 commit 64dbc8a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 deletions.
3 changes: 3 additions & 0 deletions include/kmtricks/cmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct main_all
scheduler.execute();

state::get().write();
{
std::ofstream endf(opt->end_flag, std::ios::out);
}
}
};

Expand Down
1 change: 1 addition & 0 deletions include/kmtricks/cmd/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct all_options : km_options
double focus {1.0};

std::string from;
std::string end_flag;

MODE mode;
FORMAT format;
Expand Down
17 changes: 12 additions & 5 deletions include/kmtricks/signals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ inline std::string signal_to_string(int signal)
}

static std::mutex SigLock;
static bool sig_done = false;

class SignalHandler
{
Expand All @@ -80,11 +81,11 @@ class SignalHandler
template <typename Callback = void (*)(int)>
void init(Callback c = nullptr)
{
std::signal(SIGABRT, c ? c : default_callback);
std::signal(SIGFPE, c ? c : default_callback);
std::signal(SIGILL, c ? c : default_callback);
std::signal(SIGSEGV, c ? c : default_callback);
std::signal(SIGTERM, c ? c : default_callback);
//std::signal(SIGABRT, c ? c : default_callback);
//std::signal(SIGFPE, c ? c : default_callback);
//std::signal(SIGILL, c ? c : default_callback);
//std::signal(SIGSEGV, c ? c : default_callback);
//std::signal(SIGTERM, c ? c : default_callback);

std::signal(SIGINT, c ? c : state_callback);
}
Expand All @@ -99,9 +100,15 @@ class SignalHandler
{
std::unique_lock<std::mutex> _(SigLock);

if (!sig_done)
{

state::get().write(
fmt::format(
"Killed after {}. The run can be resumed by running the same command again.", signal_to_string(signal)));

sig_done = true;
}
}

static void default_callback(int signal)
Expand Down
18 changes: 11 additions & 7 deletions include/kmtricks/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ namespace km {
m_superk.resize(f, 0);
m_count.resize(f * p, 0);
m_merge.resize(p, 0);

}

void init_f(const std::string& f, std::size_t n, std::size_t p)
{
m_nb = n;
m_part = p;
m_path = f;
std::ifstream inf(f, std::ios::in | std::ios::binary);

m_superk.resize(m_nb, 0);
Expand All @@ -46,20 +48,22 @@ namespace km {
inf.read(reinterpret_cast<char*>(m_superk.data()), sizeof(std::uint8_t) * n);
inf.read(reinterpret_cast<char*>(m_count.data()), sizeof(std::uint8_t) * (n*p));
inf.read(reinterpret_cast<char*>(m_merge.data()), sizeof(std::uint8_t) * p);

}

void write(const std::string& msg = "")
{
std::unique_lock<spinlock> _1(m_lock_1);
std::unique_lock<spinlock> _2(m_lock_2);
std::ofstream inf(m_path, std::ios::out | std::ios::binary);

inf.write(reinterpret_cast<char*>(&m_config), sizeof(m_config));
inf.write(reinterpret_cast<char*>(&m_repart), sizeof(m_repart));
inf.write(reinterpret_cast<char*>(m_superk.data()), sizeof(std::uint8_t) * m_nb);
inf.write(reinterpret_cast<char*>(m_count.data()), sizeof(std::uint8_t) * (m_nb*m_part));
inf.write(reinterpret_cast<char*>(m_merge.data()), sizeof(std::uint8_t) * m_part);

{
std::ofstream inf(m_path, std::ios::out | std::ios::binary);
inf.write(reinterpret_cast<char*>(&m_config), sizeof(m_config));
inf.write(reinterpret_cast<char*>(&m_repart), sizeof(m_repart));
inf.write(reinterpret_cast<char*>(m_superk.data()), sizeof(std::uint8_t) * m_nb);
inf.write(reinterpret_cast<char*>(m_count.data()), sizeof(std::uint8_t) * (m_nb*m_part));
inf.write(reinterpret_cast<char*>(m_merge.data()), sizeof(std::uint8_t) * m_part);
}
if (!msg.empty())
{
spdlog::info(msg);
Expand Down
3 changes: 2 additions & 1 deletion include/kmtricks/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@ class HashMergeTask : public ITask
void preprocess() {}
void postprocess()
{
state::get().merge_done(m_part_id);

if (this->m_clear)
{
for (auto& f : KmDir::get().get_files_to_merge(m_part_id, m_lz4, KM_FILE::HASH))
Expand All @@ -750,7 +752,6 @@ class HashMergeTask : public ITask
}
}
this->m_finish = true;
state::get().merge_done(m_part_id);
this->exec_callback();
}

Expand Down
13 changes: 13 additions & 0 deletions scripts/clear_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

echo $1

rm -rf $1/fpr \
$1/filters \
$1/howde_index \
$1/merge_infos \
$1/counts \
$1/histograms \
$1/partition_infos \
$1/superkmers \
$1/minimizers
4 changes: 4 additions & 0 deletions src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ km_options_t all_cli(std::shared_ptr<bc::Parser<1>> cli, all_options_t options)

all_cmd->add_group("merge options", "");

all_cmd->add_param("--end-flag", "write an empty file at the end of the run.")
->meta("STR")
->setter(options->end_flag);

auto a_min_setter = [options](const std::string& v) {
auto [f, _] = bc::check::is_file("", v);
if (f)
Expand Down

0 comments on commit 64dbc8a

Please sign in to comment.