9 #if defined(__unix) || defined(__APPLE__)
11 #include <sys/types.h>
16 #define fork() TotalInsanityAsStub<pid_t>()
17 #define wait(X) TotalInsanityAsStub<pid_t>()
20 #if defined(__GNUC_EXCL__) && __GNUC__ >= 8
21 #pragma GCC diagnostic ignored "-Wclass-memaccess"
33 template<
class X,
class Y,
int NP,
int NSTR>
40 memset(
this, 0,
sizeof(*
this) );
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 )
52 p_absmin[i] = -p_xmax;
55 p_varmax[i] = -p_xmax;
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));
87 template<
class X,
class Y,
int NP,
int NSTR>
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 );
108 template<
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 );
127 template<
class X,
class Y,
int NP,
int NSTR>
138 return p_lgLimitExceeded(x) ? p_ymax : p_func(x,runNr);
141 if( p_curcpu > p_maxcpu )
152 fprintf(
ioQQQ,
"creating the child process failed\n" );
158 p_execute_job_parallel( x, jj, runNr );
168 p_execute_job_parallel( x, jj, runNr );
177 template<
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;
193 if( !p_lgLimitExceeded(x) )
197 yval = p_func(x,runNr);
211 template<
class X,
class Y,
int NP,
int NSTR>
224 while( p_curcpu > 0 )
229 p_process_output( jlo, jhi );
234 p_process_output( jlo, jhi );
236 MPI_Bcast( &p_yp[jlo], jhi-jlo+1, MPI_type(p_yp), 0, MPI_COMM_WORLD );
243 template<
class X,
class Y,
int NP,
int NSTR>
252 for(
int jj=jlo; jj <= jhi; jj++ )
254 sprintf(fnam,
"yval_%d",jj);
255 rd_block(&p_yp[jj],
sizeof(p_yp[jj]),fnam);
257 sprintf(fnam,
"output_%d",jj);
264 template<
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++ )
278 p_xp[jj][i] = p_xc[i] + sgn*p_dmax*p_c2[j]*p_a2[j][i];
279 p_varmax[i] =
max(p_varmax[i],p_xp[jj][i]);
280 p_varmin[i] =
min(p_varmin[i],p_xp[jj][i]);
282 if( !lgMaxIterExceeded() )
286 p_yp[jj] = p_execute_job( p_xp[jj], jj, p_noptim++ );
292 p_barrier( jlo, jhi );
295 template<
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++ )
307 if( p_yp[jj] < p_ymin )
313 bool lgNewCnt = p_jmin > 0;
316 bool lgNegd2 =
false;
319 for(
int i=0; i < p_nvar; i++ )
321 Y d1 = p_yp[2*i+2] - p_yp[2*i+1];
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)) -
326 p_delta(2*i+1,p_jmin) + p_delta(2*i+2,p_jmin));
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];
345 p_a2[0][i] += xhlp[j]*p_a2[j][i];
346 p_a2[j][i] = p_delta(i,j);
347 if( j == p_nvar-1 && abs(p_a2[0][i]) > amax )
350 amax = abs(p_a2[0][i]);
356 p_a2[j][i] = p_delta(i,j);
363 p_a2[imax][0] = X(1.);
364 p_a2[imax][imax] = X(0.);
367 p_phygrm( p_a2, p_nvar );
370 for(
int i=0; i < p_nvar; i++ )
373 for(
int j=0; j < p_nvar; j++ )
375 p_c2[i] +=
pow2(p_a2[i][j]/p_c1[j]);
377 p_c2[i] =
static_cast<X
>(1./sqrt(p_c2[i]));
378 p_xc[i] = p_xp[p_jmin][i];
379 p_xp[0][i] = p_xc[i];
381 p_yp[0] = p_yp[p_jmin];
408 p_dmax =
min(
max(xnrm/p_c2[0],p_dmax*r1),p_dmax*r2);
410 p_dmax =
MIN2(p_dmax,p_dold);
413 template<
class X,
class Y,
int NP,
int NSTR>
418 if( !lgConvergedRestart() )
421 for(
int i=0; i < p_nvar; i++ )
423 p_xcold[i] = p_xc[i];
427 p_reset_transformation_matrix();
431 template<
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];
458 template<
class X,
class Y,
int NP,
int NSTR>
463 for(
int i=0; i < p_nvar; i++ )
465 if( x[i] < p_absmin[i] )
467 if( x[i] > p_absmax[i] )
473 template<
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++ )
481 p_a2[j][i] = p_delta(i,j);
484 template<
class X,
class Y,
int NP,
int NSTR>
491 ASSERT( !lgInitialized() );
493 for(
int i=0; i < nvar; i++ )
495 p_absmin[i] = pmin[i];
496 p_absmax[i] = pmax[i];
500 template<
class X,
class Y,
int NP,
int NSTR>
503 const char* host_name)
508 strncpy( p_chStr1, date, NSTR );
509 p_chStr1[NSTR-1] =
'\0';
510 if( version != NULL )
511 strncpy( p_chStr2, version, NSTR );
512 p_chStr2[NSTR-1] =
'\0';
513 if( host_name != NULL )
514 strncpy( p_chStr3, host_name, NSTR );
515 p_chStr3[NSTR-1] =
'\0';
518 template<
class X,
class Y,
int NP,
int NSTR>
524 strncpy( p_chState, fnam, NSTR-1 );
527 template<
class X,
class Y,
int NP,
int NSTR>
539 ASSERT( nvar > 0 && nvar <= NP );
551 for(
int i=0; i < p_nvar; i++ )
552 p_dmax =
max(p_dmax,abs(del[i]));
555 for(
int i=0; i < p_nvar; i++ )
558 p_xcold[i] = p_xc[i] + X(10.)*p_toler;
559 p_c1[i] = abs(del[i])/p_dmax;
561 p_xp[0][i] = p_xc[i];
562 p_varmax[i] =
max(p_varmax[i],p_xc[i]);
563 p_varmin[i] =
min(p_varmin[i],p_xc[i]);
568 p_yp[0] = p_execute_job( p_xc, 0, p_noptim++ );
574 p_reset_transformation_matrix();
576 p_wr_state( p_chState );
579 template<
class X,
class Y,
int NP,
int NSTR>
594 printf(
"optimize continue - file has incompatible version, sorry\n" );
599 printf(
"optimize continue - arrays have wrong dimension, sorry\n" );
604 printf(
"optimize continue - strings have wrong length, sorry\n" );
609 printf(
"optimize continue - wrong number of free parameters, sorry\n" );
624 template<
class X,
class Y,
int NP,
int NSTR>
629 ASSERT( lgInitialized() );
631 while( !lgConverged() )
633 p_evaluate_hyperblock();
634 if( lgMaxIterExceeded() )
636 p_setup_next_hyperblock();
637 p_wr_state( p_chState );
641 template<
class X,
class Y,
int NP,
int NSTR>
646 ASSERT( lgInitialized() );
648 while( !lgConvergedRestart() )
651 if( lgMaxIterExceeded() )
653 p_reset_hyperblock();
657 template<
class X,
class Y,
int NP,
int NSTR>
665 for(
int i=0; i < p_nvar; i++ )
666 dist +=
pow2(p_xc[i] - p_xcold[i]);
667 dist =
static_cast<X
>(sqrt(dist));
668 return ( dist <= p_toler );
683 fprintf(
ioQQQ,
"optimize_phymir: too many parameters are varied, increase LIMPAR\n" );
725 fprintf(
ioQQQ,
" Optimizer exceeding maximum iterations.\n" );
726 fprintf(
ioQQQ,
" This can be reset with the OPTIMIZE ITERATIONS command.\n" );
731 for(
int i=0; i < nvarPhymir; i++ )
733 xc[i] = phymir.
xval(i);
737 *ymin = phymir.
yval();
void p_rd_state(const char *)
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
void set_used_nCPU(long n)
NORETURN void TotalInsanity(void)
Y p_execute_job(const X[], int, int)
chi2_type optimize_func(const realnum param[], int grid_index=-1)
realnum varang[LIMPAR][2]
void p_wr_state(const char *) const
void optimize_phymir(realnum[], const realnum[], long, chi2_type *, realnum)
void p_execute_job_parallel(const X[], int, int) const
static t_version & Inst()
void init_strings(const char *, const char *, const char *)
void continue_from_state(Y(*)(const X[], int), int, const char *, X, int, phymir_mode, int)
bool fp_equal(sys_float x, sys_float y, int n=3)
void init_minmax(const X[], const X[], int)
void rd_block(void *ptr, size_t len, FILE *fdes)
bool lgConvergedRestart() const
STATIC double dist(long, realnum[], realnum[])
void p_reset_transformation_matrix()
void append_file(FILE *dest, const char *source)
void wr_block(const void *ptr, size_t len, FILE *fdes)
STATIC void start(long, realnum[], realnum[], long, long[], realnum *, int *)
void init_state_file_name(const char *)
#define DEBUG_ENTRY(funcname)
bool p_lgLimitExceeded(const X[]) const
bool lgMaxIterExceeded() const
void p_setup_next_hyperblock()
int fprintf(const Output &stream, const char *format,...)
void p_reset_hyperblock()
void optimize_with_restart()
void p_evaluate_hyperblock()
void p_process_output(int, int)
const char * STATEFILE_BACKUP
const char * host_name() const
void initial_run(Y(*)(const X[], int), int, const X[], const X[], X, int, phymir_mode, int)
#define MPI_Bcast(V, W, X, Y, Z)
void p_phygrm(X[][NP], int)