initial commit

This commit is contained in:
Michael Krayer 2021-01-19 13:13:42 +01:00
commit a34c9714ab
3 changed files with 103 additions and 0 deletions

8
README.md Normal file
View File

@ -0,0 +1,8 @@
mkparallel
---
Execute a serial command embarassingly parallel.
Usage:
mpirun -n <nproc> mkparallel <command> <digits>
<command> is the command to be executed where every occurence of {} will be replaced by the rank of the processor.
The rank can be padded with leading zeros by specifying <digits> with a value greater than zero.

2
build Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
mpicxx -o mkparallel src/mkparallel.cc

93
src/mkparallel.cc Normal file
View File

@ -0,0 +1,93 @@
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdexcept>
#include <mpi.h>
using namespace std;
string toStringZeroPadded(int,int);
void replaceAll(string&r,const string&,const string&);
int main(int argc,char *argv[])
{
int ierr;
int nproc;
int rank;
MPI_Status status;
ierr = MPI_Init(&argc,&argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);
ierr = MPI_Comm_size(MPI_COMM_WORLD,&nproc);
// Check positional arguments
if(argc!=3){
cerr << "Usage: mpirun -n " << nproc << " " << argv[0] << " <cmd> <digits>\n"
<< " <cmd> command to be executed embarassingly parallel. {} is replaced by rank\n"
<< " <digits> number of leading zeros for rank replacement\n";
MPI_Finalize();
return 1;
}
// Parse command line arguments
string arg_cmd = argv[1];
string arg_digits = argv[2];
int digits;
try{
digits = stoi(arg_digits);
}
catch(const invalid_argument& ia){
cerr << "Invalid argument to " << ia.what() << ": " << arg_digits << endl;
return 2;
}
// Construct command to be executed
string str_cmd = arg_cmd;
string str_rank;
if(digits>0)
str_rank = toStringZeroPadded(rank,digits);
else
str_rank = to_string(rank);
replaceAll(str_cmd,"{}",str_rank);
cerr << "Rank " << rank << " is executing: " << str_cmd << endl;
// Run command and return exit code
int exitcode = system(str_cmd.c_str());
cerr << "Rank " << rank << " finished with exitcode " << exitcode << endl;
// Finalize
MPI_Finalize();
return 0;
}
string toStringZeroPadded(int val,int pad){
char tmp[pad];
string str;
sprintf(tmp,"%0*d",pad,val);
str = tmp;
return str;
}
void replaceAll(string& str,const string& from,const string& to){
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos=str.find(from,start_pos))!=string::npos){
str.replace(start_pos,from.length(),to);
start_pos+=to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
/*
int checkMpiError(int ierr){
char msg[MPI_MAX_ERROR_STRING];
int msglen, ierr2;
if(ierr!=0){
ierr2 = MPI_Error_string(ierr,msg,&msglen);
cout << msg << endl;
return ierr2;
}
else
return 0;
}
*/