8#if defined(__unix) || defined(__APPLE__)
15#define fork() TotalInsanityAsStub<pid_t>()
16#define wait(X) TotalInsanityAsStub<pid_t>()
29inline void wr_block(
const void*,
size_t,
const char*);
30inline void rd_block(
void*,
size_t,
const char*);
33template<
class X,
class Y,
int NP,
int NSTR>
40 memset(
this, 0,
sizeof(*
this) );
42 p_xmax = numeric_limits<X>::max();
43 p_ymax = numeric_limits<Y>::max() / Y(2.);
44 for(
int i=0; i < 2*NP+1; ++i )
46 for(
int j=0; j < NP; ++j )
50 for(
int i=0; i < NP; ++i )
56 for(
int j=0; j < NP; ++j )
83 p_size =
static_cast<uint32
>(
reinterpret_cast<size_t>(&
p_size) -
reinterpret_cast<size_t>(
this));
87template<
class X,
class Y,
int NP,
int NSTR>
92 if(
cpu.i().lgMaster() && strlen(fnam) > 0 )
95 bool lgErr = ( fdes == NULL );
96 lgErr = lgErr || ( fwrite( &
p_size,
sizeof(uint32), 1, fdes ) != 1 );
97 lgErr = lgErr || ( fwrite(
this,
static_cast<size_t>(
p_size), 1, fdes ) != 1 );
98 lgErr = lgErr || ( fclose(fdes) != 0 );
101 printf(
"p_wr_state: error writing file: %s\n", fnam );
108template<
class X,
class Y,
int NP,
int NSTR>
115 bool lgErr = ( fread( &wrsize,
sizeof(uint32), 1, fdes ) != 1 );
116 lgErr = lgErr || (
p_size != wrsize );
117 lgErr = lgErr || ( fread(
this,
static_cast<size_t>(
p_size), 1, fdes ) != 1 );
118 lgErr = lgErr || ( fclose(fdes) != 0 );
121 printf(
"p_rd_state: error reading file: %s\n", fnam );
127template<
class X,
class Y,
int NP,
int NSTR>
152 fprintf(
ioQQQ,
"creating the child process failed\n" );
167 if( (jj%
cpu.i().nCPU()) ==
cpu.i().nRANK() )
177template<
class X,
class Y,
int NP,
int NSTR>
184 char fnam1[20], fnam2[20];
185 sprintf(fnam1,
"yval_%d",jj);
186 sprintf(fnam2,
"output_%d",jj);
188 FILE *ioQQQ_old =
ioQQQ;
211template<
class X,
class Y,
int NP,
int NSTR>
243template<
class X,
class Y,
int NP,
int NSTR>
249 if(
cpu.i().lgMaster() )
252 for(
int jj=jlo; jj <= jhi; jj++ )
254 sprintf(fnam,
"yval_%d",jj);
257 sprintf(fnam,
"output_%d",jj);
264template<
class X,
class Y,
int NP,
int NSTR>
269 int jlo = 1, jhi = 0;
270 for(
int j=0; j <
p_nvar; j++ )
273 for(
int jj=2*j+1; jj <= 2*j+2; jj++ )
276 for(
int i=0; i <
p_nvar; i++ )
295template<
class X,
class Y,
int NP,
int NSTR>
300 const Y F0 = Y(1.4142136);
301 const X
F1 = X(0.7071068);
305 for(
int jj=1; jj <= 2*
p_nvar; jj++ )
313 bool lgNewCnt =
p_jmin > 0;
316 bool lgNegd2 =
false;
319 for(
int i=0; i <
p_nvar; i++ )
322 Y d2 = Y(0.5)*
p_yp[2*i+2] -
p_yp[0] + Y(0.5)*
p_yp[2*i+1];
325 xhlp[i] = -
p_dmax*
p_c2[i]*(
static_cast<X
>(
max(
min((Y(0.25)*d1)/
max(d2,Y(1.e-10)),F0),-F0)) -
327 xnrm +=
pow2(xhlp[i]);
329 xnrm =
static_cast<X
>(sqrt(xnrm));
333 for(
int j=0; j <
p_nvar; j++ )
335 for(
int i=0; i <
p_nvar; i++ )
341 p_a2[0][i] *= xhlp[0];
350 amax = abs(
p_a2[0][i]);
363 p_a2[imax][0] = X(1.);
364 p_a2[imax][imax] = X(0.);
370 for(
int i=0; i <
p_nvar; i++ )
373 for(
int j=0; j <
p_nvar; j++ )
377 p_c2[i] =
static_cast<X
>(1./sqrt(
p_c2[i]));
413template<
class X,
class Y,
int NP,
int NSTR>
421 for(
int i=0; i <
p_nvar; i++ )
431template<
class X,
class Y,
int NP,
int NSTR>
438 for(
int i=0; i < n; i++ )
441 for(
int k=0; k < n; k++ )
444 for(
int k=0; k < n; k++ )
446 for(
int j=i+1; j < n; j++ )
449 for(
int k=0; k < n; k++ )
450 ip += a[i][k]*a[j][k];
451 for(
int k=0; k < n; k++ )
452 a[j][k] -= ip*a[i][k];
458template<
class X,
class Y,
int NP,
int NSTR>
463 for(
int i=0; i <
p_nvar; i++ )
473template<
class X,
class Y,
int NP,
int NSTR>
479 for(
int i=0; i <
p_nvar; i++ )
480 for(
int j=0; j <
p_nvar; j++ )
484template<
class X,
class Y,
int NP,
int NSTR>
493 for(
int i=0; i < nvar; i++ )
500template<
class X,
class Y,
int NP,
int NSTR>
503 const char* host_name)
510 if( version != NULL )
511 strncpy(
p_chStr2, version, NSTR-1 );
512 if( host_name != NULL )
513 strncpy(
p_chStr3, host_name, NSTR-1 );
516template<
class X,
class Y,
int NP,
int NSTR>
525template<
class X,
class Y,
int NP,
int NSTR>
537 ASSERT( nvar > 0 && nvar <= NP );
549 for(
int i=0; i <
p_nvar; i++ )
553 for(
int i=0; i <
p_nvar; i++ )
577template<
class X,
class Y,
int NP,
int NSTR>
592 printf(
"optimize continue - file has incompatible version, sorry\n" );
597 printf(
"optimize continue - arrays have wrong dimension, sorry\n" );
602 printf(
"optimize continue - strings have wrong length, sorry\n" );
607 printf(
"optimize continue - wrong number of free parameters, sorry\n" );
622template<
class X,
class Y,
int NP,
int NSTR>
639template<
class X,
class Y,
int NP,
int NSTR>
655template<
class X,
class Y,
int NP,
int NSTR>
663 for(
int i=0; i <
p_nvar; i++ )
681 fprintf(
ioQQQ,
"optimize_phymir: too many parameters are varied, increase LIMPAR\n" );
713 cpu.i().host_name() );
722 fprintf(
ioQQQ,
" Optimizer exceeding maximum iterations.\n" );
723 fprintf(
ioQQQ,
" This can be reset with the OPTIMIZE ITERATIONS command.\n" );
728 for(
int i=0; i < nvarPhymir; i++ )
730 xc[i] = phymir.
xval(i);
734 *ymin = phymir.
yval();
746 if( fwrite(ptr,len,
size_t(1),fdes) != 1 ) {
747 printf(
"error writing on file: %s\n",fnam );
763 if( fread(ptr,len,
size_t(1),fdes) != 1 ) {
764 printf(
"error reading on file: %s\n",fnam );
bool fp_equal(sys_float x, sys_float y, int n=3)
NORETURN void TotalInsanity(void)
#define DEBUG_ENTRY(funcname)
static t_version & Inst()
void continue_from_state(Y(*)(const X[], int), int, const char *, X, int, phymir_mode, int)
void init_strings(const char *, const char *, const char *)
bool lgInitialized() const
void p_rd_state(const char *)
bool p_lgLimitExceeded(const X[]) const
void p_reset_transformation_matrix()
void p_reset_hyperblock()
void p_phygrm(X[][NP], int)
void initial_run(Y(*)(const X[], int), int, const X[], const X[], X, int, phymir_mode, int)
Y p_execute_job(const X[], int, int)
void p_evaluate_hyperblock()
void p_setup_next_hyperblock()
void init_state_file_name(const char *)
X p_delta(int i, int j) const
void p_process_output(int, int)
void optimize_with_restart()
Y(* p_func)(const X[], int)
bool lgConvergedRestart() const
void init_minmax(const X[], const X[], int)
bool lgMaxIterExceeded() const
void p_execute_job_parallel(const X[], int, int) const
void p_wr_state(const char *) const
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
void append_file(FILE *dest, const char *source)
chi2_type optimize_func(const realnum param[], int grid_index=-1)
void optimize_phymir(realnum xc[], const realnum del[], long int nvarPhymir, chi2_type *ymin, realnum toler)
void rd_block(void *, size_t, const char *)
void wr_block(const void *, size_t, const char *)
const char * STATEFILE_BACKUP
STATIC double dist(long, realnum[], realnum[])
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)