Re: [petsc-users] petsc-users Digest, Vol 205, Issue 2
I hope that I am replying in the correct manner to respond to the "Having trouble with basic Fortran-PETSc interoperability" thread I previously started. If not, please correct me. Thank you everyone for your replies, they were very helpful. I have what appears to be a working example code using the suggested updates, specifically the VecGetArray() and VecRestoreArray() and getting the sequential vector back from PETSc using the information from the FAQ. <<Example code attached>> I have a question about this example code to make sure I am writing reasonably efficient code. It seems like I have to create an additional PETSc vector 'out_seq' which will essentially be a copy of the PETSc vector 'v1p' which is not the most efficient use of memory. It also seems to me like there isn't a way around this additional 'out_seq' vector because there needs a place to aggregate the data from the various processes. Is this a reasonable use of PETSc or is there a more efficient way? Note, I am trying to interface my existing code base with PETSc to use the solvers and this may be the performance trade-off for not developing my program fully within the PETSc ecosystem. I have another question only tangentially related to this topic. Should I ask it as part of this thread or create a new topic? Michael ________________________________ From: petsc-users <[email protected]> on behalf of [email protected] <[email protected]> Sent: Saturday, January 3, 2026 1:00 PM To: [email protected] <[email protected]> Subject: petsc-users Digest, Vol 205, Issue 2 External Email - Use Caution Send petsc-users mailing list submissions to [email protected] To subscribe or unsubscribe via the World Wide Web, visit https://urldefense.us/v3/__https://nam02.safelinks.protection.outlook.com/?u... <https://lists.mcs.anl.gov/mailman/listinfo/petsc-users > or, via email, send a message with subject or body 'help' to [email protected] You can reach the person managing the list at [email protected] When replying, please edit your Subject line so it is more specific than "Re: Contents of petsc-users digest..." Today's Topics: 1. Re: Having trouble with basic Fortran-PETSc interoperability (Barry Smith) 2. Re: Having trouble with basic Fortran-PETSc interoperability (Matthew Knepley) ---------------------------------------------------------------------- Message: 1 Date: Fri, 2 Jan 2026 16:33:27 -0500 From: Barry Smith <[email protected]> To: Michael Whitten <[email protected]> Cc: "[email protected]" <[email protected]> Subject: Re: [petsc-users] Having trouble with basic Fortran-PETSc interoperability Message-ID: <[email protected]> Content-Type: text/plain; charset="utf-8" VecGetValues() uses 0 based indexing in both Fortran and C. You don't want to use VecGetValues() and VecSetValues() usually since they result in two copies of the arrays and copying entire arrays back and forth. You can avoid copying between PETSc vectors and your arrays by using VecGetArray(), VecGetArrayWrite(), and VecGetArrayRead(). You can also use VecCreateMPIWithArray() to create a PETSc vector using your array; for example for input to the right hand side of a KSP. These arrays start their indexing with the Fortran default of 1. Barry
On Jan 2, 2026, at 2:42?PM, Michael Whitten via petsc-users <[email protected]> wrote:
Hi PETSc mailing list users,
I have managed to install PETSc and run some PETSc examples and little test codes of my own in Fortran. I am now trying to make PETSc work with my existing Fortran code. I have tried to build little test examples of the functionality that I can then incorporate into my larger code base. However, I am having trouble just passing vectors back and forth between PETSc and Fortran.
I have attached a minimum semi-working example that can be compiled with the standard 'Makefile.user'. It throws an error when I try to copy the PETSc vector back to a Fortran vector using VecGetValues(). I get that it can only access values of the array on the local process but how do I fix this? Is this even the right approach?
In the final implementation I want to be able to assemble my matrix and vector, convert them to PETSc data structures, use PETSc to solve, and then convert the solution vector back to Fortran and return. I want to be able to do this with both the linear and nonlinear solvers. It seems like this is what PETSc is, in part, built to do. Is this a reasonable expectation to achieve? Is this a reasonable use case for PETSc?
Thanks in advance for any help you can offer.
best, Michael <test.F90>
1) For the code v1 = 1.0d0 .... PetscCallA(VecCreate(PETSC_COMM_WORLD,v1p,ierr)) ... PetscCallA(VecGetArray(v1p,v1ptr,ierr)) v1ptr = v1 PetscCallA(VecRestoreArray(v1p,v1ptr,ierr)) This produces two copies of the array plus time to copy values over. Instead drop the v1 completely and just use PetscCallA(VecGetArray(v1p,v1ptr,ierr)) v1ptr = 1.0d0 ! or fill it up with a loop etc PetscCallA(VecRestoreArray(v1p,v1ptr,ierr)) 2) If you want/need PETSc's entire parallel vector on each process then you have no choice but to use the code you wrote with VecScatter. Normally when writing a new MPI code from scratch one designs it so the entire vector is never needed together on a single process (because that is not scalable) but if you have a current code you need to work with this is an ok way to start. Barry
On Jan 16, 2026, at 8:40 AM, Michael Whitten via petsc-users <[email protected]> wrote:
I hope that I am replying in the correct manner to respond to the "Having trouble with basic Fortran-PETSc interoperability" thread I previously started. If not, please correct me.
Thank you everyone for your replies, they were very helpful.
I have what appears to be a working example code using the suggested updates, specifically the VecGetArray() and VecRestoreArray() and getting the sequential vector back from PETSc using the information from the FAQ. <<Example code attached>>
I have a question about this example code to make sure I am writing reasonably efficient code. It seems like I have to create an additional PETSc vector 'out_seq' which will essentially be a copy of the PETSc vector 'v1p' which is not the most efficient use of memory. It also seems to me like there isn't a way around this additional 'out_seq' vector because there needs a place to aggregate the data from the various processes. Is this a reasonable use of PETSc or is there a more efficient way? Note, I am trying to interface my existing code base with PETSc to use the solvers and this may be the performance trade-off for not developing my program fully within the PETSc ecosystem.
I have another question only tangentially related to this topic. Should I ask it as part of this thread or create a new topic?
Michael From: petsc-users <[email protected] <mailto:[email protected]>> on behalf of [email protected] <mailto:[email protected]> <[email protected] <mailto:[email protected]>> Sent: Saturday, January 3, 2026 1:00 PM To: [email protected] <mailto:[email protected]> <[email protected] <mailto:[email protected]>> Subject: petsc-users Digest, Vol 205, Issue 2
External Email - Use Caution
Send petsc-users mailing list submissions to [email protected] <mailto:[email protected]>
To subscribe or unsubscribe via the World Wide Web, visit https://urldefense.us/v3/__https://nam02.safelinks.protection.outlook.com/?u... <https://lists.mcs.anl.gov/mailman/listinfo/petsc-users > or, via email, send a message with subject or body 'help' to [email protected] <mailto:[email protected]>
You can reach the person managing the list at [email protected] <mailto:[email protected]>
When replying, please edit your Subject line so it is more specific than "Re: Contents of petsc-users digest..."
Today's Topics:
1. Re: Having trouble with basic Fortran-PETSc interoperability (Barry Smith) 2. Re: Having trouble with basic Fortran-PETSc interoperability (Matthew Knepley)
----------------------------------------------------------------------
Message: 1 Date: Fri, 2 Jan 2026 16:33:27 -0500 From: Barry Smith <[email protected] <mailto:[email protected]>> To: Michael Whitten <[email protected] <mailto:[email protected]>> Cc: "[email protected] <mailto:[email protected]>" <[email protected] <mailto:[email protected]>> Subject: Re: [petsc-users] Having trouble with basic Fortran-PETSc interoperability Message-ID: <[email protected] <mailto:[email protected]>> Content-Type: text/plain; charset="utf-8"
VecGetValues() uses 0 based indexing in both Fortran and C.
You don't want to use VecGetValues() and VecSetValues() usually since they result in two copies of the arrays and copying entire arrays back and forth.
You can avoid copying between PETSc vectors and your arrays by using VecGetArray(), VecGetArrayWrite(), and VecGetArrayRead(). You can also use VecCreateMPIWithArray() to create a PETSc vector using your array; for example for input to the right hand side of a KSP. These arrays start their indexing with the Fortran default of 1.
Barry
On Jan 2, 2026, at 2:42?PM, Michael Whitten via petsc-users <[email protected] <mailto:[email protected]>> wrote:
Hi PETSc mailing list users,
I have managed to install PETSc and run some PETSc examples and little test codes of my own in Fortran. I am now trying to make PETSc work with my existing Fortran code. I have tried to build little test examples of the functionality that I can then incorporate into my larger code base. However, I am having trouble just passing vectors back and forth between PETSc and Fortran.
I have attached a minimum semi-working example that can be compiled with the standard 'Makefile.user'. It throws an error when I try to copy the PETSc vector back to a Fortran vector using VecGetValues(). I get that it can only access values of the array on the local process but how do I fix this? Is this even the right approach?
In the final implementation I want to be able to assemble my matrix and vector, convert them to PETSc data structures, use PETSc to solve, and then convert the solution vector back to Fortran and return. I want to be able to do this with both the linear and nonlinear solvers. It seems like this is what PETSc is, in part, built to do. Is this a reasonable expectation to achieve? Is this a reasonable use case for PETSc?
Thanks in advance for any help you can offer.
best, Michael <test.F90>
participants (2)
-
Barry Smith -
Michael Whitten