mercredi 20 juin 2018

Very simple PETSc Mat construction gives SEGV 11 error - C++

I want to create a very simple 2x2 identity matrix in C++ using PETSc library. I have searched various tutorials on the internet and found that all of them indicate one way of doing this. However I get a "Caught signal number 11 SEGV: Segmentation Violation" error. This code is part of a much larger program where I need to create some matrices used in conjunction with other elements of it. Moreover, this code is a mere test, in which I am trying to find out how exactly does PETSc work with matrices. I could say I am still in the learning phase.

The code is the following:

Mat Y11;

 MatCreate(MPI_COMM_WORLD, &Y11);
 MatSetSizes(Y11, PETSC_DECIDE, PETSC_DECIDE, 2, 2);
 std::cout << "set mat type" << std::endl;
 MatSetType(Y11, MATSEQMAIJ);

 MatSetFromOptions(Y11);

 PetscScalar temp22[] = {std::complex<double>(1.0,0), std::complex<double>(0.0,0), std::complex<double>(0.0,0.0), std::complex<double>(1.0,0.0)};
 PetscInt tempis[] = {0, 1};
 std::cout << "MatSetValues" << std::endl;
 MatSetValues(Y11, 1, &tempis[0], 1, &tempis[0], &temp22[0], INSERT_VALUES);
 MatSetValues(Y11, 1, &tempis[0], 1, &tempis[1], &temp22[1], INSERT_VALUES);
 MatSetValues(Y11, 1, &tempis[1], 1, &tempis[0], &temp22[2], INSERT_VALUES);
 MatSetValues(Y11, 1, &tempis[1], 1, &tempis[1], &temp22[3], INSERT_VALUES);

 MatAssemblyBegin(Y11,MAT_FINAL_ASSEMBLY);
 MatAssemblyEnd(Y11,MAT_FINAL_ASSEMBLY);

 std::cout << std::endl << "Y11 block:" << std::endl;
 MatView(Y11,PETSC_VIEWER_STDOUT_WORLD);

The error stems from MatSetValues function.

Could anyone please tell me what could probably be wrong with this code? There are no simple examples like this on the internet (at least of what I can search, in C++). Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire