From 0b207132e1f195cc6d962e37392a8e2cb2b434d6 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 10 Mar 2017 08:42:43 +0100 Subject: Make multithreading work --- .gitignore | 2 ++ Makefile | 3 ++- roots.cpp | 25 ++++++++++++++++--------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 04ab9e4..f470cfc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ roots *.o *.swp +out.txt +out.png diff --git a/Makefile b/Makefile index ccea193..69b46fb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ CXX = g++ CXXFLAGS = -Wall -Wextra -std=c++11 -O2 -fwrapv +LDFLAGS = -pthread .PHONY: all clean remake @@ -13,7 +14,7 @@ remake: clean roots: $(patsubst %.cpp,%.o,$(wildcard *.cpp)) - $(CXX) -o $@ $^ + $(CXX) -o $@ $^ $(LDFLAGS) %.o: %.cpp $(wildcard *.h) $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/roots.cpp b/roots.cpp index d0cffe7..25102ba 100644 --- a/roots.cpp +++ b/roots.cpp @@ -235,14 +235,14 @@ void check(const vector &v){ namespace Gens { class Generator{ public: - virtual Polynomial get(i64 n); - virtual i64 total(); + virtual Polynomial get(i64 n) const = 0; + virtual i64 total() const = 0; }; template class Christensen : public Generator{ public: - Polynomial get(i64 n){ + Polynomial get(i64 n) const { int d=1; while(d<=D){ i64 p=ipow(2*N+1,d); @@ -260,7 +260,7 @@ namespace Gens { return poly; } - i64 total(){ + i64 total() const { i64 n=0; for(int d=1;d<=D;d++){ n+=ipow(2*N+1,d); @@ -272,7 +272,7 @@ namespace Gens { template class Derbyshire : public Generator{ public: - Polynomial get(i64 n){ + Polynomial get(i64 n) const { n+=2; const int d=ilog(n); if(d>D)return {}; @@ -284,7 +284,7 @@ namespace Gens { return poly; } - i64 total(){ + i64 total() const { return (1<<(D+1))-2; } }; @@ -358,7 +358,7 @@ struct Settings{ }; -void tallyThreadFunc(Gens::Generator &gen,i64 start,i64 end,int *tally,const Settings &S){ +void tallyThreadFunc(const Gens::Generator &gen,i64 start,i64 end,int *tally,const Settings &S){ for(i64 index=start;index r=FR::allroots(poly); @@ -385,25 +385,32 @@ int main(int argc,char **argv){ if(argc==1){ // Gens::Christensen<4,5> gen; - Gens::Derbyshire<15> gen; + Gens::Derbyshire<20> gen; i64 ntotal=gen.total(); int ncores=thread::hardware_concurrency(); cerr<<"Factoring "<