[petsc-users] Putting petsc in a namespace
Satish Balay
balay at mcs.anl.gov
Tue Sep 9 10:38:25 CDT 2014
On Tue, 9 Sep 2014, Florian Lindner wrote:
> Am Montag, 8. September 2014, 10:53:27 schrieb Barry Smith:
> >
> > On Sep 8, 2014, at 2:19 AM, Florian Lindner <mailinglists at xgm.de> wrote:
> >
> > >
> > > I want to offer an interface that uses some Petsc types (like Vec and Mat) but I do not want to import all petsc symbols into the global namespace for anyone using the interface. That does not seam possible though...
> > >
> > > Thanks,
> > >
> > > Florian
> >
> > Which symbols do you want to have available and which ones not? Do you not want to use the other symbols but not let the user use them, for example, you want to use KSP but don’t want the user to have access to them? Or something else. Note that you can configure PETSc into several libraries and then use only the ones you want with -with-single-library=no it creates a separate TS, SNES, KSP, DM, Mat,and Vec library.
> >
> > Let us know what you want in more detail and we may have suggestions on how to achieve it.
>
> Thanks for your help!
>
> If a user of my class library does an #include "mypetsc.h" he should not get all the petsc symbols in his global namespace, e.g.
>
> #include "mypetsc.h"
>
> Vector v; // is an object of my class lib
> Vec pv; // should not work, since I do not want petsc symbols in my global namespace
> petsc::Vec pv; // fine, since petsc symbols are contained in namespace petsc. But this does not seem to be possible.
>
> petsc::Vec = v.vector // vector of type petsc::Vec is a public member of Vector. That's why "mypetsc.h" needs to #include "petscvec.h". Because of that an #include "mypetsc" imports all the symbols of "petscvec" into anyone wanting to use Vector.
>
> Since "mypetsc.h" only contains declarations and no code I tried it with forward declarations of Vec, but no success.
>
Hm - it should work. PETSc uses opaque objects (pointers) in C. You should be able to do the same from C++
http://en.wikipedia.org/wiki/Opaque_pointer
>>>>>>>>
from include/petscmat.h
typedef struct _p_Mat* Mat;
from include/petsc-private/matimpl.h
struct _p_Mat {
PETSCHEADER(struct _MatOps);
<snip>
};
<<<<<<<
You should be able to use similar mode. Attaching a [non-working]
example - that compiles without errors.
Note: if you are exposing more than PETSc objects [like enums, or
other things - you might need to duplicate them in your interface..
[i.e I don't think you can wrap petsc headers with 'namespace' and
have all PETSc public stuff available automatically]
Satish
--------------
$ make
mpicxx -o ex1.o -c -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas -g -O0 -fPIC -I/home/balay/petsc/include -I/home/balay/petsc/arch-maint/include -I/usr/include/mpich-x86_64 `pwd`/ex1.cpp
mpicxx -o mypetsc.o -c -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas -g -O0 -fPIC -I/home/balay/petsc/include -I/home/balay/petsc/arch-maint/include -I/usr/include/mpich-x86_64 `pwd`/mypetsc.cpp
mpicc -fPIC -Wall -Wwrite-strings -Wno-strict-aliasing -Wno-unknown-pragmas -g3 -O0 -o ex1 ex1.o mypetsc.o -Wl,-rpath,/home/balay/petsc/arch-maint/lib -L/home/balay/petsc/arch-maint/lib -lpetsc -llapack -lblas -lX11 -lpthread -lm -Wl,-rpath,/usr/lib64/mpich/lib -L/usr/lib64/mpich/lib -Wl,-rpath,/usr/lib/gcc/x86_64-redhat-linux/4.9.1 -L/usr/lib/gcc/x86_64-redhat-linux/4.9.1 -lmpichf90 -lgfortran -lm -lgfortran -lm -lquadmath -lm -lmpichcxx -lstdc++ -Wl,-rpath,/usr/lib64/mpich/lib -L/usr/lib64/mpich/lib -Wl,-rpath,/usr/lib/gcc/x86_64-redhat-linux/4.9.1 -L/usr/lib/gcc/x86_64-redhat-linux/4.9.1 -ldl -Wl,-rpath,/usr/lib64/mpich/lib -lmpich -lopa -lmpl -lrt -lpthread -lgcc_s -ldl
/usr/bin/rm -f ex1.o mypetsc.o
balay at asterix /home/balay/tmp/test
$ cat mypetsc.h
namespace petsc {
typedef struct _p_Vec* Vec;
typedef struct _p_Mat* Mat;
}
class Vector {
public:
int Scale();
private:
petsc::Vec vector;
};
balay at asterix /home/balay/tmp/test
$ cat mypetsc.cpp
#include "petsc.h"
#include "petsc-private/vecimpl.h"
#include "petsc-private/matimpl.h"
#include "mypetsc.h"
int Vector::Scale()
{
PetscScalar a=1.0;
VecScale(Vector::vector,a);
return 0;
}
balay at asterix /home/balay/tmp/test
$ cat ex1.cpp
#include "mypetsc.h"
int main ()
{
Vector v;
v.Scale();
return 0;
}
balay at asterix /home/balay/tmp/test
$
------------------------------------
> At the end an #include "mypetsc.h" should import only my own symbols like Vector and Matrix. It would be ok, if it also imports Vec and Mat (which are types of public members / return types of public functions). It would also be ok, if it imports all other petsc symbols (like VecGetOwnershipRange) in a seperate namespace.
>
> I hope I was able to convey what I want...
>
> Thx,
> Florian
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.tar.gz
Type: application/gzip
Size: 599 bytes
Desc:
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140909/917c5f14/attachment.bin>
More information about the petsc-users
mailing list