| 1 | /* \file MatlabMatrixToPetscMatrix.cpp
|
|---|
| 2 | * \brief: convert a sparse or dense matlab matrix to a serial Petsc matrix:
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | #ifdef HAVE_CONFIG_H
|
|---|
| 6 | #include <config.h>
|
|---|
| 7 | #else
|
|---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
|---|
| 9 | #endif
|
|---|
| 10 | #include "../../c/shared/shared.h"
|
|---|
| 11 |
|
|---|
| 12 | /*Petsc includes: */
|
|---|
| 13 | #include <petscmat.h>
|
|---|
| 14 | #include <petscvec.h>
|
|---|
| 15 | #include <petscksp.h>
|
|---|
| 16 |
|
|---|
| 17 | /*Matlab includes: */
|
|---|
| 18 | #include "./matlabio.h"
|
|---|
| 19 |
|
|---|
| 20 | PetscMat* MatlabMatrixToPetscMat(const mxArray* mxmatrix){
|
|---|
| 21 |
|
|---|
| 22 | int dummy;
|
|---|
| 23 | PetscMat* matrix=new PetscMat();
|
|---|
| 24 |
|
|---|
| 25 | MatlabMatrixToPetscMat(&matrix->matrix, &dummy, &dummy, mxmatrix);
|
|---|
| 26 |
|
|---|
| 27 | return matrix;
|
|---|
| 28 | }
|
|---|
| 29 | int MatlabMatrixToPetscMat(Mat* pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
|
|---|
| 30 |
|
|---|
| 31 | int rows, cols;
|
|---|
| 32 | double *mxmatrix_ptr = NULL;
|
|---|
| 33 | double *tmatrix = NULL;
|
|---|
| 34 | int ierr;
|
|---|
| 35 | int i,j;
|
|---|
| 36 |
|
|---|
| 37 | /*output: */
|
|---|
| 38 | Mat matrix = NULL;
|
|---|
| 39 |
|
|---|
| 40 | /*matlab indices: */
|
|---|
| 41 | mwIndex *ir = NULL;
|
|---|
| 42 | mwIndex *jc = NULL;
|
|---|
| 43 | double *pr = NULL;
|
|---|
| 44 | int count;
|
|---|
| 45 | int nnz;
|
|---|
| 46 | int nz;
|
|---|
| 47 |
|
|---|
| 48 | /*petsc indices: */
|
|---|
| 49 | int *idxm = NULL;
|
|---|
| 50 | int *idxn = NULL;
|
|---|
| 51 |
|
|---|
| 52 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
|---|
| 53 | if (mxIsSparse(mxmatrix)){
|
|---|
| 54 |
|
|---|
| 55 | /*Dealing with sparse matrix: recover size first: */
|
|---|
| 56 | mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
|
|---|
| 57 | rows=mxGetM(mxmatrix);
|
|---|
| 58 | cols=mxGetN(mxmatrix);
|
|---|
| 59 | nnz=mxGetNzmax(mxmatrix);
|
|---|
| 60 | if(rows){
|
|---|
| 61 | nz=(int)((double)nnz/(double)rows);
|
|---|
| 62 | }
|
|---|
| 63 | else{
|
|---|
| 64 | nz=0;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | ierr=MatCreateSeqAIJ(PETSC_COMM_SELF,rows,cols,nz,PETSC_NULL,&matrix);CHKERRQ(ierr);
|
|---|
| 68 |
|
|---|
| 69 | /*Now, get ir,jc and pr: */
|
|---|
| 70 | pr=mxGetPr(mxmatrix);
|
|---|
| 71 | ir=mxGetIr(mxmatrix);
|
|---|
| 72 | jc=mxGetJc(mxmatrix);
|
|---|
| 73 |
|
|---|
| 74 | /*Now, start inserting data into sparse matrix: */
|
|---|
| 75 | count=0;
|
|---|
| 76 | for(i=0;i<cols;i++){
|
|---|
| 77 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
|---|
| 78 | MatSetValue(matrix,ir[count],i,pr[count],INSERT_VALUES);
|
|---|
| 79 | count++;
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | else{
|
|---|
| 84 | /*Dealing with dense matrix: recover pointer and size: */
|
|---|
| 85 | mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
|
|---|
| 86 | rows=mxGetM(mxmatrix);
|
|---|
| 87 | cols=mxGetN(mxmatrix);
|
|---|
| 88 |
|
|---|
| 89 | /*transpose, as Petsc now does not allows MAT_COLUMN_ORIENTED matrices in MatSetValues: */
|
|---|
| 90 | tmatrix=xNew<double>(rows*cols);
|
|---|
| 91 | for(i=0;i<cols;i++){
|
|---|
| 92 | for(j=0;j<rows;j++){
|
|---|
| 93 | *(tmatrix+rows*i+j)=*(mxmatrix_ptr+cols*j+i);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*Create serial matrix: */
|
|---|
| 98 | ierr=MatCreateSeqDense(PETSC_COMM_SELF,rows,cols,NULL,&matrix);CHKERRQ(ierr);
|
|---|
| 99 |
|
|---|
| 100 | /*Insert mxmatrix_ptr values into petsc matrix: */
|
|---|
| 101 | idxm=xNew<int>(rows);
|
|---|
| 102 | idxn=xNew<int>(cols);
|
|---|
| 103 |
|
|---|
| 104 | for(i=0;i<rows;i++)idxm[i]=i;
|
|---|
| 105 | for(i=0;i<cols;i++)idxn[i]=i;
|
|---|
| 106 |
|
|---|
| 107 | ierr=MatSetValues(matrix,rows,idxm,cols,idxn,tmatrix,INSERT_VALUES); CHKERRQ(ierr);
|
|---|
| 108 |
|
|---|
| 109 | xDelete<double>(tmatrix);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /*Assemble matrix: */
|
|---|
| 113 | MatAssemblyBegin(matrix,MAT_FINAL_ASSEMBLY);
|
|---|
| 114 | MatAssemblyEnd(matrix,MAT_FINAL_ASSEMBLY);
|
|---|
| 115 |
|
|---|
| 116 | /*Assign output pointer: */
|
|---|
| 117 | *pmatrix=matrix;
|
|---|
| 118 | if(pmatrix_rows) *pmatrix_rows=rows;
|
|---|
| 119 | if(pmatrix_cols) *pmatrix_cols=cols;
|
|---|
| 120 |
|
|---|
| 121 | return 1;
|
|---|
| 122 | }
|
|---|