Defines | Enumerations | Functions

matlab_gs_utils.c File Reference

#include <stdio.h>
#include <string.h>
#include <matrix.h>
#include <mex.h>
#include "grpc.h"
#include "comm_data.h"
#include "matlab_gs.h"
Include dependency graph for matlab_gs_utils.c:

Go to the source code of this file.

Defines

#define MAX_FILES_IN_PACKEDFILE   1024
#define COPY_A_TO_B(TYPEA, a, aoffset, TYPEB, b, boffset, size)
#define COPY_MXARRAY_TO_GSCOMPLEX(MX_DATA_T, mxr, mxi, mxoffset, GS_COMPLEX_T, gs_cplx, gsoffset, GS_DATA_T, size)

Enumerations

enum  gs_alloc_t { DO_NOT_ALLOC, MAYBE_ALLOC }

Functions

void * matlab_gs_calloc (size_t N, size_t S)
 Allocate memory.
void * matlab_gs_free (void *ptr)
 Free memory.
int matlab_gs_arg_is_sparse_index_or_pointer (gs_argument_t *argp, gs_problem_t *problem)
 Check if given argument acts as an index or pointer for a sparse argument.
static mxArray * matlab_gs_create_matrix (int objecttype, int M, int N, int nnz, int datatype)
 Allocate and create a Matlab matrix (verious datatypes, sparse, full, etc).
static int transfer_mx_to_gs_int (mxClassID mxclassid, void *pr, int poffset, void **datapp, int doffset, int nelem, gs_alloc_t allocp)
 Transfer Matlab array to GridSolve data.
static int transfer_mx_to_gs_double (mxClassID mxclassid, void *pr, int poffset, void **datapp, int doffset, int nelem, gs_alloc_t allocp)
 Transfer Matlab array to GridSolve data.
static int transfer_mx_to_gs_float (mxClassID mxclassid, void *pr, int poffset, void **datapp, int doffset, int nelem, gs_alloc_t allocp)
 Transfer Matlab array to GridSolve data.
static int transfer_mx_to_gs_scomplex (mxClassID mxclassid, void *pr, void *pi, int poffset, void **datapp, int doffset, int nelem, gs_alloc_t allocp)
 Transfer Matlab array to GridSolve complex data.
static int transfer_mx_to_gs_dcomplex (mxClassID mxclassid, void *pr, void *pi, int poffset, void **datapp, int doffset, int nelem, gs_alloc_t allocp)
 Transfer Matlab array to GridSolve complex data.
static mxArray * transfer_gs_to_mx (gs_argument_t *argp)
 Transfer GridSolve data to Matlab.
static int arg_data_same_as_mx_dat (const mxArray *mxarray, gs_argument_t *argp)
 Check if data/datatytpes in Matlab and GridSolve match and can be used without allocating new space.
static int matlab_gs_sparse_matcsc_to_csr (int m, int n, int nnz, int mxclassid, void *ar, void *ai, int *colind, int *rowptr, int gsdatatype, void *gsat, int *rowind, int *colptr)
 Convert sparse matrix from Matlab (CSC) to GridSolve (CSR) formats.
static int matlab_gs_sparse_csr_to_matcsc (int m, int n, int nnz, int datatype, void *a, int *colind, int *rowptr, double *at, double *ati, int *rowind, int *colptr)
 Convert sparse matrix from GridSolve (CSR) to Matlab (CSC) formats.
static int matlab_gs_setup_scalar_arguments (gs_problem_t *problem, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
 Copy scalar arguments from Matlab to the problem structure.
int matlab_gs_setup_args (gs_problem_t *problem, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
 Setup all arguments provided from Matlab.
int matlab_gs_get_output (gs_problem_t *problem, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
int matlab_gs_free_input_args (gs_problem_t *problem, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
int matlab_gs_free_args (gs_problem_t *problem, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

Detailed Description

Matlab client utilities. Mostly used to transfer input from Matlab to GridSolve and output from GridSolve to Matlab. Space may be allocated for the GridSolve data, if it does not exactly match the Matlab data types and sizes (this space will need to be released later).

Definition in file matlab_gs_utils.c.


Define Documentation

#define COPY_A_TO_B ( TYPEA,
a,
aoffset,
TYPEB,
b,
boffset,
size   ) 
Value:
do {int ss = 0; \
TYPEA* ss_a = (TYPEA*) a; \
TYPEB* ss_b = (TYPEB*) b; \
ss_a+=aoffset; ss_b+=boffset; \
for (ss = 0; ss < size; ss++) \
*((ss_b)++) = (TYPEB) *((ss_a)++);\
} while (0)

Definition at line 28 of file matlab_gs_utils.c.

#define COPY_MXARRAY_TO_GSCOMPLEX ( MX_DATA_T,
mxr,
mxi,
mxoffset,
GS_COMPLEX_T,
gs_cplx,
gsoffset,
GS_DATA_T,
size   ) 
Value:
do {int ss = 0; \
MX_DATA_T* ss_mxr = (MX_DATA_T*) mxr; \
MX_DATA_T* ss_mxi = (MX_DATA_T*) mxi; \
GS_COMPLEX_T* ss_gs_cplx = (GS_COMPLEX_T*) gs_cplx; \
ss_mxr+=mxoffset; ss_mxi+=mxoffset; ss_gs_cplx+=gsoffset; \
for (ss = 0; ss < size; ss++) { \
  ss_gs_cplx[ss].r = (GS_DATA_T) *((ss_mxr)++); \
  if (mxi == NULL) \
    ss_gs_cplx[ss].i = (GS_DATA_T) 0.0; \
  else \
    ss_gs_cplx[ss].i = (GS_DATA_T) *((ss_mxi)++); \
  } \
} while (0)

Definition at line 37 of file matlab_gs_utils.c.

#define MAX_FILES_IN_PACKEDFILE   1024

Definition at line 24 of file matlab_gs_utils.c.


Enumeration Type Documentation

enum gs_alloc_t
Enumerator:
DO_NOT_ALLOC 
MAYBE_ALLOC 

Definition at line 26 of file matlab_gs_utils.c.

{DO_NOT_ALLOC, MAYBE_ALLOC} gs_alloc_t;


Function Documentation

static int arg_data_same_as_mx_dat ( const mxArray *  mxarray,
gs_argument_t *  argp 
) [static]

Check if data/datatytpes in Matlab and GridSolve match and can be used without allocating new space.

Parameters:
mxarray -- Matlab array
argp == GridSolve argument
Returns:
1 if data space can be reused, 0 otherwise

Definition at line 492 of file matlab_gs_utils.c.

{
  int gsdatatype = argp->datatype;
  mxClassID mxclassid = mxGetClassID(mxarray);

  if (argp->inout != GS_IN) return 0;

  if (mxclassid==mxDOUBLE_CLASS && gsdatatype==GS_DOUBLE) 
    return 1;
  else if (mxclassid==mxSINGLE_CLASS && gsdatatype==GS_FLOAT) 
    return 1;
  else if (mxclassid==mxINT32_CLASS && gsdatatype==GS_INT) 
    return 1;
  else 
    return 0;
}

Here is the caller graph for this function:

int matlab_gs_arg_is_sparse_index_or_pointer ( gs_argument_t *  argp,
gs_problem_t *  problem 
)

Check if given argument acts as an index or pointer for a sparse argument.

Parameters:
argp -- Pointer to argument to check
problem -- Pointer to entire problem structure to check against.
Returns:
1 if it is a sparse attribute, 0 otherwise

Definition at line 100 of file matlab_gs_utils.c.

{
  int arg_is_sparse_attr = 0;
  gs_argument_t* arg_attr = NULL;
  
  for (arg_attr = problem->arglist; arg_attr != NULL; arg_attr = arg_attr->next)
    if ((arg_attr->objecttype == GS_SPARSEMATRIX)  && 
    (!strcmp(argp->name, arg_attr->sparse_attr.indices) || 
     !strcmp(argp->name, arg_attr->sparse_attr.pointer))) {
      arg_is_sparse_attr = 1;
      break;
    }
  return arg_is_sparse_attr;
}

Here is the caller graph for this function:

void* matlab_gs_calloc ( size_t  N,
size_t  S 
)

Allocate memory.

May use Matlab routines to allocated memory, allowing Matlab to release memory when the module is unloaded.

Parameters:
N -- Number of objects to be allocted
S -- Size of each object
Returns:
pointer to new memory, or NULL on error

Definition at line 64 of file matlab_gs_utils.c.

{
  void *ptr;
  /* ptr = mxCalloc(N, S); */
  ptr = calloc(N, S);
  if (ptr == NULL) {
    ERRPRINTF("Matlab is out of memory in GridSolve client\n");
    return NULL;
  }
  /* else mexMakeMemoryPersistent(ptr); */
  return ptr;
}

Here is the caller graph for this function:

static mxArray* matlab_gs_create_matrix ( int  objecttype,
int  M,
int  N,
int  nnz,
int  datatype 
) [static]

Allocate and create a Matlab matrix (verious datatypes, sparse, full, etc).

Parameters:
M -- Num of rows
N -- Num of cols
nnz -- Num of non zeros if sparse
datatype -- GridSolve datatype (GS_INT, GS_DOUBLE, ...)
Returns:
Pointer to created Matlab mxArray object

Definition at line 127 of file matlab_gs_utils.c.

{
  mxComplexity realorcomplex = mxREAL;
  mxClassID classid = mxDOUBLE_CLASS;
  mxArray *ptr;

  switch (datatype) {
  case GS_INT: 
  case GS_FLOAT:
  case GS_DOUBLE:
    classid = mxDOUBLE_CLASS;
    realorcomplex = mxREAL;
    break;
  case GS_SCOMPLEX:
  case GS_DCOMPLEX:
    classid = mxDOUBLE_CLASS;
    realorcomplex = mxCOMPLEX;
    break;
  case GS_CHAR:
    break;
  case GS_BAD_DTYPE:
    return NULL;
    break;
  default:
    return NULL;
    break;
  }

  if (objecttype == GS_SPARSEMATRIX) {
    ptr = mxCreateSparse(M, N ,nnz, realorcomplex);
  } else {
    ptr = mxCreateNumericMatrix(M, N, classid, realorcomplex); 
  }
  return ptr;
}

Here is the caller graph for this function:

void* matlab_gs_free ( void *  ptr  ) 

Free memory.

Free memory allocaed using matlab_gs_calloc.

Parameters:
ptr -- Pointer to memory

Definition at line 84 of file matlab_gs_utils.c.

{
  /* if ( ptr ) mxFree(ptr); */
  if ( ptr ) free(ptr);  
  return NULL;
}

Here is the caller graph for this function:

int matlab_gs_free_args ( gs_problem_t *  problem,
int  nlhs,
mxArray *  plhs[],
int  nrhs,
const mxArray *  prhs[] 
)

Free space allocated by the matlab client as part of the problem data structure for the arguments.

Parameters:
nlhs - Number of left hand side arguments from Matlab
plhs - Pointer to left hand side arguments from Matlab
rlhs - Number of right hand side arguments from Matlab
plhs - Pointer to right hand side arguments from Matlab
Returns:
0 if OK or -1 if error

Definition at line 1299 of file matlab_gs_utils.c.

{
  int ilhs = 0;
  int i;
  gs_argument_t* argp = NULL;

  if (problem==NULL) return 0;

  DBGPRINTF("Freeing all output args\n");

  for (argp=problem->arglist,ilhs=0; argp!=NULL && ilhs<nlhs; argp=argp->next) {
    if (argp->inout==GS_INOUT || argp->inout==GS_OUT || argp->inout==GS_VAROUT ) {
      switch(argp->objecttype) {
      case GS_SCALAR:
    break;
      case GS_VECTOR:
      case GS_MATRIX:
    argp->data = matlab_gs_free(argp->data);
    break;
      case GS_SPARSEMATRIX:
    argp->data = matlab_gs_free(argp->data);
    break;
      case GS_FILE:
    argp->data = matlab_gs_free(argp->data);
    break;
      case GS_PACKEDFILE:
    for (i=0; i<argp->rows*argp->cols; i++) 
      matlab_gs_free(((char **)argp->data)[i]);
    argp->data = matlab_gs_free(argp->data);
    break;
      default:
    break;
    
      }
    }
  }
  
  DBGPRINTF("Finished\n");
  return 0;

}

Here is the call graph for this function:

Here is the caller graph for this function:

int matlab_gs_free_input_args ( gs_problem_t *  problem,
int  nlhs,
mxArray *  plhs[],
int  nrhs,
const mxArray *  prhs[] 
)

Free space allocated by the matlab client as part of the problem data structure for the INPUT arguments. This is called after any send has been done, so the arg->data data is no longer needed.

Parameters:
nlhs - Number of left hand side arguments from Matlab
plhs - Pointer to left hand side arguments from Matlab
rlhs - Number of right hand side arguments from Matlab
plhs - Pointer to right hand side arguments from Matlab
Returns:
0 if OK or -1 if error

Definition at line 1239 of file matlab_gs_utils.c.

{
  int irhs, i;
  gs_argument_t* argp = NULL;
  if (problem==NULL) return 0;

  DBGPRINTF("Freeing input args\n");

  for (argp=problem->arglist,irhs=2; argp!=NULL && irhs<=nrhs; argp=argp->next) {
    if (argp->inout==GS_IN) {
      switch(argp->objecttype) {
      case GS_SCALAR:
    break;
      case GS_VECTOR:
      case GS_MATRIX:
    if (arg_data_same_as_mx_dat(prhs[irhs], argp)) 
      argp->data = NULL;
    else 
      argp->data = matlab_gs_free(argp->data);
    break;
      case GS_SPARSEMATRIX:
    argp->data = matlab_gs_free(argp->data);
    break;
      case GS_FILE:
    argp->data = matlab_gs_free(argp->data);    
    break;
      case GS_PACKEDFILE:
    for (i=0; i<argp->rows*argp->cols; i++) 
      matlab_gs_free(((char **)argp->data)[i]);
    argp->data = matlab_gs_free(argp->data);
    break;
      default:
    break;
      }

      /* increment irhs index if not sparse related, since sparse
       * index/pointer are not passed in in Matlab */
      if (!matlab_gs_arg_is_sparse_index_or_pointer(argp, problem))
    irhs++;         
    }
  }

  DBGPRINTF("Finished\n");
  return 0;

}

Here is the call graph for this function:

Here is the caller graph for this function:

int matlab_gs_get_output ( gs_problem_t *  problem,
int  nlhs,
mxArray *  plhs[],
int  nrhs,
const mxArray *  prhs[] 
)

Transfer output arguments from GridSolve space back to Matlab space, fixing any data type mismatches.

Parameters:
problem - The current problem data structure
nlhs - Number of left hand side arguments from Matlab
plhs - Pointer to left hand side arguments from Matlab
rlhs - Number of right hand side arguments from Matlab
plhs - Pointer to right hand side arguments from Matlab
Returns:
0 if OK or -1 if error

Definition at line 1127 of file matlab_gs_utils.c.

{
  gs_argument_t *argp = NULL;
  gs_argument_t *arg_pointer = NULL;  
  gs_argument_t *arg_indices = NULL;  
  gs_argument_t *row_arg, *col_arg;
  int M, N, ilhs;
  
  DBGPRINTF("Transferring output arguments to matlab\n");
  for (argp=problem->arglist,ilhs=0; argp!=NULL && ilhs<nlhs; argp=argp->next) {
    
    if (argp->inout==GS_OUT || argp->inout==GS_INOUT || argp->inout==GS_VAROUT) {

      /* skip this if it's a sparse matrix attribute, since the sparse
     attributes are not sent as matlab parameter, they will be
     handled when the actual sparse argument is handled  */
      if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) {
    DBGPRINTF("Skipping sparse attribute: %s \n", argp->name);
    continue;
      }

      switch (argp->objecttype) {
    
      case GS_FILE:
    DBGPRINTF("Transfer GS_FILE %s using filename %s\n", argp->name, (char *)argp->data);
    plhs[ilhs] = mxCreateString((char*)argp->data);
    break;
    
      case GS_PACKEDFILE:
    DBGPRINTF("Transfer GS_PACKEDFILE\n");
    plhs[ilhs] = mxCreateCharMatrixFromStrings(argp->rows, (const char**)argp->data);
    break;

      case GS_SPARSEMATRIX:
    DBGPRINTF("Transfer gs (%s [%d,%d] %s %s %s) to mx (ilhs %d) \n", argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype], ilhs); fflush(0);
    if (argp->inout==GS_VAROUT) { ERRPRINTF("VAROUT SPARSEMATRIX not supported\n");  goto error_output; }
    /* Lookup which gridsolve arg should hold indices and pointers */
    arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, problem->arglist);
    arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, problem->arglist);
    M = argp->sparse_attr.rows_val_saved;
    N = argp->sparse_attr.cols_val_saved;
    /* Copy CSR to CSC */
    if ((plhs[ilhs] = matlab_gs_create_matrix(argp->objecttype, M, N, argp->sparse_attr.nnz, argp->datatype)) == NULL) goto error_output;
    if (matlab_gs_sparse_csr_to_matcsc(M, N, argp->sparse_attr.nnz, argp->datatype, argp->data, arg_indices->data, arg_pointer->data, mxGetPr(plhs[ilhs]), mxGetPi(plhs[ilhs]), mxGetIr(plhs[ilhs]), mxGetJc(plhs[ilhs])) == -1) goto error_output;
    break;
    
      case GS_SCALAR:
    DBGPRINTF("Transfer gs (%s [%d,%d] %s %s %s) to mx (ilhs %d) \n", argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype], ilhs); fflush(0);
    plhs[ilhs] = transfer_gs_to_mx(argp);
    if (plhs[ilhs] == NULL) return -1;
    break;
      
      case GS_VECTOR:
      case GS_MATRIX:
    DBGPRINTF("Transfer gs (%s [%d,%d] %s %s %s) to mx (ilhs %d) \n", argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype], ilhs); fflush(0);
    if (argp->inout==GS_VAROUT) { 
      /* If VAROUT, find row and col value holders, get the value
       * and transfer those values to the argument */
      argp->rows = 1;
      argp->cols = 1;
      row_arg = gs_arg_lookup_by_name(argp->rowexp, problem->arglist);
      if (row_arg==NULL || row_arg->inout!=GS_OUT || row_arg->datatype!=GS_INT 
          || row_arg->objecttype!=GS_SCALAR) goto error_output;
      argp->rows = row_arg->scalar_val.int_val;
      col_arg = gs_arg_lookup_by_name(argp->colexp, problem->arglist);
      if (col_arg==NULL) 
        argp->cols = 1;
      else {        /* if col_arg != NULL */
        if ( col_arg->inout!=GS_OUT || col_arg->datatype!=GS_INT
         || col_arg->objecttype!=GS_SCALAR) goto error_output;
        argp->cols = col_arg->scalar_val.int_val;
      }
    }
    plhs[ilhs] = transfer_gs_to_mx(argp);
    if (plhs[ilhs] == NULL) return -1;
    break;
    
      default:
    DBGPRINTF("Unknown object type name %s type %d\n", argp->name, argp->objecttype);
    goto error_output;
    break;
      }

      /* Increment index for output arguments */
      ilhs++;           

    }
  }
  
  return 0;

 error_output:
  ERRPRINTF("GridSolve: Could not transfer output to Matlab (output arg %d) for GridSolve output (arg %s type %s)", ilhs, argp->name, gs_c_datatype[argp->datatype]);
  return -1;

}

Here is the call graph for this function:

Here is the caller graph for this function:

int matlab_gs_setup_args ( gs_problem_t *  problem,
int  nlhs,
mxArray *  plhs[],
int  nrhs,
const mxArray *  prhs[] 
)

Setup all arguments provided from Matlab.

Covert arguments that need to be passed to a GridSolve service which requires the arguments to have other specific types. Datatypes are converted from Matlab to GridSolve as needed. So we need to get the problem description from the agent, and use it to calloc/update/copy the arguments and thus fix the storage-type information.

Parameters:
nlhs - Number of left hand side arguments from Matlab
plhs - Pointer to left hand side arguments from Matlab
rlhs - Number of right hand side arguments from Matlab
plhs - Pointer to right hand side arguments from Matlab
Returns:
0 if OK or -1 if error

Definition at line 819 of file matlab_gs_utils.c.

{
  gs_argument_t* argp = NULL;
  double *pr = NULL;
  double *pi = NULL;
  mxChar *mxcharp = NULL;
  int nelements;
  int i,j,m,n,irhs=0;
  char buffer[256];
  FILE *fp;
  char *tmpname;
  char **filenames = NULL;
  gs_argument_t* arg_indices = NULL;
  gs_argument_t* arg_pointer = NULL;
  int *ir = NULL;
  int *jc = NULL;
  int nnz;
  
  if (problem==NULL) goto error_unknown;

  /* TODO Check that we have correct number of input and output args */

  /* Setup scalar arguments, copying from Matlab as needed */
  if (matlab_gs_setup_scalar_arguments(problem, nlhs, plhs, nrhs, prhs) < 0) goto error_scalar;

  /* Compute sizes for all other arguments using scalar arguments */
  if (gs_receiver_compute_arg_sizes(problem, GS_IN) < 0) {
    ERRPRINTF("GridSolve: Could not compute argument sizes");  
    return -1;
  }
  
  /* For each non-scalar argument, check datatype, and attach data to the argp->data */
  DBGPRINTF("For each argument, check datatype, attach data for non-scalar args\n");
  for (argp=problem->arglist,irhs=2; argp!=NULL && irhs<=nrhs; argp=argp->next) {
    
    /* skip this if it's a sparse matrix attribute, since the sparse
       attributes (for sparse arguments) (IN, INOUT or OUT) are not
       sent from matlab, they will be handled when the actual sparse
       argument is handled  */
    if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) {
      DBGPRINTF("Skipping sparse attribute: %s \n", argp->name);
      continue;
    }

    /* ***************************** */
    if (argp->inout==GS_IN || argp->inout==GS_INOUT) {
    /* ***************************** */

      m = mxGetM(prhs[irhs]); 
      n = mxGetN(prhs[irhs]); 
      pr = mxGetPr(prhs[irhs]); 
      pi = mxGetPi(prhs[irhs]); 

      /* Setup input non scalars, copying args from Matlab to GridSolve, allocating space for arguments */
      switch (argp->objecttype) {

      case GS_SCALAR:
    /* Nothing to do for scalars, they are already handled */
    break;

      case GS_SPARSEMATRIX:
    DBGPRINTF("Transfer mx (rhs %d) to gs (%s [%d,%d] %s %s %s) \n", irhs-2, argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype]); fflush(0);
    nnz = mxGetNzmax(prhs[irhs]);
    /* Lookup which gridsolve arg should hold indices and pointers */
    arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, problem->arglist);
    arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, problem->arglist);
    if (arg_indices==NULL || arg_pointer==NULL) return -1;
    /* Allocate space for indices, pointers, data */
    if ((arg_indices->data = matlab_gs_calloc(nnz, sizeof(int))) == NULL) return -1;
    if ((arg_pointer->data = matlab_gs_calloc(n+1, sizeof(int))) == NULL) return -1;
    if ((argp->data = matlab_gs_calloc(nnz, gs_datatype_sizeof(argp->datatype))) == NULL) return -1;
    /* Copy CSC to CSR */
    ir = mxGetIr(prhs[irhs]);
    jc = mxGetJc(prhs[irhs]);
    if (matlab_gs_sparse_matcsc_to_csr(m, n, nnz, mxGetClassID(prhs[irhs]), pr, pi, ir, jc, argp->datatype, argp->data, arg_indices->data, arg_pointer->data) == -1) goto error_input;
    break;
    
      case GS_MATRIX:
      case GS_VECTOR:
    DBGPRINTF("Transfer mx (rhs %d) to gs (%s [%d,%d] %s %s %s) \n", irhs-2, argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype]); fflush(0);
    nelements = argp->rows*argp->cols;

    if (arg_data_same_as_mx_dat(prhs[irhs], argp)) {
      /* If argument datatypes match then we can simply point to
       * the input provided by Matlab */
      argp->data = pr;
    } else {
      /* Copy data from Matlab to GridSolve */
      switch (argp->datatype) {
      case GS_DOUBLE:
        if (transfer_mx_to_gs_double(mxGetClassID(prhs[irhs]), pr, 0, &argp->data, 0, nelements, MAYBE_ALLOC) < 0)  goto error_input;
        break;
      case GS_INT:
        if (transfer_mx_to_gs_int(mxGetClassID(prhs[irhs]), pr, 0, &argp->data, 0, nelements, MAYBE_ALLOC) < 0) goto error_input;
        break;
      case GS_FLOAT:
        if (transfer_mx_to_gs_float(mxGetClassID(prhs[irhs]), pr, 0, &argp->data, 0, nelements, MAYBE_ALLOC) < 0) goto error_input;
        break;
      case GS_SCOMPLEX:
        if (transfer_mx_to_gs_scomplex(mxGetClassID(prhs[irhs]), pr, pi, 0, &argp->data, 0, nelements, MAYBE_ALLOC) < 0) goto error_input;
        break;
      case GS_DCOMPLEX:
        if (transfer_mx_to_gs_dcomplex(mxGetClassID(prhs[irhs]), pr, pi, 0, &argp->data, 0, nelements, MAYBE_ALLOC) < 0) goto error_input;
        break;
      case GS_CHAR:
        if ((argp->data = (void*)matlab_gs_calloc(nelements, gs_datatype_sizeof(argp->datatype)))==NULL) goto error_input;
        for (j=0; j<nelements; j++) ((char*)argp->data)[j] = (char)((mxChar*)mxGetData(prhs[irhs]))[j];
        break;
      default:
        DBGPRINTF("Datatype not handled\n");
        break;          
      } /* switch(argp->datatype) */
    } /* else arg_data_same_as_mx_dat */
    break;

      case GS_FILE:
    DBGPRINTF("Transfer mx (rhs %d) to gs (%s [%d,%d] %s %s %s) \n", irhs-2, argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype]); fflush(0);
    if (!mxIsChar(prhs[irhs])) {
      ERRPRINTF("Arg %s is of class %s but should be a string\n", argp->name, mxGetClassName(prhs[irhs]));
      goto error_input;
    }
    if ((argp->data = (char *)matlab_gs_calloc(m*n+1, sizeof(char))) == NULL) return -1;
    mxGetString(prhs[irhs], argp->data, n*m+1);
    break;
    
      case GS_PACKEDFILE:
    DBGPRINTF("Transfer mx (rhs %d) to gs (%s [%d,%d] %s %s %s) \n", irhs-2, argp->name, argp->rows, argp->cols, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype]); fflush(0);
    if (!mxIsChar(prhs[irhs])) {
      ERRPRINTF("Arg %s is of class %s but should be a matrix of strings\n", argp->name, mxGetClassName(prhs[irhs]));
      goto error_input;
    }
    mxcharp = (mxChar*)mxGetPr(prhs[irhs]);
    if ((filenames = (char **)matlab_gs_calloc(m, sizeof(char*))) == NULL) return -1;
    for (i=0; i<m; i++) {
      if ((filenames[i] = (char *)matlab_gs_calloc(n+1, sizeof(char))) == NULL) return -1;
      /* Copy over full name, going from column major to row major */
      for (j=0; j<n; j++) filenames[i][j] = (char)((char)mxcharp[i+j*m]);
      /* Make last char blank */
      filenames[i][n] = ' ';
      /* Backing up from end, get rid of blanks */
      for (j=n; j>0; j++) 
        if (filenames[i][j] == ' ') 
          filenames[i][j] = '\0';
        else break;
      DBGPRINTF("Packed input/inout file %d **%s** \n", i, filenames[i]);
    }
    argp->data = filenames;
    break;
    
      default:
    ERRPRINTF("Unknown objectype\n");   
    goto error_input;
    break;
    
      } /* argp->objectype */
      
      /* increment irhs index */
      irhs++;           
      /* increment irhs index */
      
      
      /* ***************************** */
    } else if (argp->inout==GS_WORKSPACE) {
      /* ***************************** */

      /* Do nothing, just move to next argument */
      argp->data = NULL;

      /* ***************************** */
    } else if (argp->inout==GS_OUT) {
      /* ***************************** */
      
      DBGPRINTF("Handling GS_OUT: %s %s %s %s \n", argp->name, gs_inout[argp->inout], gs_objecttype[argp->objecttype], gs_c_datatype[argp->datatype]); fflush(0);
      
      /* Setup output non scalars, allocating space in GridSolve as needed */
      switch (argp->objecttype) {
    
      case GS_SCALAR:
    break;
    
      case GS_SPARSEMATRIX:
    DBGPRINTF("Attaching output sparse attributes \n");
    /* Lookup which gridsolve arg should hold indices and pointers */
    arg_indices = gs_arg_lookup_by_name(argp->sparse_attr.indices, problem->arglist);
    arg_pointer = gs_arg_lookup_by_name(argp->sparse_attr.pointer, problem->arglist);
    if (arg_indices==NULL || arg_pointer==NULL) goto error_output;
    /* Allocate space for indices, pointer and data */
    if ((arg_indices->data = matlab_gs_calloc(argp->sparse_attr.nnz, sizeof(int))) == NULL) goto error_memory;
    if ((arg_pointer->data = matlab_gs_calloc(argp->sparse_attr.cols_val_saved+1, sizeof(int))) == NULL) goto error_memory;
    if ((argp->data = matlab_gs_calloc(argp->sparse_attr.nnz, gs_datatype_sizeof(argp->datatype))) == NULL) goto error_memory;
    break;
    
      case GS_VECTOR:
      case GS_MATRIX:
    /* Compute number of elements to be allocated and allocate space for output */
    nelements = argp->rows*argp->cols;
    if ((argp->data = (double*)matlab_gs_calloc(nelements, gs_datatype_sizeof(argp->datatype)))==NULL) goto error_memory;
    break;
    
      case GS_FILE: 
    /* In matlab, the client does not pass in the GS_FILE name for
       an output argument, so use problem_arg_XXXXXX as the
       filename */
    snprintf(buffer, 256, "./%s_%s_XXXXXX", problem->name, argp->name);
    tmpname = mktemp(buffer);
    if (tmpname == NULL) { ERRPRINTF("Could not make tmp file\n"); goto error_output; }
    fp = fopen(tmpname, "w");
    if (fp == NULL) { ERRPRINTF("Could not make tmp file\n"); goto error_output; }
    fclose(fp);
    argp->data = strdup(buffer);
    if (argp->data==NULL) goto error_memory;
    DBGPRINTF("output file name %s\n", (char*)argp->data);
    break;
    
      case GS_PACKEDFILE:
    /* In matlab, the client does not pass in the GS_FILE name for
       an output argument, so use problem_arg_%d_XXXXXX as the
       filename */
    nelements = argp->rows*argp->cols;
    filenames = (char **)matlab_gs_calloc(MAX_FILES_IN_PACKEDFILE, sizeof(char*));
    /* TODO Free memory */
    if (filenames==NULL) goto error_memory;
    for (i=0; i<nelements; i++) {
      snprintf(buffer, 256, "./%s_%s_%d_XXXXXX", problem->name, argp->name, i+1);
      tmpname = mktemp(buffer);
      if (tmpname == NULL) { ERRPRINTF("Could not make tmp file\n"); goto error_output; }
      fp = fopen(tmpname, "w");
      if (fp == NULL) { ERRPRINTF("Could not make tmp file\n"); goto error_output; }
      fclose(fp);
      filenames[i] = strdup(buffer);
      if (filenames[i]==NULL) goto error_memory;
      DBGPRINTF("packed output filename %d %s\n", i, filenames[i]);
    }
    argp->data = filenames;
    break;
    
      default:
    DBGPRINTF("Object type not handled %d\n", argp->objecttype);
    break;
      } /* switch(argp->objectype) */
      

      /* ***************************** */
    } else if (argp->inout==GS_VAROUT) {
      /* ***************************** */
      
      switch (argp->objecttype) {
      case GS_SPARSEMATRIX: 
    ERRPRINTF("VAROUT SPARSEMATIX is not supported \n");
    return -1;
    break;
      default:
    /* If VAROUT, create a (char **) and use it as the arguments data space  */
    argp->data = (char **)matlab_gs_calloc(1,sizeof(char*));
    if (!argp->data) goto error_memory;
    break;
      } /* switch objectype */
      
      /* ***************************** */
    } else {
      /* ***************************** */

      ERRPRINTF("Unknown inout mode\n");
      return -1;
    }
    
  }
  DBGPRINTF("Done\n");
  return 0;


 error_scalar:
  ERRPRINTF("GridSolve: Could not transfer scalar arguments");
  return -1;

 error_input:
  ERRPRINTF("GridSolve: Data conversion problem between matlab input data (arg %d type %s) and GridSolve (arg %s type %s)", irhs-2, mxGetClassName(prhs[irhs]), argp->name, gs_c_datatype[argp->datatype]);
  return -1;

 error_output:
  ERRPRINTF("GridSolve: Data preparation problem for GridSolve output (arg %s type %s)", argp->name, gs_c_datatype[argp->datatype]);
  return -1;

 error_memory:
  ERRPRINTF("GridSolve: Memory allocation problem for GridSolve argument (arg %s type %s)", argp->name, gs_c_datatype[argp->datatype]);
  return -1;

 error_unknown:
  ERRPRINTF("GridSolve: Unknown error occured");
  return -1;

}

Here is the call graph for this function:

Here is the caller graph for this function:

static int matlab_gs_setup_scalar_arguments ( gs_problem_t *  problem,
int  nlhs,
mxArray *  plhs[],
int  nrhs,
const mxArray *  prhs[] 
) [static]

Copy scalar arguments from Matlab to the problem structure.

After this is done, the scalar arguments can be used to try to determine the sizes of all the non-scalars, so we can tell how much data is to be transferred.

problem -- Pointer to problem structure

Parameters:
nlhs -- Num of left hand side arguments from Matlab
plhs -- Array of left hand side arguments (mxArray) from Matlab
nrhs -- Num of right hand side arguments from Matlab
nrhs -- Array of right hand side arguments (mxArray) from Matlab
Returns:
0 if OK, -1 on error

Definition at line 702 of file matlab_gs_utils.c.

{
  gs_argument_t* argp = NULL;
  double *pr = NULL;
  double *pi = NULL;
  int irhs = 0;

  for (argp=problem->arglist,irhs=2; argp!=NULL && irhs<=nrhs; argp=argp->next) {
    
    /* Skip sparse indices and pointers, scalar nnz values must be provided */
    if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) {
      DBGPRINTF("Skipping sparse attribute: %s \n", argp->name);
      continue;
    }

    /* Grab scalar input values so that we can calulate sizes of non scalars */
    if (argp->inout==GS_IN || argp->inout==GS_INOUT ) {
      switch (argp->objecttype) {
      case GS_SCALAR:
    DBGPRINTF("Transfer scalar Matlab arg %d (%s %d) to GridSolve arg (%s %s)\n", irhs-2, mxGetClassName(prhs[irhs]), mxGetNumberOfElements(prhs[irhs]),argp->name, gs_c_datatype[argp->datatype]);
    if (mxGetNumberOfElements(prhs[irhs])!=1) { DBGPRINTF("numelements mismatch\n"); goto error; }
    pr = mxGetPr(prhs[irhs]); 
    pi = mxGetPi(prhs[irhs]); 
    argp->data = &(argp->scalar_val);
    switch (argp->datatype) {
    case GS_DOUBLE:
      if (transfer_mx_to_gs_double(mxGetClassID(prhs[irhs]), pr, 0, (void **)&(argp->data), 0, 1, DO_NOT_ALLOC)<0) goto error;
      break;
    case GS_INT:
      if (transfer_mx_to_gs_int(mxGetClassID(prhs[irhs]), pr, 0, (void **)&(argp->data), 0, 1, DO_NOT_ALLOC)<0) goto error;
      break;
    case GS_FLOAT:
      if (transfer_mx_to_gs_float(mxGetClassID(prhs[irhs]), pr, 0, (void **)&(argp->data), 0, 1, DO_NOT_ALLOC)<0) goto error;
      break;
    case GS_SCOMPLEX:
      if (transfer_mx_to_gs_scomplex(mxGetClassID(prhs[irhs]), pr, pi, 0, (void **)&(argp->data), 0, 1, DO_NOT_ALLOC)<0) goto error;
      break;
    case GS_DCOMPLEX:
      if (transfer_mx_to_gs_dcomplex(mxGetClassID(prhs[irhs]), pr, pi, 0, (void **)&(argp->data), 0, 1, DO_NOT_ALLOC)<0) goto error;
      break;
    case GS_CHAR:
      argp->scalar_val.char_val = (char)mxGetScalar(prhs[irhs]);
      break;
    default:
      argp->data = NULL;
      ERRPRINTF("GridSolve datatype (%s) is not handled yet\n", gs_c_datatype[argp->datatype]);
      goto error;
      break;
    }
    break;
      default:
    DBGPRINTF("Vector, matrix or other non-scalar input (rhs arg %d name %s )\n", irhs-2, argp->name);
    break;
      }
      irhs++;
      
    } else if (argp->inout==GS_OUT) {
      /* For scalar output, the argp->data will point to arg->scalar_val */
      switch (argp->objecttype) {
      case GS_SCALAR:
    DBGPRINTF("Scalar output (name %s)\n", argp->name);
    argp->data = &(argp->scalar_val);
    break;
      default:
    DBGPRINTF("Non scalar output (arg %d name %s )\n", irhs-2, argp->name);
    break;
      }
      
    } else if (argp->inout==GS_VAROUT) {
      /* For scalar varout, the argp->data will point to arg->scalar_val */
      switch (argp->objecttype) {
      case GS_SCALAR:
    ERRPRINTF("GridSolve scalars arguments cannot be marked as VAROUT in the interface (matlab input arg %d)\n", irhs-2);
    goto error;
    break;
      default:
    DBGPRINTF("Non scalar varout (arg %d name %s )\n", irhs-2, argp->name);
    break;
      }

    } else if (argp->inout==GS_WORKSPACE) {
      /* Do nothing, just move to next argument */
      argp->data = NULL;
      
    } else {
      ERRPRINTF("GridSolve interface in/out type is unknown (not IN, OUT, INOUT, or VAROUT or WORKSPACE) (matlab input argument %d) \n", irhs-2);
      goto error;
    }
  }
  return 0;

 error:
  ERRPRINTF("GridSolve: Scalar argument conversion problem: Matlab arg num %d (type %s size %d) could not be converted to GridSolve arg (name %s type %s)\n", irhs-2, mxGetClassName(prhs[irhs]), mxGetNumberOfElements(prhs[irhs]),argp->name, gs_c_datatype[argp->datatype]);
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int matlab_gs_sparse_csr_to_matcsc ( int  m,
int  n,
int  nnz,
int  datatype,
void *  a,
int *  colind,
int *  rowptr,
double *  at,
double *  ati,
int *  rowind,
int *  colptr 
) [static]

Convert sparse matrix from GridSolve (CSR) to Matlab (CSC) formats.

Copy a sparse matrix from GridSolve (Compressed-Sparse-Row) to Matlab (Compressed-Sparse-Column). If the data is complex, it may have to go from a vector(real,imag) format to a (real_vector,imag_vector) format. This routine is taken from NetSolve and may have some earlier history. The GridSolve data is coerced so that the returned Matlab sparse array will be double precision.

Parameters:
m -- Num of rows (input)
n -- Num of cols (input)
nnz -- Num of non-zeros (input)
datatype -- GridSolve datatype (GS_INT, GS_FLOAT, ..) for the output data (input)
a -- Vector of data values; may be complex structs or another datatype (input)
colind -- Vector of col indices (input)
rowptr -- Vector of row ptrs (input)
a -- Vector of real data values of a for Matlab (output)
ati -- Vector (possibly empty) of imaginary data values of for Matlab (output)
rowind -- Vector of row indices for Matlab (output)
colptr -- Vector of col pointers for Matlab (output)
Returns:
0 on succeess, -1 on failure

Definition at line 626 of file matlab_gs_utils.c.

{
  register int i, j, col, relpos;
  int *marker;

  marker = (int *)matlab_gs_calloc(n, sizeof(int));
  if (marker == NULL) return -1;
 
  /* Get counts of each column of A, and set up column pointers */
  for (i = 0; i < m; ++i)
    for (j = rowptr[i]; j < rowptr[i+1]; ++j) ++marker[colind[j]];

  colptr[0] = 0;

  for (j = 0; j < n; ++j) {
    colptr[j+1] = colptr[j] + marker[j];
    marker[j] = colptr[j];
  }
 
  /* Transfer the matrix into the compressed column storage. */
  for (i = 0; i < m; ++i) {
    for (j = rowptr[i]; j < rowptr[i+1]; ++j) {
      col = colind[j];
      relpos = marker[col];
      rowind[relpos] = i;
      
      switch(datatype) {
      case GS_INT:
    ((double *)at)[relpos] = (double)((int *)a)[j];
    break;
      case GS_FLOAT:
    ((double *)at)[relpos] = (double)((float *)a)[j];
    break;
      case GS_DOUBLE:
    ((double *)at)[relpos] = ((double *)a)[j];
    break;
      case GS_SCOMPLEX:
    ((double *)at)[relpos] = (double)(((gs_scomplex *)a)[j].r);
    ((double *)ati)[relpos] = (double)(((gs_scomplex *)a)[j].i);
    break;
      case GS_DCOMPLEX:
    ((double *)at)[relpos] = (double)((gs_dcomplex *)a)[j].r;
    ((double *)ati)[relpos] = (double)((gs_dcomplex *)a)[j].i;
    break;
      default:
    ERRPRINTF("GridSolve sparse of datatype (%s) not supported \n", gs_c_datatype[datatype]);
    /* Return an error  */
    return -1;
    break;
      }
      ++marker[col];
    }
  }
  marker = matlab_gs_free(marker);
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int matlab_gs_sparse_matcsc_to_csr ( int  m,
int  n,
int  nnz,
int  mxclassid,
void *  ar,
void *  ai,
int *  colind,
int *  rowptr,
int  gsdatatype,
void *  gsat,
int *  rowind,
int *  colptr 
) [static]

Convert sparse matrix from Matlab (CSC) to GridSolve (CSR) formats.

Copy a sparse matrix from Matlab (Compressed-Sparse-Column) to GridSolve (Compressed-Sparse-Row). If the data is complex, it may have to go from a (vector_of_real,vector_of_imag) format to a vector(real,imag) format. This routine is taken from NetSolve and may have some earlier history. Datatypes will be coerced from the Matlab type to the desired GridSolve type if possible.

Parameters:
m -- Num of rows (input)
n -- Num of cols (input)
nnz -- Num of non-zeros (input)
mxclassid -- Class id for the Matlab data (eg double float int8 uint8...)
ar -- Vector of real data values of a from Matlab (input)
ai -- Vector (possibly empty) of imaginary data values of a from Matlab (input)
colind -- Vector of column indices from Matlab (input)
rowptr -- Vector of row pointers from Matlab (input)
gsdatatype -- GridSolve datatype (GS_INT, GS_FLOAT, ..) for the output data (input)
gsat -- Output vector of GS values; may be complex structs (output) or another datatype
rowind -- Vector of row indices for CSR format (output)
colptr -- Vector of col ptrs for CSR format (output)
Returns:
0 on succeess, -1 on failurea

Definition at line 535 of file matlab_gs_utils.c.

{
  register int i, j, col, relpos;
  int *marker;
  
  marker = (int *) matlab_gs_calloc(n, sizeof(int));
  if (marker == NULL) return -1;
 
  /* Get counts of each column of A, and set up column pointers */
  for (i = 0; i < m; ++i)
    for (j = rowptr[i]; j < rowptr[i+1]; ++j) 
      ++marker[colind[j]];
  
  colptr[0] = 0;

  for (j = 0; j < n; ++j) {
    colptr[j+1] = colptr[j] + marker[j];
    marker[j] = colptr[j];
  }
 
  /* Transfer the matrix into the compressed column storage. */
  for (i = 0; i < m; ++i) {
    for (j = rowptr[i]; j < rowptr[i+1]; ++j) {
      col = colind[j];
      relpos = marker[col];
      rowind[relpos] = i;

      /* Copy one data item at a time */
      switch(gsdatatype) {
      case GS_INT:
    /* Transfer 1 item from ar (starting at j) to gsat (starting at relpos) */
    if (transfer_mx_to_gs_int(mxclassid, ar, j, &gsat, relpos, 1, DO_NOT_ALLOC) == -1) goto error;
    break;
      case GS_FLOAT:
    if (transfer_mx_to_gs_float(mxclassid, ar, j, &gsat, relpos, 1, DO_NOT_ALLOC) == -1) goto error;
    break;
      case GS_DOUBLE:
    if (transfer_mx_to_gs_double(mxclassid, ar, j, &gsat, relpos, 1, DO_NOT_ALLOC) == -1) goto error;
    break;
      case GS_SCOMPLEX:
    if (transfer_mx_to_gs_scomplex(mxclassid, ar, ai, j, &gsat, relpos, 1, DO_NOT_ALLOC) == -1) goto error;
    break;
      case GS_DCOMPLEX:
    if (transfer_mx_to_gs_dcomplex(mxclassid, ar, ai, j, &gsat, relpos, 1, DO_NOT_ALLOC) == -1) goto error;
    break;
      default:
    ERRPRINTF("GridSolve sparse of datatype (%s) not supported \n", gs_c_datatype[gsdatatype]);
    /* Return an error  */
    return -1;
    break;
      }

      ++marker[col];
    }
  }
  marker = matlab_gs_free(marker);
  return 0;

 error:
  ERRPRINTF("Could not convert matlab data to GridSolve sparse CSR format (%s)\n", gs_c_datatype[gsdatatype]);
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static mxArray* transfer_gs_to_mx ( gs_argument_t *  argp  )  [static]

Transfer GridSolve data to Matlab.

Allocate the Matlab array in double precision, and then copy data over from the GridSolve argument.

Parameters:
argp -- GridSolve argument pointer
Returns:
0 on failure, 1 on success

Definition at line 413 of file matlab_gs_utils.c.

{
  int nelements, N, M, j;
  mxArray *mxarray;
  void *datap;

  if (argp->inout==GS_VAROUT) 
    datap = *((void **)argp->data);
  else 
    datap = argp->data;

  nelements = argp->rows*argp->cols; 
  M = argp->rows;
  N = argp->cols;

  switch (argp->datatype) {
  case GS_DOUBLE:
    if ((mxarray = matlab_gs_create_matrix(argp->objecttype, M, N, nelements, argp->datatype)) == NULL) goto error_output;
    for (j=0; j<nelements; j++) 
      mxGetPr(mxarray)[j] = ((double*)((double *)datap))[j];
    break;
  case GS_INT:
    if ((mxarray = matlab_gs_create_matrix(argp->objecttype, M, N, nelements, argp->datatype)) == NULL) goto error_output;
    for (j=0; j<nelements; j++) 
      mxGetPr(mxarray)[j] = (double)(((int*)(datap))[j]);
    break;
  case GS_FLOAT:
    if ((mxarray = matlab_gs_create_matrix(argp->objecttype, M, N, nelements, argp->datatype)) == NULL) goto error_output;
    for (j=0; j<nelements; j++) 
      mxGetPr(mxarray)[j] = (double)(((float*)(datap))[j]);
    break;
  case GS_SCOMPLEX:
    if ((mxarray = matlab_gs_create_matrix(argp->objecttype, M, N, nelements, argp->datatype)) == NULL) goto error_output;
    for (j=0; j<nelements; j++) {
      mxGetPr(mxarray)[j] = (double)(((gs_scomplex*)(datap))[j].r);
      mxGetPi(mxarray)[j] = (double)(((gs_scomplex*)(datap))[j].i);
    }
    break;
  case GS_DCOMPLEX:
    if ((mxarray = matlab_gs_create_matrix(argp->objecttype, M, N, nelements, argp->datatype)) == NULL) goto error_output;
    for (j=0; j<nelements; j++) {
      mxGetPr(mxarray)[j] = (double)(((gs_dcomplex*)(datap))[j].r);
      mxGetPi(mxarray)[j] = (double)(((gs_dcomplex*)(datap))[j].i);
    }
    break;
  case GS_CHAR:
  {
    mxChar *charData;
    int dims[2] = {argp->rows, argp->cols};
    if ((mxarray = mxCreateCharArray(2, dims)) == NULL) goto error_output;
    charData = (mxChar *)mxGetData(mxarray);
    for(j=0; j<argp->rows*argp->cols; j++) 
      charData[j] = (mxChar)((char*)datap)[j]; 
    break;
  }
  default:
    ERRPRINTF("Unknown datatype %d\n", argp->datatype);
    goto error_output;
    break;
  }
  return mxarray;

 error_output:
  ERRPRINTF("GridSolve: Error transfering  GridSolve output (arg %s type %s) to Matlab", argp->name, gs_c_datatype[argp->datatype]);
  return NULL;


}

Here is the call graph for this function:

Here is the caller graph for this function:

static int transfer_mx_to_gs_dcomplex ( mxClassID  mxclassid,
void *  pr,
void *  pi,
int  poffset,
void **  datapp,
int  doffset,
int  nelem,
gs_alloc_t  allocp 
) [static]

Transfer Matlab array to GridSolve complex data.

Parameters:
mxclassid -- Matlab datatype/class
pr -- Pointer to Matlab real data
pi -- Pointer to Matlab imaginary data
poffset -- Offset to start transfer (needed for sparse transfer)
datatpp -- GridSolve data (pointer to pointer, so that memory allocation can be done)
doffset -- Offset to start transfer (needed for sparse transfer)
nelem -- Number of elements to be transferred
allocp -- Should data be allocated?
Returns:
0 on failure, 1 on success

Definition at line 369 of file matlab_gs_utils.c.

{
  /* Allocate space, and copy data over to it */
  if (allocp != DO_NOT_ALLOC) 
    if ((*datapp = (gs_dcomplex*)matlab_gs_calloc(nelem, sizeof(gs_dcomplex)))==NULL) return -1; 
  
  if (mxclassid == mxDOUBLE_CLASS) 
    COPY_MXARRAY_TO_GSCOMPLEX(double, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxSINGLE_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(float, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxINT8_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(int8_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxUINT8_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(uint8_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxINT16_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(int16_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxUINT16_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(uint16_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxINT32_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(int32_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
  else if (mxclassid == mxUINT32_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(uint32_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem);
/*   else if (mxclassid == mxINT64_CLASS) */
/*     COPY_MXARRAY_TO_GSCOMPLEX(int64_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem); */
/*   else if (mxclassid == mxUINT64_CLASS) */
/*     COPY_MXARRAY_TO_GSCOMPLEX(uint64_t, pr, pi, poffset, gs_dcomplex, *datapp, doffset, double, nelem); */
  else {
    ERRPRINTF("Could not convert matlab data to GridSolve data type (double precision complex)\n");
    return -1;
  }
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int transfer_mx_to_gs_double ( mxClassID  mxclassid,
void *  pr,
int  poffset,
void **  datapp,
int  doffset,
int  nelem,
gs_alloc_t  allocp 
) [static]

Transfer Matlab array to GridSolve data.

Parameters:
mxclassid -- Matlab datatype/class
pr -- Pointer to Matlab real data
poffset -- Offset to start transfer (needed for sparse transfer)
datatpp -- GridSolve data (pointer to pointer, so that memory allocation can be done)
doffset -- Offset to start transfer (needed for sparse transfer)
nelem -- Number of elements to be transferred
allocp -- Should data be allocated?
Returns:
0 on failure, 1 on success

Definition at line 226 of file matlab_gs_utils.c.

{
  /* Allocate space if needed, and copy data over to it */
  if (allocp != DO_NOT_ALLOC) 
    if ((*datapp = (double*)matlab_gs_calloc(nelem, sizeof(double)))==NULL) return -1;

  if (mxclassid == mxDOUBLE_CLASS) 
    COPY_A_TO_B(double, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxSINGLE_CLASS)
    COPY_A_TO_B(float, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxINT8_CLASS)
    COPY_A_TO_B(int8_t, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT8_CLASS)
    COPY_A_TO_B(uint8_t, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxINT16_CLASS)
    COPY_A_TO_B(int16_t, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT16_CLASS)
    COPY_A_TO_B(uint16_t, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxINT32_CLASS)
    COPY_A_TO_B(int32_t, pr, poffset, double, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT32_CLASS)
    COPY_A_TO_B(uint32_t, pr, poffset, double, *datapp, doffset, nelem);
/*   else if (mxclassid == mxINT64_CLASS) */
/*     COPY_A_TO_B(int64_t, pr, poffset, double, *datapp, doffset, nelem); */
/*   else if (mxclassid == mxUINT64_CLASS) */
/*     COPY_A_TO_B(uint64_t, pr, poffset, double, *datapp, doffset, nelem); */
  else {
    ERRPRINTF("Could not convert matlab data to GridSolve data type (double)\n");
    return -1;
  }

  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int transfer_mx_to_gs_float ( mxClassID  mxclassid,
void *  pr,
int  poffset,
void **  datapp,
int  doffset,
int  nelem,
gs_alloc_t  allocp 
) [static]

Transfer Matlab array to GridSolve data.

Parameters:
mxclassid -- Matlab datatype/class
pr -- Pointer to Matlab real data
poffset -- Offset to start transfer (needed for sparse transfer)
datatpp -- GridSolve data (pointer to pointer, so that memory allocation can be done)
doffset -- Offset to start transfer (needed for sparse transfer)
nelem -- Number of elements to be transferred
allocp -- Should data be allocated?
Returns:
0 on failure, 1 on success

Definition at line 273 of file matlab_gs_utils.c.

{
  /* Allocate space if needed, and copy data over to it */
  if (allocp != DO_NOT_ALLOC) 
    if ((*datapp = (double*)matlab_gs_calloc(nelem, sizeof(float)))==NULL) return -1;

  if (mxclassid == mxDOUBLE_CLASS) 
    COPY_A_TO_B(double, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxSINGLE_CLASS)
    COPY_A_TO_B(float, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxINT8_CLASS)
    COPY_A_TO_B(int8_t, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT8_CLASS)
    COPY_A_TO_B(uint8_t, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxINT16_CLASS)
    COPY_A_TO_B(int16_t, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT16_CLASS)
    COPY_A_TO_B(uint16_t, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxINT32_CLASS)
    COPY_A_TO_B(int32_t, pr, poffset, float, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT32_CLASS)
    COPY_A_TO_B(uint32_t, pr, poffset, float, *datapp, doffset, nelem);
/*   else if (mxclassid == mxINT64_CLASS) */
/*     COPY_A_TO_B(int64_t, pr, poffset, float, *datapp, doffset, nelem); */
/*   else if (mxclassid == mxUINT64_CLASS) */
/*     COPY_A_TO_B(uint64_t, pr, poffset, float, *datapp, doffset, nelem); */
  else {
    ERRPRINTF("Could not convert matlab data to GridSolve data type (float)\n");
    return -1;
  }
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int transfer_mx_to_gs_int ( mxClassID  mxclassid,
void *  pr,
int  poffset,
void **  datapp,
int  doffset,
int  nelem,
gs_alloc_t  allocp 
) [static]

Transfer Matlab array to GridSolve data.

Parameters:
mxclassid -- Matlab datatype/class
pr -- Pointer to Matlab real data
poffset -- Offset to start transfer (needed for sparse transfer)
datatpp -- GridSolve data (pointer to pointer, so that memory allocation can be done)
doffset -- Offset to start transfer (needed for sparse transfer)
nelem -- Number of elements to be transferred
allocp -- Should data be allocated?
Returns:
0 on failure, 1 on success

Definition at line 178 of file matlab_gs_utils.c.

{
  /* Allocate space, and copy data over to it */
  if (allocp != DO_NOT_ALLOC) 
    if ((*datapp = (int*)matlab_gs_calloc(nelem, sizeof(int)))==NULL) return -1;
  
  if (mxclassid == mxDOUBLE_CLASS) 
    COPY_A_TO_B(double, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxSINGLE_CLASS)
    COPY_A_TO_B(float, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxINT8_CLASS)
    COPY_A_TO_B(int8_t, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT8_CLASS)
    COPY_A_TO_B(uint8_t, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxINT16_CLASS)
    COPY_A_TO_B(int16_t, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT16_CLASS)
    COPY_A_TO_B(uint16_t, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxINT32_CLASS)
    COPY_A_TO_B(int32_t, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxUINT32_CLASS)
    COPY_A_TO_B(uint32_t, pr, poffset, int, *datapp, doffset, nelem);
  else if (mxclassid == mxCHAR_CLASS)
    COPY_A_TO_B(char, pr, poffset, int, *datapp, doffset, nelem);
/*   else if (mxclassid == mxINT64_CLASS) */
/*     COPY_A_TO_B(int64_t, pr, poffset, int, *datapp, doffset, nelem); */
/*   else if (mxclassid == mxUINT64_CLASS) */
/*     COPY_A_TO_B(int64_t, pr, poffset, int, *datapp, doffset, nelem); */
  else {
    ERRPRINTF("Could not convert matlab data to GridSolve data type (int)\n");
    return -1;
  }
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int transfer_mx_to_gs_scomplex ( mxClassID  mxclassid,
void *  pr,
void *  pi,
int  poffset,
void **  datapp,
int  doffset,
int  nelem,
gs_alloc_t  allocp 
) [static]

Transfer Matlab array to GridSolve complex data.

Parameters:
mxclassid -- Matlab datatype/class
pr -- Pointer to Matlab real data
pi -- Pointer to Matlab imaginary data
poffset -- Offset to start transfer (needed for sparse transfer)
datatpp -- GridSolve data (pointer to pointer, so that memory allocation can be done)
doffset -- Offset to start transfer (needed for sparse transfer)
nelem -- Number of elements to be transferred
allocp -- Should data be allocated?
Returns:
0 on failure, 1 on success

Definition at line 321 of file matlab_gs_utils.c.

{
  /* Allocate space, and copy data over to it */
  if (allocp != DO_NOT_ALLOC) 
    if ((*datapp = (gs_scomplex*)matlab_gs_calloc(nelem, sizeof(gs_scomplex)))==NULL) return -1; 
  
  if (mxclassid == mxDOUBLE_CLASS) 
    COPY_MXARRAY_TO_GSCOMPLEX(double, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxSINGLE_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(float, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxINT8_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(int8_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxUINT8_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(uint8_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxINT16_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(int16_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxUINT16_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(uint16_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxINT32_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(int32_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
  else if (mxclassid == mxUINT32_CLASS)
    COPY_MXARRAY_TO_GSCOMPLEX(uint32_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem);
/*   else if (mxclassid == mxINT64_CLASS) */
/*     COPY_MXARRAY_TO_GSCOMPLEX(int64_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem); */
/*   else if (mxclassid == mxUINT64_CLASS) */
/*     COPY_MXARRAY_TO_GSCOMPLEX(uint64_t, pr, pi, poffset, gs_scomplex, *datapp, doffset, float, nelem); */
  else {
    ERRPRINTF("Could not convert matlab data to GridSolve data type (single precision complex)\n");
    return -1;
  }
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function: