commit 0ab75686ef40686cd3c843ba6f6118c554ea5903
parent 594f62057b6b82ae1fb0bc46099fd1a0a064bb0b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 29 Jun 2020 17:48:45 +0200
Save the RNG state of the estimator buffer
Diffstat:
8 files changed, 115 insertions(+), 52 deletions(-)
diff --git a/src/sdis.h b/src/sdis.h
@@ -629,6 +629,11 @@ sdis_estimator_buffer_get_realisation_time
(const struct sdis_estimator_buffer* buf,
struct sdis_mc* time);
+SDIS_API res_T
+sdis_estimator_buffer_get_rng_state
+ (const struct sdis_estimator_buffer* buf,
+ struct ssp_rng** rng_state);
+
/*******************************************************************************
* A medium encapsulates the properties of either a fluid or a solid.
******************************************************************************/
@@ -927,6 +932,8 @@ sdis_estimator_for_each_path
SDIS_API res_T
sdis_estimator_get_rng_state
(const struct sdis_estimator* estimator,
+ /* The returned value may be NULL as for instance an estimator retrieved
+ * from an estimator buffer */
struct ssp_rng** rng_state);
/*******************************************************************************
diff --git a/src/sdis_device.c b/src/sdis_device.c
@@ -23,6 +23,7 @@
#include <star/s2d.h>
#include <star/s3d.h>
+#include <star/ssp.h>
#include <omp.h>
@@ -138,3 +139,59 @@ sdis_device_ref_put(struct sdis_device* dev)
return RES_OK;
}
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+create_rng_from_rng_proxy
+ (struct sdis_device* dev,
+ const struct ssp_rng_proxy* proxy,
+ struct ssp_rng** out_rng)
+{
+ struct ssp_rng_type rng_type;
+ struct ssp_rng* rng = NULL;
+ FILE* stream = NULL;
+ res_T res = RES_OK;
+ ASSERT(dev && proxy && out_rng);
+
+ stream = tmpfile();
+ if(!stream) {
+ log_err(dev,
+ "Could not open a temporary stream to store the RNG state.\n");
+ res = RES_IO_ERR;
+ goto error;
+ }
+
+ SSP(rng_proxy_get_type(proxy, &rng_type));
+ res = ssp_rng_create(dev->allocator, &rng_type, &rng);
+ if(res != RES_OK) {
+ log_err(dev, "Could not create the RNG -- %s\n", res_to_cstr(res));
+ goto error;
+ }
+
+ res = ssp_rng_proxy_write(proxy, stream);
+ if(res != RES_OK) {
+ log_err(dev, "Could not serialize the RNG state -- %s\n",
+ res_to_cstr(res));
+ goto error;
+ }
+
+ rewind(stream);
+ res = ssp_rng_read(rng, stream);
+ if(res != RES_OK) {
+ log_err(dev, "Could not read the serialized RNG state -- %s\n",
+ res_to_cstr(res));
+ goto error;
+ }
+
+exit:
+ if(out_rng) *out_rng = rng;
+ if(stream) fclose(stream);
+ return res;
+error:
+ if(rng) {
+ SSP(rng_ref_put(rng));
+ rng = NULL;
+ }
+ goto exit;
+}
diff --git a/src/sdis_device_c.h b/src/sdis_device_c.h
@@ -23,6 +23,10 @@
#include <rsys/logger.h>
#include <rsys/ref_count.h>
+/* Forward declarations */
+struct ssp_rng;
+struct ssp_rng_proxy;
+
struct name { FITEM; };
#define FITEM_TYPE name
#include <rsys/free_list.h>
@@ -43,5 +47,11 @@ struct sdis_device {
ref_T ref;
};
+extern LOCAL_SYM res_T
+create_rng_from_rng_proxy
+ (struct sdis_device* dev,
+ const struct ssp_rng_proxy* proxy,
+ struct ssp_rng** out_rng);
+
#endif /* SDIS_DEVICE_C_H */
diff --git a/src/sdis_estimator.c b/src/sdis_estimator.c
@@ -288,60 +288,12 @@ estimator_save_rng_state
(struct sdis_estimator* estimator,
const struct ssp_rng_proxy* proxy)
{
- struct ssp_rng_type rng_type;
- FILE* stream = NULL;
- res_T res = RES_OK;
ASSERT(estimator && proxy);
-
/* Release the previous RNG state if any */
if(estimator->rng) {
SSP(rng_ref_put(estimator->rng));
estimator->rng = NULL;
}
-
- stream = tmpfile();
- if(!stream) {
- log_err(estimator->dev,
- "Could not open a temporary stream to store "
- "the RNG state of the estimation.\n");
- res = RES_IO_ERR;
- goto error;
- }
-
- SSP(rng_proxy_get_type(proxy, &rng_type));
- res = ssp_rng_create(estimator->dev->allocator, &rng_type, &estimator->rng);
- if(res != RES_OK) {
- log_err(estimator->dev,
- "Could not create the RNG to save the RNG state of the estimation "
- "-- %s\n", res_to_cstr(res));
- goto error;
- }
-
- res = ssp_rng_proxy_write(proxy, stream);
- if(res != RES_OK) {
- log_err(estimator->dev,
- "Could not serialize the RNG state of the estimation -- %s\n",
- res_to_cstr(res));
- goto error;
- }
-
- rewind(stream);
- res = ssp_rng_read(estimator->rng, stream);
- if(res != RES_OK) {
- log_err(estimator->dev,
- "Could not save the RNG state of the estimation -- %s\n",
- res_to_cstr(res));
- goto error;
- }
-
-exit:
- if(stream) fclose(stream);
- return res;
-error:
- if(estimator->rng) {
- SSP(rng_ref_put(estimator->rng));
- estimator->rng = NULL;
- }
- goto exit;
+ return create_rng_from_rng_proxy(estimator->dev, proxy, &estimator->rng);
}
diff --git a/src/sdis_estimator_buffer.c b/src/sdis_estimator_buffer.c
@@ -19,6 +19,8 @@
#include "sdis_estimator_buffer_c.h"
#include "sdis_log.h"
+#include <star/ssp.h>
+
struct sdis_estimator_buffer {
struct sdis_estimator** estimators; /* Row major per pixe lestimators */
size_t width;
@@ -29,6 +31,9 @@ struct sdis_estimator_buffer {
size_t nrealisations; /* #successes */
size_t nfailures;
+ /* State of the RNG after the simulation */
+ struct ssp_rng* rng;
+
ref_T ref;
struct sdis_device* dev;
};
@@ -54,6 +59,7 @@ estimator_buffer_release(ref_T* ref)
MEM_RM(dev->allocator, buf->estimators);
}
+ if(buf->rng) SSP(rng_ref_put(buf->rng));
MEM_RM(dev->allocator, buf);
SDIS(device_ref_put(dev));
}
@@ -136,13 +142,22 @@ sdis_estimator_buffer_get_temperature
res_T
sdis_estimator_buffer_get_realisation_time
-(const struct sdis_estimator_buffer* buf, struct sdis_mc* mc)
+ (const struct sdis_estimator_buffer* buf, struct sdis_mc* mc)
{
if(!buf || !mc) return RES_BAD_ARG;
SETUP_MC(mc, &buf->realisation_time);
return RES_OK;
}
+res_T
+sdis_estimator_buffer_get_rng_state
+ (const struct sdis_estimator_buffer* buf, struct ssp_rng** rng_state)
+{
+ if(!buf || !rng_state) return RES_BAD_ARG;
+ *rng_state = buf->rng;
+ return RES_OK;
+}
+
#undef SETUP_MC
/*******************************************************************************
@@ -243,3 +258,18 @@ estimator_buffer_setup_realisation_time
buf->realisation_time.count = buf->nrealisations;
}
+res_T
+estimator_buffer_save_rng_state
+ (struct sdis_estimator_buffer* buf,
+ const struct ssp_rng_proxy* proxy)
+{
+ ASSERT(buf && proxy);
+
+ /* Release the previous RNG state if any */
+ if(buf->rng) {
+ SSP(rng_ref_put(buf->rng));
+ buf->rng = NULL;
+ }
+ return create_rng_from_rng_proxy(buf->dev, proxy, &buf->rng);
+}
+
diff --git a/src/sdis_estimator_buffer_c.h b/src/sdis_estimator_buffer_c.h
@@ -54,5 +54,10 @@ estimator_buffer_setup_realisation_time
const double sum,
const double sum2);
+extern LOCAL_SYM res_T
+estimator_buffer_save_rng_state
+ (struct sdis_estimator_buffer* buf,
+ const struct ssp_rng_proxy* proxy);
+
#endif /* SDIS_ESTIMATOR_BUFFER_C_H */
diff --git a/src/sdis_solve.c b/src/sdis_solve.c
@@ -494,6 +494,8 @@ sdis_solve_camera
estimator_buffer_setup_realisations_count(buf, nrealisations, nsuccesses);
estimator_buffer_setup_temperature(buf, acc_temp.sum, acc_temp.sum2);
estimator_buffer_setup_realisation_time(buf, acc_time.sum, acc_time.sum2);
+ res = estimator_buffer_save_rng_state(buf, rng_proxy);
+ if(res != RES_OK) goto error;
exit:
if(rngs) {
diff --git a/src/test_sdis_solve_camera.c b/src/test_sdis_solve_camera.c
@@ -35,7 +35,7 @@
/*
* The scene is composed of a solid cube whose temperature is unknown. The
* emissivity of the cube is 1 and its convection coefficient with the
- * surrounding fluid at 290Kk is 0.1. At the center of the cube there is a spherical
+ * surrounding fluid at 300K is 0.1. At the center of the cube there is a spherical
* fluid cavity whose temperature is 350K. The convection coefficient between
* the solid and the cavity is 1 and the emissivity of this interface is null.
* The ambient radiative temperature of the system is 300K.
@@ -563,7 +563,7 @@ main(int argc, char** argv)
create_fluid(dev, &fluid_param, &fluid0);
/* Create the fluid1 */
- fluid_param.temperature = 290;
+ fluid_param.temperature = 300;
fluid_param.rho = 0;
fluid_param.cp = 0;
create_fluid(dev, &fluid_param, &fluid1);