Skip to content

Commit

Permalink
Add max-run-time variable to stop after a specified number of seconds.
Browse files Browse the repository at this point in the history
Note that the code only actually checks every 1000 iterations, so
for heavily computational searches, it could run over time by a
few minutes. So leave some slop.
  • Loading branch information
apoelstra committed Jan 29, 2013
1 parent 594f050 commit 5613af8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Sets a variable. Not all variables are effective for each search space.
max-depth: The maximum depth to search the space.
Default value: (none)

max-run-time: The maximum time (in seconds) to search before stopping.
Default value: (none)

max-iterations: The number of iterations to run before stopping.
Default value: (none)

Expand Down
4 changes: 4 additions & 0 deletions process.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ void process (struct _global_data *state)
dc_list *dlist;
const setting_t *max_iters_set = SETTING ("max_iterations");
const setting_t *max_depth_set = SETTING ("max_depth");
const setting_t *max_run_time_set = SETTING ("max_run_time");
const setting_t *stall_after_set = SETTING ("stall_after");
const setting_t *alphabet_set = SETTING ("alphabet");
const setting_t *gap_set_set = SETTING ("gap_set");
Expand Down Expand Up @@ -304,6 +305,9 @@ void process (struct _global_data *state)
if (max_iters_set)
stream_printf (state->out_stream, " Stop after: \t%ld iterations\n",
max_iters_set->get_int_value (max_iters_set));
if (max_run_time_set)
stream_printf (state->out_stream, " Stop after: \t%ld seconds\n",
max_run_time_set->get_int_value (max_run_time_set));
if (stall_after_set)
stream_printf (state->out_stream, " Stall after: \t%ld iterations\n",
stall_after_set->get_int_value (stall_after_set));
Expand Down
6 changes: 6 additions & 0 deletions ramsey/ramsey.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef RAMSEY_H
#define RAMSEY_H

#include <time.h>

#include "../global.h"
#include "../stream.h"
#include "../recurse.h"
Expand Down Expand Up @@ -59,6 +61,10 @@ struct _ramsey_t {
long r_stall_after;
/*! \brief Current number of iterations with no progress. */
long r_stall_index;
/*! \brief The time (in seconds) that the recursion was started */
time_t r_start_time;
/*! \brief Maximum allowable runtime (in seconds) */
long r_max_run_time;

/* vtable */
/*! \brief Returns a string describing the object. */
Expand Down
10 changes: 10 additions & 0 deletions recurse.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ int recursion_preamble (ramsey_t *rt, global_data_t *state)
if (rt->r_stall_after &&
(rt->r_iterations - rt->r_stall_index > rt->r_stall_after))
return 0;
if (rt->r_max_run_time && rt->r_iterations % 1000 == 0 &&
(time (NULL) - rt->r_start_time) > rt->r_max_run_time)
return 0;

++rt->r_depth;

Expand Down Expand Up @@ -68,7 +71,9 @@ void recursion_init (ramsey_t *rt)
rt->r_stall_after =
rt->r_stall_index =
rt->r_max_depth =
rt->r_max_run_time =
rt->r_prune_tree = 0;

}

void recursion_reset (ramsey_t *rt, global_data_t *state)
Expand All @@ -77,6 +82,7 @@ void recursion_reset (ramsey_t *rt, global_data_t *state)
const setting_t *max_depth_set = SETTING ("max_depth");
const setting_t *stall_after_set = SETTING ("stall_after");
const setting_t *prune_tree_set = SETTING ("prune_tree");
const setting_t *max_run_time_set = SETTING ("max_run_time");

recursion_init (rt);

Expand All @@ -88,5 +94,9 @@ void recursion_reset (ramsey_t *rt, global_data_t *state)
rt->r_stall_after = stall_after_set->get_int_value (stall_after_set);
if (prune_tree_set)
rt->r_prune_tree = prune_tree_set->get_int_value (prune_tree_set);
if (max_run_time_set)
rt->r_max_run_time = max_run_time_set->get_int_value (max_run_time_set);

rt->r_start_time = time (NULL);
}

0 comments on commit 5613af8

Please sign in to comment.