[petsc-dev] marking objects as const in PETSc methods
Barry Smith
bsmith at mcs.anl.gov
Thu Dec 2 14:41:39 CST 2010
I was thinking of something more outside of the usual C syntax. Something that the compiler ignores or does not see. For example
Input Parameters:
+ const mat - the factored matrix
- const b - the right-hand-side vector
. x - the result vector
Which brings up a different point, what does Input Parameters and Output Parameters: mean, at times we are inconsistent for MatMult() we have
Input Parameters:
+ mat - the factored matrix
- b - the right-hand-side vector
Output Parameter:
. x - the result vector
this kind of means that mat and b are const and x is not, but usually the Output Paramater is reserved for when an object is created and passed out (which I like better).
Barry
On Dec 2, 2010, at 2:23 PM, Jed Brown wrote:
> On Wed, Dec 1, 2010 at 20:27, Barry Smith <bsmith at mcs.anl.gov> wrote:
> A more difficult example is that the Jacobian matrix argument to SNES is const (in some sense) but it is not to TS (since TS "adjusts" it before passing to SNES).
>
> This does not happen with TSComputeIJacobian, the user produces exactly what SNES needs.
>
> Note that "const" here may not have the strict C meaning of not changing anything in the object. For example VecNorm() is const on the vector elements but caches the computed norm so does change the data inside the object.
>
> This is easy to handle by casting internally.
>
>
> The trouble is, of course, that once you typedef something,
>
> typedef struct _p_Vec *Vec;
>
> then "const Vec" just means that the pointer is const, not the struct behind it, thus the const keyword in
>
> PetscErrorCode VecNorm(const Vec X,NormType,PetscReal*);
>
> has exactly zero meaning as far as the interface is concerned. Using
>
> #define Vec struct _p_Vec*
>
> instead of the typedef would cause the const to apply to the struct instead of the pointer, but this is really ugly for lots of reasons including (note that VecType already has this problem)
>
> Vec X,Y; /* Y is a struct, yuck */
>
> So the only way, within the C type system, to do what you want would be to
>
> typedef const struct _p_Vec *const_Vec;
>
> and then
>
> PetscErrorCode VecNorm(const_Vec X,NormType,PetscReal*);
>
> This works as you would expect, but it's an unconventional C style so I would be apprehensive about recommending it.
>
> Jed
More information about the petsc-dev
mailing list