Skip to content

Commit

Permalink
Merge branch 'io-files'
Browse files Browse the repository at this point in the history
  • Loading branch information
mkskeller committed Oct 20, 2020
2 parents ff2666b + 09021d6 commit 23001e3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
29 changes: 29 additions & 0 deletions Processor/OnlineOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ OnlineOptions::OnlineOptions() : playerno(-1)
memtype = "empty";
direct = false;
bucket_size = 3;
cmd_private_input_file = "Player-Data/Input";
cmd_private_output_file = "";
}

OnlineOptions::OnlineOptions(ez::ezOptionParser& opt, int argc,
Expand All @@ -40,6 +42,29 @@ OnlineOptions::OnlineOptions(ez::ezOptionParser& opt, int argc,
"-I", // Flag token.
"--interactive" // Flag token.
);
opt.add(
cmd_private_input_file.c_str(), // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Prefix for input file path (default: Player-Data/Private-Input). "
"Input will be read from {prefix}-P{id}-{thread_id}.", // Help description.
"-IF", // Flag token.
"--input-file" // Flag token.
);
opt.add(
cmd_private_output_file.c_str(), // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Prefix for output file path "
"(default: output to stdout for party 0 (silent otherwise "
"unless interactive mode is active). "
"Output will be written to {prefix}-P{id}-{thread_id}.", // Help description.
"-OF", // Flag token.
"--output-file" // Flag token.
);

string default_lgp = to_string(lgp);
if (variable_prime_length)
{
Expand Down Expand Up @@ -149,6 +174,10 @@ OnlineOptions::OnlineOptions(ez::ezOptionParser& opt, int argc,
live_prep = opt.get("-L")->isSet;
opt.get("-b")->getInt(batch_size);
opt.get("--memory")->getString(memtype);

opt.get("-IF")->getString(cmd_private_input_file);
opt.get("-OF")->getString(cmd_private_output_file);

direct = opt.isSet("--direct");

opt.get("--bucket-size")->getInt(bucket_size);
Expand Down
2 changes: 2 additions & 0 deletions Processor/OnlineOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class OnlineOptions
std::string memtype;
bool direct;
int bucket_size;
std::string cmd_private_input_file;
std::string cmd_private_output_file;

OnlineOptions();
OnlineOptions(ez::ezOptionParser& opt, int argc, const char** argv,
Expand Down
1 change: 1 addition & 0 deletions Processor/Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ArithmeticProcessor : public ProcessorBase
ifstream public_input;
ofstream public_output;
ofstream private_output;
ofstream stdout_redirect_file;

int sent, rounds;

Expand Down
10 changes: 9 additions & 1 deletion Processor/Processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ Processor<sint, sgf2n>::Processor(int thread_num,Player& P,
public_output.open(get_filename(PREP_DIR "Public-Output-",true).c_str(), ios_base::out);
private_output.open(get_filename(PREP_DIR "Private-Output-",true).c_str(), ios_base::out);

open_input_file(P.my_num(), thread_num);
open_input_file(P.my_num(), thread_num, machine.opts.cmd_private_input_file);

secure_prng.ReSeed();
shared_prng.SeedGlobally(P);

out.activate(P.my_num() == 0 or machine.opts.interactive);

if (!machine.opts.cmd_private_output_file.empty())
{
const string stdout_filename = get_parameterized_filename(P.my_num(), thread_num, opts.cmd_private_output_file);
stdout_redirect_file.open(stdout_filename.c_str(), ios_base::out);
out.redirect_to_file(stdout_redirect_file);
}

}


Expand Down
4 changes: 3 additions & 1 deletion Processor/ProcessorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ProcessorBase
// Optional argument to tape
int arg;

string get_parameterized_filename(int my_num, int thread_num, string prefix);

public:
ExecutionStats stats;

Expand All @@ -42,7 +44,7 @@ class ProcessorBase
}

void open_input_file(const string& name);
void open_input_file(int my_num, int thread_num);
void open_input_file(int my_num, int thread_num, string prefix="");

template<class T>
T get_input(bool interactive, const int* params);
Expand Down
14 changes: 11 additions & 3 deletions Processor/ProcessorBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ void ProcessorBase::open_input_file(const string& name)
}

inline
void ProcessorBase::open_input_file(int my_num, int thread_num)
string ProcessorBase::get_parameterized_filename(int my_num, int thread_num, string prefix)
{
string input_file = "Player-Data/Input-P" + to_string(my_num) + "-" + to_string(thread_num);
open_input_file(input_file);
string filename = prefix + "-P" + to_string(my_num) + "-" + to_string(thread_num);
return filename;
}
inline
void ProcessorBase::open_input_file(int my_num, int thread_num, string prefix)
{
if (prefix.empty())
prefix = "Player-Data/Input";

open_input_file(get_parameterized_filename(my_num, thread_num, prefix));
}

template<class T>
Expand Down
21 changes: 15 additions & 6 deletions Tools/SwitchableOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#define TOOLS_SWITCHABLEOUTPUT_H_

#include <iostream>
#include <fstream>
using namespace std;

class SwitchableOutput
{
bool on;
ostream* out;

public:
SwitchableOutput(bool on = true)
Expand All @@ -21,23 +22,31 @@ class SwitchableOutput

void activate(bool on)
{
this->on = on;
if (on)
out = &cout;
else
out = 0;
}

void redirect_to_file(ofstream& out_file)
{
out = &out_file;
}

template<class T>
SwitchableOutput& operator<<(const T& value)
{
if (on)
cout << value;
if (out)
*out << value;
return *this;

cout << flush;
}

SwitchableOutput& operator<<(ostream& (*__pf)(ostream&))
{
if (on)
cout << __pf;
if (out)
*out << __pf;
return *this;
}
};
Expand Down

0 comments on commit 23001e3

Please sign in to comment.