#include <matrix.h>#include <mex.h>#include "portability.h"#include "grpc.h"

Go to the source code of this file.
Defines | |
| #define | DO_ERROR_MACRO(errnum) do { matlab_gs_error_num=errnum; matlab_gs_minor_errno=grpc_minor_errno; goto error; } while (0); |
Enumerations | |
| enum | matlab_gs_case { GS_CALL = 1, GS_CALL_ASYNC = 2, GS_WAIT = 3, GS_ERROR_STRING = 4, GS_GET_LAST_ERROR = 5, GS_PROBE = 6, GS_CANCEL = 7, GS_INFO = 8, GS_PUTENV = 9 } |
Functions | |
| void * | matlab_gs_calloc (size_t N, size_t S) |
| Allocate memory. | |
| void * | matlab_gs_free (void *ptr) |
| Free memory. | |
| 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[]) |
| void | matlab_gs_call (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_call_async (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_error_string (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_get_last_error (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_wait (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_probe (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_cancel (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_info (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_putenv (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| void | matlab_gs_probe_or (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
| 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. | |
Variables | |
| int | matlab_gs_error_num |
| int | matlab_gs_minor_errno |
Header file for the matlab client.
Definition in file matlab_gs.h.
| #define DO_ERROR_MACRO | ( | errnum | ) | do { matlab_gs_error_num=errnum; matlab_gs_minor_errno=grpc_minor_errno; goto error; } while (0); |
Definition at line 62 of file matlab_gs.h.
| enum matlab_gs_case |
| GS_CALL | |
| GS_CALL_ASYNC | |
| GS_WAIT | |
| GS_ERROR_STRING | |
| GS_GET_LAST_ERROR | |
| GS_PROBE | |
| GS_CANCEL | |
| GS_INFO | |
| GS_PUTENV |
Definition at line 57 of file matlab_gs.h.
{ GS_CALL=1, GS_CALL_ASYNC=2, GS_WAIT=3, GS_ERROR_STRING=4, GS_GET_LAST_ERROR=5, GS_PROBE=6, GS_CANCEL=7, GS_INFO=8, GS_PUTENV=9};
| 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.
| argp | -- Pointer to argument to check | |
| problem | -- Pointer to entire problem structure to check against. |
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;
}

| void matlab_gs_call | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Make a GridSolve synchronous call and return the output arguments.
| 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 |
Definition at line 35 of file matlab_gs_call.c.
{
int i;
grpc_function_handle_t *handle = NULL;
grpc_arg_stack *argstack = NULL;
char func_name[1024];
gs_argument_t *argp = NULL;
grpc_error_t status;
double t1;
int argcount = 0;
int rc;
/* reset global error setting */
matlab_gs_error_num = 0;
/* Left hand side should consist of one integer argument to hold the sessionid */
if (nrhs < 2) {
mexPrintf("Usage: [out, args, ...] = gs_call('service_name', ...) \n");
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
}
/* Initialize */
rc = grpc_initialize(NULL);
if ((rc != GRPC_NO_ERROR) && (rc != GRPC_ALREADY_INITIALIZED)) DO_ERROR_MACRO(grpc_errno);
/* Matlab uses column major indexing */
if (grpc_set_client_major("Column") != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
/* Get problem name from Matlab */
if (!mxIsChar(prhs[1])) DO_ERROR_MACRO(GRPC_FUNCTION_NOT_FOUND);
if (mxGetString(prhs[1],func_name,1024) != 0) DO_ERROR_MACRO(GRPC_FUNCTION_NOT_FOUND);
/* Get problem info from agent, along with a default server assignment */
t1 = walltime();
handle = (grpc_function_handle_t*)matlab_gs_calloc(1, sizeof(grpc_function_handle_t));
if (handle == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if ((status = grpc_function_handle_default(handle, func_name)) != GRPC_NO_ERROR) DO_ERROR_MACRO(status);
if (handle->problem_desc == NULL) DO_ERROR_MACRO(GRPC_INVALID_FUNCTION_HANDLE);
DBGPRINTF("Time for get handle %f \n ", walltime() - t1);
/* Fix arguments and their types, creating input and output space, and push args on stack */
t1 = walltime();
if (matlab_gs_setup_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0)
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
DBGPRINTF("Time for setup args from matlab %f \n ", walltime() - t1);
/* Push args on stack */
for (argcount=0,argp=handle->problem_desc->arglist; argp!=NULL; argp=argp->next)
if (argp->inout != GS_WORKSPACE) argcount++;
if ((argstack = grpc_arg_stack_new(argcount)) == NULL) DO_ERROR_MACRO(grpc_errno);
for (argp=handle->problem_desc->arglist; argp!=NULL; argp=argp->next) {
if (argp->inout != GS_WORKSPACE) /* skip workspace args */
if (grpc_arg_stack_push_arg(argstack, argp->data) < 0)
DO_ERROR_MACRO(grpc_errno);
}
/* Call the solve routine using stack interface */
t1 = walltime();
DBGPRINTF("Call the solve routine using grpc_call_arg_stack interface\n"); fflush(0);
status = grpc_call_arg_stack_ft(handle, argstack);
if (status != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
DBGPRINTF("Time for solve %f \n ", walltime() - t1);
/* Convert/transfer output to Matlab objects */
t1 = walltime();
DBGPRINTF("Call matlab_gs_get_output\n"); fflush(0);
if (matlab_gs_get_output(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0)
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
DBGPRINTF("Time for transfering output to matlab %f \n ", walltime() - t1);
/* Finalize */
DBGPRINTF("Clean up\n");
if (matlab_gs_free_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if (argstack && grpc_arg_stack_destruct(argstack) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
if (handle && grpc_function_handle_destruct(handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
handle = matlab_gs_free(handle);
return;
/* Error */
error:
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
if (argstack && grpc_arg_stack_destruct(argstack) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
if (handle && grpc_function_handle_destruct(handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
handle = matlab_gs_free(handle);
return;
}


| void matlab_gs_call_async | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Make a GridSolve asynchronous call and returns the status of the grpc_call_async (i.e. the requestID or -1 on error).
| 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 |
Definition at line 34 of file matlab_gs_call_async.c.
{
grpc_function_handle_t* handle = NULL;
char func_name[1024];
grpc_arg_stack* argstack = NULL;
gs_argument_t *argp = NULL;
grpc_sessionid_t sessionID;
grpc_error_t status;
int i;
int argcount = 0;
int rc;
/* reset global error setting */
matlab_gs_error_num = 0;
/* Left hand side should consist of one integer argument to hold the sessionid */
if ((nlhs != 1) || (nrhs < 2)) {
mexPrintf("Usage: [request_id] = gs_call_async('service_name', ...) \n");
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
}
/* Initialize */
rc = grpc_initialize(NULL);
if ((rc != GRPC_NO_ERROR) && (rc != GRPC_ALREADY_INITIALIZED)) DO_ERROR_MACRO(grpc_errno);
/* Matlab uses column major indexing */
if (grpc_set_client_major("Column") != GRPC_NO_ERROR)
DO_ERROR_MACRO(grpc_errno);
/* Get problem name from rhs */
if (!mxIsChar(prhs[1])) DO_ERROR_MACRO(GRPC_FUNCTION_NOT_FOUND);
if (mxGetString(prhs[1],func_name,1024) != 0) DO_ERROR_MACRO(GRPC_FUNCTION_NOT_FOUND);
/* Get problem info from agent, along with a default server assignment */
handle = (grpc_function_handle_t*)matlab_gs_calloc(1, sizeof(grpc_function_handle_t));
if (handle == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if ((status = grpc_function_handle_default(handle, func_name)) != GRPC_NO_ERROR) DO_ERROR_MACRO(status);
if (handle->problem_desc == NULL) DO_ERROR_MACRO(GRPC_INVALID_FUNCTION_HANDLE);
/* Setup input objects to be sent in the problem structure. NOTE,
we need to have the problem description here in order to convert
the matlab arguments (all double arrays) to the proper types for
the function, so we cannot use gridsolve style "bind servers at
call time" function handle creation */
if (matlab_gs_setup_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0)
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
/* Push args on stack */
for (argcount=0,argp=handle->problem_desc->arglist; argp!=NULL; argp=argp->next)
if (argp->inout != GS_WORKSPACE) argcount++;
if ((argstack = grpc_arg_stack_new(argcount)) == NULL) DO_ERROR_MACRO(grpc_errno);
for (argp=handle->problem_desc->arglist; argp!=NULL; argp=argp->next) {
if (argp->inout != GS_WORKSPACE) /* skip workspace args */
if (grpc_arg_stack_push_arg(argstack, argp->data) < 0)
DO_ERROR_MACRO(grpc_errno);
}
/* Call the solve routine using stack interface */
status = grpc_call_arg_stack_async_ft(handle, &sessionID, argstack);
if (status != GRPC_NO_ERROR) DO_ERROR_MACRO(status);
if (matlab_gs_free_input_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
/* Put the sessionID into the output */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)sessionID;
return;
/* Error */
error:
/* Return -1 as sessionID, as 0th lhs */
if (nlhs > 0) {
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double) -1;
}
/* Set rest of lhs */
for (i=1;i<nlhs;i++)
plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
if (matlab_gs_free_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if (argstack && grpc_arg_stack_destruct(argstack) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
if (handle && grpc_function_handle_destruct(handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
handle = matlab_gs_free(handle);
return;
}


| 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.
| N | -- Number of objects to be allocted | |
| S | -- Size of each object |
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;
}

| void matlab_gs_cancel | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Cancel an asynchronous call and returns the status of the grpc_cancel (i.e. O on OK or -1 on error).
| 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 |
Definition at line 34 of file matlab_gs_cancel.c.
{
int i;
grpc_sessionid_t sessionID;
int retval;
/* reset global error setting */
matlab_gs_error_num = 0;
/* Check arguments */
if (nrhs != 2 || nlhs != 1) {
mexErrMsgTxt("Usage: status = gs_cancel(sessionId)\n");
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
}
/* Get sessionid from rhs */
sessionID = (int)mxGetPr(prhs[1])[0];
if (sessionID < 0) {
matlab_gs_error_num = GRPC_OTHER_ERROR_CODE;
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
}
/* Call cancel */
retval = grpc_cancel(sessionID);
if (retval != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
/* If OK, output result, else error */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)retval;
return;
error:
/* Create blank output for left hand side */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)-1;
for (i=1;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


| void matlab_gs_error_string | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Return the string representation of a error_code in the first right hand side argument (calling grpc_error_string).
| 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 |
Definition at line 29 of file matlab_gs_error_string.c.
{
int i;
grpc_error_t error_num;
if ((nlhs != 1)||(nrhs != 2)) {
mexErrMsgTxt("Usage: str = gs_error_string(error_code)\n");
goto error;
}
error_num = (grpc_error_t)(*mxGetPr(prhs[0]));
plhs[0] = mxCreateString(grpc_error_string(error_num));
return;
error:
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


| void* matlab_gs_free | ( | void * | ptr | ) |
Free memory.
Free memory allocaed using matlab_gs_calloc.
| ptr | -- Pointer to memory |
Definition at line 84 of file matlab_gs_utils.c.
{
/* if ( ptr ) mxFree(ptr); */
if ( ptr ) free(ptr);
return NULL;
}

| 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.
| 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 |
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;
}


| 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.
| 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 |
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;
}


| void matlab_gs_get_last_error | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Return the error_code for the last error in the right hand side by calling grpc_get_last_error.
| 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 |
Definition at line 30 of file matlab_gs_get_last_error.c.
{
int i;
if ((nlhs != 1)||(nrhs != 1)) {
mexErrMsgTxt("Usage: num = gridsolve_errno\n");
goto error;
}
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
/* Use Matlab global error number */
mxGetPr(plhs[0])[0] = (double)matlab_gs_error_num;
return;
error:
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


| 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.
| 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 |
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;
}


| void matlab_gs_info | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Make a GridSolve synchronous call and returns the status of the grpc_call (0 on OK and -1 on error).
| 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 |
Definition at line 30 of file matlab_gs_info.c.
{
int i;
grpc_function_handle_t *handle = NULL;
char func_name[1024];
gs_argument_t *argp = NULL;
gs_problem_t *problem;
char *problem_encoding = NULL;
grpc_error_t status;
int first_arg;
char *str1 = NULL;
char *str2 = NULL;
int rc;
/* reset global error setting */
matlab_gs_error_num = 0;
/* Initialize */
rc = grpc_initialize(NULL);
if ((rc != GRPC_NO_ERROR) && (rc != GRPC_ALREADY_INITIALIZED)) DO_ERROR_MACRO(grpc_errno);
/* Matlab uses column major indexing */
if (grpc_set_client_major("Column") != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
/* Get some global information */
if (nrhs == 1) {
if (grpc_get_servers(&str1) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if (grpc_get_problems(&str2) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
mexPrintf("%s \n", str1);
mexPrintf("%s \n", str2);
free(str1);
free(str2);
} else if (nrhs == 2) {
/* Get problem name from Matlab */
if (!mxIsChar(prhs[1])) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if (mxGetString(prhs[1],func_name,1024) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
/* Get problem info from agent, along with a default server assignment */
/* TODO We want to avoid assigning server, because that will cause
the load to be temporarily increased as a heuristic */
handle = (grpc_function_handle_t*)matlab_gs_calloc(1, sizeof(grpc_function_handle_t));
if (handle == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if ((status=grpc_function_handle_default(handle, func_name)) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
if (handle->problem_desc == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
/* Write out problem description */
problem = handle->problem_desc;
/* Write out problem description */
if (gs_encode_problem(&problem_encoding, problem) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if (problem_encoding) {
mexPrintf("Description of call:\n");
mexPrintf("%s\n", problem_encoding);
}
free(problem_encoding); /* allocated by gridrpc so memory is not handled in matlab */
/* Write out problem description */
mexPrintf("Matlab call prototype:\n");
first_arg = 1;
mexPrintf("[ ");
for (argp=problem->arglist; argp!=NULL; argp=argp->next)
if (argp->inout == GS_OUT || argp->inout == GS_INOUT || argp->inout == GS_VAROUT) {
if (argp->inout == GS_WORKSPACE) continue;
if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) continue;
if (! first_arg) mexPrintf(",", argp->name);
if (argp->name) mexPrintf("%s ", argp->name);
first_arg = 0;
}
mexPrintf("]");
mexPrintf(" = gs_call ('%s'", problem->name);
for (argp=problem->arglist; argp!=NULL; argp=argp->next) {
if (argp->inout == GS_IN || argp->inout == GS_INOUT) {
if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) continue;
mexPrintf(", %s", argp->name);
}
}
mexPrintf(")\n");
/* Finalize */
if ((status=grpc_function_handle_destruct(handle)) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
handle = matlab_gs_free(handle);
return;
} else {
mexPrintf("Usage: gs_info \n");
mexPrintf("Usage: gs_info('service_name') \n");
return;
}
return;
/* Error */
error:
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
printf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
if (problem_encoding) free(problem_encoding);
if (handle && (status=grpc_function_handle_destruct(handle)) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
handle = matlab_gs_free(handle);
return;
}


| void matlab_gs_probe | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Return the status of a previous asynchronous request in the prhs array using grpc_probe.
| 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 |
Definition at line 30 of file matlab_gs_probe.c.
{
int i;
grpc_sessionid_t sessionId;
grpc_error_t retval;
if (nrhs != 2 || nlhs != 1) {
mexErrMsgTxt("Usage: status = gs_probe(sessionId)\n");
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
return;
}
/* reset global error setting */
matlab_gs_error_num = 0;
matlab_gs_minor_errno = 256;
/* Init GRPC */
retval = grpc_initialize(NULL);
if ((retval != GRPC_NO_ERROR) && (retval != GRPC_ALREADY_INITIALIZED))
DO_ERROR_MACRO(retval);
/* Get sessionid from rhs */
sessionId = (int)mxGetPr(prhs[1])[0];
if (sessionId < 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
retval = grpc_probe_ft(sessionId);
if ((retval != GRPC_NO_ERROR) & (retval != GRPC_NOT_COMPLETED))
DO_ERROR_MACRO(retval);
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)retval;
return;
error:
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)-1;
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


| void matlab_gs_probe_or | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Return the status of a previous asynchronous request in the prhs array using grpc_probe_or.
| 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 |
Definition at line 30 of file matlab_gs_probe_or.c.
{
int i;
grpc_sessionid_t sessionId;
grpc_error_t retval;
int mrows, ncols;
grpc_sessionid_t *sessionId_array = NULL;
if (nrhs != 2 || nlhs != 2) {
mexErrMsgTxt("Usage: [status, sessionId] = gs_probe_or(sessionId_array)\n");
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
return;
}
/* reset global error setting */
matlab_gs_error_num = 0;
matlab_gs_minor_errno = 256;
/* Init GRPC */
retval = grpc_initialize(NULL);
if ((retval != GRPC_NO_ERROR) && (retval != GRPC_ALREADY_INITIALIZED))
DO_ERROR_MACRO(retval);
/* Get sessionid array from rhs and its dimensions */
mrows = (int)mxGetM(prhs[1]);
ncols = (int)mxGetN(prhs[1]);
sessionId_array = (int *)malloc(sizeof(grpc_sessionid_t)*mrows*ncols);
for (i=0; i<mrows*ncols; i++)
sessionId_array[i] = (int)mxGetPr(prhs[1])[i];
/* Make call to GridSolve C API */
retval = grpc_probe_or(sessionId_array, mrows*ncols, &sessionId);
/* Release space for sessionId array */
if (sessionId_array) free(sessionId_array);
/* Check for errors */
if ((retval != GRPC_NO_ERROR) & (retval != GRPC_NONE_COMPLETED))
DO_ERROR_MACRO(retval);
/* Return values */
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)retval;
mxGetPr(plhs[1])[0] = (double)sessionId;
return;
error:
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)-1;
mxGetPr(plhs[1])[0] = (double)-1;
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


| void matlab_gs_putenv | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Put a variable=value pair into the environment (gs_putenv("VAR=value")).
| 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 |
Definition at line 27 of file matlab_gs_putenv.c.
{
char *env_equal_val;
int i, len, retval;
/* Check arguments */
if (nrhs != 2) {
mexPrintf("Usage: gs_putenv('VAR=value') \n");
goto error;
}
/* Get string */
if (!mxIsChar(prhs[1])) goto error;
len = (mxGetM(prhs[1]) * mxGetN(prhs[1]) * sizeof(mxChar)) + 1;
if ((env_equal_val = matlab_gs_calloc(len, 1)) == NULL) goto error;
if (mxGetString(prhs[1],env_equal_val,len) != 0) goto error;
retval = putenv(env_equal_val);
if (nlhs > 1) {
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[0])[0] = (double)retval;
}
/* Finalize */
return;
/* Error handling */
error:
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


| 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.
| 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 |
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;
}


| void matlab_gs_wait | ( | int | nlhs, | |
| mxArray * | plhs[], | |||
| int | nrhs, | |||
| const mxArray * | prhs[] | |||
| ) |
Wait for the completion of a prevously submitted asynchronous call using grpc_wait, returning the status of the grpc_wait call (0 if OK, -1 if error).
| 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 |
Definition at line 32 of file matlab_gs_wait.c.
{
int i = -1;
int rc = -1;
grpc_request_t *request = NULL;
grpc_sessionid_t sessionID;
grpc_error_t status;
/* reset global error setting */
matlab_gs_error_num = 0;
/* Check arguments */
if (nrhs < 2) {
mexErrMsgTxt("Usage: [outputargs,...] = gs_wait(sessionID, inputargs, ...)\n");
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
}
/* Get sessionid from rhs */
sessionID = (int)mxGetPr(prhs[1])[0];
if (sessionID < 0) DO_ERROR_MACRO(GRPC_INVALID_SESSION_ID);
/* Matlab uses column major indexing */
if (grpc_set_client_major("Column") != GRPC_NO_ERROR)
DO_ERROR_MACRO(grpc_errno);
/* Locate request structure */
request = grpc_get_request(sessionID);
if (request == NULL) DO_ERROR_MACRO(grpc_errno);
/* We call gs_wait_common, because we need manipulate a request
handle to extract the output and transfer to matlab before
destroying it. The standard gs_wait assumes that the user
already has pointers to the output data location. Note: This is
not the fault tolerant version of gs_wait */
/* TODO Handle fault tolerant wait */
status = gs_wait_common(request);
if (status != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
/* DBGPRINTF("get output\n"); */
rc = matlab_gs_get_output(request->problem, nlhs, plhs, nrhs, prhs);
if (rc != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
/* Finalize */
DBGPRINTF("Clean up\n");
if (matlab_gs_free_args(request->handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
if (request && request->handle) if (grpc_function_handle_destruct(request->handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
if (request && request->handle) request->handle = matlab_gs_free(request->handle);
if (grpc_request_destruct_free_clear(sessionID) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
/* TODO argstack? */
return;
error:
for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
mexPrintf("Warning: GridSolve call failed: %d:%d: %s; %s\n", grpc_errno, grpc_minor_errno, grpc_error_string(grpc_errno), grpc_minor_error_string(grpc_minor_errno));
return;
}


Definition at line 59 of file matlab_gs.h.
Definition at line 60 of file matlab_gs.h.
1.6.3-20100507