matlab_gs_wait.c

Go to the documentation of this file.
00001 
00007 /* $Id: matlab_gs_wait.c,v 1.10 2007/07/05 19:19:38 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 #include "grpc.h"
00017 #include "comm_data.h"
00018 #include "matlab_gs.h"
00019 
00031 void
00032 matlab_gs_wait( int nlhs, mxArray *plhs[],
00033         int nrhs, const mxArray *prhs[] )
00034 {
00035   int i = -1;
00036   int rc = -1;
00037   grpc_request_t *request = NULL;
00038   grpc_sessionid_t sessionID;
00039   grpc_error_t status;
00040 
00041   /* reset global error setting */
00042   matlab_gs_error_num = 0;  
00043 
00044   /* Check arguments */
00045   if (nrhs < 2) {
00046     mexErrMsgTxt("Usage: [outputargs,...] = gs_wait(sessionID, inputargs, ...)\n");
00047     for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
00048     DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00049   }
00050   
00051   /* Get sessionid from rhs */
00052   sessionID = (int)mxGetPr(prhs[1])[0];
00053   if (sessionID < 0) DO_ERROR_MACRO(GRPC_INVALID_SESSION_ID);
00054                
00055   /* Matlab uses column major indexing */
00056   if (grpc_set_client_major("Column") != GRPC_NO_ERROR) 
00057     DO_ERROR_MACRO(grpc_errno);
00058   
00059   /* Locate request structure */
00060   request = grpc_get_request(sessionID);
00061   if (request == NULL) DO_ERROR_MACRO(grpc_errno);
00062   
00063   /* We call gs_wait_common, because we need manipulate a request
00064      handle to extract the output and transfer to matlab before
00065      destroying it.  The standard gs_wait assumes that the user
00066      already has pointers to the output data location. Note: This is
00067      not the fault tolerant version of gs_wait */
00068   /* TODO Handle fault tolerant wait */
00069   status = gs_wait_common(request);
00070   if (status != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00071   
00072   /* DBGPRINTF("get output\n"); */
00073   rc = matlab_gs_get_output(request->problem, nlhs, plhs, nrhs, prhs);
00074   if (rc != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00075 
00076   /* Finalize */
00077   DBGPRINTF("Clean up\n");
00078   if (matlab_gs_free_args(request->handle->problem_desc, nlhs, plhs, nrhs, prhs) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00079   if (request && request->handle) if (grpc_function_handle_destruct(request->handle) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00080   if (request && request->handle) request->handle = matlab_gs_free(request->handle); 
00081   if (grpc_request_destruct_free_clear(sessionID) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00082   /* TODO argstack? */
00083 
00084   return;
00085 
00086 
00087 
00088  error:
00089   for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL);
00090   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));  
00091 
00092 
00093   return;
00094 }
00095