aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-10 08:42:43 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-10 08:42:43 +0100
commit0b207132e1f195cc6d962e37392a8e2cb2b434d6 (patch)
treeb5054e1813e45b263882f60cb6dc207b13dfb51e
parentbb20eddefe24a3528e46ab1c51a7e14e38277b65 (diff)
Make multithreading work
-rw-r--r--.gitignore2
-rw-r--r--Makefile3
-rw-r--r--roots.cpp25
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<Com> &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 <int N,int D>
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 <int D>
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<end;index++){
const Polynomial &poly=gen.get(index);
vector<Com> 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 "<<ntotal<<" polynomials..."<<endl;
- cerr<<"Using "<<ncores<<" threads"<<endl;
+ cerr<<"Using "<<ncores<<" thread"<<(ncores==1?"":"s")<<endl;
thread ths[ncores];
int **tallies=new int*[ncores];
+ cerr<<"Spawning threads: ";
for(int i=0;i<ncores;i++){
tallies[i]=new int[S.width*S.height]();
ths[i]=thread(tallyThreadFunc,gen,ntotal*i/ncores,ntotal*(i+1)/ncores,tallies[i],S);
+ if(i!=0)cerr<<", ";
+ cerr<<i;
}
+ cerr<<endl<<"Joining threads: ";
tally=new int[S.width*S.height];
for(int i=0;i<ncores;i++){
ths[i].join();
+ if(i!=0)cerr<<", ";
+ cerr<<i;
for(int j=0;j<S.width*S.height;j++){
tally[j]+=tallies[i][j];
}
delete[] tallies[i];
}
+ cerr<<endl;
cerr<<"Writing data..."<<endl;
writeData("out.txt",tally,S.width,S.height);