Actual source code: aijmatlab.c

petsc-3.11.1 2019-04-17
Report Typos and Errors

  2: /*
  3:         Provides an interface for the MATLAB engine sparse solver

  5: */
  6:  #include <../src/mat/impls/aij/seq/aij.h>
  7:  #include <petscmatlab.h>
  8: #include <engine.h>   /* MATLAB include file */
  9: #include <mex.h>      /* MATLAB include file */

 11: PETSC_EXTERN mxArray *MatSeqAIJToMatlab(Mat B)
 12: {
 14:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)B->data;
 15:   mwIndex        *ii,*jj;
 16:   mxArray        *mat;
 17:   PetscInt       i;

 20:   mat  = mxCreateSparse(B->cmap->n,B->rmap->n,aij->nz,mxREAL);
 21:   PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));if (ierr) return NULL;
 22:   /* MATLAB stores by column, not row so we pass in the transpose of the matrix */
 23:   jj = mxGetIr(mat);
 24:   for (i=0; i<aij->nz; i++) jj[i] = aij->j[i];
 25:   ii = mxGetJc(mat);
 26:   for (i=0; i<B->rmap->n+1; i++) ii[i] = aij->i[i];
 27:   PetscFunctionReturn(mat);
 28: }

 30: PETSC_EXTERN PetscErrorCode MatlabEnginePut_SeqAIJ(PetscObject obj,void *mengine)
 31: {
 33:   mxArray        *mat;

 36:   mat  = MatSeqAIJToMatlab((Mat)obj);if (!mat) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot create MATLAB matrix");
 37:   PetscObjectName(obj);
 38:   engPutVariable((Engine*)mengine,obj->name,mat);
 39:   return(0);
 40: }

 42: /*@C
 43:     MatSeqAIJFromMatlab - Given a MATLAB sparse matrix, fills a SeqAIJ matrix with its transpose.

 45:    Not Collective

 47:    Input Parameters:
 48: +     mmat - a MATLAB sparse matris
 49: -     mat - an already created MATSEQAIJ

 51:   Level: intermediate

 53: @*/
 54: PETSC_EXTERN PetscErrorCode MatSeqAIJFromMatlab(mxArray *mmat,Mat mat)
 55: {
 57:   PetscInt       nz,n,m,*i,*j,k;
 58:   mwIndex        nnz,nn,nm,*ii,*jj;
 59:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;

 62:   nn  = mxGetN(mmat);   /* rows of transpose of matrix */
 63:   nm  = mxGetM(mmat);
 64:   nnz = (mxGetJc(mmat))[nn];
 65:   ii  = mxGetJc(mmat);
 66:   jj  = mxGetIr(mmat);
 67:   n   = (PetscInt) nn;
 68:   m   = (PetscInt) nm;
 69:   nz  = (PetscInt) nnz;

 71:   if (mat->rmap->n < 0 && mat->cmap->n < 0) {
 72:     /* matrix has not yet had its size set */
 73:     MatSetSizes(mat,n,m,PETSC_DETERMINE,PETSC_DETERMINE);
 74:     MatSetUp(mat);
 75:   } else {
 76:     if (mat->rmap->n != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change size of PETSc matrix %D to %D",mat->rmap->n,n);
 77:     if (mat->cmap->n != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change size of PETSc matrix %D to %D",mat->cmap->n,m);
 78:   }
 79:   if (nz != aij->nz) {
 80:     /* number of nonzeros in matrix has changed, so need new data structure */
 81:     MatSeqXAIJFreeAIJ(mat,&aij->a,&aij->j,&aij->i);
 82:     aij->nz = nz;
 83:     PetscMalloc3(aij->nz,&aij->a,aij->nz,&aij->j,mat->rmap->n+1,&aij->i);

 85:     aij->singlemalloc = PETSC_TRUE;
 86:   }

 88:   PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));
 89:   /* MATLAB stores by column, not row so we pass in the transpose of the matrix */
 90:   i = aij->i;
 91:   for (k=0; k<n+1; k++) i[k] = (PetscInt) ii[k];
 92:   j = aij->j;
 93:   for (k=0; k<nz; k++) j[k] = (PetscInt) jj[k];

 95:   for (k=0; k<mat->rmap->n; k++) aij->ilen[k] = aij->imax[k] = aij->i[k+1] - aij->i[k];

 97:   mat->nonzerostate++; /* since the nonzero structure can change anytime force the Inode information to always be rebuilt */
 98:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
 99:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
100:   return(0);
101: }

103: PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject obj,void *mengine)
104: {
106:   Mat            mat = (Mat)obj;
107:   mxArray        *mmat;

110:   mmat = engGetVariable((Engine*)mengine,obj->name);
111:   MatSeqAIJFromMatlab(mmat,mat);
112:   return(0);
113: }

115: PetscErrorCode MatSolve_Matlab(Mat A,Vec b,Vec x)
116: {
118:   const char     *_A,*_b,*_x;

121:   /* make sure objects have names; use default if not */
122:   PetscObjectName((PetscObject)b);
123:   PetscObjectName((PetscObject)x);

125:   PetscObjectGetName((PetscObject)A,&_A);
126:   PetscObjectGetName((PetscObject)b,&_b);
127:   PetscObjectGetName((PetscObject)x,&_x);
128:   PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)b);
129:   PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = u%s\\(l%s\\(p%s*%s));",_x,_A,_A,_A,_b);
130:   PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_b);
131:   /* PetscMatlabEnginePrintOutput(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),stdout);  */
132:   PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)x);
133:   return(0);
134: }

136: PetscErrorCode MatLUFactorNumeric_Matlab(Mat F,Mat A,const MatFactorInfo *info)
137: {
139:   size_t         len;
140:   char           *_A,*name;
141:   PetscReal      dtcol = info->dtcol;

144:   if (F->factortype == MAT_FACTOR_ILU || info->dt > 0) {
145:     /* the ILU form is not currently registered */
146:     if (info->dtcol == PETSC_DEFAULT) dtcol = .01;
147:     F->ops->solve = MatSolve_Matlab;
148:     F->factortype = MAT_FACTOR_LU;

150:     PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)A);
151:     _A   = ((PetscObject)A)->name;
152:     PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"info_%s = struct('droptol',%g,'thresh',%g);",_A,info->dt,dtcol);
153:     PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"[l_%s,u_%s,p_%s] = luinc(%s',info_%s);",_A,_A,_A,_A,_A);
154:     PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_A);

156:     PetscStrlen(_A,&len);
157:     PetscMalloc1(len+2,&name);
158:     sprintf(name,"_%s",_A);
159:     PetscObjectSetName((PetscObject)F,name);
160:     PetscFree(name);
161:   } else {
162:     PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),(PetscObject)A);
163:     _A   = ((PetscObject)A)->name;
164:     PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"[l_%s,u_%s,p_%s] = lu(%s',%g);",_A,_A,_A,_A,dtcol);
165:     PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"%s = 0;",_A);
166:     PetscStrlen(_A,&len);
167:     PetscMalloc1(len+2,&name);
168:     sprintf(name,"_%s",_A);
169:     PetscObjectSetName((PetscObject)F,name);
170:     PetscFree(name);

172:     F->ops->solve = MatSolve_Matlab;
173:   }
174:   return(0);
175: }

177: PetscErrorCode MatLUFactorSymbolic_Matlab(Mat F,Mat A,IS r,IS c,const MatFactorInfo *info)
178: {
180:   if (A->cmap->N != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"matrix must be square");
181:   F->ops->lufactornumeric = MatLUFactorNumeric_Matlab;
182:   F->assembled            = PETSC_TRUE;
183:   return(0);
184: }

186: PetscErrorCode MatFactorGetSolverType_seqaij_matlab(Mat A,MatSolverType *type)
187: {
189:   *type = MATSOLVERMATLAB;
190:   return(0);
191: }

193: PetscErrorCode MatDestroy_matlab(Mat A)
194: {
196:   const char     *_A;

199:   PetscObjectGetName((PetscObject)A,&_A);
200:   PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PetscObjectComm((PetscObject)A)),"delete %s l_%s u_%s;",_A,_A,_A);
201:   return(0);
202: }

204: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat A,MatFactorType ftype,Mat *F)
205: {

209:   if (A->cmap->N != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"matrix must be square");
210:   MatCreate(PetscObjectComm((PetscObject)A),F);
211:   MatSetSizes(*F,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
212:   PetscStrallocpy("matlab",&((PetscObject)*F)->type_name);
213:   MatSetUp(*F);

215:   (*F)->ops->destroy           = MatDestroy_matlab;
216:   (*F)->ops->getinfo           = MatGetInfo_External;
217:   (*F)->ops->lufactorsymbolic  = MatLUFactorSymbolic_Matlab;
218:   (*F)->ops->ilufactorsymbolic = MatLUFactorSymbolic_Matlab;

220:   PetscObjectComposeFunction((PetscObject)(*F),"MatFactorGetSolverType_C",MatFactorGetSolverType_seqaij_matlab);

222:   (*F)->factortype = ftype;
223:   PetscFree((*F)->solvertype);
224:   PetscStrallocpy(MATSOLVERMATLAB,&(*F)->solvertype);
225:   return(0);
226: }


229: PETSC_EXTERN PetscErrorCode MatSolverTypeRegister_Matlab(void)
230: {

234:   MatSolverTypeRegister(MATSOLVERMATLAB,MATSEQAIJ,        MAT_FACTOR_LU,MatGetFactor_seqaij_matlab);
235:   return(0);
236: }

238: /* --------------------------------------------------------------------------------*/

240: PetscErrorCode MatView_Info_Matlab(Mat A,PetscViewer viewer)
241: {

245:   PetscViewerASCIIPrintf(viewer,"MATLAB run parameters:  -- not written yet!\n");
246:   return(0);
247: }

249: PetscErrorCode MatView_Matlab(Mat A,PetscViewer viewer)
250: {
251:   PetscErrorCode    ierr;
252:   PetscBool         iascii;
253:   PetscViewerFormat format;

256:   MatView_SeqAIJ(A,viewer);
257:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
258:   if (iascii) {
259:     PetscViewerGetFormat(viewer,&format);
260:     if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
261:       MatView_Info_Matlab(A,viewer);
262:     }
263:   }
264:   return(0);
265: }


268: /*MC
269:   MATSOLVERMATLAB - "matlab" - Providing direct solver LU for sequential aij matrix via the external package MATLAB.


272:   Works with MATSEQAIJ matrices.

274:   Options Database Keys:
275: . -pc_factor_mat_solver_type matlab - selects MATLAB to do the sparse factorization

277:   Notes:
278:     You must ./configure with the options --with-matlab --with-matlab-engine

280:   Level: beginner

282: .seealso: PCLU

284: .seealso: PCFactorSetMatSolverType(), MatSolverType
285: M*/