chiark / gitweb /
better machinery; before make view interpolate-compatible
authorIan Jackson <ian@davenant.greenend.org.uk>
Fri, 18 Jan 2008 22:12:53 +0000 (22:12 +0000)
committerIan Jackson <ian@davenant.greenend.org.uk>
Fri, 18 Jan 2008 22:12:53 +0000 (22:12 +0000)
.bzrignore
Makefile
energy.c
interpolate.c
minimise.c
minimise.h

index 2f356c250c36c1344c08d41dd7c870ffac038710..93c99e1a953247c8e2ea60d9e6c7cfecec6cbab3 100644 (file)
@@ -9,3 +9,4 @@ core
 vgcore.*
 prime.data
 *.cfm
+*.CFM
index 42ebbd59caca045940666ede5f5396a780292cf5..a2409fc4aa49f6128509671b81e526400cc7f94d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,8 @@ o= >$@.new && mv -f $@.new $@
 
 all:           $(TARGETS)
 
+compute:       best.CFM
+
 minimise:      energy.o graph.o common.o mgraph.o minimise.o half.o
                $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBGSL)
 
@@ -38,6 +40,9 @@ lumpy.cfm: oldmoebius-converter prime.data ../moebius/ins-new ../moebius/a.out
 ring.cfm: oldmoebius-converter prime.data /dev/null ../moebius/a.out
                ./$^ -o$@
 
+best.CFM:      minimise sgtatham.cfm
+               ./$^ -iwip.cfm -o$@
+
 view-%:                view+%.o mgraph+%.o common.o
                $(CC) $(CFLAGS) -o $@ $^ $(LIBGSL) -L/usr/X11R6/lib -lX11
 
@@ -60,10 +65,10 @@ interpolate+%.o: interpolate.c
 clean:
                rm -f prime.data $(TARGETS)
                rm -f *.o *.new *.tmp *.rej *.orig core vgcore.* *~
-               rm -f *.d
+               rm -f *.d *.cfm
 
 realclean:     clean
-               rm -f best
+               rm -f *.CFM
 
 %.d:
 
index bba938d45ae7fc2e4e15cc7e4cbee7f0d7998cdf..478969824cc6b324fe760048c92f336721eb9a4e 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -19,6 +19,8 @@ void energy_init(void) {
 /*---------- main energy computation and subroutines ----------*/
 
 double compute_energy(const struct Vertices *vs) {
+  static int bests_unprinted;
+  
   double energy;
   int printing;
 
@@ -28,7 +30,7 @@ double compute_energy(const struct Vertices *vs) {
 
   printing= printing_check(pr_cost,0);
 
-  if (printing) printf("cost > energy |");
+  if (printing) printf("%15lld c>e |", evaluations);
 
   COST(3e2, line_bending_cost(vs->a));
   COST(1e3, edge_length_variation_cost(vs->a));
@@ -42,12 +44,18 @@ double compute_energy(const struct Vertices *vs) {
     FILE *best_f;
     int r;
 
-    if (printing) printf(" BEST");
+    if (printing) {
+      printf(" BEST");
+      if (bests_unprinted) printf(" [%4d]",bests_unprinted);
+      bests_unprinted= 0;
+    } else {
+      bests_unprinted++;
+    }
 
-    best_f= fopen(output_file_tmp,"wb");  if (!best_f) diee("fopen new out");
+    best_f= fopen(best_file_tmp,"wb");  if (!best_f) diee("fopen new out");
     r= fwrite(vs->a,sizeof(vs->a),1,best_f); if (r!=1) diee("fwrite");
     if (fclose(best_f)) diee("fclose new best");
-    if (rename(output_file_tmp,output_file)) diee("rename install new best");
+    if (rename(best_file_tmp,best_file)) diee("rename install new best");
 
     best_energy= energy;
   }
@@ -56,12 +64,13 @@ double compute_energy(const struct Vertices *vs) {
     flushoutput();
   }
 
+  evaluations++;
   return energy;
 }
 
 static void addcost(double *energy, double tweight, double tcost, int pr) {
   double tenergy= tweight * tcost;
-  if (pr) printf(" %# e x %# e > %# e* |", tcost, tweight, tenergy);
+  if (pr) printf(" %# e x %g > %# e* |", tcost, tweight, tenergy);
   *energy += tenergy;
 }
 
index 6f519e414a2d35664a443babb6b665f1b3cae4cd..d9d391009ca587894e2519d01d0aa5e2671cf44f 100644 (file)
@@ -16,35 +16,46 @@ static void characterise_input(void) {
   
   r= fstat(0,&stab);  if (r) diee("fstat input to find length");
 
-  if (!stab.st_size || stab.st_size % sizeof(double) ||
+  if (!stab.st_size || stab.st_size % (sizeof(double)*D3) ||
       stab.st_size > INT_MAX)
     fail("input file is not reasonable whole number of doubles\n");
 
-  oldsz= stab.st_size / sizeof(double);
+  oldsz= stab.st_size / (sizeof(double)*D3);
   for (shift=1;
-       shift > XBITS+1 && shift > YBITS+1;
+       shift < XBITS+1 && shift < YBITS+1;
        shift++) {
     oldxbits= XBITS-1;
     oldybits= YBITS-1;
     oldx=  1<<oldxbits;
     oldy= (1<<oldybits)-1;
+    fprintf(stderr,"sizeof(double)=%d XYBITS=%d,%d, XY=%d*%d=%d"
+           " oldsz=%d shift=%d oldxybits=%d,%d oldxy=%d*%d=%d\n",
+           (int)sizeof(double), XBITS,YBITS, X,Y,N,
+           oldsz,shift,oldxbits,oldybits,oldx,oldy,oldx*oldy);
     if (oldx*oldy == oldsz) goto found;
   }
   fail("input file size cannot be interpolated to target file size\n");
 
  found:
   inc= 1<<shift;
+  fprintf(stderr,"inc=%d\n",inc);
 }
 
 static void read_input(void) {
-  int x,y;
+  int x,y, ox,oy, v,ov;
   
-  for (y=0; y<Y; y+=inc)
-    for (x=0; x<X; x+=inc) {
+  for (oy=y=0; y<Y; oy++, y+=inc) {
+    fprintf(stderr, "y=%2d>%2d", oy,y);
+    for (ox=x=0; x<X; ox++, x+=inc) {
       errno= 0;
-      if (fread(all.a[(y << YSHIFT) | x], sizeof(double), D3, stdin) != D3)
-       diee("read input");
+      ov= (oy << oldxbits) | ox;
+      v= (y << YSHIFT) | x;
+      fprintf(stderr, " 0%02o->0x%02x", ov, v);
+      if (fread(all.a[v], sizeof(double), D3, stdin) != D3)
+       diee("\nread input");
     }
+    fputc('\n',stderr);
+  }
 }
 
   /* We use GSL's interpolation functions.  Each xa array is simple
index e5bbc87bb6e3523ec43cd50b9665a80aae281b96..8f6a26173c5a9d6e39e1c658432384a8e7aeb5e5 100644 (file)
 #include "half.h"
 #include "minimise.h"
 
-const char *input_file, *output_file;
-char *output_file_tmp;
+const char *input_file, *best_file;
+char *best_file_tmp;
+long long evaluations;
 
 static void printing_init(void);
 
-static gsl_multimin_fminimizer *minimiser;
-
 static const double stop_epsilon= 1e-6;
+static gsl_multimin_fminimizer *minimiser;
+static const char *final_file;
+static char *final_file_tmp;
 
 static double minfunc_f(const gsl_vector *x, void *params) {
   struct Vertices vs;
@@ -55,6 +57,11 @@ static double minfunc_f(const gsl_vector *x, void *params) {
   return compute_energy(&vs);
 }
 
+static void badusage(void) {
+  fputs("usage: minimise <input> [-i<intermediate>] -o<output\n",stderr);
+  exit(8);
+}
+
 int main(int argc, const char *const *argv) {
   gsl_multimin_function multimin_function;
   double size;
@@ -64,12 +71,22 @@ int main(int argc, const char *const *argv) {
   gsl_vector initial_gsl, step_size_gsl;
   int r, i;
 
-  if (argc!=3 || argv[1][0]=='-' || strncmp(argv[2],"-o",2))
-    { fputs("usage: minimise <input> -o<output\n",stderr); exit(8); }
+  if (argc==3) {
+    input_file= argv[1];
+    if (strncmp(argv[2],"-o",2)) badusage();   best_file= argv[2]+2;
+    final_file= 0;
+  } else if (argc==4) {
+    input_file= argv[1];
+    if (strncmp(argv[2],"-i",2)) badusage();   best_file= argv[2]+2;
+    if (strncmp(argv[3],"-o",2)) badusage();   final_file= argv[3]+2;
+  } else {
+    badusage();
+  }
+  if (argv[1][0]=='-') badusage();
 
-  input_file= argv[1];
-  output_file= argv[2]+2;
-  if (asprintf(&output_file_tmp,"%s.new",output_file) <= 0) diee("asprintf");
+  if (asprintf(&best_file_tmp,"%s.new",best_file) <= 0) diee("asprintf");
+  if (final_file)
+    if (asprintf(&final_file_tmp,"%s.new",final_file) <= 0) diee("asprintf");
 
   graph_layout_prepare();
   printing_init();
@@ -112,13 +129,21 @@ int main(int argc, const char *const *argv) {
     size= gsl_multimin_fminimizer_size(minimiser);
     r= gsl_multimin_test_size(size, stop_epsilon);
 
-    if (printing_check(pr_size,155))
-      printf("size %# e, r=%d\n", size, r);
+    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 (final_file) {
+    if (unlink(final_file_tmp) && errno != ENOENT) diee("unlink final .tmp");
+    if (link(best_file,final_file_tmp)) diee("link final .tmp");
+    if (rename(final_file_tmp,final_file)) diee("install final");
+  }
+  printf("FINISHED (%lld evaluations)\n",evaluations);
+  
   return 0;
 }
 
@@ -142,9 +167,10 @@ int printing_check(enum printing_instance which, int indent) {
   print_todo &= ~bits;
   sigprocmask(SIG_UNBLOCK,&print_alarmset,0);
 
+  printf("%*s",indent,"");
   sk= skipped[which];
-  if (sk) printf("%*s[%4d] ",indent,"", sk);
-  else printf("       ");
+  if (sk) printf("[%4d] ",sk);
+  else printf("      ");
   skipped[which]= 0;
 
   return 1;
index 2374c92dbeb20c5a0f27783848d8ecf4cb4f3161..28bef2c9e7eb9f296bfaa4dfd11b236cafc71b31 100644 (file)
@@ -22,8 +22,9 @@ double noncircular_rim_cost(const Vertices vertices);
 double edge_length_variation_cost(const Vertices vertices);
 double rim_proximity_cost(const Vertices vertices);
 
-extern const char *input_file, *output_file;
-extern char *output_file_tmp;
+extern const char *input_file, *best_file;
+extern char *best_file_tmp;
+extern long long evaluations;
 
 enum printing_instance { pr_cost, pr_size, pr__max };
 int printing_check(enum printing_instance, int indent);