argc and argv with mpi and command line argument

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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>
#include <string>
#include "mpi.h"
using namespace std;

int main( int argc, char* argv[] )
{
   int proc;

   MPI_Init( &argc, &argv );
   MPI_Comm_rank( MPI_COMM_WORLD, &proc );

   stringstream ss;
   ss << "Processor " << proc << " receives argc = " << argc << " with argv = " << argv[0];
   for ( int i = 1; i < argc; i++ ) ss << ", " << argv[i];
   cout << ss.str() << '\n';

   MPI_Finalize();
}



mpiexec -n 4 args.exe This is cool!
Processor 2 receives argc = 4 with argv = args.exe, This, is, cool!
Processor 3 receives argc = 4 with argv = args.exe, This, is, cool!
Processor 0 receives argc = 4 with argv = args.exe, This, is, cool!
Processor 1 receives argc = 4 with argv = args.exe, This, is, cool!

Last edited on
Slightly more useful example. Does numerical integration (mid-ordinate rule) from a to b on N intervals.

mpiexec -n 4 integrate.exe 1 5 256000000
(integrates x^2 from x=1 to x=5 using 256000000 intervals)

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
#include <iostream>
#include <cstdlib>
#include "mpi.h"
using namespace 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();
}


Integral = 41.3333

Last edited on
@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.
Last edited on
Your example is NOT using commandline arguments.

You are re-directing input and output - that is a completely different thing.
Topic archived. No new replies allowed.