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.
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: