diff --git a/src/mcx_core.cu b/src/mcx_core.cu index 78fc821e..bab29f98 100644 --- a/src/mcx_core.cu +++ b/src/mcx_core.cu @@ -2821,7 +2821,7 @@ void mcx_run_simulation(Config* cfg, GPUInfo* gpu) { /** Updating host simulation configuration \c cfg, only allow the master thread to modify cfg, others are read-only */ #pragma omp master { - if (cfg->exportfield == NULL) { + if (cfg->exportfield == NULL && cfg->issave2pt) { if (cfg->seed == SEED_FROM_FILE && cfg->replaydet == -1) { cfg->exportfield = (float*)calloc(sizeof(float) * dimxyz, gpu[gpuid].maxgate * (1 + (cfg->outputtype == otRF)) * cfg->detnum); } else { diff --git a/src/mcx_utils.c b/src/mcx_utils.c index 82e6db3a..3fac3a91 100644 --- a/src/mcx_utils.c +++ b/src/mcx_utils.c @@ -3698,6 +3698,10 @@ void mcx_validatecfg(Config* cfg, float* detps, int dimdetps[2], int seedbyte) { + SAVE_W0(cfg->savedetflag); hostdetreclen += cfg->polmedianum ? (4 * SAVE_IQUV(cfg->savedetflag)) : 0; // for polarized photon simulation + if (!cfg->issave2pt && cfg->issaveref) { + cfg->issaveref = 0; + } + if (!cfg->issrcfrom0) { cfg->srcpos.x--; cfg->srcpos.y--; diff --git a/src/mcxlab.cpp b/src/mcxlab.cpp index f28963c7..24297b27 100644 --- a/src/mcxlab.cpp +++ b/src/mcxlab.cpp @@ -260,7 +260,7 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { } /** Initialize all buffers necessary to store the output variables */ - if (nlhs >= 1) { + if (nlhs >= 1 && cfg.issave2pt) { int fieldlen = cfg.dim.x * cfg.dim.y * cfg.dim.z * (int)((cfg.tend - cfg.tstart) / cfg.tstep + 0.5) * cfg.srcnum; if (cfg.replay.seed != NULL && cfg.replaydet == -1) { @@ -412,10 +412,11 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { fieldlen = fielddim[0] * fielddim[1] * fielddim[2] * fielddim[3] * fielddim[4] * fielddim[5]; - if (cfg.issaveref) { + if (cfg.issaveref && cfg.exportfield) { int highdim = fielddim[3] * fielddim[4] * fielddim[5]; int voxellen = cfg.dim.x * cfg.dim.y * cfg.dim.z; float* dref = (float*)malloc(fieldlen * sizeof(float)); + memcpy(dref, cfg.exportfield, fieldlen * sizeof(float)); for (int voxelid = 0; voxelid < voxellen; voxelid++) { @@ -438,13 +439,16 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { free(dref); } - if (cfg.issave2pt) { + if (cfg.issave2pt && cfg.exportfield) { mxSetFieldByNumber(plhs[0], jstruct, 0, mxCreateNumericArray(((fielddim[5] > 1) ? 6 : (4 + (fielddim[4] > 1))), fielddim, mxSINGLE_CLASS, mxREAL)); memcpy((float*)mxGetPr(mxGetFieldByNumber(plhs[0], jstruct, 0)), cfg.exportfield, fieldlen * sizeof(float)); } - free(cfg.exportfield); + if (cfg.exportfield) { + free(cfg.exportfield); + } + cfg.exportfield = NULL; /** also return the run-time info in outut.runtime */