Skip to content

Commit

Permalink
force flushing in terminal and matlab
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jan 3, 2023
1 parent 849d09a commit 3ddff2b
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 51 deletions.
26 changes: 13 additions & 13 deletions src/mmc_cl_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
*******************************************************************************/

/***************************************************************************//**
\file mmc_host.c
\file mmc_cl_host.c
\brief << Driver program of MMC >>
\brief OpenCL host code for OpenCL based MMC simulations
*******************************************************************************/

#include <stdlib.h>
Expand Down Expand Up @@ -358,7 +358,7 @@ void mmc_run_cl(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, void (*progress
MMC_FPRINTF(cfg->flog, "initializing streams ...\t");

MMC_FPRINTF(cfg->flog, "init complete : %d ms\n", GetTimeMillis() - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

OCL_ASSERT(((mcxprogram = clCreateProgramWithSource(mcxcontext, 1, (const char**) & (cfg->clsource), NULL, &status), status)));

Expand Down Expand Up @@ -474,7 +474,7 @@ void mmc_run_cl(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, void (*progress
}

MMC_FPRINTF(cfg->flog, "build program complete : %d ms\n", GetTimeMillis() - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

mcxkernel = (cl_kernel*)malloc(workdev * sizeof(cl_kernel));

Expand Down Expand Up @@ -515,7 +515,7 @@ void mmc_run_cl(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, void (*progress
}

MMC_FPRINTF(cfg->flog, "set kernel arguments complete : %d ms %d\n", GetTimeMillis() - tic, param.method);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

if (cfg->exportfield == NULL) {
cfg->exportfield = mesh->weight;
Expand Down Expand Up @@ -543,13 +543,13 @@ void mmc_run_cl(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, void (*progress

MMC_FPRINTF(cfg->flog, "lauching mcx_main_loop for time window [%.1fns %.1fns] ...\n"
, twindow0 * 1e9, twindow1 * 1e9);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

//total number of repetition for the simulations, results will be accumulated to field
for (iter = 0; iter < cfg->respin; iter++) {
MMC_FPRINTF(cfg->flog, "simulation run#%2d ... \n", iter + 1);
fflush(cfg->flog);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
mcx_fflush(cfg->flog);
param.tstart = twindow0;
param.tend = twindow1;

Expand Down Expand Up @@ -596,7 +596,7 @@ void mmc_run_cl(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, void (*progress
tic1 = GetTimeMillis();
toc += tic1 - tic0;
MMC_FPRINTF(cfg->flog, "kernel complete: \t%d ms\nretrieving flux ... \t", tic1 - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

if (cfg->runtime < tic1 - tic) {
cfg->runtime = tic1 - tic;
Expand Down Expand Up @@ -662,7 +662,7 @@ is more than what your have specified (%d), please use the -H option to specify
OCL_ASSERT((clEnqueueReadBuffer(mcxqueue[devid], gweight[devid], CL_TRUE, 0, sizeof(cl_float)*fieldlen * 2,
rawfield, 0, NULL, NULL)));
MMC_FPRINTF(cfg->flog, "transfer complete: %d ms\n", GetTimeMillis() - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

for (i = 0; i < fieldlen; i++) { //accumulate field, can be done in the GPU
field[(i >> cfg->nbuffer)] += rawfield[i] + rawfield[i + fieldlen]; //+rawfield[i+fieldlen];
Expand Down Expand Up @@ -740,7 +740,7 @@ is more than what your have specified (%d), please use the -H option to specify

if (cfg->isnormalized) {
MMC_FPRINTF(cfg->flog, "normalizing raw data ...\t");
fflush(cfg->flog);
mcx_fflush(cfg->flog);

cfg->energyabs = cfg->energytot - cfg->energyesc;
mesh_normalize(mesh, cfg, cfg->energyabs, cfg->energytot, 0);
Expand All @@ -750,7 +750,7 @@ is more than what your have specified (%d), please use the -H option to specify
MMC_FPRINTF(cfg->flog, "saving data to file ...\t");
mesh_saveweight(mesh, cfg, 0);
MMC_FPRINTF(cfg->flog, "saving data complete : %d ms\n\n", GetTimeMillis() - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
}

if (cfg->issavedet && cfg->parentid == mpStandalone && cfg->exportdetected) {
Expand All @@ -770,7 +770,7 @@ is more than what your have specified (%d), please use the -H option to specify
cfg->nphoton, cfg->nphoton, workdev, reporter.raytet, (double)cfg->nphoton / toc);
MMC_FPRINTF(cfg->flog, "total simulated energy: %.2f\tabsorbed: %5.5f%%\n(loss due to initial specular reflection is excluded in the total)\n",
cfg->energytot, (cfg->energytot - cfg->energyesc) / cfg->energytot * 100.f);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

OCL_ASSERT(clReleaseMemObject(gprogress[0]));

Expand Down
4 changes: 2 additions & 2 deletions src/mmc_cl_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
*******************************************************************************/

/***************************************************************************//**
\file mcx_host_cl.hpp
\file mcx_cl_host.hpp
\brief Definitions of mmc high-level driver functions for OpenCL backend
\brief OpenCL host code for OpenCL based MMC simulations
*******************************************************************************/

#ifndef _MMC_HOSTCODE_CL_H
Expand Down
32 changes: 17 additions & 15 deletions src/mmc_cu_host.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
** GPL v3, see LICENSE.txt for details
*******************************************************************************/

/**
\file mmc_host.c
\brief << Driver program of MMC >>
*/
/***************************************************************************//**
\file mmc_cu_host.c
\brief CUDA host code for NVIDIA GPUs
*******************************************************************************/


#define inlinefun __device__

Expand All @@ -42,8 +44,8 @@

#include "mmc_core.cu"

/************************************************************************** In
this unit, we first launch a master thread and initialize the necessary data
/******************************************************************************
In this unit, we first launch a master thread and initialize the necessary data
structures.This include the command line options(cfg), tetrahedral mesh(mesh)
and the ray tracer precomputed data (tracer).
******************************************************************************/
Expand Down Expand Up @@ -538,7 +540,7 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo
#endif
MMC_FPRINTF(cfg->flog, "- compiled with: [RNG] %s [Seed Length] %d\n",
MCX_RNG_NAME, RAND_SEED_WORD_LEN);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
}
#pragma omp barrier

Expand All @@ -561,14 +563,14 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo
MMC_FPRINTF(cfg->flog,
"lauching mcx_main_loop for time window [%.1fns %.1fns] ...\n",
twindow0 * 1e9, twindow1 * 1e9);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

// total number of repetition for the simulations, results will be
// accumulated to field
for (int iter = 0; iter < cfg->respin; iter++) {
MMC_FPRINTF(cfg->flog, "simulation run#%2d ... \n", iter + 1);
fflush(cfg->flog);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
mcx_fflush(cfg->flog);
param.tstart = twindow0;
param.tend = twindow1;

Expand Down Expand Up @@ -614,7 +616,7 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo
MMC_FPRINTF(cfg->flog,
"kernel complete: \t%d ms\nretrieving flux ... \t",
tic1 - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
#pragma omp critical

if (cfg->runtime < tic1 - tic) {
Expand Down Expand Up @@ -690,7 +692,7 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo
cudaMemcpyDeviceToHost));
MMC_FPRINTF(cfg->flog, "transfer complete: %d ms\n",
GetTimeMillis() - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);

for (i = 0; i < fieldlen; i++) { // accumulate field, can be done in the GPU
field[(i >> cfg->nbuffer)] += rawfield[i] + rawfield[i + fieldlen]; //+rawfield[i+fieldlen];
Expand Down Expand Up @@ -765,7 +767,7 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo

if (cfg->isnormalized) {
MMC_FPRINTF(cfg->flog, "normalizing raw data ...\t");
fflush(cfg->flog);
mcx_fflush(cfg->flog);

cfg->energyabs = cfg->energytot - cfg->energyesc;
mesh_normalize(mesh, cfg, cfg->energyabs, cfg->energytot, 0);
Expand All @@ -776,7 +778,7 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo
mesh_saveweight(mesh, cfg, 0);
MMC_FPRINTF(cfg->flog, "saving data complete : %d ms\n\n",
GetTimeMillis() - tic);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
}

if (cfg->issavedet && cfg->parentid == mpStandalone &&
Expand Down Expand Up @@ -805,7 +807,7 @@ void mmc_run_simulation(mcconfig* cfg, tetmesh* mesh, raytracer* tracer, GPUInfo
"initial specular reflection is excluded in the total)\n",
cfg->energytot,
(cfg->energytot - cfg->energyesc) / cfg->energytot * 100.f);
fflush(cfg->flog);
mcx_fflush(cfg->flog);
}
#pragma omp barrier
CUDA_ASSERT(cudaFree(gnode));
Expand Down
4 changes: 2 additions & 2 deletions src/mmc_cu_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
*******************************************************************************/

/***************************************************************************//**
\file mmcx_core.h
\file mmc_cu_host.h
@brief MMC GPU kernel header file
\brief CUDA host code for NVIDIA GPUs
*******************************************************************************/

#ifndef _MMCX_HOSTCODE_H
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/***************************************************************************//**
\file mmc_host.h
\brief Definition of mmc high-level driver functions
\brief OpenMP host code for CPU-based MMC simulations
*******************************************************************************/

#ifndef _MMC_HOSTCODE_H
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file simpmesh.c
\file mmc_mesh.c
\brief Basic vector math and mesh operations
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file simpmesh.h
\file mmc_mesh.h
\brief Definition of basic mesh data structures and inline vector operations
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_rand_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file rng_common.h
\file mmc_rand_common.h
\brief random number generator (RNG) independent interface functions
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_rand_drand48.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file drand48_r_libgw32c.h
\file mmc_rand_drand48.h
\brief Windows 32 port of drand48_r random number generator from libgw2c
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_rand_logistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file logistic_rand.h
\file mmc_rand_logistic.h
\brief An interface to use a coupled chaotic Logistic lattice RNG
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_rand_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file posix_randr.h
\file mmc_rand_posix.h
\brief An interface to use a 48bit multi-threaded GNU RNG
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_rand_sfmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file sfmt_rand.h
\file mmc_rand_sfmt.h
\brief An interface to use the SFMT-19937 random number generator
*******************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_rand_xorshift128p.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file sfmt_rand.h
\file mmc_rand_xorshift128p.h
\brief An interface to use the SFMT-19937 random number generator
*******************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions src/mmc_raytrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file tettracing.c
\file mmc_raytrace.c
\brief Core unit for mash-based photon transport using ray tracing algorithms
*******************************************************************************/
Expand Down Expand Up @@ -87,7 +87,7 @@ const int nc[4][3] = {{3, 0, 1}, {3, 1, 2}, {2, 0, 3}, {1, 0, 2}};
* the normal vec of each face pointing outwards.
*/

extern const int out[4][3]; /**< defined in simpmesh.c, node orders for each face, in counter-clock-wise orders*/
extern const int out[4][3]; /**< defined in mmc_mesh.c, node orders for each face, in counter-clock-wise orders*/

/**
* \brief The local index of the node with an opposite face to the i-th face defined in nc[][]
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_raytrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file tettracing.h
\file mmc_raytrace.h
\brief Definition of the core ray-tracing functions
*******************************************************************************/
Expand Down
6 changes: 6 additions & 0 deletions src/mmc_tictoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
** GPL v3, see LICENSE.txt for details
*******************************************************************************/

/***************************************************************************//**
\file mmc_tictoc.c
\brief Interface of the timing unit
*******************************************************************************/

#include "mmc_tictoc.h"

#define _DEFAULT_SOURCE
Expand Down
2 changes: 1 addition & 1 deletion src/mmc_tictoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*******************************************************************************/

/***************************************************************************//**
\file tictoc.h
\file mmc_tictoc.h
\brief Interface of the timing unit
*******************************************************************************/
Expand Down
22 changes: 17 additions & 5 deletions src/mmc_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,21 @@ int mcx_parsedebugopt(char* debugopt) {
return debuglevel;
}


/**
* @brief Flush command line output
*
* @param[in] out: the stream to be flushed
*/

void mcx_fflush(FILE* out) {
#ifdef MCX_CONTAINER
mcx_matlab_flush();
#else
fflush(out);
#endif
}

/**
* @brief Print a progress bar
*
Expand Down Expand Up @@ -2210,11 +2225,8 @@ void mcx_progressbar(float percent, void* cfg) {
}

MMC_FPRINTF(stdout, "] %3d%%" S_RESET, (int)(percent * 100));
#ifdef MCX_CONTAINER
mcx_matlab_flush();
#else
fflush(stdout);
#endif

mcx_fflush(stdout);
}
}

Expand Down
Loading

0 comments on commit 3ddff2b

Please sign in to comment.