错误:开关量不是整数开关((MPI_Datatype)数据类型)
Posted
技术标签:
【中文标题】错误:开关量不是整数开关((MPI_Datatype)数据类型)【英文标题】:Error: switch quantity not an integer switch ((MPI_Datatype) datatype) 【发布时间】:2019-05-04 13:12:13 【问题描述】:我正在尝试编写 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:错误:输入结束时预期的“”
我该如何处理?
【问题讨论】:
MPI_Datatype
的类型是什么,为什么你认为它会在 switch 语句中起作用?
我认为您更愿意使用type support facilities 中的某些内容并使用if / else if
级联或一些模板元编程。
【参考方案1】:
感谢您的指导,但请相信我,我没有阅读您的解决方案。我尝试了 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()
【讨论】:
我给出了答案并发布了一个全新的问题,但我的积分仍然被扣分,为什么?祖尔菲。 您的回答仍然没有解释MPI_Datatype
究竟是什么,以及为什么它不能与switch()
一起使用。以上是关于错误:开关量不是整数开关((MPI_Datatype)数据类型)的主要内容,如果未能解决你的问题,请参考以下文章