Syntax:
Ein Prototyp für die Funktion hat folgendes Aussehen:
typedef void MPI_User_function(void *invec, void *inoutvec, int *len,
MPI_Datatype datatype)
invec und inoutvec werden elementweise durch die Operation, welche die
Funktion berechnet, verknüpft, und inoutvec wird mit den Ergebnissen
überschrieben. Also: inoutvec[i] = invec[i] "operation" inoutvec[i] (für 0 < i < len-1)
Aufruf:
#include "mpi.h"
...
typedef struct {
int real;
int imag;
} Complex;
/* Funktion zur Addition komplexer Zahlen */
void function(Complex *invec, Complex *inoutvec, int *len,
MPI_Datatype *datatype) {
int i;
Complex c;
for (i=0; i<*len; i++, invec++, inoutvec++) {
c.real = invec->real + inoutvec->real;
c.imag = invec->imag + inoutvec->imag;
*inoutvec = c;
}
}
...
int main(...){
MPI_Datatype complext;
MPI_Op op;
...
MPI_Type_contiguous (2, MPI_INT, &complext);
MPI_Type_commit (&complext);
MPI_Op_create((MPI_User_function *)&function, TRUE, &op);
MPI_Reduce (..., complext, op, ...);
...
MPI_Op_free (&op);
...
}
Beispiel