matlab_gs_call_async.c

Go to the documentation of this file.
00001 
00007 /* $Id: matlab_gs_call_async.c,v 1.11 2007/07/24 19:24:32 yarkhan Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include <stdio.h>
00011 #include <string.h>
00012 
00013 #include <matrix.h>
00014 #include <mex.h>
00015 
00016 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #endif /* HAVE_CONFIG_H */
00019 
00020 #include "grpc.h"
00021 #include "comm_data.h"
00022 #include "matlab_gs.h"
00023 
00033 void
00034 matlab_gs_call_async( int nlhs, mxArray *plhs[],
00035               int nrhs, const mxArray *prhs[] )
00036 {
00037   grpc_function_handle_t* handle = NULL;
00038   char func_name[1024];
00039   grpc_arg_stack* argstack = NULL;
00040   gs_argument_t *argp = NULL;
00041   grpc_sessionid_t sessionID;
00042   grpc_error_t status;
00043   int i;
00044   int argcount = 0;
00045   int rc;
00046   
00047   /* reset global error setting */
00048   matlab_gs_error_num = 0;  
00049 
00050   /* Left hand side should consist of one integer argument to hold the sessionid */
00051   if ((nlhs != 1) || (nrhs < 2))  {
00052     mexPrintf("Usage: [request_id] = gs_call_async('service_name', ...) \n");
00053     DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00054   }
00055 
00056   /* Initialize */
00057   rc = grpc_initialize(NULL);
00058   if ((rc != GRPC_NO_ERROR) && (rc != GRPC_ALREADY_INITIALIZED)) DO_ERROR_MACRO(grpc_errno);
00059 
00060   /* Matlab uses column major indexing */
00061   if (grpc_set_client_major("Column") != GRPC_NO_ERROR) 
00062     DO_ERROR_MACRO(grpc_errno);
00063 
00064   /* Get problem name from rhs */
00065   if (!mxIsChar(prhs[1])) DO_ERROR_MACRO(GRPC_FUNCTION_NOT_FOUND);
00066   if (mxGetString(prhs[1],func_name,1024) != 0) DO_ERROR_MACRO(GRPC_FUNCTION_NOT_FOUND);
00067   
00068   /* Get problem info from agent, along with a default server assignment */
00069   handle = (grpc_function_handle_t*)matlab_gs_calloc(1, sizeof(grpc_function_handle_t));
00070   if (handle == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00071   if ((status = grpc_function_handle_default(handle, func_name)) != GRPC_NO_ERROR) DO_ERROR_MACRO(status);
00072   if (handle->problem_desc == NULL) DO_ERROR_MACRO(GRPC_INVALID_FUNCTION_HANDLE);
00073   
00074   /* Setup input objects to be sent in the problem structure.  NOTE,
00075      we need to have the problem description here in order to convert
00076      the matlab arguments (all double arrays) to the proper types for
00077      the function, so we cannot use gridsolve style "bind servers at
00078      call time" function handle creation */
00079   if (matlab_gs_setup_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) 
00080     DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00081 
00082   /* Push args on stack */
00083   for (argcount=0,argp=handle->problem_desc->arglist; argp!=NULL; argp=argp->next) 
00084     if (argp->inout != GS_WORKSPACE) argcount++;
00085   if ((argstack = grpc_arg_stack_new(argcount)) == NULL) DO_ERROR_MACRO(grpc_errno);
00086   for (argp=handle->problem_desc->arglist; argp!=NULL; argp=argp->next) {
00087     if (argp->inout != GS_WORKSPACE) /* skip workspace args */
00088       if (grpc_arg_stack_push_arg(argstack, argp->data) < 0) 
00089     DO_ERROR_MACRO(grpc_errno);
00090   }
00091   
00092   /* Call the solve routine using stack interface */
00093   status = grpc_call_arg_stack_async_ft(handle, &sessionID, argstack);
00094   if (status != GRPC_NO_ERROR) DO_ERROR_MACRO(status);
00095   if (matlab_gs_free_input_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00096   
00097   /* Put the sessionID into the output */
00098   plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
00099   mxGetPr(plhs[0])[0] = (double)sessionID;
00100 
00101   return;
00102 
00103 
00104 
00105   /* Error */
00106  error:
00107 
00108   /* Return -1 as sessionID, as 0th lhs */
00109   if (nlhs > 0) {
00110     plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
00111     mxGetPr(plhs[0])[0] = (double) -1;
00112   }
00113   /* Set rest of lhs */
00114   for (i=1;i<nlhs;i++)
00115     plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
00116   
00117   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));  
00118 
00119   if (matlab_gs_free_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00120   if (argstack && grpc_arg_stack_destruct(argstack) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00121   if (handle && grpc_function_handle_destruct(handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00122   handle = matlab_gs_free(handle);
00123 
00124   return;
00125 
00126 }
00127 
00128 
00129 
00130