diff options
author | John Langford <jl@humpty.(none)> | 2010-10-18 22:00:01 +0400 |
---|---|---|
committer | John Langford <jl@humpty.(none)> | 2010-10-18 22:00:01 +0400 |
commit | 5627c8156e0a77e5d6abbeb26c9baa64675b415b (patch) | |
tree | fb6414814d79be41a13fb249fc3e4aa577c16e92 /parse_regressor.cc | |
parent | 91647c103f169e314b5124e88161fd1a0a9c469d (diff) |
atomification of regressor writing
Diffstat (limited to 'parse_regressor.cc')
-rw-r--r-- | parse_regressor.cc | 77 |
1 files changed, 45 insertions, 32 deletions
diff --git a/parse_regressor.cc b/parse_regressor.cc index 231f733b..aec334a6 100644 --- a/parse_regressor.cc +++ b/parse_regressor.cc @@ -14,6 +14,7 @@ using namespace std; #include "parse_regressor.h" #include "loss_functions.h" #include "global_data.h" +#include "io.h" void initialize_regressor(regressor &r) { @@ -196,45 +197,57 @@ void free_regressor(regressor &r) } } -void dump_regressor(ofstream &o, regressor &r) +void dump_regressor(string reg_name, regressor &r) { - if (o.is_open()) + if (reg_name == string("")) + return; + string start_name = reg_name+string(".writing"); + io_buf io_temp; + + int f = io_temp.open_file(start_name.c_str(),io_buf::WRITE); + + if (f<0) { - size_t v_length = version.length()+1; - o.write((char*)&v_length, sizeof(v_length)); - o.write(version.c_str(),v_length); - - o.write((char*)&global.min_label, sizeof(global.min_label)); - o.write((char*)&global.max_label, sizeof(global.max_label)); - - o.write((char *)&global.num_bits, sizeof(global.num_bits)); - o.write((char *)&global.thread_bits, sizeof(global.thread_bits)); - o.write((char *)&global.adaptive, sizeof(global.adaptive)); - int len = global.pairs.size(); - o.write((char *)&len, sizeof(len)); - for (vector<string>::iterator i = global.pairs.begin(); i != global.pairs.end();i++) - o << (*i)[0] << (*i)[1]; - o.write((char*)&global.ngram, sizeof(global.ngram)); - o.write((char*)&global.skips, sizeof(global.skips)); - - uint32_t length = 1 << global.num_bits; - size_t num_threads = global.num_threads(); - for(uint32_t i = 0; i < length; i++) - { - weight v = r.weight_vectors[i%num_threads][i/num_threads]; - if (v != 0.) - { - o.write((char *)&i, sizeof (i)); - o.write((char *)&v, sizeof (v)); - } + cout << "can't open: " << start_name << " for writing, exiting" << endl; + exit(1); + } + size_t v_length = version.length()+1; + io_temp.write_file(f,(char*)&v_length, sizeof(v_length)); + io_temp.write_file(f,version.c_str(),v_length); + + io_temp.write_file(f,(char*)&global.min_label, sizeof(global.min_label)); + io_temp.write_file(f,(char*)&global.max_label, sizeof(global.max_label)); + + io_temp.write_file(f,(char *)&global.num_bits, sizeof(global.num_bits)); + io_temp.write_file(f,(char *)&global.thread_bits, sizeof(global.thread_bits)); + io_temp.write_file(f,(char *)&global.adaptive, sizeof(global.adaptive)); + int len = global.pairs.size(); + io_temp.write_file(f,(char *)&len, sizeof(len)); + for (vector<string>::iterator i = global.pairs.begin(); i != global.pairs.end();i++) + io_temp.write_file(f,i->c_str(),2); + + io_temp.write_file(f,(char*)&global.ngram, sizeof(global.ngram)); + io_temp.write_file(f,(char*)&global.skips, sizeof(global.skips)); + + uint32_t length = 1 << global.num_bits; + size_t num_threads = global.num_threads(); + for(uint32_t i = 0; i < length; i++) + { + weight v = r.weight_vectors[i%num_threads][i/num_threads]; + if (v != 0.) + { + io_temp.write_file(f,(char *)&i, sizeof (i)); + io_temp.write_file(f,(char *)&v, sizeof (v)); } } - o.close(); + rename(start_name.c_str(),reg_name.c_str()); + + io_temp.close_file(); } -void finalize_regressor(ofstream &o, regressor &r) +void finalize_regressor(string reg_name, regressor &r) { - dump_regressor(o,r); + dump_regressor(reg_name,r); free_regressor(r); } |