-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
111 lines (89 loc) · 2.62 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Includes
// ==========
// STL includes
#include <iostream>
#include <mpi.h>
#include <string>
#include <fstream>
// Boost includes
#include <boost/algorithm/string/join.hpp>
// Program includes
#include "lib/Process/const.h"
#include "lib/Process/ProcessMaster.h"
#include "lib/Process/ProcessSlave.h"
/**
* Get command-line argument to program run.
*
* @param std::string arg Argument name (should be preceeded by a dash in the command line)
* @param int argc Number of program call arguments
* @param char* argv[] Program call arguments
*/
std::string getCommandArg( std::string arg, int argc, char* argv[] ) {
for ( int i = 1; i < argc; i += 1 ) {
if ( argv[ i ] == "-" + arg ) {
return argv[ i + 1 ];
}
}
return "";
}
/**
* Main program.
*
* @param int argc Number of program call arguments
* @param char* argv[] Program call arguments
*/
int main( int argc, char* argv[] ) {
// Root process ID and process ID of current MPI process
const int root_process_id = 0;
int process_id;
// Number of processes in use
int num_processes;
// Initialize MPI and fetch process ID
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &process_id );
MPI_Comm_size( MPI_COMM_WORLD, &num_processes );
// Create process
Process* process;
if ( process_id == root_process_id ) {
process = new ProcessMaster( process_id, num_processes );
}
else {
process = new ProcessSlave( process_id, num_processes );
}
// Handle command-line settings
// =====
// Verbosity
bool verbose = false;
if ( process->isMaster() || process_id == 1 ) {
// Whether to output debugging info
verbose = ( getCommandArg( "v", argc, argv ) != "0" );
process->setVerbose( verbose );
}
// Settings regarding dynamic load balancing
bool help_enabled = ( getCommandArg( "h", argc, argv ) != "0" );
process->setEnableDynamicLoadBalancing( help_enabled );
if ( help_enabled ) {
// Help request degree threshold for requesting help from other processes
int help_request_threshold = atoi( getCommandArg( "t", argc, argv ).c_str() );
process->setHelpRequestThreshold( help_request_threshold );
if ( process_id == 1 && verbose ) {
printf( "Degree threshold: %d\n", help_request_threshold );
}
}
// Output file
if ( process->isMaster() ) {
std::string output_fname = getCommandArg( "o", argc, argv );
if ( output_fname == "1" ) {
output_fname = "log/log.txt";
}
process->setResultsOutputFileName( output_fname );
}
// Input file path
process->handleInputFilePath( getCommandArg( "d", argc, argv ), getCommandArg( "f", argc, argv ) );
// Run each process
// =====
process->run();
// Quit MPI
MPI_Finalize();
return 0;
}