diff --git a/src/clocks.c b/src/clocks.c index 9144830572a833d89521b84ded3a54039daa00e7..0b0aa699c32067efcebfc0aeb4ba46d38e95cb69 100644 --- a/src/clocks.c +++ b/src/clocks.c @@ -47,6 +47,9 @@ static unsigned long long clocks_cpufreq = 0; * time. */ ticks clocks_start_ticks = 0; +/* Whether to use a signal random seed value. Could be useful for testing. */ +static int clocks_fixed_seed = 0; + /* The units of any returned times. */ static const char *clocks_units[] = {"ms", "~ms"}; static int clocks_units_index = 0; @@ -308,6 +311,12 @@ void clocks_get_cputimes_used(double *usertime, double *systime) { * @result an integer. */ int clocks_random_seed(void) { + + /* If using a fixed seed, nothing to do. */ + if (clocks_fixed_seed != 0) { + return clocks_fixed_seed; + } + #ifdef HAVE_CLOCK_GETTIME struct timespec timespec; clock_gettime(CLOCK_REALTIME, ×pec); @@ -317,6 +326,25 @@ int clocks_random_seed(void) { #endif } +/** + * @brief Set a fixed seed value. + * + * Only expected to be useful when developing and attempting to keep the + * randomness under control. Doubt it will help that much. + * + * @param seed the seed value, if 0 then a single random seed will be + * generated for use. + * @result the fixed seed value. + */ +int clocks_fixed_random_seed(int seed) { + if (seed == 0) { + clocks_fixed_seed = clocks_random_seed(); + } else { + clocks_fixed_seed = seed; + } + return clocks_fixed_seed; +} + /** * @brief Get the current time, either in SWIFT format, "%T %F %Z" * or seconds in the epoch. diff --git a/src/clocks.h b/src/clocks.h index e39d8e8195439f37dff477a62401f023b755078d..f5ca07382390122e4fa694e659de82e9c4efdc58 100644 --- a/src/clocks.h +++ b/src/clocks.h @@ -54,6 +54,7 @@ double clocks_get_hours_since_start(void); void clocks_get_cputimes_used(double *usertime, double *systime); int clocks_random_seed(void); +int clocks_fixed_random_seed(int seed); const char *clocks_now(int swift); diff --git a/src/partition.c b/src/partition.c index 26d0e10595732582b328befa1c94924fbc699350..001745c2af1b4032c7a573755a15d0f0004e152b 100644 --- a/src/partition.c +++ b/src/partition.c @@ -1953,11 +1953,11 @@ void partition_initial_partition(struct partition *initial_partition, /* Restoring the partition from a file saved during a previous run (or * created by the user!). */ - partition_restore_partition(initial_partition->savedfilename, + partition_restore_partition(initial_partition->savedfilename, s->cells_top, s->nr_cells); #ifdef SWIFT_DEBUG_CHECKS - partition_save_partition("initial_partition_check.dat", + partition_save_partition("initial_partition_check.dat", s->cells_top, s->nr_cells); #endif @@ -2062,7 +2062,7 @@ void partition_initial_partition(struct partition *initial_partition, if (initial_partition->type != INITPART_SAVED_FILE && initial_partition_save) { partition_save_partition(initial_partition->savedfilename, s->cells_top, s->nr_cells); } - + if (s->e->verbose) message("took %.3f %s.", clocks_from_ticks(getticks() - tic), clocks_getunit()); @@ -2138,7 +2138,7 @@ void partition_init(struct partition *partition, } /* In case of grid, read more parameters */ - if (part_type[0] == 'g') { + if (partition->type == INITPART_GRID) { parser_get_opt_param_int_array(params, "DomainDecomposition:initial_grid", 3, partition->grid); } @@ -2152,6 +2152,12 @@ void partition_init(struct partition *partition, parser_get_opt_param_string(params, "DomainDecomposition:saved_filename", partition->savedfilename, "initial_partition.dat"); + /* In saved initial partition mode let's also fixed randomness a bit. */ + if (partition->type == INITPART_SAVED_FILE || initial_partition_save) { + int seed = clocks_fixed_random_seed(42); + message("using fixed random seed: %d", seed); + } + /* Now let's check what the user wants as a repartition strategy */ parser_get_opt_param_string(params, "DomainDecomposition:repartition_type", part_type, default_repart); @@ -2667,7 +2673,7 @@ void partition_struct_restore(struct repartition *reparttype, FILE *stream) { * @param nr_cells the number of cells. */ void partition_save_partition(const char *fname, struct cell *cells_top, - int nr_cells) { + int nr_cells) { FILE *file = NULL; file = fopen(fname, "w"); @@ -2680,7 +2686,7 @@ void partition_save_partition(const char *fname, struct cell *cells_top, /* Output */ for (int i = 0; i < nr_cells; i++) { struct cell *c = &cells_top[i]; - fprintf(file, "%6d %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6d\n", i, + fprintf(file, "%6d %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6d\n", i, c->loc[0], c->loc[1], c->loc[2], c->width[0], c->width[1], c->width[2], c->nodeID); } @@ -2698,7 +2704,7 @@ void partition_save_partition(const char *fname, struct cell *cells_top, * @param nr_cells the number of cells. */ void partition_restore_partition(const char *fname, struct cell *cells_top, - int nr_cells) { + int nr_cells) { FILE *file = NULL; file = fopen(fname, "r"); diff --git a/src/space.c b/src/space.c index d955654f9afbe212d532f3076d5a00e5cc8104d2..cac7eefb7ffe5cc3555be1f4b0168a6727ae7a22 100644 --- a/src/space.c +++ b/src/space.c @@ -1101,7 +1101,7 @@ void space_init(struct space *s, struct swift_params *params, #endif /* Initiate some basic randomness */ - srand(42); + srand(clocks_random_seed()); /* Are we remapping the IDs to the range [1, NumPart]? */ if (remap_ids) {