Skip to content

Commit

Permalink
use mmclab('gpuinfo') to query gpu devices
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jul 16, 2019
1 parent a45f5a1 commit ef0ef3f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mmclab/mmclab.m
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@
end
end

if(nargin==1 && ischar(varargin{1}) && strcmp(varargin{1},'gpuinfo'))
varargout{1}=mmcl('gpuinfo');
return;
end

if(nargin==0)
return;
Expand Down
13 changes: 13 additions & 0 deletions src/mcx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ void mcx_clearcfg(mcconfig *cfg){
mcx_initcfg(cfg);
}

/**
* @brief Reset and clear the GPU information data structure
*
* Clearing the GPU information data structure
*/

void mcx_cleargpuinfo(GPUInfo **gpuinfo){
if(*gpuinfo){
free(*gpuinfo);
*gpuinfo=NULL;
}
}

/**
* @brief Save volumetric output (fluence etc) to an Nifty format binary file
*
Expand Down
1 change: 1 addition & 0 deletions src/mcx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ void mcx_version(mcconfig *cfg);
int mcx_loadfromjson(char *jbuf,mcconfig *cfg);
void mcx_prep(mcconfig *cfg);
void mcx_printheader(mcconfig *cfg);
void mcx_cleargpuinfo(GPUInfo **gpuinfo);

#ifdef MCX_CONTAINER
#ifdef _OPENMP
Expand Down
55 changes: 55 additions & 0 deletions src/mmclab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
//! Macro to read one 4-element vector member of cfg
#define GET_VEC4_FIELD(u,v) else if(strcmp(name,#v)==0) {double *val=mxGetPr(item);u->v.x=val[0];u->v.y=val[1];u->v.z=val[2];u->v.w=val[3];\
printf("mmcl.%s=[%g %g %g %g];\n",#v,(float)(u->v.x),(float)(u->v.y),(float)(u->v.z),(float)(u->v.w));}
/**< Macro to output GPU parameters as field */
#define SET_GPU_INFO(output,id,v) mxSetField(output,id,#v,mxCreateDoubleScalar(gpuinfo[i].v));

#define ABS(a) ((a)<0?-(a):(a)) //! Macro to calculate the absolute value
#define MAX(a,b) ((a)>(b)?(a):(b)) //! Macro to calculate the max of two floating points
#define MEXERROR(a) mcx_error(999,a,__FILE__,__LINE__) //! Macro to add unit name and line number in error printing
Expand All @@ -82,14 +85,20 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
tetmesh mesh;
raytracer tracer={NULL,0,NULL,NULL,NULL};
unsigned int threadid=0,t0,dt;
GPUInfo *gpuinfo=NULL;

mxArray *tmp;
int ifield, jstruct;
int ncfg, nfields;
dimtype fielddim[5];
int usewaitbar=1;
int errorflag=0;
cl_uint workdev;

const char *outputtag[]={"data"};
const char *gpuinfotag[]={"name","id","devcount","major","minor","globalmem",
"constmem","sharedmem","regcount","clock","sm","core",
"autoblock","autothread","maxgate"};
#ifdef MATLAB_MEX_FILE
waitbar *hprop=NULL;
#endif
Expand All @@ -102,6 +111,52 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
return;
}

/**
* If a single string is passed, and if this string is 'gpuinfo', this function
* returns the list of GPUs on this host and return.
*/
if(nrhs==1 && mxIsChar(prhs[0])){
char shortcmd[MAX_SESSION_LENGTH];
mxGetString(prhs[0], shortcmd, MAX_SESSION_LENGTH);
shortcmd[MAX_SESSION_LENGTH-1]='\0';
if(strcmp(shortcmd,"gpuinfo")==0){
mcx_initcfg(&cfg);
cfg.isgpuinfo=3;
try{
mcx_list_gpu(&cfg,&workdev,NULL,&gpuinfo);
}catch(...){
mexErrMsgTxt("OpenCL is not supported or not fully installed on your system");
}
if(!workdev){
mexErrMsgTxt("no active GPU device found");
}
if(workdev>MAX_DEVICE)
workdev=MAX_DEVICE;

plhs[0] = mxCreateStructMatrix(gpuinfo[0].devcount,1,15,gpuinfotag);
for(cl_uint i=0;i<workdev;i++){
mxSetField(plhs[0],i,"name",mxCreateString(gpuinfo[i].name));
SET_GPU_INFO(plhs[0],i,id);
SET_GPU_INFO(plhs[0],i,devcount);
SET_GPU_INFO(plhs[0],i,major);
SET_GPU_INFO(plhs[0],i,minor);
SET_GPU_INFO(plhs[0],i,globalmem);
SET_GPU_INFO(plhs[0],i,constmem);
SET_GPU_INFO(plhs[0],i,sharedmem);
SET_GPU_INFO(plhs[0],i,regcount);
SET_GPU_INFO(plhs[0],i,clock);
SET_GPU_INFO(plhs[0],i,sm);
SET_GPU_INFO(plhs[0],i,core);
SET_GPU_INFO(plhs[0],i,autoblock);
SET_GPU_INFO(plhs[0],i,autothread);
SET_GPU_INFO(plhs[0],i,maxgate);
}
mcx_cleargpuinfo(&gpuinfo);
mcx_clearcfg(&cfg);
}
return;
}

/**
* If a structure is passed to this function, a simulation will be launched.
*/
Expand Down

0 comments on commit ef0ef3f

Please sign in to comment.