Functions

matlab_gs_call_async.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_call_async.c:

Go to the source code of this file.

Functions

void matlab_gs_call_async (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

Detailed Description

Matlab client call

Definition in file matlab_gs_call_async.c.


Function Documentation

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).

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

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;

}

Here is the call graph for this function:

Here is the caller graph for this function: