Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to compile the following program but its giving me lots of errors:

C++
#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 (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=processor;
   return MPI_SUCCESS;
  }



I am getting several errors but I want to remove the following one:

$ mpic++ mpiBlkProb3.cpp
mpiBlkProb3.cpp: In function ‘int mikes_MPI_SIZE(MPI_Datatype)’:
mpiBlkProb3.cpp:43:21: error: switch quantity not an integer
     switch (datatype){



Some body please guide me.

Zulfi.

What I have tried:

I can't understand this error.
Posted
Updated 4-May-19 3:39am
v2

Nor do I - the version of mpi.h that I found (http://www.bu.edu/tech/files/text/mpi.h.txt[^]) uses typedef to define MPI_Datatype as an alias for int - so the switch should be seamless.

Check the mpi.h content, and try creating a "simpler" version of your use code that generates the same error - it may be easier to work out from that and scale up to your code.
 
Share this answer
 
According to MPI_Datatype enumeration - Message Passing Interface | Microsoft Docs[^], MPI_Datatype is an enum, so you probably need to cast it to an int for the switch to work.
 
Share this answer
 
Comments
zak100 4-May-19 8:48am    
I have changed to following:#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;
  }

zak100 4-May-19 8:49am    
I am getting following errors:
$ 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
}
^
zak100 4-May-19 9:18am    
I have casted it to int but still getting errors:
#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 ((int) 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;
  }


Compilation
$ mpic++ reduce.cpp
reduce.cpp: In function ‘int mikes_MPI_SIZE(MPI_Datatype)’:
reduce.cpp:18:19: error: cast from ‘MPI_Datatype {aka ompi_datatype_t*}’ to ‘int’ loses precision [-fpermissive]
     switch ((int) datatype){
                   ^~~~~~~~
reduce.cpp: At global scope:
reduce.cpp:70:3: error: expected ‘}’ at end of input
   }
   ^


Somebody please guide me.

Zulfi.
Richard MacCutchan 4-May-19 9:34am    
I just tried compiling a small extract of that code with the Microsoft C++ compiler and it works fine. I do not know mpic++.
zak100 4-May-19 9:22am    
But I can't cast because in my program I am using "double" values. I dont know how to use the switch then. I am trying if-else.

Zulfi.
Hi,
I solved the error using if-else.

Zulfi.

#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(){
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900