gpt4 book ai didi

c++ - 错误: switch quantity not an integer switch ((MPI_Datatype) datatype){

转载 作者:太空宇宙 更新时间:2023-11-04 06:02:01 24 4
gpt4 key购买 nike

我正在尝试编写 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: In function ‘int mikes_MPI_SIZE(MPI_Datatype)’: reduce.cpp:18:36: error: switch quantity not an integer switch ((MPI_Datatype) datatype){ ^ reduce.cpp: At global scope: reduce.cpp:70:3: error: expected ‘}’ at end of input }

我该如何解决这个问题?

最佳答案

感谢您的指导,但相信我,我没有阅读您的解决方案。我尝试了 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(){
}

关于c++ - 错误: switch quantity not an integer switch ((MPI_Datatype) datatype){,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com