我正在尝试编码MPI_Reduce(....)。我找到了以下代码,但在switch语句中却给了我错误:
#include <iostream>
#include <fstream>
#include <cmath>
#include <mpi.h>
#include <ctime>
#include <vector>
#define MYBUFFERLENGTH 1024
char myinbuffer[MYBUFFERLENGTH];
char myoutbuffer[MYBUFFERLENGTH];
extern "C" {
int mikes_MPI_SIZE (MPI_Datatype datatype) {
/* sizeof doesn't work for MPI_Datatype, thus this function */
/* I probably should do this with a table, but then error
checking is harder */
/* see man MPI_COMM_WORLD */
switch ((MPI_Datatype) datatype){
/* case MPI_CHAR:
case MPI_BYTE:
case MPI_UNSIGNED_CHAR:
return sizeof(char);
case MPI_SHORT:
case MPI_UNSIGNED_SHORT:
return sizeof(short);
case MPI_INT:
case MPI_UNSIGNED:
return sizeof(int);
case MPI_LONG:
case MPI_UNSIGNED_LONG:
return sizeof(long);
case MPI_FLOAT:
return sizeof(float);
case MPI_DOUBLE:
return sizeof(double);
case MPI_FLOAT_INT:
return sizeof(float)+sizeof(int);
case MPI_LONG_INT:
return sizeof(long)+sizeof(int);
case MPI_DOUBLE_INT:
return sizeof(double)+sizeof(int);
case MPI_SHORT_INT:
return sizeof(short)+sizeof(int);
case MPI_2INT:
return 2*sizeof(int);
default:
die("need to insert size for new datatype in mikes_MPI_SIZE()");*/
}
return -1;
}
int MMPI_Reduce(void * sendbuf, void * recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
{
int bit,processor1,i;
int *iaccum1;
int *iaccum2;
double *daccum1;
double *daccum2;
int * locaccum1;
int * locaccum2;
iaccum1=(int *) myinbuffer;
iaccum2=(int *)(myinbuffer+mikes_MPI_SIZE(datatype));
daccum1=(double *) myinbuffer;
daccum2=(double *)(myinbuffer+mikes_MPI_SIZE(datatype));
locaccum1=(int*)(myinbuffer+mikes_MPI_SIZE(datatype)-sizeof(int));
locaccum2=(int*)(myinbuffer+2*mikes_MPI_SIZE(datatype)- sizeof(int));
*locaccum1=*locaccum2=processor1;
return MPI_SUCCESS;
}
用mpic ++编译时出现以下错误:
$ mpic ++ reduce.cpp reduce.cpp:在功能“ int mikes_MPI_SIZE(MPI_Datatype)”中: reduce.cpp:18:36:错误:开关数量不是整数 开关((MPI_Datatype)数据类型){ ^ reduce.cpp:在全球范围内: reduce.cpp:70:3:错误:在输入末尾预期为“}” }
我该如何处理?
答案 0 :(得分:-2)
感谢您的指导,但请相信我,我没有阅读您的解决方案。我尝试了if-else并成功了。
#include <iostream>
#include <fstream>
#include <cmath>
#include <mpi.h>
#include <ctime>
#include <vector>
#define MYBUFFERLENGTH 1024
char myinbuffer[MYBUFFERLENGTH];
char myoutbuffer[MYBUFFERLENGTH];
extern "C" {
int mikes_MPI_SIZE (MPI_Datatype datatype) {
if(datatype ==MPI_CHAR){
return sizeof(char);
}
else if(datatype == MPI_DOUBLE){
return sizeof(double);
}
return -1;
}
}
int MMPI_Reduce(void * sendbuf, void * recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
int bit,processor1,i;
int *iaccum1;
int *iaccum2;
double *daccum1;
double *daccum2;
int * locaccum1;
int * locaccum2;
iaccum1=(int *) myinbuffer;
iaccum2=(int *)(myinbuffer+mikes_MPI_SIZE(datatype));
daccum1=(double *) myinbuffer;
daccum2=(double *)(myinbuffer+mikes_MPI_SIZE(datatype));
locaccum1=(int*)(myinbuffer+mikes_MPI_SIZE(datatype)-sizeof(int));
locaccum2=(int*)(myinbuffer+2*mikes_MPI_SIZE(datatype)-sizeof(int));
*locaccum1=*locaccum2=processor1;
return MPI_SUCCESS;
}
int main(){
}