Well, after trying a second time, the binary file is fine and loads into python 
without issues. No idea how the first file got corrupted. I've also tried 
without the PetscViewerPushFormat call, and that works equally well. Thanks for 
your help!

Chris

________________________________________

dr. ir. Christiaan Klaij
 | Senior Researcher | Research & Development
T +31 317 49 33 44 |  c.kl...@marin.nl | 
https://urldefense.us/v3/__http://www.marin.nl__;!!G_uCfscf7eWS!a9GfF_LhoavWZziwYJYPGdit19o_V12PJ7Va7HZ4FOqQrLcBCZOHGqnoEpTXlLRFh4MBui66Jv03qp9Zf3lKuoo$
 
From: Barry Smith <bsm...@petsc.dev>
Sent: Thursday, June 6, 2024 7:08 PM
To: Klaij, Christiaan
Cc: Stefano Zampini; PETSc users list
Subject: Re: [petsc-users] matload and petsc4py

You don't often get email from bsm...@petsc.dev. Learn why this is 
important<https://urldefense.us/v3/__https://aka.ms/LearnAboutSenderIdentification__;!!G_uCfscf7eWS!a9GfF_LhoavWZziwYJYPGdit19o_V12PJ7Va7HZ4FOqQrLcBCZOHGqnoEpTXlLRFh4MBui66Jv03qp9ZRPyqNHc$
 >

   Try without this line CALL PetscViewerPushFormat(viewer, 
PETSC_VIEWER_DEFAULT, ier)
it shouldn't matter but worth trying.

On Jun 6, 2024, at 9:50 AM, Klaij, Christiaan <c.kl...@marin.nl> wrote:

This Message Is From an External Sender
This message came from outside your organization.

The matrix was saved as with this code:

CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_WRITE, viewer, 
ier)
CALL PetscViewerPushFormat(viewer, PETSC_VIEWER_DEFAULT, ier)
CALL MatView(A, viewer, ier)
CALL PetscViewerDestroy(viewer, ier)

I will try again to check for file corruption

________________________________________
From: Stefano Zampini 
<stefano.zamp...@gmail.com<mailto:stefano.zamp...@gmail.com>>
Sent: Thursday, June 6, 2024 3:01 PM
To: Klaij, Christiaan
Cc: PETSc users list
Subject: Re: [petsc-users] matload and petsc4py

You don't often get email from 
stefano.zamp...@gmail.com<mailto:stefano.zamp...@gmail.com>. Learn why this is 
important<https://urldefense.us/v3/__https://aka.ms/LearnAboutSenderIdentification__;!!G_uCfscf7eWS!dhd_vSPt_mzy9yVfgQZvw3KcZQwq--Wojmk9JdzuGmnjbC_54Z7IF0WH6qosvtdRyyjv94blDuN1lAlZepr0msg$>


On Thu, Jun 6, 2024, 14:53 Klaij, Christiaan 
<c.kl...@marin.nl<mailto:c.kl...@marin.nl><mailto:c.kl...@marin.nl>> wrote:
I'm writing a matrix to file from my fortran code (that uses petsc-3. 19. 4) 
with -mat_view binary. Then, I'm trying to load this mat into python (that uses 
petsc-3. 21. 0). This works fine using single or multiple procs when the marix
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd
I'm writing a matrix to file from my fortran code (that uses petsc-3.19.4) with 
-mat_view binary. Then, I'm trying to load this mat into python (that uses 
petsc-3.21.0). This works fine using single or multiple procs when the marix 
was written using a single proc (attached file a_mat_np_1.dat). However, when 
the matrix was written using mulitple procs (attached file a_mat_n_2.dat) I get 
the error below. Is this supposed to work? If so, what I'm I doing wrong?


This should work. And your script seems ok too . How did you save the matrix in 
parallel? I suspect that file is corrupt

$ cat test_matrixImport_binary.py
import sys
import petsc4py
from petsc4py import PETSc
from mpi4py import MPI

# mat files
#filename = "./a_mat_np_1.dat" # Works
filename = "./a_mat_np_2.dat" # Doesn't work

# Initialize PETSc
petsc4py.init(sys.argv)

# Create a viewer for reading the binary file
viewer = PETSc.Viewer().createBinary(filename, mode='r', comm=PETSc.COMM_WORLD)

# Create a matrix and load data from the binary file
A = PETSc.Mat().create(comm=PETSc.COMM_WORLD)
A.load(viewer)

$ python test_matrixImport_binary.py
Traceback (most recent call last):
File "/projects/P35662.700/test_cklaij/test_matrixImport_binary.py", line 18, 
in <module>
A.load(viewer)
File "petsc4py/PETSc/Mat.pyx", line 2025, in petsc4py.PETSc.Mat.load
petsc4py.PETSc.Error: error code 79
[0] MatLoad() at /home/cklaij/petsc-3.21.0/src/mat/interface/matrix.c:1344
[0] MatLoad_SeqAIJ() at 
/home/cklaij/petsc-3.21.0/src/mat/impls/aij/seq/aij.c:5091
[0] MatLoad_SeqAIJ_Binary() at 
/home/cklaij/petsc-3.21.0/src/mat/impls/aij/seq/aij.c:5142
[0] Unexpected data in file
[0] Inconsistent matrix data in file: nonzeros = 460, sum-row-lengths = 761

$ mpirun -n 2 python test_matrixImport_binary.py
Traceback (most recent call last):
File "/projects/P35662.700/test_cklaij/test_matrixImport_binary.py", line 18, 
in <module>
Traceback (most recent call last):
File "/projects/P35662.700/test_cklaij/test_matrixImport_binary.py", line 18, 
in <module>
A.load(viewer)
File "petsc4py/PETSc/Mat.pyx", line 2025, in petsc4py.PETSc.Mat.load
A.load(viewer)
File "petsc4py/PETSc/Mat.pyx", line 2025, in petsc4py.PETSc.Mat.load
petsc4py.PETSc.Error: error code 79
[0] MatLoad() at /home/cklaij/petsc-3.21.0/src/mat/interface/matrix.c:1344
[0] MatLoad_MPIAIJ() at 
/home/cklaij/petsc-3.21.0/src/mat/impls/aij/mpi/mpiaij.c:3035
[0] MatLoad_MPIAIJ_Binary() at 
/home/cklaij/petsc-3.21.0/src/mat/impls/aij/mpi/mpiaij.c:3087
[0] Unexpected data in file
[0] Inconsistent matrix data in file: nonzeros = 460, sum-row-lengths = 761
petsc4py.PETSc.Error: error code 79
[1] MatLoad() at /home/cklaij/petsc-3.21.0/src/mat/interface/matrix.c:1344
[1] MatLoad_MPIAIJ() at 
/home/cklaij/petsc-3.21.0/src/mat/impls/aij/mpi/mpiaij.c:3035
[1] MatLoad_MPIAIJ_Binary() at 
/home/cklaij/petsc-3.21.0/src/mat/impls/aij/mpi/mpiaij.c:3087
[1] Unexpected data in file
[1] Inconsistent matrix data in file: nonzeros = 460, sum-row-lengths = 761


[cid:ii_18feda229ee74859a161]
dr. ir.​​​​     Christiaan       Klaij
 |      Senior Researcher        |      Research & Development
T +31 317 49 33 44<tel:+31%20317%2049%2033%2044>         |       
c.kl...@marin.nl<mailto:c.kl...@marin.nl><mailto:c.kl...@marin.nl>      | 
https://urldefense.us/v3/__http://www.marin.nl__;!!G_uCfscf7eWS!dhd_vSPt_mzy9yVfgQZvw3KcZQwq--Wojmk9JdzuGmnjbC_54Z7IF0WH6qosvtdRyyjv94blDuN1lAlZod9brMU$<https://urldefense.us/v3/__https://www.marin.nl/__;!!G_uCfscf7eWS!YrcVeQ6V8OD3jKxSzzxpyuTgFdncWh4YcL1SgDT8NHqystMpzO1pkd17oNGni-ll5I8qH9_ueOtj3WYWBYeP9zg$>
[Facebook]<https://urldefense.us/v3/__https://www.facebook.com/marin.wageningen__;!!G_uCfscf7eWS!YrcVeQ6V8OD3jKxSzzxpyuTgFdncWh4YcL1SgDT8NHqystMpzO1pkd17oNGni-ll5I8qH9_ueOtj3WYWzqK6wKw$>
[LinkedIn]<https://urldefense.us/v3/__https://www.linkedin.com/company/marin__;!!G_uCfscf7eWS!YrcVeQ6V8OD3jKxSzzxpyuTgFdncWh4YcL1SgDT8NHqystMpzO1pkd17oNGni-ll5I8qH9_ueOtj3WYWIUyfXbM$>
[YouTube]<https://urldefense.us/v3/__https://www.youtube.com/marinmultimedia__;!!G_uCfscf7eWS!YrcVeQ6V8OD3jKxSzzxpyuTgFdncWh4YcL1SgDT8NHqystMpzO1pkd17oNGni-ll5I8qH9_ueOtj3WYWjtUkoeg$>


<image337700.png><image563921.png><image268336.png><image799923.png>

Reply via email to