I'm working on a code which takes 3 arguments as command line. I applied the MPI on the same code and tried to run it using mpiexec -np 2 codename.exe, but it seems the code is not able to see other arguments. I add few lines to code to return error if other arguments are being ignored and they were. any help would be appreciated.
#include <iostream>
#include <cstdlib>
#include "mpi.h"usingnamespace std;
double f( double x ) { return x * x; }
int main( int argc, char* argv[] )
{
int rank, size;
double a = atof( argv[1] ), b = atof( argv[2] ); // limits of integration
int N = atoi( argv[3] ); // number of intervals
double dx = ( b - a ) / N;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
// The processor with rank r will look at the interval [a + r * (b-a)/size] [a + (r+1) * (b-a)/size]
double locala = a + rank * ( b - a ) / size;
int localN = N / size;
double sum = 0, integral;
for ( int i = 0; i < localN; i++ ) sum += f( locala + ( i + 0.5 ) * dx ); // mid-ordinate rule
sum *= dx;
MPI_Reduce( &sum, &integral, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );if ( !rank ) cout << "Integral = " << integral << '\n';
MPI_Finalize();
}
@lastchance
Thanks for you answer. I understand what you did but my question is how I can use mpiexec -np 2 codename.exe when I have some command arguments in Debugging in visual studio configuration. For example there is a mesh.mesh and mesh.res which come as command arguments. I tried to do something like mpiexec -np 2 codename.exe <mesh.mesh mesh.res> but it didn't work.