#include<stdio.h>
#include<mpi.h>
typedef struct
{
double real,imag;
} complex;
void cprod(complex *in,complex *inout,int* len,MPI_Datatype *dptr)
{
int i;
complex c;
for(i=0;i<*len;++i)
{
c.real=(*in).real*(*inout).real-(*in).imag*(*inout).imag;
c.imag=(*in).real*(*inout).imag+(*in).imag*(*inout).real;
*inout=c;
in++;
inout++;
}
}
void main(int argc,char*argv[])
{
int rank;
int root;
complex source,result;
MPI_Op myop;
MPI_Datatype ctype;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Type_contiguous(2,MPI_DOUBLE,&ctype);
MPI_Type_commit(&ctype);
MPI_Op_create(cprod,true,&myop);
root=2;
source.real=rank+1;
source.imag=rank+2;
MPI_Reduce(&source,&result,1,ctype,myop,root,MPI_COMM_WORLD);
if(rank==root) fprintf(stderr,"P:%d result is %lf + %lfi\n",rank,result.real,result.imag);
MPI_Finalize();
}
——————————————————————————————————————————————————————————–
以上是照着书编的。但是编译提示出错。检查了好久,都不知道为什么出错,以下是错误信息:
error C2664: ‘MPI_Op_create’ : cannot convert parameter 1 from ‘void (__cdecl *)(complex *,complex *,int *,MPI_Datatype *)’ to ‘MPI_User_function (__cdecl *)’
None of the functions with this name in scope match the target type
————————————————————————————————————————————————————————————
刚学,盼解答~

有么有知道的啊。。。。
知道了一种解决方法。。改成:MPI_Op_creat((MPI_User_function*)cprod,true,&myop);就正确了。。但是清华的《高性能计算之并行编程技术》上及网上找的外文参考资料里都没加MPI_User_function*. 是不是我哪里没做对?