Skip to content

Commit

Permalink
fix warnings for gcc 8 and 9.3, compiles windows mex file
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Apr 3, 2020
1 parent e5f2881 commit 67576f6
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 46 deletions.
23 changes: 15 additions & 8 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ INCLUDEDIRS=

CUCCOPT:=
CCOPT=-g -pedantic -Wall -O3 -DMCX_EMBED_CL -DMCX_OPENCL -DUSE_OS_TIMER
CPPOPT:=$(CCOPT) -Wno-variadic-macros
CPPOPT:=$(CCOPT)
CCOPT+=-std=c99

DLLFLAG=-fPIC
Expand Down Expand Up @@ -72,19 +72,21 @@ CLPROGRAM=mcx_core
ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN)
CCC=nvcc
LINKOPT=-L$(LIBOPENCLDIR) -lOpenCL
INCLUDEDIRS +=-I/c/CUDA/include
INCLUDEDIRS +=-I"./mingw64/include"
CPPOPT =-c -D_CRT_SECURE_NO_DEPRECATE -DWIN32
OBJSUFFIX=.obj
EXESUFFIX=.exe
MEX=cmd /c mex
LIBOPENCL ="c:\Windows\System32\OpenCL.dll"
MEXCCOPT+=-DMX_COMPAT_32
EXTRALIB +=-static
MEX :=cmd /c mex.bat -v -f mexopts_msys2_gcc.xml
DLLFLAG=
else ifeq ($(findstring MINGW64,$(PLATFORM)), MINGW64)
MW_MINGW64_LOC=/c/msys64/usr/
MEX :=cmd //c mex.bat -v
MW_MINGW64_LOC=/c/ProgramData/MATLAB/SupportPackages/R2017b/3P.instrset/mingw_w64.instrset/
MEX :=cmd //c mex.bat -f mexopts_msys2_gcc.xml
INCLUDEDIRS+=-I"./mingw64/include"
LIBOPENCL ="c:\Windows\System32\OpenCL.dll"
LINKOPT=-g -L$(LIBOPENCLDIR) $(LIBOPENCL) -static
MKMEXOPT =-f mexopts_msys2_gcc.xml COMPFLAGS='$$COMPFLAGS $(CCFLAGS) $(USERCCFLAGS)' LDFLAGS='$$LDFLAGS -static $(OPENMPLIB) $(LIBOPENCL) $(MEXLINKOPT)' $(FASTMATH) -outdir ../mmclab
MEXCCOPT+=-DMX_COMPAT_32
EXTRALIB +=-static
DLLFLAG =
else ifeq ($(findstring Darwin,$(PLATFORM)), Darwin)
Expand All @@ -96,10 +98,15 @@ else
LINKOPT=-g -L$(LIBOPENCLDIR) $(LIBOPENCL)
endif

ifeq ($(findstring x86_64,$(ARCH)), x86_64)
CCOPT +=-m64
CPPOPT +=-m64
endif

all static: CUCCOPT+=

mex: AR=$(MEX)
mex: LINKOPT+= CXXFLAGS='$$CXXFLAGS -g -DMCX_CONTAINER $(MEXCCOPT) $(USERCCOPT)' LDFLAGS='-L$$TMW_ROOT$$MATLABROOT/sys/os/$$ARCH $$LDFLAGS -g -L$(LIBOPENCLDIR) $(LIBOPENCL) $(USERLINKOPT)'
mex: LINKOPT+= CXXFLAGS='$$CXXFLAGS -g -DMCX_CONTAINER $(CPPOPT) $(MEXCCOPT) $(USERCCOPT)' LDFLAGS='$$LDFLAGS -g -L$(LIBOPENCLDIR) $(LIBOPENCL) $(USERLINKOPT)' COMPFLAGS='' DEFINES=''
mex oct: OUTPUT_DIR=../mcxlabcl
mex: OUTPUTFLAG:=-output
mex: BINARY=mcxcl
Expand Down
10 changes: 6 additions & 4 deletions src/buildmcxcl.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function buildmmc(varargin)
function buildmcxcl(varargin)
%
% Format:
% buildmex or buildmex('option1',value1,'option2',value2,...)
Expand Down Expand Up @@ -47,7 +47,7 @@ function buildmmc(varargin)
opt=struct(varargin{:});
pname='mcx';

cflags=' -g -pedantic -Wall -O3 -DMCX_EMBED_CL -DMCX_OPENCL -DUSE_OS_TIMER -std=c99 -DMCX_CONTAINER -c ';
cflags=' -g -pedantic -Wall -O3 -DMCX_EMBED_CL -DMCX_OPENCL -DUSE_OS_TIMER -std=c99 -DMCX_CONTAINER -c ';

filelist={'mcx_utils.c','tictoc.c','cjson/cJSON.c','mcx_host.cpp',...
'mcxcl.c','mcx_shapes.c','mcxlabcl.cpp'};
Expand All @@ -71,11 +71,13 @@ function buildmmc(varargin)
if(~exist('OCTAVE_VERSION','builtin'))
for i=1:length(filelist)
flag='CFLAGS';
cflag=cflags;
if(regexp(filelist{i},'\.[Cc][Pp][Pp]$'))
flag='CXXFLAGS';
cflag=regexprep(cflags,'-std=c99','-std=gnu++0x');
end
disp(sprintf('mex OBJEXT=.o %s=''%s'' -c ''%s'' ',flag,cflags,filelist{i}));
eval(sprintf('mex OBJEXT=.o %s=''%s'' -c ''%s'' ',flag,cflags,filelist{i}));
disp(sprintf('mex OBJEXT=.o %s=''%s'' -c ''%s'' ',flag,cflag,filelist{i}));
eval(sprintf('mex OBJEXT=.o %s=''%s'' -c ''%s'' ',flag,cflag,filelist{i}));
end
if(isfield(opt,'lib'))
linkflags=[linkflags ' ' opt.lib];
Expand Down
36 changes: 24 additions & 12 deletions src/mcx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ void mcx_cleargpuinfo(GPUInfo **gpuinfo){

void mcx_savenii(float *dat, size_t len, char* name, int type32bit, int outputformatid, Config *cfg){
FILE *fp;
char fname[MAX_PATH_LENGTH]={'\0'};
char fname[MAX_FULL_PATH+100]={'\0'};
nifti_1_header hdr;
nifti1_extender pad={{0,0,0,0}};
float *logval=dat;
Expand Down Expand Up @@ -389,7 +389,7 @@ void mcx_savenii(float *dat, size_t len, char* name, int type32bit, int outputfo
void mcx_savedata(float *dat, int len, Config *cfg){
FILE *fp;
char name[MAX_FULL_PATH];
char fname[MAX_FULL_PATH];
char fname[MAX_FULL_PATH+100];
unsigned int glformat=GL_RGBA32F;

if(cfg->rootpath[0])
Expand Down Expand Up @@ -630,7 +630,7 @@ void mcx_readconfig(char *fname, Config *cfg){
}
if(fp!=NULL) fclose(fp);
if(cfg->session[0]=='\0'){
strncpy(cfg->session,fname,MAX_SESSION_LENGTH);
strncpy(cfg->session,fname,MAX_SESSION_LENGTH-1);
}
}
if(cfg->rootpath[0]!='\0'){
Expand Down Expand Up @@ -716,7 +716,7 @@ void mcx_loadconfig(FILE *in, Config *cfg){
#else
sprintf(comment,"%s/%s",cfg->rootpath,filename);
#endif
strncpy(filename,comment,MAX_PATH_LENGTH);
memcpy(filename,comment,MAX_PATH_LENGTH);
}
comm=fgets(comment,MAX_PATH_LENGTH,in);

Expand Down Expand Up @@ -892,7 +892,11 @@ void mcx_prepdomain(char *filename, Config *cfg){
if(cfg->shapedata && strstr(cfg->shapedata,":")!=NULL){
int status;
Grid3D grid={&(cfg->vol),&(cfg->dim),{1.f,1.f,1.f},cfg->isrowmajor};
if(cfg->issrcfrom0) memset(&(grid.orig.x),0,sizeof(float3));
if(cfg->issrcfrom0){
grid.orig.x=0.f;
grid.orig.y=0.f;
grid.orig.z=0.f;
}
status=mcx_parse_shapestring(&grid,cfg->shapedata);
if(status){
MCX_ERROR(status,mcx_last_shapeerror());
Expand Down Expand Up @@ -982,7 +986,7 @@ int mcx_loadjson(cJSON *root, Config *cfg){
cJSON *meds,*val;
val=FIND_JSON_OBJ("VolumeFile","Domain.VolumeFile",Domain);
if(val){
strncpy(volfile, val->valuestring, MAX_PATH_LENGTH);
strncpy(volfile, val->valuestring, MAX_PATH_LENGTH-1);
if(cfg->rootpath[0]){
#ifdef WIN32
sprintf(filename,"%s\\%s",cfg->rootpath,volfile);
Expand Down Expand Up @@ -1215,7 +1219,7 @@ int mcx_loadjson(cJSON *root, Config *cfg){
}
}
if(Session){
char val[1];
char val[2]={'\0'};
if(!flagset['E']) cfg->seed=FIND_JSON_KEY("RNGSeed","Session.RNGSeed",Session,-1,valueint);
if(!flagset['n']) cfg->nphoton=FIND_JSON_KEY("Photons","Session.Photons",Session,0,valuedouble);
if(cfg->session[0]=='\0') strncpy(cfg->session, FIND_JSON_KEY("ID","Session.ID",Session,"default",valuestring), MAX_SESSION_LENGTH);
Expand Down Expand Up @@ -1262,7 +1266,11 @@ int mcx_loadjson(cJSON *root, Config *cfg){
if(Shapes){
int status;
Grid3D grid={&(cfg->vol),&(cfg->dim),{1.f,1.f,1.f},cfg->isrowmajor};
if(cfg->issrcfrom0) memset(&(grid.orig.x),0,sizeof(float3));
if(cfg->issrcfrom0){
grid.orig.x=0.f;
grid.orig.y=0.f;
grid.orig.z=0.f;
}
status=mcx_parse_jsonshapes(root, &grid);
if(status){
MCX_ERROR(status,mcx_last_shapeerror());
Expand Down Expand Up @@ -1318,7 +1326,11 @@ void mcx_loadvolume(char *filename,Config *cfg){
if(strstr(filename,".json")!=NULL){
int status;
Grid3D grid={&(cfg->vol),&(cfg->dim),{1.f,1.f,1.f},cfg->isrowmajor};
if(cfg->issrcfrom0) memset(&(grid.orig.x),0,sizeof(float3));
if(cfg->issrcfrom0){
grid.orig.x=0.f;
grid.orig.y=0.f;
grid.orig.z=0.f;
}
status=mcx_load_jsonshapes(&grid,filename);
if(status){
MCX_ERROR(status,mcx_last_shapeerror());
Expand Down Expand Up @@ -1818,7 +1830,7 @@ void mcx_parsecmd(int argc, char* argv[], Config *cfg){
i=mcx_readarg(argc,argv,i,cfg->compileropt+strlen(cfg->compileropt),"string");
break;
case 'D':
if(i<argc-1 && isalpha(argv[i+1][0]) )
if(i<argc-1 && isalpha((int)argv[i+1][0]) )
cfg->debuglevel=mcx_parsedebugopt(argv[++i],debugflag);
else
i=mcx_readarg(argc,argv,i,&(cfg->debuglevel),"int");
Expand All @@ -1838,7 +1850,7 @@ void mcx_parsecmd(int argc, char* argv[], Config *cfg){
if (cfg->issaveref) cfg->issaveref=1;
break;
case 'w':
if(i+1<argc && isalpha(argv[i+1][0]) ){
if(i+1<argc && isalpha((int)argv[i+1][0]) ){
cfg->savedetflag=mcx_parsedebugopt(argv[++i],saveflag);
}else
i=mcx_readarg(argc,argv,i,&(cfg->savedetflag),"int");
Expand Down Expand Up @@ -1895,7 +1907,7 @@ void mcx_parsecmd(int argc, char* argv[], Config *cfg){
case 'K':
i=mcx_readarg(argc,argv,i,&(cfg->maxdetphoton),"int");
break;
if(i+1<argc && isalpha(argv[i+1][0]) ){
if(i+1<argc && isalpha((int)argv[i+1][0]) ){
cfg->mediabyte=mcx_keylookup(argv[++i],mediaformat);
if(cfg->mediabyte==-1)
MCX_ERROR(-1,"Unsupported media format.");
Expand Down
2 changes: 1 addition & 1 deletion src/mcx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define MAX_FULL_PATH 2048
#define MAX_PATH_LENGTH 1024
#define MAX_SESSION_LENGTH 256
#define MAX_SESSION_LENGTH 64
#define MAX_DEVICE 256

#define MCX_ASSERT(x) mcx_assess((x),"assert error",__FILE__,__LINE__)
Expand Down
43 changes: 22 additions & 21 deletions src/mcxlabcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void mcx_validate_config(Config *cfg);
void mcxlab_usage();

float *detps=NULL; //! buffer to receive data from cfg.detphotons field
int dimdetps[2]={0,0}; //! dimensions of the cfg.detphotons array
uint dimdetps[2]={0,0}; //! dimensions of the cfg.detphotons array
int seedbyte=0;

/** @brief Mex function for the MCX host function for MATLAB/Octave
Expand All @@ -96,7 +96,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
dimtype fielddim[5];
int errorflag=0;
int threadid=0;
cl_uint detected=0,workdev;
cl_uint workdev;
cl_device_id devices[MAX_DEVICE];

const char *outputtag[]={"data"};
Expand All @@ -120,7 +120,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
*/
if(nrhs==1 && mxIsChar(prhs[0])){
char shortcmd[MAX_SESSION_LENGTH];
int totaldev=0;
mxGetString(prhs[0], shortcmd, MAX_SESSION_LENGTH);
shortcmd[MAX_SESSION_LENGTH-1]='\0';
if(strcmp(shortcmd,"gpuinfo")==0){
Expand All @@ -138,7 +137,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
workdev=MAX_DEVICE;

plhs[0] = mxCreateStructMatrix(gpuinfo[0].devcount,1,15,gpuinfotag);
for(int i=0;i<workdev;i++){
for(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);
Expand Down Expand Up @@ -193,7 +192,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
*/
for (jstruct = 0; jstruct < ncfg; jstruct++) { /* how many configs */
try{
unsigned int partialdata,hostdetreclen;
unsigned int partialdata,hostdetreclen=0;
printf("Running simulations for configuration #%d ...\n", jstruct+1);

/** Initialize cfg with default values first */
Expand Down Expand Up @@ -381,7 +380,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){

/** return the relative workload between multiple GPUs */
val = mxCreateDoubleMatrix(1,workdev,mxREAL);
for(int i=0;i<workdev;i++)
for(uint i=0;i<workdev;i++)
*(mxGetPr(val)+i) = cfg.workload[i];
mxSetFieldByNumber(stat,0,5, val);

Expand Down Expand Up @@ -420,7 +419,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
const char *name=mxGetFieldNameByNumber(root,idx);
const dimtype *arraydim;
char *jsonshapes=NULL;
int i,j;
int j;

if(strcmp(name,"nphoton")==0 && cfg->replay.seed!=NULL)
return;
Expand Down Expand Up @@ -470,6 +469,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
GET_VEC4_FIELD(cfg,srcparam2)
else if(strcmp(name,"vol")==0){
dimtype dimxyz;
int i;
cfg->mediabyte=0;
arraydim=mxGetDimensions(item);

Expand Down Expand Up @@ -562,7 +562,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
if(cfg->detpos) free(cfg->detpos);
cfg->detpos=(float4 *)malloc(cfg->detnum*sizeof(float4));
for(j=0;j<4;j++)
for(i=0;i<cfg->detnum;i++)
for(uint i=0;i<cfg->detnum;i++)
((float *)(&cfg->detpos[i]))[j]=val[j*cfg->detnum+i];
printf("mcx.detnum=%d;\n",cfg->detnum);
}else if(strcmp(name,"prop")==0){
Expand All @@ -574,7 +574,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
if(cfg->prop) free(cfg->prop);
cfg->prop=(Medium *)malloc(cfg->medianum*sizeof(Medium));
for(j=0;j<4;j++)
for(i=0;i<cfg->medianum;i++)
for(uint i=0;i<cfg->medianum;i++)
((float *)(&cfg->prop[i]))[j]=val[j*cfg->medianum+i];
printf("mcx.medianum=%d;\n",cfg->medianum);
}else if(strcmp(name,"session")==0){
Expand Down Expand Up @@ -662,7 +662,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
double *val=mxGetPr(item);
if(cfg->srcpattern) free(cfg->srcpattern);
cfg->srcpattern=(float*)malloc(arraydim[0]*arraydim[1]*dimz*sizeof(float));
for(i=0;i<arraydim[0]*arraydim[1]*dimz;i++)
for(int i=0;i<arraydim[0]*arraydim[1]*dimz;i++)
cfg->srcpattern[i]=val[i];
printf("mcx.srcpattern=[%d %d %d];\n",arraydim[0],arraydim[1],dimz);
}else if(strcmp(name,"shapes")==0){
Expand Down Expand Up @@ -738,7 +738,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)
arraydim=mxGetDimensions(item);
if(arraydim[0]*arraydim[1]>MAX_DEVICE)
mexErrMsgTxt("the workload list can not be longer than 256");
for(i=0;i<arraydim[0]*arraydim[1];i++)
for(int i=0;i<arraydim[0]*arraydim[1];i++)
cfg->workload[i]=val[i];
printf("mcx.workload=<<%d>>;\n",arraydim[0]*arraydim[1]);
}else{
Expand Down Expand Up @@ -766,7 +766,7 @@ void mcx_set_field(const mxArray *root,const mxArray *item,int idx, Config *cfg)


void mcx_replay_prep(Config *cfg){
int i,j, hasdetid=0, offset;
int hasdetid=0, offset;
float plen;

if(cfg->seed==SEED_FROM_FILE && detps==NULL)
Expand All @@ -789,14 +789,14 @@ void mcx_replay_prep(Config *cfg){
cfg->replay.detid=(int*)calloc(cfg->nphoton,sizeof(int));

cfg->nphoton=0;
for(i=0;i<dimdetps[1];i++){
for(uint i=0;i<dimdetps[1];i++){
if(cfg->replaydet<=0 || cfg->replaydet==(int)(detps[i*dimdetps[0]])){
if(i!=cfg->nphoton)
memcpy((char *)(cfg->replay.seed)+cfg->nphoton*seedbyte, (char *)(cfg->replay.seed)+i*seedbyte, seedbyte);
cfg->replay.weight[cfg->nphoton]=1.f;
cfg->replay.tof[cfg->nphoton]=0.f;
cfg->replay.detid[cfg->nphoton]=(hasdetid) ? (int)(detps[i*dimdetps[0]]) : 1;
for(j=hasdetid;j<cfg->medianum-1+hasdetid;j++){
for(uint j=hasdetid;j<cfg->medianum-1+hasdetid;j++){
plen=detps[i*dimdetps[0]+offset+j]*cfg->unitinmm;
cfg->replay.weight[cfg->nphoton]*=expf(-cfg->prop[j-hasdetid+1].mua*plen);
cfg->replay.tof[cfg->nphoton]+=plen*R_C0*cfg->prop[j-hasdetid+1].n;
Expand All @@ -821,7 +821,8 @@ void mcx_replay_prep(Config *cfg){
*/

void mcx_validate_config(Config *cfg){
int i,gates,idx1d;
int i;
uint gates;
const char boundarycond[]={'_','r','a','m','c','\0'};
unsigned int partialdata=(cfg->medianum-1)*(SAVE_NSCAT(cfg->savedetflag)+SAVE_PPATH(cfg->savedetflag)+SAVE_MOM(cfg->savedetflag));
unsigned int hostdetreclen=partialdata+SAVE_DETID(cfg->savedetflag)+3*(SAVE_PEXIT(cfg->savedetflag)+SAVE_VEXIT(cfg->savedetflag))+SAVE_W0(cfg->savedetflag);
Expand All @@ -845,9 +846,9 @@ void mcx_validate_config(Config *cfg){
cfg->crop0.x=MAX((int)(cfg->srcpos.x-cfg->sradius),0);
cfg->crop0.y=MAX((int)(cfg->srcpos.y-cfg->sradius),0);
cfg->crop0.z=MAX((int)(cfg->srcpos.z-cfg->sradius),0);
cfg->crop1.x=MIN((int)(cfg->srcpos.x+cfg->sradius),cfg->dim.x-1);
cfg->crop1.y=MIN((int)(cfg->srcpos.y+cfg->sradius),cfg->dim.y-1);
cfg->crop1.z=MIN((int)(cfg->srcpos.z+cfg->sradius),cfg->dim.z-1);
cfg->crop1.x=MIN((int)(cfg->srcpos.x+cfg->sradius),(int)cfg->dim.x-1);
cfg->crop1.y=MIN((int)(cfg->srcpos.y+cfg->sradius),(int)cfg->dim.y-1);
cfg->crop1.z=MIN((int)(cfg->srcpos.z+cfg->sradius),(int)cfg->dim.z-1);
}else if(cfg->sradius==0.f){
memset(&(cfg->crop0),0,sizeof(uint3));
memset(&(cfg->crop1),0,sizeof(uint3));
Expand Down Expand Up @@ -879,15 +880,15 @@ void mcx_validate_config(Config *cfg){
mexErrMsgTxt("unknown boundary condition specifier");

if(cfg->medianum){
for(int i=0;i<cfg->medianum;i++)
for(uint i=0;i<cfg->medianum;i++)
if(cfg->prop[i].mus==0.f){
cfg->prop[i].mus=EPS;
cfg->prop[i].g=1.f;
}
}
if(cfg->unitinmm!=1.f){
cfg->steps.x=cfg->unitinmm; cfg->steps.y=cfg->unitinmm; cfg->steps.z=cfg->unitinmm;
for(i=1;i<cfg->medianum;i++){
for(uint i=1;i<cfg->medianum;i++){
cfg->prop[i].mus*=cfg->unitinmm;
cfg->prop[i].mua*=cfg->unitinmm;
}
Expand All @@ -905,7 +906,7 @@ void mcx_validate_config(Config *cfg){
if(cfg->seed<0 && cfg->seed!=SEED_FROM_FILE) cfg->seed=time(NULL);
if((cfg->outputtype==otJacobian || cfg->outputtype==otWP) && cfg->seed!=SEED_FROM_FILE)
mexErrMsgTxt("Jacobian output is only valid in the reply mode. Please define cfg.seed");
for(i=0;i<cfg->detnum;i++){
for(uint i=0;i<cfg->detnum;i++){
if(!cfg->issrcfrom0){
cfg->detpos[i].x--;cfg->detpos[i].y--;cfg->detpos[i].z--; /*convert to C index*/
}
Expand Down
Loading

0 comments on commit 67576f6

Please sign in to comment.