[petsc-dev] Preprocessor hell: #define VecType

Karl Rupp rupp at mcs.anl.gov
Fri Sep 28 11:25:51 CDT 2012


Hi guys,

a long time ago, I stumbled upon one thing that caused a lot of 
headaches when interfacing my code with PETSc. Stripping away all the 
details, my solver interface looked about like this:

template <typename MatrixType, typename VecType>
VecType solve(MatrixType const & A, VecType const & b);

Everything went well if PETSc was not in use. However, I got really 
weird compiler errors as soon as I've included petscksp.h.

Now, fast-forward to today. While browsing through the existing 
CUDA-enabled implementations, I found the following in 
src/ksp/pc/impls/sacusp.cu:
   #undef VecType
   #include <cusp/precond/smoothed_aggregation.h>
   #define VecType char*
While this preprocessor define is inside an implementation file, not in 
a header file, one may consider that acceptable.

However, a quick grep on the main include/ folder reveals
  petscvec.h:#define VecType char*
which is ultimately the reason why my template code went nuts. While I 
do understand that name clashes can appear, I tried to find the deeper 
meaning of VecType. The manual lists only one reference on page 170:
  VecLoad(PetscViewer viewer,VecType outtype,Vec *newvec);
and the associated hyperlink
 
http://www.mcs.anl.gov/petsc/petsc-dev/docs/manualpages/Vec/VecLoad.html#VecLoad
points to an HTML page defining the prototype
  #include "petscvec.h"
  PetscErrorCode  VecLoad(Vec newvec, PetscViewer viewer)

This suggests that VecType is not in use here, and it may not be in 
serious use at all. I can find about five uses of VecType in the whole 
include/-folder, so it shouldn't be too hard to get rid of that (e.g. by 
renaming to PetscVecType or any other more descriptive name), should it? 
Do I miss something?

Best regards,
Karli


PS: For reference, the following code compiles and prints "Hello World!":

#include <iostream>
#include "petscksp.h"

int main(int argc, char **args)
{
   VecType test = "Hello world!";

   std::cout << test << std::endl;
   return EXIT_SUCCESS;
}

To eliminate any warnings about deprecated conversion of strings, just 
add 'const' in front of 'VecType'.



More information about the petsc-dev mailing list