I have a 3x3 global matrix is built (diag: 1, 2, 1): it's made of 2 overlapping
2x2 local matrix (diag: 1, 1).
Getting non assembled local matrix is OK with MatISGetLocalMat.
How to get assembled local matrix (initial local matrix + neigbhor
contributions on the borders) ? (expected result is diag: 2, 1)
Franck
// Using PETSc MatIS, how to get local matrix (= one domain) before and after assembly (neumann/dirichlet).
//
// A 3x3 global matrix is built (diag: 1, 2, 1): it's made of 2 overlapping 2x2 local matrix (diag: 1, 1).
// Get non assembled local matrix : OK with MatISGetLocalMat.
//
// How to get assembled local matrix (initial local matrix + neigbhor contributions on the borders) ?
// Need to use MatGetLocalSubMatrix ? Not working ?! Expected result is Diag: 2, 1.
//
// ~> g++ -o matISLocalMat.exe matISLocalMat.cpp -lpetsc -lm; mpirun -n 2 matISLocalMat.exe
#include <iostream>
#include "petsc.h"
int main(int argc,char **argv) {
PetscInitialize(&argc, &argv, NULL, NULL);
int size = 0; MPI_Comm_size(MPI_COMM_WORLD, &size); if (size != 2) return 1;
int rank = 0; MPI_Comm_rank(MPI_COMM_WORLD, &rank);
PetscInt localSize = 2, globalSize = localSize*2 /*2 MPI*/;
PetscInt localIdx[2] = {0, 0};
if (rank == 0) {localIdx[0] = 0; localIdx[1] = 1;}
else {localIdx[0] = 1; localIdx[1] = 2;}
ISLocalToGlobalMapping map;
ISLocalToGlobalMappingCreate(PETSC_COMM_WORLD, 1, localSize, localIdx, PETSC_COPY_VALUES, &map);
Mat A;
MatCreateIS(PETSC_COMM_WORLD, 1, localSize, localSize, globalSize, globalSize, map, map, &A);
ISLocalToGlobalMappingDestroy(&map);
MatISSetPreallocation(A, localSize, NULL, localSize, NULL);
PetscScalar localVal[4] = {1., 0., 0., 1.};
MatSetValues(A, 2, localIdx, 2, localIdx, localVal, ADD_VALUES);
MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
MatView(A, PETSC_VIEWER_STDOUT_WORLD); PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD); // Diag: 1, 2, 1
if (rank > 0) { // Do not pollute stdout: print only 1 proc
std::cout << std::endl << "non assembled local matrix:" << std::endl << std::endl;
Mat nonAssembledLocalMat;
MatISGetLocalMat(A, &nonAssembledLocalMat);
MatView(nonAssembledLocalMat, PETSC_VIEWER_STDOUT_SELF); // Diag: 1, 1
std::cout << std::endl << "assembled local matrix:" << std::endl << std::endl;
Mat assembledLocalMat;
IS is; ISCreateGeneral(PETSC_COMM_SELF, localSize, localIdx, PETSC_COPY_VALUES, &is);
MatGetLocalSubMatrix(A, is, is, &assembledLocalMat); // KO ?!...
MatView(assembledLocalMat, PETSC_VIEWER_STDOUT_SELF); // Would like to get => Diag: 2, 1
}
PetscFinalize();
return 0;
}
Mat Object: 2 MPI processes
type: is
Mat Object: (is_) 1 MPI processes
type: seqaij
row 0: (0, 1.) (1, 0.)
row 1: (0, 0.) (1, 1.)
Mat Object: (is_) 1 MPI processes
type: seqaij
row 0: (0, 1.) (1, 0.)
row 1: (0, 0.) (1, 1.)
non assembled local matrix:
Mat Object:(is_) 1 MPI processes
type: seqaij
row 0: (0, 1.) (1, 0.)
row 1: (0, 0.) (1, 1.)
assembled local matrix:
[1]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
[1]PETSC ERROR: Argument out of range
[1]PETSC ERROR: Local index 2 too large 1 (max) at 1
[1]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html for trouble shooting.
[1]PETSC ERROR: Petsc Release Version 3.7.6, Apr, 24, 2017
[1]PETSC ERROR: ./matISLocalMat.exe on a arch-linux2-c-debug named yoda by fghoussen Sun May 21 14:15:15 2017
[1]PETSC ERROR: Configure options --prefix=/home/fghoussen/Documents/INRIA/petsc-3.7.6/local --with-mpi=1 --with-pthread=1 --download-f2cblaslapack=yes
[1]PETSC ERROR: #1 ISLocalToGlobalMappingApply() line 423 in /home/fghoussen/Documents/INRIA/petsc-3.7.6/src/vec/is/utils/isltog.c
[1]PETSC ERROR: #2 ISL2GCompose() line 141 in /home/fghoussen/Documents/INRIA/petsc-3.7.6/src/mat/impls/localref/mlocalref.c
[1]PETSC ERROR: #3 MatCreateLocalRef() line 260 in /home/fghoussen/Documents/INRIA/petsc-3.7.6/src/mat/impls/localref/mlocalref.c
[1]PETSC ERROR: #4 MatGetLocalSubMatrix() line 10101 in /home/fghoussen/Documents/INRIA/petsc-3.7.6/src/mat/interface/matrix.c
[1]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
[1]PETSC ERROR: Null argument, when expecting valid pointer
[1]PETSC ERROR: Null Object: Parameter # 1
[1]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html for trouble shooting.
[1]PETSC ERROR: Petsc Release Version 3.7.6, Apr, 24, 2017
[1]PETSC ERROR: ./matISLocalMat.exe on a arch-linux2-c-debug named yoda by fghoussen Sun May 21 14:15:15 2017
[1]PETSC ERROR: Configure options --prefix=/home/fghoussen/Documents/INRIA/petsc-3.7.6/local --with-mpi=1 --with-pthread=1 --download-f2cblaslapack=yes
[1]PETSC ERROR: #5 MatView() line 836 in /home/fghoussen/Documents/INRIA/petsc-3.7.6/src/mat/interface/matrix.c