chiark / gitweb /
on SIGINT, do not exit 0
[moebius2.git] / minimise.c
index 8f6a26173c5a9d6e39e1c658432384a8e7aeb5e5..430517f6fd80dfc16a32c43437d4fdaff106a864 100644 (file)
 const char *input_file, *best_file;
 char *best_file_tmp;
 long long evaluations;
+double stop_epsilon= 1e-20;
 
 static void printing_init(void);
 
-static const double stop_epsilon= 1e-6;
 static gsl_multimin_fminimizer *minimiser;
 static const char *final_file;
 static char *final_file_tmp;
@@ -62,6 +62,12 @@ static void badusage(void) {
   exit(8);
 }
 
+static sig_atomic_t quit_requested;
+
+static void sigint_handler(int ignored) {
+  quit_requested= 1;
+}
+
 int main(int argc, const char *const *argv) {
   gsl_multimin_function multimin_function;
   double size;
@@ -69,7 +75,8 @@ int main(int argc, const char *const *argv) {
   double initial_half[DIM], step_size[DIM];
   FILE *initial_f;
   gsl_vector initial_gsl, step_size_gsl;
-  int r, i;
+  int r, i, size_check_counter= 0;
+  struct sigaction sa;
 
   if (argc==3) {
     input_file= argv[1];
@@ -88,6 +95,12 @@ int main(int argc, const char *const *argv) {
   if (final_file)
     if (asprintf(&final_file_tmp,"%s.new",final_file) <= 0) diee("asprintf");
 
+  memset(&sa,0,sizeof(sa));
+  sa.sa_handler= sigint_handler;
+  sa.sa_flags= SA_RESETHAND;
+  r= sigaction(SIGINT,&sa,0);
+  if (r) diee("sigaction SIGINT");
+
   graph_layout_prepare();
   printing_init();
   energy_init();
@@ -124,17 +137,24 @@ int main(int argc, const char *const *argv) {
                                  &initial_gsl, &step_size_gsl) );
 
   for (;;) {
+    if (quit_requested) {
+      fprintf(stderr,"SIGINT caught.\n");
+      exit(1);
+    }
+
     GA( gsl_multimin_fminimizer_iterate(minimiser) );
 
-    size= gsl_multimin_fminimizer_size(minimiser);
-    r= gsl_multimin_test_size(size, stop_epsilon);
+    if (!(size_check_counter++ % DIM)) {
+      size= gsl_multimin_fminimizer_size(minimiser);
+      r= gsl_multimin_test_size(size, stop_epsilon);
 
-    if (printing_check(pr_size,215))
-      printf("r=%2d, size %# e\n", r, size);
-    flushoutput();
+      if (printing_check(pr_size,215))
+       printf("r=%2d, size %# e\n", r, size);
+      flushoutput();
 
-    if (r==GSL_SUCCESS) break;
-    assert(r==GSL_CONTINUE);
+      if (r==GSL_SUCCESS) break;
+      assert(r==GSL_CONTINUE);
+    }
   }
 
   if (final_file) {