Actual source code: gcreatev.c
1: #include <petscvec.h>
2: #include <petsc/private/petscimpl.h>
4: #include <engine.h> /* MATLAB include file */
5: #include <mex.h> /* MATLAB include file */
7: PETSC_EXTERN PetscErrorCode VecMatlabEnginePut_Default(PetscObject obj, void *mengine)
8: {
9: PetscInt n;
10: Vec vec = (Vec)obj;
11: const PetscScalar *array;
12: mxArray *mat;
14: PetscFunctionBegin;
15: PetscCall(VecGetArrayRead(vec, &array));
16: PetscCall(VecGetLocalSize(vec, &n));
17: mat = mxCreateDoubleMatrix(n, 1, PetscDefined(USE_COMPLEX) ? mxCOMPLEX : mxREAL);
18: PetscCall(PetscArraycpy(mxGetPr(mat), array, n));
19: PetscCall(PetscObjectName(obj));
20: engPutVariable((Engine *)mengine, obj->name, mat);
22: PetscCall(VecRestoreArrayRead(vec, &array));
23: PetscFunctionReturn(PETSC_SUCCESS);
24: }
26: PETSC_EXTERN PetscErrorCode VecMatlabEngineGet_Default(PetscObject obj, void *mengine)
27: {
28: PetscInt n;
29: Vec vec = (Vec)obj;
30: PetscScalar *array;
31: mxArray *mat;
33: PetscFunctionBegin;
34: PetscCall(VecGetArray(vec, &array));
35: PetscCall(VecGetLocalSize(vec, &n));
36: mat = engGetVariable((Engine *)mengine, obj->name);
37: PetscCheck(mat, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to get object %s from matlab", obj->name);
38: PetscCall(PetscArraycpy(array, mxGetPr(mat), n));
39: PetscCall(VecRestoreArray(vec, &array));
40: PetscFunctionReturn(PETSC_SUCCESS);
41: }