matlab_gs_info.c

Go to the documentation of this file.
00001 
00007 /* $Id: matlab_gs_info.c,v 1.11 2007/04/05 17:26:06 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 
00029 void
00030 matlab_gs_info(int nlhs, mxArray *plhs[],
00031            int nrhs, const mxArray *prhs[] )
00032 {
00033   int i;
00034   grpc_function_handle_t *handle = NULL;
00035   char func_name[1024];
00036   gs_argument_t *argp = NULL;
00037   gs_problem_t *problem;
00038   char *problem_encoding = NULL;
00039   grpc_error_t status;
00040   int first_arg;
00041   char *str1 = NULL;
00042   char *str2 = NULL;
00043   int rc;
00044 
00045   /* reset global error setting */
00046   matlab_gs_error_num = 0;  
00047 
00048   /* Initialize */
00049   rc = grpc_initialize(NULL);
00050   if ((rc != GRPC_NO_ERROR) && (rc != GRPC_ALREADY_INITIALIZED)) DO_ERROR_MACRO(grpc_errno);
00051 
00052   /* Matlab uses column major indexing */
00053   if (grpc_set_client_major("Column") != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00054 
00055   /* Get some global information */
00056   if (nrhs == 1) {
00057     if (grpc_get_servers(&str1) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00058     if (grpc_get_problems(&str2) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00059     mexPrintf("%s \n", str1);
00060     mexPrintf("%s \n", str2);
00061     free(str1);
00062     free(str2);
00063 
00064   } else if (nrhs == 2) {
00065     
00066     /* Get problem name from Matlab */
00067     if (!mxIsChar(prhs[1])) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00068     if (mxGetString(prhs[1],func_name,1024) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00069     
00070     /* Get problem info from agent, along with a default server assignment */
00071     /* TODO We want to avoid assigning server, because that will cause
00072        the load to be temporarily increased as a heuristic */
00073     handle = (grpc_function_handle_t*)matlab_gs_calloc(1, sizeof(grpc_function_handle_t));
00074     if (handle == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00075     if ((status=grpc_function_handle_default(handle, func_name)) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00076     if (handle->problem_desc == NULL) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00077     
00078     /* Write out problem description */
00079     problem = handle->problem_desc;
00080     
00081     /* Write out problem description */
00082     if (gs_encode_problem(&problem_encoding, problem) != 0) DO_ERROR_MACRO(GRPC_OTHER_ERROR_CODE);
00083     if (problem_encoding) {
00084       mexPrintf("Description of call:\n");
00085       mexPrintf("%s\n", problem_encoding);
00086     }
00087     free(problem_encoding); /* allocated by gridrpc so memory is not handled in matlab */
00088 
00089     /* Write out problem description */
00090     mexPrintf("Matlab call prototype:\n");
00091     first_arg = 1;
00092     mexPrintf("[ ");
00093     for (argp=problem->arglist; argp!=NULL; argp=argp->next) 
00094       if (argp->inout == GS_OUT || argp->inout == GS_INOUT || argp->inout == GS_VAROUT) {
00095     if (argp->inout == GS_WORKSPACE) continue;
00096     if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) continue;
00097     if (! first_arg) mexPrintf(",", argp->name);
00098     if (argp->name) mexPrintf("%s ", argp->name);
00099     first_arg = 0;
00100       }
00101     mexPrintf("]");
00102     mexPrintf(" = gs_call ('%s'", problem->name);
00103     for (argp=problem->arglist; argp!=NULL; argp=argp->next) {
00104       if (argp->inout == GS_IN || argp->inout == GS_INOUT) {
00105     if (matlab_gs_arg_is_sparse_index_or_pointer(argp, problem)) continue;
00106     mexPrintf(", %s", argp->name);
00107       }
00108     }
00109     mexPrintf(")\n");
00110     
00111     /* Finalize */
00112     if ((status=grpc_function_handle_destruct(handle)) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00113     handle = matlab_gs_free(handle);
00114     return;
00115 
00116   } else {
00117 
00118     mexPrintf("Usage: gs_info \n");
00119     mexPrintf("Usage: gs_info('service_name') \n");
00120     return;
00121   }
00122 
00123   return;
00124 
00125 
00126   /* Error */
00127  error:
00128   for (i=0;i<nlhs;i++) plhs[i] = mxCreateDoubleMatrix(0,0,mxREAL); 
00129   printf("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));  
00130 
00131   if (problem_encoding) free(problem_encoding);
00132   if (handle && (status=grpc_function_handle_destruct(handle)) != GRPC_NO_ERROR) DO_ERROR_MACRO(grpc_errno);
00133   handle = matlab_gs_free(handle);
00134 
00135   return;
00136 }
00137 
00138 
00139