matlab_gs_call.c

Go to the documentation of this file.
00001 
00009 /* $Id: matlab_gs_call.c,v 1.15 2007/07/24 19:24:32 yarkhan Exp $ */
00010 /* $UTK_Copyright: $ */
00011 
00012 #include <stdio.h>
00013 #include <string.h>
00014 
00015 #include <matrix.h>
00016 #include <mex.h>
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include "config.h"
00020 #endif /* HAVE_CONFIG_H */
00021 
00022 #include "grpc.h"
00023 #include "comm_data.h"
00024 #include "matlab_gs.h"
00025 
00034 void
00035 matlab_gs_call( int nlhs, mxArray *plhs[],
00036              int nrhs, const mxArray *prhs[] )
00037 {
00038   int i;
00039   grpc_function_handle_t *handle = NULL;
00040   grpc_arg_stack *argstack = NULL;
00041   char func_name[1024];
00042   gs_argument_t *argp = NULL;
00043   grpc_error_t status;
00044   double t1;
00045   int argcount = 0;
00046   int rc;
00047 
00048   /* reset global error setting */
00049   matlab_gs_error_num = 0;  
00050 
00051   /* Left hand side should consist of one integer argument to hold the sessionid */
00052   if (nrhs < 2)  {
00053     mexPrintf("Usage: [out, args, ...] = gs_call('service_name', ...) \n");
00054     DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00055   }
00056 
00057   /* Initialize */
00058   rc = grpc_initialize(NULL);
00059   if ((rc != GRPC_NO_ERROR) && (rc != GRPC_ALREADY_INITIALIZED)) DO_ERROR_MACRO(grpc_errno);
00060 
00061   /* Matlab uses column major indexing */
00062   if (grpc_set_client_major("Column") != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00063   
00064   /* Get problem name from Matlab */
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   t1 = walltime();
00070   handle = (grpc_function_handle_t*)matlab_gs_calloc(1, sizeof(grpc_function_handle_t));
00071   if (handle == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00072   if ((status = grpc_function_handle_default(handle, func_name)) != GRPC_NO_ERROR) DO_ERROR_MACRO(status);
00073   if (handle->problem_desc == NULL) DO_ERROR_MACRO(GRPC_INVALID_FUNCTION_HANDLE);
00074   DBGPRINTF("Time for get handle %f \n ", walltime() - t1);
00075 
00076   /* Fix arguments and their types, creating input and output space, and push args on stack */
00077   t1 = walltime();
00078   if (matlab_gs_setup_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) 
00079     DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00080   DBGPRINTF("Time for setup args from matlab %f \n ", walltime() - t1);
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   t1 = walltime();
00094   DBGPRINTF("Call the solve routine using grpc_call_arg_stack interface\n"); fflush(0);
00095   status = grpc_call_arg_stack_ft(handle, argstack);
00096   if (status != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00097   DBGPRINTF("Time for solve %f \n ", walltime() - t1);
00098   
00099   /* Convert/transfer output to Matlab objects */
00100   t1 = walltime();
00101   DBGPRINTF("Call matlab_gs_get_output\n"); fflush(0);
00102   if (matlab_gs_get_output(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) 
00103     DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00104   DBGPRINTF("Time for transfering output to matlab %f \n ", walltime() - t1);  
00105 
00106   /* Finalize */
00107   DBGPRINTF("Clean up\n");
00108   if (matlab_gs_free_args(handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00109   if (argstack && grpc_arg_stack_destruct(argstack) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00110   if (handle && grpc_function_handle_destruct(handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00111   handle = matlab_gs_free(handle);
00112 
00113   return;
00114 
00115 
00116 
00117   /* Error */
00118  error:
00119 
00120   for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
00121   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));  
00122 
00123   if (argstack && grpc_arg_stack_destruct(argstack) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00124   if (handle && grpc_function_handle_destruct(handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00125   handle = matlab_gs_free(handle);
00126 
00127   return;
00128 }
00129 
00130 
00131