[petsc-users] Set the options NOT from argc and argv

Satish Balay balay at mcs.anl.gov
Tue May 21 16:08:53 CDT 2013


You should be able to use 'petscrc' file as mentioned before. [without
any code changes]

Alternative is to call fortran initialize/finalize from your c code -
but this is a bit convoluted.. [but appears to work for me]

Satish
----------
balay at asterix /home/balay/download-pine
$ cat main.F
      program main
      call foo()
      end
balay at asterix /home/balay/download-pine
$ cat sub.c
#include <petsc.h>
#include <petsc-private/fortranimpl.h>

#if defined(PETSC_HAVE_FORTRAN_CAPS)
#define petscinitialize_ PETSCINITIALIZE
#define petscfinalize_   PETSCFINALIZE
#define foo_             FOO
#elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
#define petscinitialize_ petscinitialize
#define petscfinalize_   petscfinalize
#define foo_             foo
#endif
PETSC_EXTERN void PETSC_STDCALL petscinitialize_(CHAR filename PETSC_MIXED_LEN(len),PetscErrorCode *ierr PETSC_END_LEN(len));
PETSC_EXTERN void PETSC_STDCALL petscfinalize_(PetscErrorCode *ierr);

void foo_(void)
{
  PetscErrorCode ierr;
#if defined(PETSC_HAVE_FORTRAN_MIXED_STR_ARG)
  petscinitialize_(PETSC_NULL_CHARACTER_Fortran,0,&ierr);
#else
  petscinitialize_(PETSC_NULL_CHARACTER_Fortran,&ierr,0);
#endif
  petscfinalize_(&ierr);
  return;
}
balay at asterix /home/balay/download-pine
$ 



On Tue, 21 May 2013, Longxiang Chen wrote:

> Here is an example:
> 
> I only have a function written in C(and it is called in fortran, I cannot
> change fortran code):
> PetscInitialize() is called in C function.
> 
> void P_solve(A, b, x, size)
> {
>    // definition and declaration
> 
>   PetscInitialize();
> 
>   // petsc KSP functions
> 
> }
> 
> I use PetscOptionsSetValue() to set the options. But need to compile it
> every time I change options, I would fix it in the end.
> Could I get the command line options in this situation? Like petscrc in
> fortran for this C function?
> 
> Thanks,
> 
> Best regards,
> Longxiang Chen
> 
> Do something every day that gets you closer to being done.
> --------------------------------------------------------------
> 465 Winston Chung Hall
> Computer Science Engineering
> University of California, Riverside
> 
> 
> 
> On Tue, May 21, 2013 at 7:48 AM, Satish Balay <balay at mcs.anl.gov> wrote:
> 
> > If PetscInitialize() is called from fortran - command line should work.
> >
> > An easy alternative is to have a file 'petscrc' in the executable run
> > dir - with the command line options listed [one option per line]
> >
> > Satish
> >
> > On Tue, 21 May 2013, Matthew Knepley wrote:
> >
> > > On Mon, May 20, 2013 at 11:01 PM, Longxiang Chen <suifengls at gmail.com
> > >wrote:
> > >
> > > > The fortran program is too long (several files with more than 10,000
> > > > lines).
> > > > And I just want to insert the P_solve inside one of the subroutine to
> > > > solve Ax=b.
> > > > Or it can only use argc and argv?
> > > >
> > >
> > > Barry is correct that you can use PetscOptionsSetValue(), however I think
> > > you are
> > > still missing the point.
> > >
> > > Command line options are processed in PetscInitialize(). You do not have
> > to
> > > do anything
> > > else. You do not have to change your huge Fortran code. You just use the
> > > command line.
> > > It is much better than hard coding a solver type since you can use ALL
> > > solver types. There
> > > is no reason not to use it.
> > >
> > >   Thanks,
> > >
> > >      Matt
> > >
> > >
> > > > Thanks.
> > > > Longxiang
> > > >
> > > > Best regards,
> > > > Longxiang Chen
> > > >
> > > > Do something every day that gets you closer to being done.
> > > > --------------------------------------------------------------
> > > > 465 Winston Chung Hall
> > > > Computer Science Engineering
> > > > University of California, Riverside
> > > >
> > > >
> > > >
> > > > On Mon, May 20, 2013 at 8:52 PM, Barry Smith <bsmith at mcs.anl.gov>
> > wrote:
> > > >
> > > >>
> > > >> On May 20, 2013, at 10:38 PM, Longxiang Chen <suifengls at gmail.com>
> > wrote:
> > > >>
> > > >> > To whom it may concern,
> > > >> >
> > > >> > I am using KSP to solve Ax=b.
> > > >> > The main() is in Fortran, and it calls a function I write in C.The
> > > >> parameter is array A, x, b.
> > > >> >
> > > >> > void P_solve(double x[ ], double b[ ], double A[ ], int size);
> > > >> >
> > > >> > In the function, I should call PetscInitialize() before I create the
> > > >> matrix and vectors for A, x, b, and also call MatSetFromOptions() and
> > > >> VecSetFromOptions().
> > > >> >
> > > >> > But I don't have the argc and argv from main function.
> > > >>
> > > >>     If Fortran is the main program the options database still has
> > access
> > > >> to the command line arguments. You should still be able to use
> > command line
> > > >> arguments and not need to set them in the program.
> > > >>
> > > >>      Does this not work? Can you send a sample program where it does
> > not
> > > >> work?
> > > >>
> > > >>    Barry
> > > >>
> > > >> >
> > > >> > I just want to fix the KSP type to bcgs and the PC type.
> > > >> > Is there another way that I can set the options not through argc and
> > > >> argv, just set them in the program.
> > > >> >
> > > >> > e.g. options[] = {"-ksp_type", "bcgs"}.
> > > >> >
> > > >> > Thanks,
> > > >> > Longxiang
> > > >>
> > > >>
> > > >
> > >
> > >
> > >
> >
> >
> 



More information about the petsc-users mailing list