From 6daac5888ebff4aaad24963169fba96fe8224012 Mon Sep 17 00:00:00 2001 From: Florian Stecker Date: Sat, 23 Oct 2021 12:09:40 -0500 Subject: [PATCH] restore old singular_values program in addition to mpi one --- .gitignore | 1 + Makefile | 14 +- cdf.plt | 57 ++++ singular_values.c | 737 ++++++++++++++++++++---------------------- singular_values.plt | 30 +- singular_values_mpi.c | 642 ++++++++++++++++++++++++++++++++++++ 6 files changed, 1066 insertions(+), 415 deletions(-) create mode 100644 cdf.plt create mode 100644 singular_values_mpi.c diff --git a/.gitignore b/.gitignore index ebf22f7..1f23877 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ triangle_group/singular_values .#* singular_values +singular_values_mpi output/ special_element max_slope_picture/generate diff --git a/Makefile b/Makefile index 0ff4f43..a88cd22 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ SPECIAL_OPTIONS=-O3 -pg -funroll-loops -fno-inline OPTIONS=-I../mps/include -L../mps/lib -pthread -m64 -std=gnu99 -D_GNU_SOURCE $(SPECIAL_OPTIONS) -all: singular_values special_element convert billiard_words +all: singular_values special_element singular_values_mpi convert billiard_words convert: convert.hs ghc --make -dynamic convert.hs @@ -17,13 +17,19 @@ billiard_words: billiard_words.hs ghc --make -dynamic billiard_words.hs singular_values: singular_values.o coxeter.o mat.o - mpicc $(OPTIONS) -o singular_values coxeter.o singular_values.o mat.o -lm -lgmp -lmps + gcc $(OPTIONS) -o singular_values coxeter.o singular_values.o mat.o -lm -lgmp -lmps + +singular_values_mpi: singular_values_mpi.o coxeter.o mat.o + mpicc $(OPTIONS) -o singular_values_mpi coxeter.o singular_values_mpi.o mat.o -lm -lgmp -lmps special_element: special_element.o coxeter.o linalg.o mat.o gcc $(OPTIONS) -o special_element coxeter.o linalg.o special_element.o mat.o -lm -lgmp -lmps -lgsl -lcblas singular_values.o: singular_values.c $(HEADERS) - mpicc $(OPTIONS) -c singular_values.c + gcc $(OPTIONS) -c singular_values.c + +singular_values_mpi.o: singular_values_mpi.c $(HEADERS) + mpicc $(OPTIONS) -c singular_values_mpi.c special_element.o: special_element.c $(HEADERS) gcc $(OPTIONS) -c special_element.c @@ -38,4 +44,4 @@ mat.o: mat.c $(HEADERS) gcc $(OPTIONS) -c mat.c clean: - rm -f singular_values special_element coxeter.o linalg.o singular_values.o mat.o special_element.o convert.hi convert.o convert billiard_words.hi billiard_words.o billiard_words + rm -f singular_values special_element singular_values_mpi coxeter.o linalg.o singular_values.o singular_values_mpi.o mat.o special_element.o convert.hi convert.o convert billiard_words.hi billiard_words.o billiard_words diff --git a/cdf.plt b/cdf.plt new file mode 100644 index 0000000..f6b68a2 --- /dev/null +++ b/cdf.plt @@ -0,0 +1,57 @@ +#if(!exists("logt")) logt = log(1.80) +if(!exists("n")) n = 263 +if(!exists("logt")) logt = log(1) +if(!exists("logs")) logs = log(1) + +#logt = 0.01*n +logt = log(1000000000) + +file = sprintf("< ./singular_values 713698 %f %f", exp(logs), exp(logt)) +#file = sprintf("< ./singular_values 1621 %f %f", exp(logs), exp(logt)) +#outfile = sprintf("cdf/cdf_hires_%05d.png", n) +outfile = sprintf("cdf/cdf_hires_limit.png") + +set log x +set zeroaxis +set samples 1000 +set size square +set xrange [0.5:2] +set yrange [0:500000] +#set yrange [0:1000] +set trange [0:30] +set grid +set parametric + +set terminal pngcairo enhanced size 1024, 1024 +set output outfile +print sprintf("n = %d, t = %.2f", n, exp(logt)) + +# plot file using 2:3 w p pt 7 ps 0.5 lc 1 t title + +#tr(a,b) = exp((2*a+b)/3) + exp((b-a)/3) + exp(-(a+2*b)/3) +#trinv(a,b) = exp(-(2*a+b)/3) + exp((a-b)/3) + exp((a+2*b)/3) + +tr(a,b) = exp(a) + exp(b-a) + exp(-b) +trinv(a,b) = exp(-a) + exp(a-b) + exp(b) + +#plot file using 6:7 w p pt 7 ps 0.5 lc 1 t columnheader, +# log(tr(t,t*2)),log(trinv(t,2*t)) w l lw 2 t "", \ +# log(tr(t,t/2)),log(trinv(t,t/2)) w l lw 2 t "" + +plot file using 8:3 w steps lw 2 lc 1 t sprintf("t = %.2f", exp(logt)) + +#plot for[i=-10:10] log(tr(t,t*exp(log(2)*i/10.0))),log(trinv(t,t*exp(log(2)*i/10.0))) w l lw 2 t "" + +#plot for[i=-10:10] t,log(tr(t,t*exp(log(2)*i/10.0)))-t w l lw 2 t "" + +##plot for[i=20:20] t,log(tr(1/t,exp(2*log(2)*i/20.0-log(2)))) w l lw 2 t "" + +#n=n+1 +#if(n < 1000) reread + +# pause mouse keypress +# if(MOUSE_KEY == 60) logt=logt-0.02 +# if(MOUSE_KEY == 62) logt=logt+0.02 +# if(MOUSE_KEY == 44) logs=logs-0.02 +# if(MOUSE_KEY == 46) logs=logs+0.02 +# if(MOUSE_KEY != 113) reread diff --git a/singular_values.c b/singular_values.c index 0f9e1e4..973fe6c 100644 --- a/singular_values.c +++ b/singular_values.c @@ -1,113 +1,26 @@ #include "coxeter.h" -//#include "linalg.h" +#include "linalg.h" #include "mat.h" -//#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#define MIN(x,y) ((x)<(y)?(x):(y)) #define SWAP(t,x,y) do { t _tmp = (x); (x) = (y); (y) = _tmp; } while (0); -#define DEBUG(msg, ...) do { print_time(); fprintf(stderr, msg, ##__VA_ARGS__); } while (0); -//#define DEBUG(msg, ...) +//#define DEBUG(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__) +#define DEBUG(msg, ...) -#define TDIV 10 -#define TFROM 1 -#define TTO 9 -#define MDIV 10 -#define MFROM 1 -#define MTO 9 -#define JOBNR(t,m) (((t)-TFROM) + ((m)-MFROM)*(TTO-TFROM+1)) -#define NJOBS ((TTO-TFROM+1)*(MTO-MFROM+1)) -#define FLUSH_INTERVAL 100 - -enum message_tag { - JOB_ORDER, - JOB_RESULT, - JOB_SHUTDOWN, -}; - -struct job { - int tparam, mparam; - int done; - double max_slope; - double time; -}; +#define OUTPUT_POINTS +//#define OUTPUT_POINTS struct result { + int id; + int count; mpq_t tr; mpq_t trinv; + double x; + double y; }; -struct global_data { - int n; - group_t *group; - mat* matrices; - struct result *invariants; - struct result **distinct_invariants; - mps_context *solver; -}; - - -struct timespec starttime; -char processor_name[MPI_MAX_PROCESSOR_NAME]; -int world_rank; -int world_size; -MPI_Datatype job_datatype; - -void print_time() -{ - double diff; - struct timespec current; - - clock_gettime(CLOCK_REALTIME, ¤t); - - diff = (current.tv_sec - starttime.tv_sec) + (current.tv_nsec - starttime.tv_nsec)*1e-9; - - fprintf(stderr, "[%04d %.3f] ", world_rank, diff); -} - -static struct global_data allocate_global_data(int n) -{ - struct global_data result; - result.n = n; - result.matrices = malloc(n*sizeof(mat)); - for(int i = 0; i < n; i++) - mat_init(result.matrices[i], 3); - result.invariants = malloc(n*sizeof(struct result)); - result.distinct_invariants = malloc(n*sizeof(struct result*)); - for(int i = 0; i < n; i++) { - mpq_init(result.invariants[i].tr); - mpq_init(result.invariants[i].trinv); - result.distinct_invariants[i] = &result.invariants[i]; - } - result.solver = mps_context_new(); - mps_context_set_output_prec(result.solver, 20); // relative precision - mps_context_set_output_goal(result.solver, MPS_OUTPUT_GOAL_APPROXIMATE); - - return result; -} - -void free_global_data(struct global_data dat) -{ - for(int i = 0; i < dat.n; i++) - mat_clear(dat.matrices[i]); - free(dat.matrices); - for(int i = 0; i < dat.n; i++) { - mpq_clear(dat.invariants[i].tr); - mpq_clear(dat.invariants[i].trinv); - } - free(dat.invariants); - free(dat.distinct_invariants); - mps_context_free(dat.solver); -} - static int compare_result(const void *a_, const void *b_) { int d = 0; @@ -116,12 +29,45 @@ static int compare_result(const void *a_, const void *b_) struct result **b = (struct result **)b_; d = mpq_cmp((*a)->tr,(*b)->tr); - if(d == 0) + if(d == 0) { d = mpq_cmp((*a)->trinv, (*b)->trinv); + } return d; } +static int compare_result_with_id(const void *a_, const void *b_) +{ + int d = 0; + + struct result **a = (struct result **)a_; + struct result **b = (struct result **)b_; + + d = mpq_cmp((*a)->tr,(*b)->tr); + if(d == 0) { + d = mpq_cmp((*a)->trinv, (*b)->trinv); + if(d == 0) { + d = (*b)->id - (*a)->id; + } + } + + return d; +} + +static int compare_result_by_slope(const void *a_, const void *b_) +{ + int d = 0; + + struct result **a = (struct result **)a_; + struct result **b = (struct result **)b_; + + double slopea = (*a)->x / (*a)->y; + double slopeb = (*b)->x / (*b)->y; + + return slopea > slopeb ? -1 : slopea < slopeb ? 1 : 0; +} + + int solve_characteristic_polynomial(mps_context *solv, mpq_t tr, mpq_t trinv, double *eigenvalues) { mpq_t coeff1, coeff2, zero; @@ -215,75 +161,99 @@ void quartic(mpq_t out, mpq_t in, int a, int b, int c, int d, int e) mpq_clear(tmp); } -// this version is only for the (4,4,4) group -void initialize_triangle_generators(mat_workspace *ws, mat *gen, mpq_t m, mpq_t t) +void initialize_triangle_generators(mat_workspace *ws, mat *gen, mpq_t s, mpq_t q) { - mpq_t s,sinv,q,x,y; - mpq_t zero, one, two; - mpq_t tmp; + mat r1,r2,r3; + mpq_t rho1, rho2, rho3; + mpq_t b1,c1,a2,c2,a3,b3; + mpq_t sinv; - mpq_inits(s,sinv,q,x,y,zero,one,two,tmp,NULL); - mpq_set_ui(zero, 0, 1); - mpq_set_ui(one, 1, 1); - mpq_set_ui(two, 2, 1); + mpq_inits(sinv,rho1,rho2,rho3,b1,c1,a2,c2,a3,b3,NULL); + mat_init(r1, 3); + mat_init(r2, 3); + mat_init(r3, 3); - // s = (1-m^2)/2m - mpq_mul(s, m, m); - mpq_sub(s, one, s); - mpq_div(s, s, m); - mpq_div(s, s, two); - mpq_div(sinv, one, s); + mpq_set_ui(sinv, 1, 1); + mpq_div(sinv, sinv, s); - // q = (1+m^2)/(1-m^2) = 2/(1-m^2) - 1 - mpq_mul(q, m, m); - mpq_sub(q, one, q); - mpq_div(q, two, q); - mpq_sub(q, q, one); + quartic(rho1, s, 0, 0, 1, -1, 1); + quartic(rho2, s, 0, 0, 1, -1, 1); + quartic(rho3, s, 0, 0, 1, 0, 1); - // x = -tq, y = -q/t - mpq_mul(x, q, t); - mpq_sub(x, zero, x); - mpq_div(y, q, t); - mpq_sub(y, zero, y); + mpq_mul(c1, rho2, q); + mpq_mul(a2, rho3, q); + mpq_mul(b3, rho1, q); + mpq_set_ui(b1, 1, 1); + mpq_set_ui(c2, 1, 1); + mpq_set_ui(a3, 1, 1); + mpq_div(b1, b1, q); + mpq_div(c2, c2, q); + mpq_div(a3, a3, q); - // q^2 = xy = 1 + 1/s^2 - // [ -s s*y 0] - // [ -s*x s*x*y - 1/s 0] - // [ -s*y s*y^2 - x 1] - LOOP(i,3) { - mat_zero(gen[i]); - mpq_sub(tmp, zero, s); - mat_set(gen[i%3], i%3, i%3, tmp); - mpq_mul(tmp, s, y); - mat_set(gen[i%3], i%3, (i+1)%3, tmp); - mpq_mul(tmp, s, x); - mpq_sub(tmp, zero, tmp); - mat_set(gen[i%3], (i+1)%3, i%3, tmp); - mpq_mul(tmp, s, x); - mpq_mul(tmp, tmp, y); - mpq_sub(tmp, tmp, sinv); - mat_set(gen[i%3], (i+1)%3, (i+1)%3, tmp); - mpq_mul(tmp, s, y); - mpq_sub(tmp, zero, tmp); - mat_set(gen[i%3], (i+2)%3, i%3, tmp); - mpq_mul(tmp, s, y); - mpq_mul(tmp, tmp, y); - mpq_sub(tmp, tmp, x); - mat_set(gen[i%3], (i+2)%3, (i+1)%3, tmp); - mat_set(gen[i%3], (i+2)%3, (i+2)%3, one); - } + // actually, we want minus everything + mat_zero(r1); + mat_zero(r2); + mat_zero(r3); + mpq_set_si(*mat_ref(r1, 0, 0), -1, 1); + mpq_set_si(*mat_ref(r1, 1, 1), 1, 1); + mpq_set_si(*mat_ref(r1, 2, 2), 1, 1); + mpq_set_si(*mat_ref(r2, 0, 0), 1, 1); + mpq_set_si(*mat_ref(r2, 1, 1), -1, 1); + mpq_set_si(*mat_ref(r2, 2, 2), 1, 1); + mpq_set_si(*mat_ref(r3, 0, 0), 1, 1); + mpq_set_si(*mat_ref(r3, 1, 1), 1, 1); + mpq_set_si(*mat_ref(r3, 2, 2), -1, 1); - LOOP(i,3) mat_pseudoinverse(ws, gen[i+3], gen[i]); + mpq_set(*mat_ref(r1, 1, 0), b1); + mpq_set(*mat_ref(r1, 2, 0), c1); + mpq_set(*mat_ref(r2, 0, 1), a2); + mpq_set(*mat_ref(r2, 2, 1), c2); + mpq_set(*mat_ref(r3, 0, 2), a3); + mpq_set(*mat_ref(r3, 1, 2), b3); + + mat_zero(gen[0]); + mat_zero(gen[1]); + mat_zero(gen[2]); + + mpq_set_ui(*mat_ref(gen[0], 0, 0), 1, 1); + mat_set(gen[0], 1, 1, sinv); + mat_set(gen[0], 2, 2, s); + + mat_set(gen[1], 0, 0, s); + mpq_set_ui(*mat_ref(gen[1], 1, 1), 1, 1); + mat_set(gen[1], 2, 2, sinv); + + mat_set(gen[2], 0, 0, sinv); + mat_set(gen[2], 1, 1, s); + mpq_set_ui(*mat_ref(gen[2], 2, 2), 1, 1); + + mat_multiply(ws, gen[0], r2, gen[0]); + mat_multiply(ws, gen[0], gen[0], r3); + mat_multiply(ws, gen[1], r3, gen[1]); + mat_multiply(ws, gen[1], gen[1], r1); + mat_multiply(ws, gen[2], r1, gen[2]); + mat_multiply(ws, gen[2], gen[2], r2); + + mat_pseudoinverse(ws, gen[3], gen[0]); + mat_pseudoinverse(ws, gen[4], gen[1]); + mat_pseudoinverse(ws, gen[5], gen[2]); - // debug output /* - gmp_printf("m = %Qd, s = %Qd, t = %Qd, q = %Qd, x = %Qd, y = %Qd\n", m, s, t, q, x, y); + mat_print(r1); + mat_print(r2); + mat_print(r3); mat_print(gen[0]); mat_print(gen[1]); mat_print(gen[2]); + mat_print(gen[3]); + mat_print(gen[4]); + mat_print(gen[5]); */ - mpq_inits(s,sinv,q,x,y,zero,one,two,tmp,NULL); + mpq_clears(sinv,rho1,rho2,rho3,b1,c1,a2,c2,a3,b3,NULL); + mat_clear(r1); + mat_clear(r2); + mat_clear(r3); } char *print_word(groupelement_t *g, char *str) @@ -299,7 +269,7 @@ char *print_word(groupelement_t *g, char *str) return str; } -void enumerate(group_t *group, mat *matrices, mpq_t m, mpq_t t) +void enumerate(group_t *group, mat *matrices, mpq_t s, mpq_t t) { mat_workspace *ws; mat tmp; @@ -312,7 +282,7 @@ void enumerate(group_t *group, mat *matrices, mpq_t m, mpq_t t) mat_init(gen[i], 3); mat_init(tmp, 3); - initialize_triangle_generators(ws, gen, m, t); + initialize_triangle_generators(ws, gen, s, t); mat_identity(matrices[0]); for(int i = 1; i < group->size; i++) { @@ -348,55 +318,194 @@ void enumerate(group_t *group, mat *matrices, mpq_t m, mpq_t t) mat_workspace_clear(ws); } - -double compute_max_slope(struct global_data dat, mpq_t t, mpq_t m) +void output_invariants(group_t *group, mat *matrices, mpq_t s, mpq_t q, mps_context *solver) { -// mpq_set_ui(t, ttick, 100); -// mpq_set_ui(m, mtick, 100); // 414/1000 ~ sqrt(2)-1 <-> s=1 -// s = (1-mpq_get_d(m)*mpq_get_d(m))/(2*mpq_get_d(m)); + mpq_t tr, trinv; + char buf[100]; + double evs[3]; + int retval; - int n = 0; - int nmax = dat.n; - int nuniq; - double max_slope; + mpq_inits(tr, trinv, NULL); + + for(int i = 0; i < group->size; i++) { + if(group->elements[i].length % 2 != 0 || !group->elements[i].inverse) + continue; + + mat_trace(tr, matrices[i]); + mat_trace(trinv, matrices[group->elements[i].inverse->id]); + + retval = solve_characteristic_polynomial(solver, tr, trinv, evs); + if(retval == 1) { + fprintf(stderr, "Error! Could not solve polynomial.\n"); + continue; + } else if(retval == 2) { + continue; + } + + if(fabs(evs[0]) < fabs(evs[1])) + SWAP(double, evs[0], evs[1]); + if(fabs(evs[1]) < fabs(evs[2])) + SWAP(double, evs[1], evs[2]); + if(fabs(evs[0]) < fabs(evs[1])) + SWAP(double, evs[0], evs[1]); + + gmp_printf("%d %d %s %Qd %Qd %f %f\n", i, group->elements[i].length, print_word(&group->elements[i], buf), tr, trinv, log(evs[0]), -log(evs[2])); + } + + mpq_clears(tr, trinv, NULL); +} + +/* +double max_slope(groupelement_t *group, mat *matrices, mpq_t s, mpq_t t, int *index) +{ + double max = 0; + double slope; + + mpq_t tr, trinv; + char buf[100]; + + mpq_inits(tr, trinv, NULL); + + for(int i = 0; i < MAX_ELEMENTS; i++) { + if(group[i].length % 2 != 0 || !group[i].inverse) + continue; + + mat_trace(tr, matrices[i]); + mat_trace(trinv, matrices[group[i].inverse->id]); + + slope = log(mpq_get_d(trinv))/log(mpq_get_d(tr)); + if(slope > max) + { + *index = i; + max = slope; + } + } + + mpq_clears(tr, trinv, NULL); + + return max; +} +*/ + +int main(int argc, char *argv[]) +{ + mpq_t s, q, t, tmp; + double sapprox, tapprox, qapprox, tqfactor; + mat *matrices; + group_t *group; + int index; + mps_context *solver; + int acc = 100; + int n, nuniq, nmax; int retval; double evs[3]; + double max_slope; + char buf[100]; + char buf2[100]; - group_t *group = dat.group; - mat *matrices = dat.matrices; - struct result *invariants = dat.invariants; - struct result **distinct_invariants = dat.distinct_invariants; - mps_context *solver = dat.solver; + struct result *invariants; + struct result **distinct_invariants; -// DEBUG("Compute matrices\n"); - enumerate(group, matrices, m, t); + if(argc < 4) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + + nmax = atoi(argv[1]); + + DEBUG("Allocate\n"); + + mpq_inits(s, q, t, tmp, NULL); + matrices = malloc(nmax*sizeof(mat)); + for(int i = 0; i < nmax; i++) + mat_init(matrices[i], 3); + invariants = malloc(nmax*sizeof(struct result)); + distinct_invariants = malloc(nmax*sizeof(struct result)); + for(int i = 0; i < nmax; i++) { + mpq_init(invariants[i].tr); + mpq_init(invariants[i].trinv); + distinct_invariants[i] = &invariants[i]; + } + + solver = mps_context_new(); + mps_context_set_output_prec(solver, 20); // relative precision + mps_context_set_output_goal(solver, MPS_OUTPUT_GOAL_APPROXIMATE); + + DEBUG("Approximate parameters\n"); + + // get approximate s and q values + sapprox = atof(argv[2]); + tapprox = atof(argv[3]); + tqfactor = pow((sapprox*sapprox-sapprox+1)*(sapprox*sapprox-sapprox+1)*(sapprox*sapprox+1), 1/6.0); + qapprox = tapprox/tqfactor; + + for(int i = 0; ; i++) { + continued_fraction_approximation(tmp, sapprox, i); + if(fabs(mpq_get_d(t)-sapprox) < 1e-10 + || (mpz_cmpabs_ui(mpq_numref(tmp),acc) > 0 && mpz_cmpabs_ui(mpq_denref(tmp),acc) > 0)) + break; + mpq_set(s, tmp); + } + mpq_canonicalize(s); + + for(int i = 0; ; i++) { + continued_fraction_approximation(tmp, qapprox, i); + if(fabs(mpq_get_d(t)-qapprox) < 1e-10 + || (mpz_cmpabs_ui(mpq_numref(tmp),acc) > 0 && mpz_cmpabs_ui(mpq_denref(tmp),acc) > 0)) + break; + mpq_set(q, tmp); + } + mpq_canonicalize(q); + + tqfactor = pow((mpq_get_d(s)*mpq_get_d(s)-mpq_get_d(s)+1)*(mpq_get_d(s)*mpq_get_d(s)-mpq_get_d(s)+1)*(mpq_get_d(s)*mpq_get_d(s)+1), 1/6.0); + +#ifdef OUTPUT_POINTS +// gmp_fprintf(stdout, "\"s = %Qd = %.3f, q = %Qd, t = %.3f\"\n", s, mpq_get_d(s), q, mpq_get_d(q)*tqfactor); +#endif + + // group + DEBUG("Generate group\n"); + group = coxeter_init_triangle(4, 3, 3, nmax); + + DEBUG("Compute matrices\n"); + enumerate(group, matrices, s, q); -// DEBUG("Compute traces\n"); n = 0; + DEBUG("Compute traces\n"); for(int i = 0; i < nmax; i++) { if(group->elements[i].length % 2 != 0 || !group->elements[i].inverse) continue; + invariants[i].id = i; mat_trace(invariants[i].tr, matrices[i]); - mat_trace(invariants[i].trinv, matrices[group->elements[i].inverse->id]); + mat_trace(invariants[i].trinv, matrices[group->elements[i].inverse->id]); - distinct_invariants[n++] = &invariants[i]; + distinct_invariants[n++] = &invariants[i]; } -// DEBUG("Get unique traces\n"); - - qsort(distinct_invariants, n, sizeof(struct result*), compare_result); + DEBUG("Get unique traces\n"); nuniq = 0; for(int i = 0; i < n; i++) { - if(i == 0 || compare_result(&distinct_invariants[i], &distinct_invariants[nuniq-1]) != 0) - distinct_invariants[nuniq++] = distinct_invariants[i]; + if(i == 0 || compare_result(&distinct_invariants[i], &distinct_invariants[nuniq-1]) != 0) { + distinct_invariants[nuniq] = distinct_invariants[i]; + distinct_invariants[nuniq]->count = 1; + nuniq++; + } else { + distinct_invariants[nuniq-1]->count++; + int oldlength = group->elements[distinct_invariants[nuniq-1]->id].length; + int newlength = group->elements[distinct_invariants[i]->id].length; + if(newlength < oldlength) + distinct_invariants[nuniq-1]->id = distinct_invariants[i]->id; + } + + gmp_printf("%d %d %s\n", i, nuniq-1, print_word(&group->elements[i], buf)); } max_slope = 0; int max_slope_index; -// DEBUG("Solve characteristic polynomials\n"); + DEBUG("Solve characteristic polynomials\n"); for(int i = 0; i < nuniq; i++) { retval = solve_characteristic_polynomial(solver, distinct_invariants[i]->tr, distinct_invariants[i]->trinv, evs); if(retval == 1) { @@ -416,227 +525,75 @@ double compute_max_slope(struct global_data dat, mpq_t t, mpq_t m) double x = log(fabs(evs[0])); double y = -log(fabs(evs[2])); + distinct_invariants[i]->x = x; + distinct_invariants[i]->y = y; + if(y/x > max_slope && (x > 0.1 || y > 0.1)) { max_slope_index = distinct_invariants[i] - invariants; max_slope = y/x; } - -// gmp_printf("%Qd %Qd %f %f %f\n", distinct_invariants[i]->tr, distinct_invariants[i]->trinv, x, y, y/x); } - return max_slope; -} + qsort(distinct_invariants, nuniq, sizeof(struct result*), compare_result_by_slope); -void write_results_and_end(struct job *jobs, const char *outfile) -{ - DEBUG("writing output and shutting down\n"); +// printf("- 0 0 - - - - 0.5\n"); + int cumulative = 0; + double slope; + for(int i = 0; i < nuniq; i++) { + slope = distinct_invariants[i]->y/distinct_invariants[i]->x; - FILE *f = fopen(outfile, "w"); - for(int i = 0; i < NJOBS; i++) - fprintf(f, "%d/%d %d/%d %.10f %.10f %.10f %.3f\n", - jobs[i].tparam, TDIV, jobs[i].mparam, MDIV, - (double)jobs[i].tparam/TDIV, (double)jobs[i].mparam/MDIV, jobs[i].max_slope, - jobs[i].time); - fclose(f); - - for(int i = 1; i < world_size; i++) - MPI_Send(NULL, 0, job_datatype, i, JOB_SHUTDOWN, MPI_COMM_WORLD); - -} - -void run_master_process(int nmax, const char *restart, const char *outfile) -{ - int total_jobs = NJOBS; - int completed = 0; - int queue_jobs = MIN(total_jobs, 2*world_size); - struct job current_job; - MPI_Status status; - FILE *f; - int continuing = 1; - int restartf; - struct job *alljobs; - struct job *current; - - restartf = open(restart, O_RDWR); - if(restartf == -1 && errno == ENOENT) { - restartf = open(restart, O_RDWR | O_CREAT, 0666); - continuing = 0; - } - if(restartf == -1) { - DEBUG("error opening restart file: %s\n", strerror(errno)); - exit(1); - } - ftruncate(restartf, total_jobs*sizeof(struct job)); - alljobs = (struct job*) mmap(0, total_jobs*sizeof(struct job), PROT_READ | PROT_WRITE, MAP_SHARED, restartf, 0); - if(alljobs == MAP_FAILED) { - DEBUG("error mapping restart file: %s\n", strerror(errno)); - exit(1); - } - - if(continuing) { - for(int i = 0; i < total_jobs; i++) - if(alljobs[i].done) - completed++; - } else { - for(int tparam = TFROM; tparam <= TTO; tparam++) { - for(int mparam = MFROM; mparam <= MTO; mparam++) { - alljobs[JOBNR(tparam,mparam)].tparam = tparam; - alljobs[JOBNR(tparam,mparam)].mparam = mparam; - alljobs[JOBNR(tparam,mparam)].done = 0; - } + mpq_set_si(tmp, 1, 1); + if(mpq_cmp(distinct_invariants[i]->tr, tmp) == 0 && mpq_cmp(distinct_invariants[i]->trinv, tmp) == 0) { + continue; } - } - - fsync(restartf); - - if(continuing) { - DEBUG("continuing from restart file, %d/%d jobs completed, %d nodes\n", completed, total_jobs, world_size); - } else { - DEBUG("starting from scratch, %d jobs, %d nodes\n", total_jobs, world_size); - } - - if(completed >= total_jobs) - { - write_results_and_end(alljobs, outfile); - goto cleanup; - } - - // assign initial jobs - current = alljobs-1; - for(int i = 0; i < 2*world_size; i++) { - do { - current++; - } while(current < alljobs + total_jobs && current->done); - if(current >= alljobs + total_jobs) // all jobs are assigned - break; - MPI_Send(current, 1, job_datatype, 1 + i%(world_size-1), JOB_ORDER, MPI_COMM_WORLD); - } - - while(1) { - MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); - if(status.MPI_TAG == JOB_RESULT) { - MPI_Recv(¤t_job, 1, job_datatype, MPI_ANY_SOURCE, JOB_RESULT, MPI_COMM_WORLD, &status); - completed++; - - DEBUG("job (%d,%d) completed by instance %d in %f seconds, result = %.3f, %d/%d done\n", - current_job.tparam, current_job.mparam, - status.MPI_SOURCE, current_job.time, current_job.max_slope, completed, total_jobs); - - int nr = JOBNR(current_job.tparam, current_job.mparam); - memcpy(&alljobs[nr], ¤t_job, sizeof(struct job)); - alljobs[nr].done = 1; - - if(completed % FLUSH_INTERVAL == 0) - fsync(restartf); - - // find the next unassigned job - do { - current++; - } while(current < alljobs + total_jobs && current->done); - - if(current < alljobs + total_jobs) { - MPI_Send(current, 1, job_datatype, status.MPI_SOURCE, JOB_ORDER, MPI_COMM_WORLD); - } - - if(completed >= total_jobs) { - write_results_and_end(alljobs, outfile); - goto cleanup; - } + mpq_set_si(tmp, 0, 1); + if(mpq_cmp(distinct_invariants[i]->tr, tmp) == 0 && mpq_cmp(distinct_invariants[i]->trinv, tmp) == 0) { + continue; } + mpq_set_si(tmp, -1, 1); + if(mpq_cmp(distinct_invariants[i]->tr, tmp) == 0 && mpq_cmp(distinct_invariants[i]->trinv, tmp) == 0) { + continue; + } + mpq_set_si(tmp, 3, 1); + if(mpq_cmp(distinct_invariants[i]->tr, tmp) == 0 && mpq_cmp(distinct_invariants[i]->trinv, tmp) == 0) { + continue; + } + + cumulative += distinct_invariants[i]->count; + gmp_printf("%d %d %d %f %f %f %f %f %s\n", + distinct_invariants[i]->id, distinct_invariants[i]->count, cumulative, + distinct_invariants[i]->tr, distinct_invariants[i]->trinv, + log(fabs(mpq_get_d(distinct_invariants[i]->tr))), log(fabs(mpq_get_d(distinct_invariants[i]->trinv))), + distinct_invariants[i]->x, distinct_invariants[i]->y, slope, + print_word(&group->elements[distinct_invariants[i]->id], buf) + ); } +// printf("- 0 %d - - - - 2.0\n", cumulative); -cleanup: +#ifdef OUTPUT_SUMMARY + fprintf(stdout, "%.3f %.3f %f %s\n", mpq_get_d(s), mpq_get_d(q)*tqfactor, max_slope, print_word(&group->elements[max_slope_index], buf)); +#endif - munmap(alljobs, total_jobs*sizeof(struct job)); - close(restartf); -} - -int main(int argc, char *argv[]) -{ - int name_len; - - MPI_Status status; - - mpq_t m, t; - double s; - struct job current_job; - int nmax; - double max_slope; - struct global_data dat; - double jobtime; - - clock_gettime(CLOCK_REALTIME, &starttime); - - if(argc < 4) { - fprintf(stderr, "Usage: mpirun -n --hostfile %s \n", argv[0]); - return 0; - } - nmax = atoi(argv[1]); - - MPI_Init(NULL, NULL); - MPI_Comm_size(MPI_COMM_WORLD, &world_size); - MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); - MPI_Get_processor_name(processor_name, &name_len); - -// DEBUG("instance %d/%d started on %s\n", world_rank, world_size, processor_name); - - int blocklengths[2] = {3, 2}; - MPI_Datatype types[2] = {MPI_INT, MPI_DOUBLE}; - MPI_Aint displacements[2] = {(size_t)&((struct job*)0)->tparam, (size_t)&((struct job*)0)->max_slope}; - MPI_Type_create_struct(2, blocklengths, displacements, types, &job_datatype); - MPI_Type_commit(&job_datatype); - - if(world_rank == 0) { // master processor - run_master_process(nmax, argv[2], argv[3]); - MPI_Finalize(); - return 0; - } - -// DEBUG("Allocate & generate group\n"); - mpq_inits(m, t, NULL); - dat = allocate_global_data(nmax); - dat.group = coxeter_init_triangle(4, 4, 4, nmax); - -// fprintf(stderr, "max word length = %d\n", dat.group->elements[nmax-1].length); - - while(1) { - MPI_Probe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); -// MPI_Recv(¤t_job, 1, job_datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); - if(status.MPI_TAG == JOB_SHUTDOWN) { -// DEBUG("instance %d shutting down\n", world_rank); - break; - } - else if(status.MPI_TAG == JOB_ORDER) { - MPI_Recv(¤t_job, 1, job_datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); - DEBUG("instance %d starting order (%d,%d)\n", world_rank, current_job.tparam, current_job.mparam); - - jobtime = -MPI_Wtime(); - - // do the actual work - mpq_set_ui(t, current_job.tparam, TDIV); - mpq_set_ui(m, current_job.mparam, MDIV); - s = (1-mpq_get_d(m)*mpq_get_d(m))/(2*mpq_get_d(m)); - - max_slope = compute_max_slope(dat, t, m); - - jobtime += MPI_Wtime(); - -// fprintf(stdout, "%.5f %.5f %.5f %f\n", -// mpq_get_d(t), mpq_get_d(m), s, max_slope); - current_job.max_slope = max_slope; - current_job.time = jobtime; - - DEBUG("instance %d finished order (%d,%d) in %f seconds\n", world_rank, current_job.tparam, current_job.mparam, jobtime); - - MPI_Send(¤t_job, 1, job_datatype, 0, JOB_RESULT, MPI_COMM_WORLD); - } - } - -// DEBUG("Clean up\n"); - coxeter_clear(dat.group); - free_global_data(dat); - mpq_clears(m, t, NULL); - - MPI_Type_free(&job_datatype); - MPI_Finalize(); +// output_invariants(group, matrices, s, q, solver); + +// for(int i = 0; i < 10; i++) { +// mpq_set_ui(t,100+i,100); +// mpq_canonicalize(t); + + //printf("%f %f\n", mpq_get_d(t), max_slope(group, matrices, s, t, &index)); +// } + + DEBUG("Clean up\n"); + for(int i = 0; i < nmax; i++) { + mpq_clear(invariants[i].tr); + mpq_clear(invariants[i].trinv); + } + free(invariants); + free(distinct_invariants); + for(int i = 0; i < nmax; i++) + mat_clear(matrices[i]); + free(matrices); + coxeter_clear(group); + mpq_clears(s, q, t, tmp, NULL); + mps_context_free(solver); } diff --git a/singular_values.plt b/singular_values.plt index f85674c..ef88c1e 100644 --- a/singular_values.plt +++ b/singular_values.plt @@ -1,5 +1,5 @@ -if(!exists("logt")) logt = log(1.78) -if(!exists("logs")) logs = log(0.5) +if(!exists("logt")) logt = log(1) +if(!exists("logs")) logs = log(1) #file = sprintf("< ./singular_values 713698 %f %f", exp(logs), exp(logt)) file = sprintf("< ./singular_values 1621 %f %f", exp(logs), exp(logt)) @@ -7,28 +7,13 @@ file = sprintf("< ./singular_values 1621 %f %f", exp(logs), exp(logt)) set zeroaxis set samples 1000 set size square -set xrange [0:30] -set yrange [0:30] -set trange [0:30] +set xrange [0:3] +set yrange [0:3] +set trange [0:5] set grid set parametric -# plot file using 2:3 w p pt 7 ps 0.5 lc 1 t title - -#tr(a,b) = exp((2*a+b)/3) + exp((b-a)/3) + exp(-(a+2*b)/3) -#trinv(a,b) = exp(-(2*a+b)/3) + exp((a-b)/3) + exp((a+2*b)/3) - -tr(a,b) = exp(a) + exp(b-a) + exp(-b) -trinv(a,b) = exp(-a) + exp(a-b) + exp(b) - -#plot file using 6:7 w p pt 7 ps 0.5 lc 1 t columnheader, -# log(tr(t,t*2)),log(trinv(t,2*t)) w l lw 2 t "", \ -# log(tr(t,t/2)),log(trinv(t,t/2)) w l lw 2 t "" - -plot file using 3:4 w p pt 7 ps 0.5 lc 1 t columnheader, \ - t,2*t w l lw 2 t "", \ - t,t/2 w l lw 2 t "" - +plot file using ($8/$9):($6/$7) w p pt 7 ps 0.3 lc 1 t sprintf("t = %.2f", exp(logt)), t, t #plot for[i=-10:10] log(tr(t,t*exp(log(2)*i/10.0))),log(trinv(t,t*exp(log(2)*i/10.0))) w l lw 2 t "" @@ -36,6 +21,9 @@ plot file using 3:4 w p pt 7 ps 0.5 lc 1 t columnheader, \ ##plot for[i=20:20] t,log(tr(1/t,exp(2*log(2)*i/20.0-log(2)))) w l lw 2 t "" +#n=n+1 +#if(n < 1000) reread + pause mouse keypress if(MOUSE_KEY == 60) logt=logt-0.02 if(MOUSE_KEY == 62) logt=logt+0.02 diff --git a/singular_values_mpi.c b/singular_values_mpi.c new file mode 100644 index 0000000..0f9e1e4 --- /dev/null +++ b/singular_values_mpi.c @@ -0,0 +1,642 @@ +#include "coxeter.h" +//#include "linalg.h" +#include "mat.h" + +//#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MIN(x,y) ((x)<(y)?(x):(y)) +#define SWAP(t,x,y) do { t _tmp = (x); (x) = (y); (y) = _tmp; } while (0); +#define DEBUG(msg, ...) do { print_time(); fprintf(stderr, msg, ##__VA_ARGS__); } while (0); +//#define DEBUG(msg, ...) + +#define TDIV 10 +#define TFROM 1 +#define TTO 9 +#define MDIV 10 +#define MFROM 1 +#define MTO 9 +#define JOBNR(t,m) (((t)-TFROM) + ((m)-MFROM)*(TTO-TFROM+1)) +#define NJOBS ((TTO-TFROM+1)*(MTO-MFROM+1)) +#define FLUSH_INTERVAL 100 + +enum message_tag { + JOB_ORDER, + JOB_RESULT, + JOB_SHUTDOWN, +}; + +struct job { + int tparam, mparam; + int done; + double max_slope; + double time; +}; + +struct result { + mpq_t tr; + mpq_t trinv; +}; + +struct global_data { + int n; + group_t *group; + mat* matrices; + struct result *invariants; + struct result **distinct_invariants; + mps_context *solver; +}; + + +struct timespec starttime; +char processor_name[MPI_MAX_PROCESSOR_NAME]; +int world_rank; +int world_size; +MPI_Datatype job_datatype; + +void print_time() +{ + double diff; + struct timespec current; + + clock_gettime(CLOCK_REALTIME, ¤t); + + diff = (current.tv_sec - starttime.tv_sec) + (current.tv_nsec - starttime.tv_nsec)*1e-9; + + fprintf(stderr, "[%04d %.3f] ", world_rank, diff); +} + +static struct global_data allocate_global_data(int n) +{ + struct global_data result; + result.n = n; + result.matrices = malloc(n*sizeof(mat)); + for(int i = 0; i < n; i++) + mat_init(result.matrices[i], 3); + result.invariants = malloc(n*sizeof(struct result)); + result.distinct_invariants = malloc(n*sizeof(struct result*)); + for(int i = 0; i < n; i++) { + mpq_init(result.invariants[i].tr); + mpq_init(result.invariants[i].trinv); + result.distinct_invariants[i] = &result.invariants[i]; + } + result.solver = mps_context_new(); + mps_context_set_output_prec(result.solver, 20); // relative precision + mps_context_set_output_goal(result.solver, MPS_OUTPUT_GOAL_APPROXIMATE); + + return result; +} + +void free_global_data(struct global_data dat) +{ + for(int i = 0; i < dat.n; i++) + mat_clear(dat.matrices[i]); + free(dat.matrices); + for(int i = 0; i < dat.n; i++) { + mpq_clear(dat.invariants[i].tr); + mpq_clear(dat.invariants[i].trinv); + } + free(dat.invariants); + free(dat.distinct_invariants); + mps_context_free(dat.solver); +} + +static int compare_result(const void *a_, const void *b_) +{ + int d = 0; + + struct result **a = (struct result **)a_; + struct result **b = (struct result **)b_; + + d = mpq_cmp((*a)->tr,(*b)->tr); + if(d == 0) + d = mpq_cmp((*a)->trinv, (*b)->trinv); + + return d; +} + +int solve_characteristic_polynomial(mps_context *solv, mpq_t tr, mpq_t trinv, double *eigenvalues) +{ + mpq_t coeff1, coeff2, zero; + cplx_t *roots; + double radii[3]; + double *radii_p[3]; + mps_monomial_poly *poly; + mps_boolean errors; + int result = 0; + + mpq_inits(coeff1, coeff2, zero, NULL); + mpq_set(coeff1, trinv); + mpq_sub(coeff2, zero, tr); + + poly = mps_monomial_poly_new(solv, 3); + mps_monomial_poly_set_coefficient_int(solv, poly, 0, -1, 0); + mps_monomial_poly_set_coefficient_q(solv, poly, 1, coeff1, zero); + mps_monomial_poly_set_coefficient_q(solv, poly, 2, coeff2, zero); + mps_monomial_poly_set_coefficient_int(solv, poly, 3, 1, 0); + + mps_context_set_input_poly(solv, (mps_polynomial*)poly); + mps_mpsolve(solv); + + roots = cplx_valloc(3); + for(int i = 0; i < 3; i++) + radii_p[i] = &(radii[i]); + mps_context_get_roots_d(solv, &roots, radii_p); + errors = mps_context_has_errors(solv); + + if(errors) { + result = 1; + } else { + for(int i = 0; i < 3; i++) { + eigenvalues[i] = cplx_Re(roots[i]); + if(fabs(cplx_Im(roots[i])) > radii[i]) // non-real root + result = 2; + } + } + + cplx_vfree(roots); + mpq_clears(coeff1, coeff2, zero, NULL); + + return result; +} + +void continued_fraction_approximation(mpq_t out, double in, int level) +{ + mpq_t tmp; + + if(in < 0) { + mpq_init(tmp); + mpq_set_ui(tmp, 0, 1); + continued_fraction_approximation(out, -in, level); + mpq_sub(out, tmp, out); + mpq_clear(tmp); + return; + } + + if(level == 0) { + mpq_set_si(out, (signed long int)round(in), 1); // floor(in) + } else { + continued_fraction_approximation(out, 1/(in - floor(in)), level - 1); + mpq_init(tmp); + mpq_set_ui(tmp, 1, 1); + mpq_div(out, tmp, out); // out -> 1/out + mpq_set_si(tmp, (signed long int)in, 1); // floor(in) + mpq_add(out, out, tmp); + mpq_clear(tmp); + } +} + +void quartic(mpq_t out, mpq_t in, int a, int b, int c, int d, int e) +{ + mpq_t tmp; + mpq_init(tmp); + + mpq_set_si(out, a, 1); + mpq_mul(out, out, in); + mpq_set_si(tmp, b, 1); + mpq_add(out, out, tmp); + mpq_mul(out, out, in); + mpq_set_si(tmp, c, 1); + mpq_add(out, out, tmp); + mpq_mul(out, out, in); + mpq_set_si(tmp, d, 1); + mpq_add(out, out, tmp); + mpq_mul(out, out, in); + mpq_set_si(tmp, e, 1); + mpq_add(out, out, tmp); + + mpq_clear(tmp); +} + +// this version is only for the (4,4,4) group +void initialize_triangle_generators(mat_workspace *ws, mat *gen, mpq_t m, mpq_t t) +{ + mpq_t s,sinv,q,x,y; + mpq_t zero, one, two; + mpq_t tmp; + + mpq_inits(s,sinv,q,x,y,zero,one,two,tmp,NULL); + mpq_set_ui(zero, 0, 1); + mpq_set_ui(one, 1, 1); + mpq_set_ui(two, 2, 1); + + // s = (1-m^2)/2m + mpq_mul(s, m, m); + mpq_sub(s, one, s); + mpq_div(s, s, m); + mpq_div(s, s, two); + mpq_div(sinv, one, s); + + // q = (1+m^2)/(1-m^2) = 2/(1-m^2) - 1 + mpq_mul(q, m, m); + mpq_sub(q, one, q); + mpq_div(q, two, q); + mpq_sub(q, q, one); + + // x = -tq, y = -q/t + mpq_mul(x, q, t); + mpq_sub(x, zero, x); + mpq_div(y, q, t); + mpq_sub(y, zero, y); + + // q^2 = xy = 1 + 1/s^2 + // [ -s s*y 0] + // [ -s*x s*x*y - 1/s 0] + // [ -s*y s*y^2 - x 1] + LOOP(i,3) { + mat_zero(gen[i]); + mpq_sub(tmp, zero, s); + mat_set(gen[i%3], i%3, i%3, tmp); + mpq_mul(tmp, s, y); + mat_set(gen[i%3], i%3, (i+1)%3, tmp); + mpq_mul(tmp, s, x); + mpq_sub(tmp, zero, tmp); + mat_set(gen[i%3], (i+1)%3, i%3, tmp); + mpq_mul(tmp, s, x); + mpq_mul(tmp, tmp, y); + mpq_sub(tmp, tmp, sinv); + mat_set(gen[i%3], (i+1)%3, (i+1)%3, tmp); + mpq_mul(tmp, s, y); + mpq_sub(tmp, zero, tmp); + mat_set(gen[i%3], (i+2)%3, i%3, tmp); + mpq_mul(tmp, s, y); + mpq_mul(tmp, tmp, y); + mpq_sub(tmp, tmp, x); + mat_set(gen[i%3], (i+2)%3, (i+1)%3, tmp); + mat_set(gen[i%3], (i+2)%3, (i+2)%3, one); + } + + LOOP(i,3) mat_pseudoinverse(ws, gen[i+3], gen[i]); + + // debug output + /* + gmp_printf("m = %Qd, s = %Qd, t = %Qd, q = %Qd, x = %Qd, y = %Qd\n", m, s, t, q, x, y); + mat_print(gen[0]); + mat_print(gen[1]); + mat_print(gen[2]); + */ + + mpq_inits(s,sinv,q,x,y,zero,one,two,tmp,NULL); +} + +char *print_word(groupelement_t *g, char *str) +{ + int i = g->length - 1; + + str[g->length] = 0; + while(g->parent) { + str[i--] = 'a' + g->letter; + g = g->parent; + } + + return str; +} + +void enumerate(group_t *group, mat *matrices, mpq_t m, mpq_t t) +{ + mat_workspace *ws; + mat tmp; + mat gen[6]; + char buf[100], buf2[100], buf3[100]; + + // allocate stuff + ws = mat_workspace_init(3); + for(int i = 0; i < 6; i++) + mat_init(gen[i], 3); + mat_init(tmp, 3); + + initialize_triangle_generators(ws, gen, m, t); + + mat_identity(matrices[0]); + for(int i = 1; i < group->size; i++) { + if(group->elements[i].length % 2 != 0) + continue; + if(!group->elements[i].inverse) + continue; + + int parent = group->elements[i].parent->id; + int grandparent = group->elements[i].parent->parent->id; + int letter; + + if(group->elements[parent].letter == 1 && group->elements[i].letter == 2) + letter = 0; // p = bc + else if(group->elements[parent].letter == 2 && group->elements[i].letter == 0) + letter = 1; // q = ca + else if(group->elements[parent].letter == 0 && group->elements[i].letter == 1) + letter = 2; // r = ab + if(group->elements[parent].letter == 2 && group->elements[i].letter == 1) + letter = 3; // p^{-1} = cb + else if(group->elements[parent].letter == 0 && group->elements[i].letter == 2) + letter = 4; // q^{-1} = ac + else if(group->elements[parent].letter == 1 && group->elements[i].letter == 0) + letter = 5; // r^{-1} = ba + + mat_multiply(ws, matrices[i], matrices[grandparent], gen[letter]); + } + + // free stuff + for(int i = 0; i < 6; i++) + mat_clear(gen[i]); + mat_clear(tmp); + mat_workspace_clear(ws); +} + + +double compute_max_slope(struct global_data dat, mpq_t t, mpq_t m) +{ +// mpq_set_ui(t, ttick, 100); +// mpq_set_ui(m, mtick, 100); // 414/1000 ~ sqrt(2)-1 <-> s=1 +// s = (1-mpq_get_d(m)*mpq_get_d(m))/(2*mpq_get_d(m)); + + int n = 0; + int nmax = dat.n; + int nuniq; + double max_slope; + int retval; + double evs[3]; + + group_t *group = dat.group; + mat *matrices = dat.matrices; + struct result *invariants = dat.invariants; + struct result **distinct_invariants = dat.distinct_invariants; + mps_context *solver = dat.solver; + +// DEBUG("Compute matrices\n"); + enumerate(group, matrices, m, t); + +// DEBUG("Compute traces\n"); + n = 0; + for(int i = 0; i < nmax; i++) { + if(group->elements[i].length % 2 != 0 || !group->elements[i].inverse) + continue; + + mat_trace(invariants[i].tr, matrices[i]); + mat_trace(invariants[i].trinv, matrices[group->elements[i].inverse->id]); + + distinct_invariants[n++] = &invariants[i]; + } + +// DEBUG("Get unique traces\n"); + + qsort(distinct_invariants, n, sizeof(struct result*), compare_result); + + nuniq = 0; + for(int i = 0; i < n; i++) { + if(i == 0 || compare_result(&distinct_invariants[i], &distinct_invariants[nuniq-1]) != 0) + distinct_invariants[nuniq++] = distinct_invariants[i]; + } + + max_slope = 0; + int max_slope_index; + +// DEBUG("Solve characteristic polynomials\n"); + for(int i = 0; i < nuniq; i++) { + retval = solve_characteristic_polynomial(solver, distinct_invariants[i]->tr, distinct_invariants[i]->trinv, evs); + if(retval == 1) { + fprintf(stderr, "Error! Could not solve polynomial.\n"); + continue; + } else if(retval == 2) { + continue; + } + + if(fabs(evs[0]) < fabs(evs[1])) + SWAP(double, evs[0], evs[1]); + if(fabs(evs[1]) < fabs(evs[2])) + SWAP(double, evs[1], evs[2]); + if(fabs(evs[0]) < fabs(evs[1])) + SWAP(double, evs[0], evs[1]); + + double x = log(fabs(evs[0])); + double y = -log(fabs(evs[2])); + + if(y/x > max_slope && (x > 0.1 || y > 0.1)) { + max_slope_index = distinct_invariants[i] - invariants; + max_slope = y/x; + } + +// gmp_printf("%Qd %Qd %f %f %f\n", distinct_invariants[i]->tr, distinct_invariants[i]->trinv, x, y, y/x); + } + + return max_slope; +} + +void write_results_and_end(struct job *jobs, const char *outfile) +{ + DEBUG("writing output and shutting down\n"); + + FILE *f = fopen(outfile, "w"); + for(int i = 0; i < NJOBS; i++) + fprintf(f, "%d/%d %d/%d %.10f %.10f %.10f %.3f\n", + jobs[i].tparam, TDIV, jobs[i].mparam, MDIV, + (double)jobs[i].tparam/TDIV, (double)jobs[i].mparam/MDIV, jobs[i].max_slope, + jobs[i].time); + fclose(f); + + for(int i = 1; i < world_size; i++) + MPI_Send(NULL, 0, job_datatype, i, JOB_SHUTDOWN, MPI_COMM_WORLD); + +} + +void run_master_process(int nmax, const char *restart, const char *outfile) +{ + int total_jobs = NJOBS; + int completed = 0; + int queue_jobs = MIN(total_jobs, 2*world_size); + struct job current_job; + MPI_Status status; + FILE *f; + int continuing = 1; + int restartf; + struct job *alljobs; + struct job *current; + + restartf = open(restart, O_RDWR); + if(restartf == -1 && errno == ENOENT) { + restartf = open(restart, O_RDWR | O_CREAT, 0666); + continuing = 0; + } + if(restartf == -1) { + DEBUG("error opening restart file: %s\n", strerror(errno)); + exit(1); + } + ftruncate(restartf, total_jobs*sizeof(struct job)); + alljobs = (struct job*) mmap(0, total_jobs*sizeof(struct job), PROT_READ | PROT_WRITE, MAP_SHARED, restartf, 0); + if(alljobs == MAP_FAILED) { + DEBUG("error mapping restart file: %s\n", strerror(errno)); + exit(1); + } + + if(continuing) { + for(int i = 0; i < total_jobs; i++) + if(alljobs[i].done) + completed++; + } else { + for(int tparam = TFROM; tparam <= TTO; tparam++) { + for(int mparam = MFROM; mparam <= MTO; mparam++) { + alljobs[JOBNR(tparam,mparam)].tparam = tparam; + alljobs[JOBNR(tparam,mparam)].mparam = mparam; + alljobs[JOBNR(tparam,mparam)].done = 0; + } + } + } + + fsync(restartf); + + if(continuing) { + DEBUG("continuing from restart file, %d/%d jobs completed, %d nodes\n", completed, total_jobs, world_size); + } else { + DEBUG("starting from scratch, %d jobs, %d nodes\n", total_jobs, world_size); + } + + if(completed >= total_jobs) + { + write_results_and_end(alljobs, outfile); + goto cleanup; + } + + // assign initial jobs + current = alljobs-1; + for(int i = 0; i < 2*world_size; i++) { + do { + current++; + } while(current < alljobs + total_jobs && current->done); + if(current >= alljobs + total_jobs) // all jobs are assigned + break; + MPI_Send(current, 1, job_datatype, 1 + i%(world_size-1), JOB_ORDER, MPI_COMM_WORLD); + } + + while(1) { + MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + if(status.MPI_TAG == JOB_RESULT) { + MPI_Recv(¤t_job, 1, job_datatype, MPI_ANY_SOURCE, JOB_RESULT, MPI_COMM_WORLD, &status); + completed++; + + DEBUG("job (%d,%d) completed by instance %d in %f seconds, result = %.3f, %d/%d done\n", + current_job.tparam, current_job.mparam, + status.MPI_SOURCE, current_job.time, current_job.max_slope, completed, total_jobs); + + int nr = JOBNR(current_job.tparam, current_job.mparam); + memcpy(&alljobs[nr], ¤t_job, sizeof(struct job)); + alljobs[nr].done = 1; + + if(completed % FLUSH_INTERVAL == 0) + fsync(restartf); + + // find the next unassigned job + do { + current++; + } while(current < alljobs + total_jobs && current->done); + + if(current < alljobs + total_jobs) { + MPI_Send(current, 1, job_datatype, status.MPI_SOURCE, JOB_ORDER, MPI_COMM_WORLD); + } + + if(completed >= total_jobs) { + write_results_and_end(alljobs, outfile); + goto cleanup; + } + } + } + +cleanup: + + munmap(alljobs, total_jobs*sizeof(struct job)); + close(restartf); +} + +int main(int argc, char *argv[]) +{ + int name_len; + + MPI_Status status; + + mpq_t m, t; + double s; + struct job current_job; + int nmax; + double max_slope; + struct global_data dat; + double jobtime; + + clock_gettime(CLOCK_REALTIME, &starttime); + + if(argc < 4) { + fprintf(stderr, "Usage: mpirun -n --hostfile %s \n", argv[0]); + return 0; + } + nmax = atoi(argv[1]); + + MPI_Init(NULL, NULL); + MPI_Comm_size(MPI_COMM_WORLD, &world_size); + MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); + MPI_Get_processor_name(processor_name, &name_len); + +// DEBUG("instance %d/%d started on %s\n", world_rank, world_size, processor_name); + + int blocklengths[2] = {3, 2}; + MPI_Datatype types[2] = {MPI_INT, MPI_DOUBLE}; + MPI_Aint displacements[2] = {(size_t)&((struct job*)0)->tparam, (size_t)&((struct job*)0)->max_slope}; + MPI_Type_create_struct(2, blocklengths, displacements, types, &job_datatype); + MPI_Type_commit(&job_datatype); + + if(world_rank == 0) { // master processor + run_master_process(nmax, argv[2], argv[3]); + MPI_Finalize(); + return 0; + } + +// DEBUG("Allocate & generate group\n"); + mpq_inits(m, t, NULL); + dat = allocate_global_data(nmax); + dat.group = coxeter_init_triangle(4, 4, 4, nmax); + +// fprintf(stderr, "max word length = %d\n", dat.group->elements[nmax-1].length); + + while(1) { + MPI_Probe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); +// MPI_Recv(¤t_job, 1, job_datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + if(status.MPI_TAG == JOB_SHUTDOWN) { +// DEBUG("instance %d shutting down\n", world_rank); + break; + } + else if(status.MPI_TAG == JOB_ORDER) { + MPI_Recv(¤t_job, 1, job_datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + DEBUG("instance %d starting order (%d,%d)\n", world_rank, current_job.tparam, current_job.mparam); + + jobtime = -MPI_Wtime(); + + // do the actual work + mpq_set_ui(t, current_job.tparam, TDIV); + mpq_set_ui(m, current_job.mparam, MDIV); + s = (1-mpq_get_d(m)*mpq_get_d(m))/(2*mpq_get_d(m)); + + max_slope = compute_max_slope(dat, t, m); + + jobtime += MPI_Wtime(); + +// fprintf(stdout, "%.5f %.5f %.5f %f\n", +// mpq_get_d(t), mpq_get_d(m), s, max_slope); + current_job.max_slope = max_slope; + current_job.time = jobtime; + + DEBUG("instance %d finished order (%d,%d) in %f seconds\n", world_rank, current_job.tparam, current_job.mparam, jobtime); + + MPI_Send(¤t_job, 1, job_datatype, 0, JOB_RESULT, MPI_COMM_WORLD); + } + } + +// DEBUG("Clean up\n"); + coxeter_clear(dat.group); + free_global_data(dat); + mpq_clears(m, t, NULL); + + MPI_Type_free(&job_datatype); + MPI_Finalize(); +}