Exemplo n.º 1
0
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
rotate_right(node_pointer p_x)
{
  node_pointer p_y = p_x->m_p_left;

  p_x->m_p_left = p_y->m_p_right;

  if (p_y->m_p_right != 0)
    p_y->m_p_right->m_p_parent = p_x;

  p_y->m_p_parent = p_x->m_p_parent;

  if (p_x == m_p_head->m_p_parent)
    m_p_head->m_p_parent = p_y;
  else if (p_x == p_x->m_p_parent->m_p_right)
    p_x->m_p_parent->m_p_right = p_y;
  else
    p_x->m_p_parent->m_p_left = p_y;

  p_y->m_p_right = p_x;
  p_x->m_p_parent = p_y;

  PB_DS_ASSERT_NODE_CONSISTENT(p_x)
  PB_DS_ASSERT_NODE_CONSISTENT(p_y)

  apply_update(p_x, (node_update* )this);
  apply_update(p_x->m_p_parent, (node_update* )this);
}
Exemplo n.º 2
0
void PreUpdater::receive(Message *update) {
    try {
        apply_update(update);
    } catch (const DataError& err) {
        BOOST_LOG_TRIVIAL(error) << "Invalid update received; " << err.what();
    }
}
Exemplo n.º 3
0
void LinearLayer::update(double learning_rate) {
  MatrixAdd apply_update(-1*learning_rate);
  apply_update.execute(m_weights, m_weights_update);
  apply_update.execute(m_bias, m_bias_update);
  
  std::cout << "Sum of weight updates linear layer" << DataAbsSum().execute(m_weights_update) << std::endl;
}
Exemplo n.º 4
0
inline void
PB_DS_CLASS_C_DEC::
update_to_top(node_pointer p_nd, Node_Update_* p_update)
{
  while (p_nd != m_p_head)
    {
      apply_update(p_nd, p_update);

      p_nd = p_nd->m_p_parent;
    }
}
Exemplo n.º 5
0
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::entry_pointer
PB_DS_CLASS_C_DEC::
find_imp(const_key_reference r_key) const
{
  if (m_p_l == NULL)
    return NULL;
  if (s_eq_fn(r_key, PB_DS_V2F(m_p_l->m_value)))
    {
      apply_update(m_p_l, s_metadata_type_indicator);
      _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key);)
      return m_p_l;
Exemplo n.º 6
0
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::entry_pointer
PB_DS_CLASS_C_DEC::
find_imp(key_const_reference r_key) const
{
  if (m_p_l == 0)
    return 0;
  if (s_eq_fn(r_key, PB_DS_V2F(m_p_l->m_value)))
    {
      apply_update(m_p_l, s_metadata_type_indicator);
      PB_DS_CHECK_KEY_EXISTS(r_key)
      return m_p_l;
    }
Exemplo n.º 7
0
static int sj_init() {
  struct v7 *v7 = s_v7;

  LOG(LL_INFO, ("Mongoose IoT Firmware %s", build_id));
  LOG(LL_INFO,
      ("RAM: %d total, %d free", sj_get_heap_size(), sj_get_free_heap_size()));

  int r = start_nwp();
  if (r < 0) {
    LOG(LL_ERROR, ("Failed to start NWP: %d", r));
    return 0;
  }

  int boot_cfg_idx = get_active_boot_cfg_idx();
  if (boot_cfg_idx < 0) return 0;
  struct boot_cfg boot_cfg;
  if (read_boot_cfg(boot_cfg_idx, &boot_cfg) < 0) return 0;

  LOG(LL_INFO, ("Boot cfg %d: 0x%llx, 0x%u, %s @ 0x%08x, %s", boot_cfg_idx,
                boot_cfg.seq, boot_cfg.flags, boot_cfg.app_image_file,
                boot_cfg.app_load_addr, boot_cfg.fs_container_prefix));

  uint64_t saved_seq = 0;
  if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    /* Tombstone the current config. If anything goes wrong between now and
     * commit, next boot will use the old one. */
    saved_seq = boot_cfg.seq;
    boot_cfg.seq = BOOT_CFG_TOMBSTONE_SEQ;
    write_boot_cfg(&boot_cfg, boot_cfg_idx);
  }

  r = init_fs(boot_cfg.fs_container_prefix);
  if (r < 0) {
    LOG(LL_ERROR, ("FS init error: %d", r));
    if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
      revert_update(boot_cfg_idx, &boot_cfg);
    }
    return 0;
  }

  if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    LOG(LL_INFO, ("Applying update"));
    if (apply_update(boot_cfg_idx, &boot_cfg) < 0) {
      revert_update(boot_cfg_idx, &boot_cfg);
    }
  }

  mongoose_init();

#ifndef CS_DISABLE_JS
  v7 = s_v7 = init_v7(&v7);

  /* Disable GC during JS API initialization. */
  v7_set_gc_enabled(v7, 0);
  sj_gpio_api_setup(v7);
  sj_i2c_api_setup(v7);
  sj_wifi_api_setup(v7);
  sj_timers_api_setup(v7);
#endif

  sj_v7_ext_api_setup(v7);
  sj_init_sys(v7);
  sj_wifi_init(v7);
#ifndef DISABLE_C_CLUBBY
  sj_clubby_init();
#endif

  sj_http_api_setup(v7);

#if !defined(DISABLE_C_CLUBBY) && !defined(CS_DISABLE_JS)
  sj_clubby_api_setup(v7);
#endif

  /* Common config infrastructure. Mongoose & v7 must be initialized. */
  init_device(v7);

  sj_updater_post_init(v7);
#ifndef DISABLE_C_CLUBBY
  init_updater_clubby(v7);
#endif

#ifndef CS_DISABLE_JS
  /* SJS initialized, enable GC back, and trigger it */
  v7_set_gc_enabled(v7, 1);
  v7_gc(v7, 1);

  v7_val_t res;
  if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) {
    fprintf(stderr, "Error: ");
    v7_fprint(stderr, v7, res);
  }
#endif

  LOG(LL_INFO, ("%s init done, RAM: %d free", "Sys", sj_get_free_heap_size()));

  if (!sj_app_init(v7)) {
    LOG(LL_ERROR, ("App init failed"));
    abort();
  }
  LOG(LL_INFO, ("%s init done, RAM: %d free", "App", sj_get_free_heap_size()));

  if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    boot_cfg.seq = saved_seq;
    commit_update(boot_cfg_idx, &boot_cfg);
    clubby_updater_finish(0);
  } else {
    /*
     * If there is no update reply state, this will just be ignored.
     * But if there is, then update was rolled back and reply will be sent.
     */
    clubby_updater_finish(-1);
  }

#ifndef CS_DISABLE_JS
  sj_prompt_init(v7);
#endif
  return 1;
}
Exemplo n.º 8
0
      bool operator()(ProblemDescriptionT & problem_description,
                      PDESystemT const & pde_system,
                      LinearSolverT& linear_solver,
                      std::size_t break_pde = 0)
      {
      #ifdef VIENNAFVM_VERBOSE
        std::streamsize cout_precision = std::cout.precision();
      #endif

        bool is_linear = pde_system.is_linear(); //TODO: Replace with an automatic detection

        if (is_linear)
        {
          for (std::size_t pde_index = 0; pde_index < pde_system.size(); ++pde_index)
          {
          #ifdef VIENNAFVM_VERBOSE
            viennafvm::Timer timer;
            timer.start();
            std::cout << " * Quantity " << pde_index << " : " << std::endl;
            std::cout << " ------------------------------------------" << std::endl;
          #endif

            MatrixType system_matrix;
            VectorType load_vector;

          #ifdef VIENNAFVM_VERBOSE
            viennafvm::Timer subtimer;
            subtimer.start();
          #endif
            viennafvm::linear_assembler fvm_assembler;
            fvm_assembler(pde_system, pde_index, problem_description.mesh(), problem_description.quantities(), system_matrix, load_vector);
          #ifdef VIENNAFVM_VERBOSE
            std::cout.precision(3);
            subtimer.get();
            std::cout << "   Assembly time : " << std::fixed << subtimer.get() << " s" << std::endl;
          #endif

            VectorType update;
            linear_solver(system_matrix, load_vector, update);
          #ifdef VIENNAFVM_VERBOSE
            std::cout << "   Solver time   : " << std::fixed << linear_solver.last_solver_time() << " s" << std::endl;
          #endif

          #ifdef VIENNAFVM_VERBOSE
            subtimer.start();
            numeric_type update_norm = apply_update(pde_system, pde_index, problem_description.mesh(), problem_description.quantities(), update, 1.0);
          #else
            apply_update(pde_system, pde_index, problem_description.mesh(), problem_description.quantities(), update, 1.0);  //this is linear, so there's no need for any damping
          #endif

          #ifdef VIENNAFVM_VERBOSE
            subtimer.get();
            std::cout << "   Update time   : " << std::fixed << subtimer.get() << " s" << std::endl;
          #endif

          #ifdef VIENNAFVM_VERBOSE
            timer.get();
            std::cout << "   Total time    : " << std::fixed << timer.get() << " s" << std::endl;

            std::cout.precision(cout_precision);
            std::cout.unsetf(std::ios_base::floatfield);

            std::cout << "   Solver iters  : " << linear_solver.last_iterations();
            if(linear_solver.last_iterations() == linear_solver.max_iterations())
              std::cout << " ( not converged ) " << std::endl;
            else std::cout << std::endl;

            std::cout << "   Solver error  : " << linear_solver.last_error() << std::endl;

            std::cout << "   Update norm   : "  << update_norm << std::endl;

            std::cout << std::endl;
          #endif

            if(linear_solver.last_iterations() == linear_solver.max_iterations())
      {
        #ifdef VIENNAFVM_VERBOSE
    std::cout << "   Linear Solver for pde: " << pde_index << "failed to converge" << std::endl;
        #endif
        return false; // TODO: should we stop solving here?
      }
          }
        }
        else // nonlinear
        {
          picard_iteration_ = true;

        #ifdef VIENNAFVM_VERBOSE
          std::vector<double> previous_update_norms(pde_system.size());
        #endif

          bool converged = false;
          std::size_t required_nonlinear_iterations = 0;
          for (std::size_t iter=0; iter < nonlinear_iterations; ++iter)
          {
            required_nonlinear_iterations++;
          #ifdef VIENNAFVM_VERBOSE
            std::cout << " --- Nonlinear iteration " << iter << " --- " << std::endl;
          #endif
            if (picard_iteration_)
            {
              for (std::size_t pde_index = 0; pde_index < pde_system.size(); ++pde_index)
              {
              #ifdef VIENNAFVM_VERBOSE
                viennafvm::Timer timer;
                timer.start();
                std::cout << " * Quantity " << pde_index << " : " << std::endl;
                std::cout << "   ------------------------------------" << std::endl;
              #endif


                MatrixType system_matrix;
                VectorType load_vector;

              #ifdef VIENNAFVM_VERBOSE
                viennafvm::Timer subtimer;
                subtimer.start();
              #endif
                // assemble linearized systems
                viennafvm::linear_assembler fvm_assembler;
                fvm_assembler(pde_system, pde_index, problem_description.mesh(), problem_description.quantities(), system_matrix, load_vector);
              #ifdef VIENNAFVM_VERBOSE
                std::cout.precision(3);
                subtimer.get();
                std::cout << "   Assembly time : " << std::fixed << subtimer.get() << " s" << std::endl;
              #endif

                VectorType update;
                linear_solver(system_matrix, load_vector, update);
              #ifdef VIENNAFVM_VERBOSE
                std::cout << "   Solver time   : " << std::fixed << linear_solver.last_solver_time() << " s" << std::endl;
              #endif

              #ifdef VIENNAFVM_VERBOSE
                subtimer.start();
              #endif
                numeric_type update_norm = apply_update(pde_system, pde_index, problem_description.mesh(), problem_description.quantities(), update, damping);
              #ifdef VIENNAFVM_VERBOSE
                subtimer.get();
                std::cout << "   Update time   : " << std::fixed << subtimer.get() << " s" << std::endl;
              #endif

              #ifdef VIENNAFVM_VERBOSE
                timer.get();
                std::cout << "   Total time    : " << std::fixed << timer.get() << " s" << std::endl;

                std::cout.precision(cout_precision);
                std::cout.unsetf(std::ios_base::floatfield);

                std::cout << "   Solver iters  : " << linear_solver.last_iterations();
                if(linear_solver.last_iterations() == linear_solver.max_iterations())
                  std::cout << " ( not converged ) " << std::endl;
                else std::cout << std::endl;

                std::cout << "   Solver error  : " << linear_solver.last_error() << std::endl;

                std::string norm_tendency_indicator;
                if(iter == 0)
                {
                  previous_update_norms[pde_index] = update_norm;
                  norm_tendency_indicator = "";
                }
                else
                {
                  if(update_norm > previous_update_norms[pde_index])
                    norm_tendency_indicator = "<up>";
                  else
                  if(update_norm < previous_update_norms[pde_index])
                    norm_tendency_indicator = "<down>";
                  else
                    norm_tendency_indicator = "<=>";
                  previous_update_norms[pde_index] = update_norm;
                }

                std::cout << "   Update norm   : "  << update_norm << " " << norm_tendency_indicator;
                if(pde_index == break_pde)
                  std::cout << " ( **** )" << std::endl;
                else
                  std::cout << std::endl;

                std::cout << std::endl;
              #endif

                if(pde_index == break_pde) // check if the potential update has converged ..
                {
                    if(update_norm <= nonlinear_breaktol) converged = true;
                }
              }
            }
            else
            {
              throw "not implemented!";
            }
          #ifdef VIENNAFVM_VERBOSE
            std::cout << std::endl;
          #endif
            if(converged) break; // .. the nonlinear for-loop

          } // nonlinear for-loop

          if(!converged)
          {
          #ifdef VIENNAFVM_VERBOSE
              std::cout << std::endl;
              std::cout << "--------" << std::endl;
              std::cout << "Warning: Simulation did not converge!" << std::endl;
              std::cout << "  Update norm of observed variable did not reach the break-tolerance of " << nonlinear_breaktol
                        << " in " << nonlinear_iterations << " iterations" << std::endl;
              std::cout << "--------" << std::endl;
          #endif
        return false;
          }

          #ifdef VIENNAFVM_VERBOSE
    std::cout << std::endl;
    std::cout << "--------" << std::endl;
    std::cout << "Success: Simulation converged successfully!" << std::endl;
    std::cout << "  Update norm of observed variable reached the break-tolerance of " << nonlinear_breaktol
        << " in " << required_nonlinear_iterations << " iterations" << std::endl;
    std::cout << "--------" << std::endl;
          #endif
        }
        return true;
      }
Exemplo n.º 9
0
 bool do_update() {
    compute_update();
    return apply_update();
 }