From 816e2eef59a46fed4818836f2491ec92f9b7287f Mon Sep 17 00:00:00 2001 From: Wojciech Politarczyk Date: Sat, 4 Feb 2017 20:09:15 +0100 Subject: [PATCH] Added another variant of periodicity test. Knotkit can read the list of knots to test from a file. --- Makefile | 7 +++-- kk.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++-- periodicity.cpp | 1 + 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index c154cff..406847f 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ KNOTKIT_HEADERS = knotkit.h planar_diagram.h dt_code.h knot_diagram.h \ PERIODICITY_HEADERS = periodicity.h -LIBS = -lgmpxx -lgmp -lz +LIBS = -lgmpxx -lgmp -lz -lpthread all: kk @@ -59,7 +59,10 @@ all: kk $(CXX) -c $(CXXFLAGS) $< -o $@ kk: kk.o $(COMMON_OBJS) - $(CXX) $(LDFLAGS) -pg -o kk $^ $(LIBS) + $(CXX) $(LDFLAGS) -o kk $^ $(LIBS) + +kk_from_file: kk_from_file.o $(COMMON_OBJS) + $(CXX) $(LDFLAGS) -o kk_from_file $^ $(LIBS) -lpthread main: main.o $(COMMON_OBJS) $(CXX) $(LDFLAGS) -o main $^ $(LIBS) diff --git a/kk.cpp b/kk.cpp index fbf8f81..2d02ca4 100644 --- a/kk.cpp +++ b/kk.cpp @@ -1,10 +1,15 @@ #include #include #include +#include +#include +#include #include #include #include +const unsigned max_threads = 4; + const char *program_name; void @@ -88,9 +93,9 @@ const char *invariant = 0; const char *field = "Z2"; std::string periodicity_test = "Przytycki"; int period = 5; - knot_diagram kd; bool reduced = 0; +std::string in_file_name = "/home/wojtek/ownCloud/Lokalny/khovanov-homology-computation/homflypt_test/to_test_kh.txt"; class hg_grading_mapper { @@ -329,6 +334,23 @@ int compute_s_inv(knot_diagram& kd) { return qmin + 1; } +inline std::string parse_data_from_file(const std::string& data) { + auto p_start = data.find(":") + 2; + auto p_stop = data.find("]"); + return data.substr(p_start, p_stop - p_start); +} + +std::string perform_computations(const std::string& knot_name) { + std::ostringstream res; + knot_diagram kd = parse_knot(knot_name.c_str()); + kd.marked_edge = 1; + Kh_periodicity_checker Kh_pc(kd, knot_name); + for(auto p: primes_list) { + res << "Kh criterion: " << Kh_pc(p) << std::endl; + } + return res.str(); +} + void check_periodicity(std::string out_file) { if(periodicity_test == "all") { Kh_periodicity_checker Kh_pc(kd, std::string(knot)); @@ -337,6 +359,41 @@ void check_periodicity(std::string out_file) { << Kh_pc(p) << std::endl; } } + else if(periodicity_test == "Kh_from_file") { + std::ifstream in_file(in_file_name); + if(!in_file) { + std::cerr << "Cannot open file " << in_file_name << "\n"; + exit(EXIT_FAILURE); + } + unsigned num_threads = std::min(max_threads, std::thread::hardware_concurrency()); + std::string line; + std::string previous_knot; + std::vector> v_future(num_threads); + bool stop = false; + while(!stop) { + unsigned i = 0; + while(i < num_threads) { + if(!in_file.eof()) { + std::getline(in_file, line); + std::string knot_name = parse_data_from_file(line); + if(knot_name == previous_knot) + continue; + else { + v_future[i] = std::async(std::launch::async, perform_computations, knot_name); + ++i; + std::cerr << "Checking " << knot_name << "\n"; + previous_knot = knot_name; + } + } + else { + stop = true; + break; + } + } + for(auto& v_f : v_future) + std::cout << v_f.get(); + } + } else if(periodicity_test == "all_seq") { std::string k(knot); // first check whether the number of crossings is bigger or lower than 10 @@ -392,7 +449,7 @@ void check_periodicity(std::string out_file) { } else { if(period == 2 || period == 3) { - std::cout << "Sorry, the criteria don't work for period " + std::cout << "Sorry, the criterion doesn't work for period " << period << "...\n"; exit(EXIT_FAILURE); } @@ -761,6 +818,14 @@ main (int argc, char **argv) } periodicity_test = argv[i]; } + else if(!strcmp(argv[i], "-i")) { + i++; + if(i == argc) { + fprintf (stderr, "error: missing argument to option `-t'\n"); + exit (EXIT_FAILURE); + } + in_file_name = argv[i]; + } else { fprintf (stderr, "error: unknown argument `%s'\n", argv[1]); fprintf (stderr, " use -h for usage\n"); diff --git a/periodicity.cpp b/periodicity.cpp index 40794ae..c1364eb 100644 --- a/periodicity.cpp +++ b/periodicity.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using polynomial_tuple = std::vector, multivariate_laurentpoly, multivariate_laurentpoly>>;