Functions

matlab_gs_call.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.c:

Go to the source code of this file.

Functions

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

Detailed Description

Matlab client for synchronous calls to GridSolve. Transfers the data from Matlab to GridSolve's argument structs and then Uses the fault tolerant variety of gs_call to handle the service.

Definition in file matlab_gs_call.c.


Function Documentation

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

Make a GridSolve synchronous call and return the output 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

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

Here is the call graph for this function:

Here is the caller graph for this function: