12#include "gadgetconfig.h"
14#include <gsl/gsl_rng.h>
22#include "../data/allvars.h"
23#include "../data/dtypes.h"
24#include "../data/mymalloc.h"
25#include "../domain/domain.h"
27#include "../io/restart.h"
28#include "../lightcone/lightcone.h"
29#include "../logs/logs.h"
30#include "../logs/timer.h"
31#include "../main/simulation.h"
32#include "../mpi_utils/mpi_utils.h"
33#include "../ngbtree/ngbtree.h"
34#include "../system/system.h"
35#include "../time_integration/timestep.h"
77 double *old_dbl = (
double *)((
char *)&
All + off);
78 double *new_dbl = (
double *)((
char *)&all + off);
80 if(*new_dbl != *old_dbl)
90 char *old_p = (
char *)&
All + off;
91 char *new_p = (
char *)&all + off;
102 int *old_int = (
int *)((
char *)&
All + off);
103 int *new_int = (
int *)((
char *)&all + off);
105 if(*new_int != *old_int)
127void restart::backup_restartfiles(
int task)
132 int bak_files_status = 0;
134 mpi_printf(
"RESTART: Backing up restart files...\n");
136 sprintf(buf,
"%s/restartfiles/%s.%d",
All.
OutputDir,
"restart", task);
137 sprintf(buf_bak,
"%s/restartfiles/bak-%s.%d",
All.
OutputDir,
"restart", task);
139 if((fcheck = fopen(buf,
"r")))
143 rename(buf, buf_bak);
145 bak_files_status = 1;
148 int bak_files_status_sum;
149 MPI_Allreduce(&bak_files_status, &bak_files_status_sum, 1, MPI_INT, MPI_SUM,
Communicator);
151 if(bak_files_status_sum !=
NTask && bak_files_status_sum != 0)
152 mpi_printf(
"RESTART: some (%d) restart files were renamed to bak, but some (%d) weren't - something is very possibly wrong!",
153 bak_files_status,
NTask - bak_files_status);
154 else if(bak_files_status_sum ==
NTask)
155 mpi_printf(
"RESTART: done renaming pre-existing restart files to bak files.\n");
156 else if(bak_files_status_sum == 0)
157 mpi_printf(
"RESTART: no pre-existing restart files for renaming were found.\n");
170void restart::do_restart(
int modus)
172#ifdef DO_NOT_PRODUCE_BIG_OUTPUT
175 mpi_printf(
"RESTART: Omitting writing restart files.\n");
186 mpi_printf(
"RESTART: Loading restart files...\n");
188 mpi_printf(
"RESTART: Writing restart files.\n");
201 mpi_printf(
"NOTICE: MaxFilesWithConcurrentIO has been reduced to the number of processors\n");
207 mpi_printf(
"NOTICE: MaxFilesWithConcurrentIO has been set to be equal to the number of processors\n");
236 Terminate(
"The restart file on task=%d is not consistent with the one on task=0\n",
ThisTask);
244 mpi_printf(
"RESTART: done. load/save took %g sec, total size %g MB, corresponds to effective I/O rate of %g MB/sec\n",
245 Logs.
timediff(t0, t1), byte_count_all / (1024.0 * 1024.0), byte_count_all / (1024.0 * 1024.0) /
Logs.
timediff(t0, t1));
250void restart::polling(
int modus)
253 if(files_completed <
NTask)
263 int source = status.MPI_SOURCE;
269 if(files_started <
NTask)
271 if((files_started % files_concurrent) == 0)
274 mpi_printf(
"RESTART: Loading restart files group #%d out of %d...\n", (files_started / files_concurrent) + 1,
277 mpi_printf(
"RESTART: Writing restart files group #%d out of %d...\n", (files_started / files_concurrent) + 1,
288void restart::work_files(
int modus)
291 if(!(seq = (seq_data *)malloc(
NTask *
sizeof(seq_data))))
299 MPI_Gather(&seq_loc,
sizeof(seq_data), MPI_BYTE, seq,
sizeof(seq_data), MPI_BYTE, 0,
Communicator);
306 std::sort(seq, seq +
NTask);
311 if((files_started % files_concurrent) == 0)
314 mpi_printf(
"RESTART: Loading restart files group #%d out of %d...\n", (files_started / files_concurrent) + 1,
317 mpi_printf(
"RESTART: Writing restart files group #%d out of %d...\n", (files_started / files_concurrent) + 1,
328 contents_restart_file(modus);
331 if(files_started <
NTask)
333 if((files_started % files_concurrent) == 0)
336 mpi_printf(
"RESTART: Loading restart files group #%d out of %d...\n", (files_started / files_concurrent) + 1,
339 mpi_printf(
"RESTART: Writing restart files group #%d out of %d...\n", (files_started / files_concurrent) + 1,
347 while(files_completed <
NTask)
358 contents_restart_file(modus);
368void restart::contents_restart_file(
int modus)
375 if(!(fd = fopen(buf,
"r")))
377 Terminate(
"RESTART: Restart file '%s' not found.\n", buf);
382 if(!(fd = fopen(buf,
"w")))
384 Terminate(
"RESTART: Restart file '%s' cannot be opened.\n", buf);
418#if defined(MERGERTREE) && defined(SUBFIND)
419 byten(&Sim->MergerTree.PrevTotNsubhalos,
sizeof(
long long), modus);
420 byten(&Sim->MergerTree.PrevNsubhalos,
sizeof(
int), modus);
424#ifdef LIGHTCONE_PARTICLES
429 in(&Sim->Lp.MaxPart, modus);
430 byten(&Sim->Lp.TotNumPart,
sizeof(Sim->Lp.TotNumPart), modus);
433 Sim->Lp.allocate_memory();
435 in(&Sim->Lp.NumPart, modus);
436 byten(&Sim->Lp.P[0], Sim->Lp.NumPart *
sizeof(lightcone_particle_data), modus);
438 in(&Sim->LightCone.NumLastCheck, modus);
442#ifdef LIGHTCONE_MASSMAPS
443 in(&Sim->Mp.MaxPart, modus);
446 Sim->Mp.allocate_memory();
448 in(&Sim->Mp.NumPart, modus);
449 byten(&Sim->Mp.P[0], Sim->Mp.NumPart *
sizeof(lightcone_massmap_data), modus);
454 Sim->LightCone.Mp->Npix = nside2npix(
All.LightConeMassMapsNside);
457 Sim->LightCone.MassMap =
458 (
double *)
Mem.mymalloc_movable_clear(&Sim->LightCone.MassMap,
"MassMap", Sim->LightCone.Mp->NpixLoc *
sizeof(
double));
461 byten(Sim->LightCone.MassMap, Sim->LightCone.Mp->NpixLoc *
sizeof(
double), modus);
467 byten(
Logs.
CPU_Step, logs::CPU_LAST *
sizeof(
double), modus);
469 byten(
Logs.
CPU_Sum, logs::CPU_LAST *
sizeof(
double), modus);
492 byten(Sim->
Sp.TimeBinSfr,
TIMEBINS *
sizeof(
double), modus);
497 in(&Sim->
Domain.NTopleaves, modus);
498 in(&Sim->
Domain.NTopnodes, modus);
544 byten(Sim->
Domain.TopNodes, Sim->
Domain.NTopnodes * Sim->
Domain.domain_sizeof_topnode_data(), modus);
545 byten(Sim->
Domain.TaskOfLeaf, Sim->
Domain.NTopleaves *
sizeof(
int), modus);
546 byten(Sim->
Domain.ListOfTopleaves, Sim->
Domain.NTopleaves *
sizeof(
int), modus);
547 byten(Sim->
Domain.FirstTopleafOfTask,
NTask *
sizeof(
int), modus);
548 byten(Sim->
Domain.NumTopleafOfTask,
NTask *
sizeof(
int), modus);
563void restart::readjust_timebase(
double TimeMax_old,
double TimeMax_new)
565 mpi_printf(
"\nRESTART: All.TimeMax has been changed in the parameterfile from %g to %g. Need to adjust integer timeline.\n",
566 TimeMax_old, TimeMax_new);
568 if(TimeMax_new < TimeMax_old)
569 Terminate(
"\nIt is not allowed to reduce All.TimeMax\n");
585#if defined(PMGRID) && !defined(TREEPM_NOTIMESPLIT)
586 All.PM_Ti_begstep /= 2;
587 All.PM_Ti_endstep /= 2;
599 Terminate(
"Error in readjust_timebase(). Minimum Timebin for particle %d reached.\n", i);
609 Terminate(
"Error in readjust_timebase(). Minimum Timebin (hydro) for sph particle %d reached.\n", i);
618void restart::byten(
void *x,
size_t n,
int modus)
631 byten_doit(p, n, modus);
636void restart::byten_doit(
void *x,
size_t n,
int modus)
650void restart::in(
int *x,
int modus) { byten(x,
sizeof(
int), modus); }
global_data_all_processes All
size_t my_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
A wrapper for the fread() function.
void reset_io_byte_count(void)
long long get_io_byte_count(void)
size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
A wrapper for the fwrite() function.
double CPU_Step[CPU_LAST]
double CPU_Step_Stored[CPU_LAST]
double timediff(double t0, double t1)
char ParametersType[MAX_PARAMETERS]
char ParametersTag[MAX_PARAMETERS][MAXLEN_PARAM_TAG]
void * ParametersValue[MAX_PARAMETERS]
char ParametersChangeable[MAX_PARAMETERS]
void load(sim *Sim_ptr)
This function loads the last restart file.
void mpi_printf(const char *fmt,...)
int Island_Smallest_WorldTask
domain< simparticles > Domain
void allocate_memory(void)
TimeBinData TimeBinsGravity
TimeBinData TimeBinsHydro
int TimeBinSynchronized[TIMEBINS]
int TreeSharedMem_ThisTask
void treeallocate_share_topnode_addresses(void)
MPI_Comm TreeSharedMemComm
void treeallocate(int max_partindex, partset *Pptr, domain< partset > *Dptr)
unsigned char * NodeLevel
#define MAXLEN_PATH_EXTRA
#define TIMER_START(counter)
Starts the timer counter.
#define TIMER_STOP(counter)
Stops the timer counter.
void sumup_longs(int n, long long *src, long long *res, MPI_Comm comm)
#define MAXLEN_PARAM_VALUE
int TimeBinCount[TIMEBINS]
int FirstInTimeBin[TIMEBINS]
long long GlobalNActiveParticles
int LastInTimeBin[TIMEBINS]
int MaxFilesWithConcurrentIO
int ComovingIntegrationOn
char OutputDir[MAXLEN_PATH]
double OutputListTimes[MAXLEN_OUTPUTLIST]
size_t get_data_size(void)
char OutputListFlag[MAXLEN_OUTPUTLIST]
char * get_data_ptr(void)
std::atomic< integertime > Ti_Current
unsigned char getTimeBinHydro(void)
unsigned char getType(void)
void setTimeBinHydro(unsigned char bin)
gsl_rng * random_generator
void subdivide_evenly(long long N, int pieces, int index_bin, long long *first, int *count)