Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion to PETSc with complex #381

Merged
merged 71 commits into from
Mar 14, 2024
Merged
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
fc3fda0
test_stencil_vector_2d_array_to_petsc for float dtype
e-moral-sanchez Feb 23, 2024
48b2060
test for stencil matrix to petsc. Add method topetsc for StencilVectors?
e-moral-sanchez Feb 23, 2024
2aa542d
Test for array and matrix to PETSc
e-moral-sanchez Feb 26, 2024
bfba76e
unified test_block_2d_serial_array_to_psydac
e-moral-sanchez Feb 26, 2024
ba1e2bb
tests for PETSc with complex
e-moral-sanchez Feb 26, 2024
3a5210b
docstrings
e-moral-sanchez Feb 27, 2024
27466c1
Put PETSc import within functions to avoid error if not installed
e-moral-sanchez Feb 27, 2024
531f7e8
PETSc installation instructions README
e-moral-sanchez Feb 27, 2024
47ca1f8
added install PETSc in continuous integration
e-moral-sanchez Feb 27, 2024
ee7f7d0
Updated Readme
e-moral-sanchez Feb 27, 2024
d004e8c
continuous integration
e-moral-sanchez Feb 27, 2024
df559ad
continuous integration
e-moral-sanchez Feb 27, 2024
34bcb74
continuous integration
e-moral-sanchez Feb 27, 2024
8b90fe6
continuous integration
e-moral-sanchez Feb 27, 2024
af376fd
continuous integration
e-moral-sanchez Feb 27, 2024
7d159a9
Added test for mass matrix
e-moral-sanchez Feb 28, 2024
839ae95
improved tests
e-moral-sanchez Feb 28, 2024
c6f569d
Split PETSc installation into steps
yguclu Feb 28, 2024
bb753d5
Add missing lines to continuous-integration.yml
yguclu Feb 28, 2024
125a936
Put back two steps
yguclu Feb 28, 2024
7eb491b
Do not build PETSc Fortran bindings
yguclu Feb 28, 2024
2384cd7
Set working directory when installing petsc4py
yguclu Feb 28, 2024
34fb608
Let pip install the dependencies of petsc4py
yguclu Feb 28, 2024
c3b4611
Use latest PETSc release
yguclu Feb 28, 2024
2c34233
Try fixing unzip command
yguclu Feb 28, 2024
307a381
Clean up
yguclu Feb 28, 2024
5d95147
Add missing line
yguclu Feb 28, 2024
fe864f0
Try installing Cython before petsc4py
yguclu Feb 28, 2024
3757a55
Define the variable PETSC_DIR
yguclu Feb 28, 2024
0d589a6
Install wheel, Cython, and numpy
yguclu Feb 28, 2024
48bf683
Try a faster installation of petsc4py
yguclu Feb 28, 2024
87b492e
Put back numpy
yguclu Feb 28, 2024
ce9ca08
Mark tests with pets to run only with installation. Added improved me…
e-moral-sanchez Feb 29, 2024
8c2ea26
Add PETSc tests
e-moral-sanchez Feb 29, 2024
4e15f1d
Installation of petsc4py through installation of PETSc. Added 64-bit …
e-moral-sanchez Mar 1, 2024
9c5541e
added numpy to PETSc configure
e-moral-sanchez Mar 1, 2024
8cf01f7
Merge branch 'devel' into PETSc_tests
yguclu Mar 1, 2024
d4c8bdc
Try optimistic installation of PETSc
yguclu Mar 1, 2024
6891c49
Install wheel
yguclu Mar 1, 2024
9ab0664
Install NumPy, increase verbosity
yguclu Mar 1, 2024
265ace0
Print installed packages
yguclu Mar 1, 2024
dee599c
Try build instead of wheel
yguclu Mar 1, 2024
571eaa2
Do not build wheel
yguclu Mar 1, 2024
924d0ae
install PETSc with make and petsc4py with pip
e-moral-sanchez Mar 4, 2024
d80dc6a
Run only PETSc tests
e-moral-sanchez Mar 4, 2024
c5eb1fd
Merge branch 'devel' into PETSc_tests
e-moral-sanchez Mar 4, 2024
83ea1dc
fix typo
e-moral-sanchez Mar 4, 2024
43a1c1e
add working directory
e-moral-sanchez Mar 4, 2024
c5deb71
add setup file name
e-moral-sanchez Mar 4, 2024
fa94c5c
add PETSC_DIR, erase backslash
e-moral-sanchez Mar 4, 2024
20cd48c
back to commit 0d589a6
e-moral-sanchez Mar 4, 2024
851aa66
Fix api test
e-moral-sanchez Mar 11, 2024
47deeb1
added ADDITION mode when setting values in PETSc Mat object. By defau…
e-moral-sanchez Mar 11, 2024
8843672
fix parallel stencil vector test to petsc
e-moral-sanchez Mar 12, 2024
58f2728
Added comments to topetsc functions
e-moral-sanchez Mar 12, 2024
32fd3de
Added comments on function petsc_to_psydac
e-moral-sanchez Mar 12, 2024
ff721c0
Fixed parallel tests stencilmatrix
e-moral-sanchez Mar 12, 2024
5412f6b
Fix Block Linear operator parallel petsc tests
e-moral-sanchez Mar 12, 2024
61bde3b
Merge branch 'devel' into PETSc_tests
e-moral-sanchez Mar 12, 2024
769339b
do not add PETSc repository in TOML file
e-moral-sanchez Mar 12, 2024
f3fe190
fix readme, run all tests
e-moral-sanchez Mar 12, 2024
9724c68
add specific PETSC_ARCH
e-moral-sanchez Mar 13, 2024
fcaa2d1
fix README
e-moral-sanchez Mar 13, 2024
4f68aa6
Merge branch 'devel' into PETSc_tests
e-moral-sanchez Mar 13, 2024
e96ec42
Merge branch 'devel' into PETSc_tests
e-moral-sanchez Mar 14, 2024
f661335
Try simpler download of PETSc
yguclu Mar 14, 2024
2053216
Simplify PETSc installation
yguclu Mar 14, 2024
243304e
Store environment variables in $GITHUB_ENV
yguclu Mar 14, 2024
e74dc2e
Update README.md
yguclu Mar 14, 2024
36a634c
Clean up workflow
yguclu Mar 14, 2024
f62ad79
Improve explanation on how to run tests
yguclu Mar 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added comments to topetsc functions
e-moral-sanchez committed Mar 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 58f2728138ecb40902e6c189049e12699a94cbe9
91 changes: 16 additions & 75 deletions psydac/linalg/topetsc.py
Original file line number Diff line number Diff line change
@@ -82,10 +82,14 @@ def vec_topetsc( vec ):
globalsize = vec.space.dimension
indices, data = flatten_vec(vec)
gvec = PETSc.Vec().create(comm=comm)
# Set global size
gvec.setSizes(globalsize)
gvec.setFromOptions()
# Set values of the vector. They are stored in a cache, so the assembly is necessary to use the vector.
gvec.setValues(indices, data)
gvec.assemble()

# Assemble vector
gvec.assemble() # Here PETSc exchanges global communication. The block corresponding to a certain process is not necessarily the same block in the Psydac StencilVector.
return gvec

def mat_topetsc( mat ):
@@ -130,87 +134,24 @@ def mat_topetsc( mat ):

if comm:
# Preallocate number of nonzeros
NNZ = comm.allreduce(data.size, op=MPI.SUM)
row_lengths = np.count_nonzero(rows[None,:] == np.unique(rows)[:,None], axis=1).max()
# NNZ is the number of non-zeros per row for the local portion of the matrix
NNZ = comm.allreduce(row_lengths, op=MPI.MAX)
gmat.setPreallocationNNZ(NNZ)

# Fill-in matrix values
for i in range(rows.size):
# The values have to be set in "addition mode", otherwise the default just takes the new value.
# This is here necessary, since the COO format can contain repeated entries and they must be added.
# This is here necessary, since the COO format can contain repeated entries.
gmat.setValues(rows[i], cols[i], data[i], addv=PETSc.InsertMode.ADD_VALUES)

# Process inserted matrix entries
################################################
# Note 12.03.2024:
# In the assembly PETSc uses global communication to distribute the matrix in a different way than Psydac.
# For this reason, at the moment we cannot compare directly each distributed 'chunck' of the Psydac and the PETSc matrices.
# In the future we would like that PETSc uses the partition from Psydac,
# which might involve passing a DM Object.
################################################
gmat.assemble()
return gmat

'''def mat_topetsc( mat ):
""" Convert operator from Psydac format to a PETSc.Mat object.

Parameters
----------
mat : psydac.linalg.stencil.StencilMatrix | psydac.linalg.basic.LinearOperator | psydac.linalg.block.BlockLinearOperator
Psydac operator

Returns
-------
gmat : PETSc.Mat
PETSc Matrix
"""
from petsc4py import PETSc

if isinstance(mat, StencilMatrix):
comm = mat.domain.cart.global_comm
elif isinstance(mat.domain.spaces[0], StencilVectorSpace):
comm = mat.domain.spaces[0].cart.global_comm
elif isinstance(mat.domain.spaces[0], BlockVectorSpace):
comm = mat.domain.spaces[0][0].cart.global_comm

mat_coo = mat.tosparse() #has the shape of the global operator
print('global shape', mat.shape)
print('dense matrix', mat_coo.todense())
print('mat_coo.row', mat_coo.row)
print('mat_coo.col', mat_coo.col)
mat_csr = mat_coo.tocsr()
print('mat_csr.indptr', mat_csr.indptr)
print('mat_csr.indices', mat_csr.indices)

gmat = PETSc.Mat().create(comm=comm)



#gmat.setSizes(mat.shape)
#mat.setSizes([[nrl, nrg], [ncl, ncg]])
gmat.setSizes([[mat.shape[0]//2, mat.shape[0]], [mat.shape[1]//2, mat.shape[1]]])

#gmat.setSizes((nrows, ncols))

# Set sparse matrix type
gmat.setType("mpiaij")
gmat.setFromOptions()

'''if comm:
# Preallocate number of nonzeros based on CSR structure
gmat.setPreallocationCSR((mat_csr.indptr, mat_csr.indices))
#NNZ = comm.allreduce(mat_csr.size, op=MPI.SUM)
#gmat.setPreallocationNNZ(NNZ)

# Fill-in matrix values from CSR data
#indptr: Stores accumulated number of non-zero entries
#indices: Stores column index of entries
#data: Stores non-zero entries

nrows = len(mat_csr.indptr)-1 #indptr always has a 0 in the first position to avoid void array
for r in range(nrows):
# get number of non zero entries to fill in row r
num_non_zero = mat_csr.indptr[r+1] - mat_csr.indptr[r]
for k in range(num_non_zero):
# set the value in correct column
gmat.setValues(r, mat_csr.indices[r+k], mat_csr.data[r+k])
#cols = mat_csr.indices[mat_csr.indptr[i]:mat_csr.indptr[i+1]]
#col_data = mat_csr.data[mat_csr.indptr[i]:mat_csr.indptr[i+1]]
#gmat.setValues(i, cols, col_data)

# Process inserted matrix entries
gmat.assemble()
return gmat
'''