I want to parallelize an numerical integration function. I want to use this function in the middle of calculation. The work before should be done in root process. Is this possible to do in MPI?
double integral_count_MPI(double (*function)(double) , double beginX, double endX, int count)
{
double step, result;
int i;
if (endX - beginX <= 0) return 0;
step = (endX - beginX) / count;
result = 0;
double *input = (double*)malloc((count+1) *sizeof(double));
for (i = 0; i <= count; i ++)
{
input[i] = beginX + i*step;
}
// Calculate and gather
}
EDIT
algorithm:
1 process calculation;
while:
1 process calculation;
integration very complex function with many processes;
1 process calculation;
end while;
1 process calculation;
MPI provides various means to build libraries that use it “behind the scenes”. For starters, you can initialise MPI on demand. MPI-2 modified the requirements for calling
MPI_Initso every compliant implementation should be able to correctly initialise withNULLarguments toMPI_Init(because the actual program arguments might not be available to the library). Since MPI should only be initialised once, the library must check if it was already initialised by callingMPI_Initialized. The code basically looks like this:The initialisation code also registers an exit handler by calling
atexit()from the C standard library. Within this exit handler it finalises the MPI library if it not already finalised. Failure to do so might result inmpiexecterminating the whole MPI job with a message that at least one process has exited without finalising MPI:This arrangement allows you to write your
integral_count_MPIfunction simply like:integral_count_MPIwill demand-initialise the MPI library on the first call. Later calls will not result in reinitialisation because of the waylibrary_initis written. Also no explicit finalisation is necessary – the exit handler will take care.Note that you will still need to launch the code via the MPI process launcher (
mpirun,mpiexec, etc.) and will have to be careful with doing I/O, since the serial part of the code would execute in each instance. Many MPI-enabled libraries provide their own I/O routines for that purpose that filter on the process rank and allow only rank 0 to perform the actual I/O. You can also use the dynamic process management facilities of MPI to spawn additional processes on demand, but that would require that you abstract a huge portion of the process management into the library that performs the integration, which would make it quite complex (and the code of your main program would look awkward).