Skip to content

Commit

Permalink
clean input and multi thread; next collect result
Browse files Browse the repository at this point in the history
  • Loading branch information
WeileiZeng committed Mar 31, 2020
1 parent b4f2f8f commit 0b1c521
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ INC_DIR=~/working/weilei_lib
files=$(INC_DIR)/mm_read.c $(INC_DIR)/mm_read.h $(INC_DIR)/mmio.c $(INC_DIR)/mmio.h $(INC_DIR)/mm_write.c $(INC_DIR)/mm_write.h $(INC_DIR)/lib.cpp $(INC_DIR)/lib.h $(INC_DIR)/dist.c $(INC_DIR)/dist.h $(INC_DIR)/concatenation_lib.c $(INC_DIR)/concatenation_lib.h $(INC_DIR)/my_lib.h makefile
#command=$(CXX) -I../../weilei_lib $(START) -o $@ $< $(word 2,$^) $(word 4, $^) $(word 6, $^) $(word 8, $^) $(word 10, $^) $(word 12, $^) $(END)

command=$(CXX) -I $(INC_DIR) $(START) -o $@ $< $(word 2,$^) $(word 4, $^) $(word 6, $^) $(word 8, $^) $(word 10, $^) $(word 12, $^) $(END) ; echo finish making
command=$(CXX) -std=c++11 -pthread -I $(INC_DIR) $(START) -o $@ $< $(word 2,$^) $(word 4, $^) $(word 6, $^) $(word 8, $^) $(word 10, $^) $(word 12, $^) $(END) ; echo finish making



Expand Down
86 changes: 67 additions & 19 deletions my_bp1.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//Weilei March 26, 2020. yse c++ async to manage multi threads
// copied from bp_decoding4.c

#include <future>
#include <string>
#include <iostream>
#include <itpp/itbase.h>
#include <itpp/itcomm.h>
#include <sstream>
Expand All @@ -14,34 +16,81 @@ using namespace itpp;
// Read the code from files and do BP decoding
//input:source file for stabilzier matrix; error propability p ;

int decode( GF2mat G, GF2mat H, double p, string filename_result_p);

int main(int argc, char **argv){
Parser parser;
parser.init(argc,argv);
//p.set_silentmode(true);
string filename_G, filename_H, filename_result;
parser.get(filename_G,"filename_G");
parser.get(filename_H,"filename_H");
parser.get(filename_result,"filename_result");
GF2mat G =MM_to_GF2mat(filename_G);
GF2mat H =MM_to_GF2mat(filename_H);

//parser.get(filename_G,"filename_G");
//parser.get(filename_H,"filename_H");
//parser.get(filename_result,"filename_result");

vector<future<int>> pool;
int pool_size=10;
std::chrono::milliseconds span (100);
//change parameter p, code size
//char * filename_result=argv[3];//prefix for the file
double p;
parser.get(p,"p");
//atof(argv[4]);
p=p/100000.0;//previous use 1000 division. Now use 100,000 division cause the thershold for toric codes seems to be around 0.1%.
//parser.get(p,"p");
int sizes[]= {13,11,9,7,5};
string stabilizer_folder="data/toric/stabilizer";
string error_folder="data/toric/bp_decoding4";
for ( int size : sizes){

filename_G = stabilizer_folder + "/toric_S_x_size_" + to_string(size) + ".mm";
filename_H = stabilizer_folder + "/toric_S_z_size_" + to_string(size) + ".mm";
filename_result = error_folder + "/toric_S_size_" + to_string(size) + ".mm_rate";
for ( int ip=100;ip<5001;ip+=100){
//atof(argv[4]);
p=ip/100000.0;//previous use 1000 division. Now use 100,000 division cause the thershold for toric codes seems to be around 0.1%.
char filename_result_p[255];
sprintf( filename_result_p,"%s%.5f",filename_result.c_str(),p);//append p in the file name
string filename_result_p_string(filename_result_p);
//cout<<filename_result_p_string<<endl;
GF2mat G =MM_to_GF2mat(filename_G);
GF2mat H =MM_to_GF2mat(filename_H);

//manage thread
while ( pool.size() >= pool_size ){
//wait until some of them finish
//break;
for(vector <future<int>> :: iterator it = pool.begin(); it != pool.end(); ++it){
//printf("%d", *it);
if ( it->wait_for(span) == future_status::ready){
cout<<"."<<endl;
pool.erase(it);
cout<<"remove thread. "<<pool.size()<<endl;
//break;
}
}
}

// future<int> fut = async(launch::async, decode,G, H, p, filename_result_p);
//pool.push_back(move(fut));
pool.push_back(async(launch::async, decode,G, H, p, filename_result_p));

cout<<"add new thread. "<<pool.size()<<endl;
//int num = fut.get();
//cout<<"num = "<<num<<endl;

//decode( G, H, p, filename_result_p);
//break;
}
//break;
}
return 0;
}

int decode( GF2mat G, GF2mat H, double p, string filename_result_p){
//parameter setup
int cycles=30;//10000;//number of cycles: fro toric code, 10000 give reletively clear result
int cycles=500;//10000;//number of cycles: fro toric code, 10000 give reletively clear result
int exit_at_iteration=50;//parameter for bp decoding set_exit_condition()
// int bound= (int) -4096 * log (p/(1-p));// -300; see note.pdf on how to choose bound
int bound = 0;
int max_repetition = 20;//10 for best result. supposed to be zero in our set up, just for a check



char filename_result_p[255];
sprintf( filename_result_p,"%s%.5f",filename_result,p);//append p in the file name

Real_Timer timer;

Expand Down Expand Up @@ -180,9 +229,9 @@ int main(int argc, char **argv){

if(ans>=0){
if ( current_iteration < 0 ){
cout<<endl;
cout<<"-------------------------------make it converge after iteration."<<endl;
cout<<endl;
//cout<<endl;
//cout<<"-------------------------------make it converge after iteration."<<endl;
//cout<<endl;
}
/*
cout<<"llr="<<llr_output<<endl;
Expand Down Expand Up @@ -229,7 +278,6 @@ cout<<"rec_bits"<<endl;
cout<<", Converge rate ="<<rate_converge<<endl;
// save_result(p,rate_converge,filename_result_p);//no need to save this. can get it by counting the size of the matrix when doing gnuplot


timer.toc_print();
return 0;
}
Expand Down
Binary file modified my_bp1.out
Binary file not shown.
4 changes: 2 additions & 2 deletions run_my_bp1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ do

echo start run_save_bp.sh for size ${size}
date
for i in {100..5000..100}
#for i in {2000..2000..100}
#for i in {100..5000..100}
for i in {2000..2000..100}
do
#add G matrix on Dec 14
./my_bp1.out filename_G=${stabilizer_folder}/toric_S_x_size_${size}.mm filename_H=${stabilizer_folder}/toric_S_z_size_${size}.mm filename_result=${error_folder}/toric_S_size_${size}.mm_rate p=$i &
Expand Down
1 change: 0 additions & 1 deletion weilei_lib

This file was deleted.

0 comments on commit 0b1c521

Please sign in to comment.