void AsyncByteStream::InitAsyncHandler() { if ( _asyncIOThread.joinable() ) throw std::logic_error("The async IO thread already been started"); _asyncIOThread = std::thread([&](){ // atomically pull out the event flags here ThreadEvent t = _event.exchange(Wait); if ( t == Terminate ) { return; } bool hasRead = false, hasWritten = false; uint8_t buf[4096]; if ( (t & ReadSpaceAvailable) == ReadSpaceAvailable ) { std::lock_guard<RingBuffer> _(_ringbuf); size_type read = this->read_for_async(buf, _ringbuf.SpaceAvailable()); if ( read != 0 ) { _ringbuf.WriteBytes(buf, read); hasRead = true; } } if ( (t & DataToWrite) == DataToWrite ) { std::lock_guard<RingBuffer> _(_ringbuf); size_type written = _ringbuf.ReadBytes(buf, _ringbuf.BytesAvailable()); written = this->write_for_async(buf, written); if ( written != 0 ) { // only remove as much as actually went out _ringbuf.RemoveBytes(written); hasWritten = true; } } if ( hasRead ) _eventHandler(AsyncEvent::HasBytesAvailable, this); if ( hasWritten ) _eventHandler(AsyncEvent::HasSpaceAvailable, this); std::unique_lock<std::mutex> __l(_condMutex); _threadSignal.wait(__l, [&](){ return _event != Wait; }); }); }
bool SolverUnconstrained<Data,Problem>::optimize( vector_type& x ) { M_solver_stats.clear(); // Controlling parameters // trust region radius value_type Delta = M_options.Delta_init; vector_type x_new ( x.size() ); vector_type stot ( x.size() ); value_type norm_Tgrad_fx = this->norm_Theta_x_grad_fx( x, Delta ); value_type norm_s_til = 0; M_prob.copy_x0_to_x( x ); // FIXME: if ( M_options.verbose() ) //M_prob.print_complete( x ); if ( M_solver_stats.collectStats() ) M_solver_stats.push( norm_Tgrad_fx, 0, 0, 0, 0, 0, 0, 0, Delta, 0, 0, 0 ); try { int iter = 0; // // Apply bound constrained Trust region algorithm // while ( iter == 0 || ( iter < M_options.max_TR_iter && norm_Tgrad_fx > M_options.TR_tol ) ) { iter++; int n_CGiter, n_restarts, n_indef, n_crosses_def, n_crosses_indef, n_truss_exit_def, n_truss_exit_indef; value_type _s_til_x_G_til_x_s_til, phi_til, rho, Delta_used = Delta; DVLOG(2) << "\n===================== iter = " << iter << " ==========================="; //DVLOG(2) << "\nx = " << x << "\n"; DVLOG(2) << "\n -> norm_Tgrad_fx = " << norm_Tgrad_fx; /** find an approximate stot for the step to make * solve : * Find \f$\tilde{s}^k = \mathrm{arg}\mathrm{min}_{s \in R^n}{\tilde{\phi}^k : ||s|| < \Delta^k}\f$ */ this->CGstep( x, Delta, stot, norm_s_til, n_CGiter, n_restarts, n_indef, n_crosses_def, n_crosses_indef, n_truss_exit_def, n_truss_exit_indef, _s_til_x_G_til_x_s_til, phi_til ); // x_new = x + stot; f_type __fx_new; M_prob.evaluate( x_new, __fx_new, diff_order<0>() ); f_type __fx; M_prob.evaluate( x, __fx, diff_order<0>() ); // compute actual merit function reduction value_type ared_til = __fx_new.value( 0 ) - __fx.value( 0 ) + 0.5 * _s_til_x_G_til_x_s_til; rho = ared_til / phi_til; ///////////////////////////////////////////////////////////////////////// // Trust region radius calculation: if ( M_options.allow_Trust_radius_calculations ) { if ( rho <= 1e-8 ) Delta = M_options.rho_decrease * Delta; else { x = x + stot; if ( rho > M_options.rho_big ) Delta = std::max( M_options.rho_increase_big*norm_s_til, Delta ); else if ( rho > M_options.rho_small ) Delta = std::max( M_options.rho_increase_small*norm_s_til, Delta ); } norm_Tgrad_fx = this->norm_Theta_x_grad_fx( x, Delta ); } else { x = x + stot; norm_Tgrad_fx = this->norm_Theta_x_grad_fx( x, Delta ); } ///////////////////////////////////////////////////////////////////////// if ( M_solver_stats.collectStats() ) { M_solver_stats.push( norm_Tgrad_fx, n_CGiter, n_restarts, n_indef, n_crosses_def, n_crosses_indef, n_truss_exit_def, n_truss_exit_indef, Delta_used, ared_til, phi_til, rho ); } if ( norm_Tgrad_fx > 1e-5 ) M_prob.setAccuracy( std::min( 1e-1, norm_Tgrad_fx ) ); DVLOG(2) << "norm_Tgrad_fx = " << norm_Tgrad_fx << "\n"; } } catch ( std::exception const& __ex ) { f_type __fx_new; M_prob.evaluate ( x, __fx_new, diff_order<2>() ); if ( norm_inf( __fx_new.gradient( 0 ) ) > 1e-10 ) throw __ex; } vector_type __l( _E_n ), __u( _E_n ); lambda_LS( x, __l, __u ); if ( M_solver_stats.collectStats() ) M_solver_stats.push( x, __l, __u ); return true; }