Reputation: 1418
I want to run some function using mpi from main
but I don't know how it should be. It looks like:
#define MAXSIZE 100
int main (int argc, char **argv) {
int i;
float matrixA[MAXSIZE][MAXSIZE], matrixB[MAXSIZE][MAXSIZE], matrixC[MAXSIZE][MAXSIZE];
for(i=0;i<10;i++){
multiply(matrixA, matrixB, matrixC);
}
}
void multiply(float matrixA[MAXSIZE][MAXSIZE], float matrixB[MAXSIZE][MAXSIZE], float matrixC[MAXSIZE][MAXSIZE]) {
int rank; //process rank
int size; //number of processes
MPI_Init(&argc, &argv); //initialize MPI operations
MPI_Comm_rank(MPI_COMM_WORLD, &rank); //get the rank
MPI_Comm_size(MPI_COMM_WORLD, &size); //get number of processes
...someoperation...
MPI_Finalize();
}
I know how to run basic MPI without using other functions but I need this construction.
Upvotes: 2
Views: 1287
Reputation: 178
This might be helpful to you.In your program, there is a for loop
for(i=0;i<10;i++)
{
multiply(matrixA, matrixB, matrixC);
}
I feel that you are trying to execute multiplication 10 times.You can give each multiplication to one process.So you can use the command, mpirun -np 10 executable.
Upvotes: 1
Reputation: 1342
In an application instance, MPI can be initialized at most once. So the code structure you provided will not work.
the correct structure for your program is as follows:
#define MAXSIZE 100
int main (int argc, char **argv) {
int i;
float matrixA[MAXSIZE][MAXSIZE], matrixB[MAXSIZE][MAXSIZE], matrixC[MAXSIZE][MAXSIZE];
int rank; //process rank
int size; //number of processes
MPI_Init(&argc, &argv); //initialize MPI operations
MPI_Comm_rank(MPI_COMM_WORLD, &rank); //get the rank
MPI_Comm_size(MPI_COMM_WORLD, &size); //get number of processes
for(i=0;i<10;i++){
multiply(matrixA, matrixB, matrixC);
}
MPI_Finalize();
}
void multiply(float matrixA[MAXSIZE][MAXSIZE], float matrixB[MAXSIZE][MAXSIZE], float matrixC[MAXSIZE][MAXSIZE]) {
...someoperation...
}
Upvotes: 8