-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (123 loc) · 3.72 KB
/
main.cpp
File metadata and controls
141 lines (123 loc) · 3.72 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Rudimentary applications to launch batch of Ichthyop simulations with MPI.
* The program takes as argument the path of a text file. The text file contains
* the list of Java commands for launching Ichthyop simulations.
*
* Example:
* mpirun -np 4 ichthyopmpi list.txt
* with list.txt:
* java -jar ichthyop.jar config-ichthyop.xml.1 > out.1
* java -jar ichthyop.jar config-ichthyop.xml.2 > out.2
* java -jar ichthyop.jar config-ichthyop.xml.3 > out.3
* java -jar ichthyop.jar config-ichthyop.xml.4 > out.4
* etc.
*
* File: main.cpp
* Author: P. Verley (philippe.verley@ird.fr
*/
#include <cstdlib>
#include <mpi.h>
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
/* Print details in the console */
const bool DEBUG=false;
/* Functions headers */
void readFile(const char *filename, vector<string> &files, int rank);
void checkFileExist(const char *filename, int rank);
/*
*
*/
int main(int argc, char** argv)
{
int rank= -1, size=0;
if (argc!=2)
{
stringstream msg;
msg<<"You must provide the path of the text file containing the list of Ichthyop configuration files."<<endl;
cerr<<msg.str();
return 1;
}
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (DEBUG)
{
stringstream msg;
msg<<"Rank "<<rank<<" | Ichthyop configuration files read from "<<argv[1]<<endl;
cout<<msg.str();
}
vector<string> files=vector<string>();
readFile(argv[1], files, rank);
if (files.size()<size)
{
stringstream msg;
msg<<"Rank "<<rank<<" | Number of configuration files in "<<argv[1]<<" must be >= the number of MPI procs. nFile "<<files.size()<<" nProc "<<size<<endl;
cerr<<msg.str();
return 1;
}
for (int iSimu=0; iSimu<files.size(); iSimu++)
{
if ((iSimu%size)==rank)
{
// stringstream msg;
// msg<<"Rank "<<rank<<" | Running Ichthyop simulation from line "<<iSimu<<" ..."<<endl;
// cout<<msg.str();
int err=system(files.at(iSimu).c_str());
if (err!=0)
{
stringstream msg;
msg<<"Rank "<<rank<<" | Failed to run Ichthyop simulation:"<<endl<<files.at(iSimu).c_str()<<endl;
cerr<<msg.str();
}
else
{
stringstream msg;
msg<<"Rank "<<rank<<" | Completed Ichthyop simulation:"<<endl<<files.at(iSimu).c_str()<<endl;
cout<<msg.str();
}
}
}
MPI_Finalize();
return 0;
}
void readFile(const char *filename, vector<string> &files, int rank)
{
checkFileExist(filename, rank);
stringstream msg;
ifstream file(filename, ios::in);
if (file)
{
string line;
while (getline(file, line))
{
if (line.empty()||line[0]=='#') continue;
files.push_back(line);
if (DEBUG)
{
msg<<"Rank "<<rank<<" | readFile() Adding command: "<<line<<" [OK]"<<endl;
cout<<msg.str();
}
}
file.close();
}
else
{
msg<<"Rank "<<rank<<" | readFile() Failed to read file "<<filename<<endl;
cerr<<msg.str();
}
}
void checkFileExist(const char *filename, int rank)
{
FILE* f;
if (!(f=fopen(filename, "r")))
{
cout<<"Rank "<<rank<<" | checkFileExist() Failed to open file: "<<filename<<'\n';
exit(1);
}
fclose(f);
}