maneka m
maneka m

Reputation: 73

Automatically detect if need to add a "const" qualifier in function parameters

I have written a PMPI profiling library that intercepts many MPI functions. On my local machine I have an OpenMPI installation and some function parameters have a const qualifier, for example:

int PMPI_Gather(const void *sendbuf, int sendcount, ...)  

So naturally, my PMPI library also has those const qualifiers in corresponding hook functions. However, the remote machine where I'm often running stuff has an MPI installation in which function parameters in mpi.h don't have const qualifiers, so when I compile my library I get a whole bunch of warnings that the function declaration is incompatible. Of course, I can just ignore the warnings, suppress them or manually delete the const qualifier.

I wonder if there is a more graceful way to handle the situation, if it's possible to detect somehow if function declarations in mpi.h have or haven't const parameters and automatically add or remove the const qualifier in the profiling library code during compilation, or perhaps it could be some kind of a configuration feature.

Upvotes: 2

Views: 71

Answers (3)

Lundin
Lundin

Reputation: 213730

An alternative to #ifdef ... is to simply check which type the function got:

typedef int PMPI_Gather_noconst (void *sendbuf, int sendcount, ...);
typedef int PMPI_Gather_const (const void *sendbuf, int sendcount, ...);

if( _Generic(PMPI_Gather, PMPI_Gather_noconst*:true, PMPI_Gather_const*:false) )
{
  PMPI_Gather_noconst* stuff;
  ...
}
else
{
  PMPI_Gather_const* stuff;
  ...
}

Upvotes: 1

Zulan
Zulan

Reputation: 22670

const-correctness for the C bindings, i.e. const pointers for IN parameters, was added in MPI 3.0. You can handle this the following:

#if MPI_VERSION >= 3
    #define MPI_CONST const
#else
    #define MPI_CONST
#endif

int PMPI_Gather(MPI_CONST void *sendbuf, int sendcount, ...)

Note: you can see the changes easily in section A.2 C Bindings of the "diff to 3.0" version of the standard.

Upvotes: 2

Andersen
Andersen

Reputation: 93

Usually in this situations where a variable or defines can be defined in multiple places is used #ifdef or #ifndef. You would have something like:

#ifndef _YOU_CONSTANT #define _YOU_CONSTANT #endif

Upvotes: 0

Related Questions