Esempio n. 1
0
double
step_linear_system(pthread_t *threads_p, linearSystem_t *system_p){
  /*Purpose: one iterative step of an affine linear transformation
   *represented by the the function argument: system_p.
   *ie: iterates a linear transformation followed by a translation.
   *
   *returns:
   */
  extern double Thread_errs[];
  extern threadLocalState_t Ts[];

  //locals
  int i;                           //iterator
  int rv;                          //return check for thread creation.
  double *ttmp_p;                  //swap pointer.

  //grossly validate parameters.
  if (!threads_p) return -1.0;
#ifdef DEBUG
  debug("Entered: %s", "step_linear_system");
#endif
  //thread dispatch loop
  for (i=0; i < NT; i++)
    {
      //initialize thread introspection info.
      Ts[i].system_p = system_p;         //the linear system on which to operate.
      Ts[i].thread_max = NT;   //thread count over which work is shared.
      Ts[i].thread_number = i;       //local thread identifier, range [0,thread_max].
      Ts[i].return_storage_p = &Thread_errs[i];  //address where to place return value;

#ifdef DEBUG
      debug("Dispatching thread %d.\n\tParameters: {%p,%d,%d}\n",i,(void*)Ts[i].system_p,Ts[i].thread_max,Ts[i].thread_number);
      fflush(stderr);
#endif
      //send thread out to work @function.
      rv = pthread_create( &threads_p[i],            //thread
                           NULL,                     //params
                           thread_transformation,    //function to execute
                           (void *)&Ts[i]);          //function arguments.
      //creation check
      if (rv < 0){
        log_err("error on thread creation %d.\n",i);
        exit(EXIT_FAILURE);
      }
    }
  //collect threads and compute max error.
  for (i=0; i < NT; i++)
    {
      rv = pthread_join(threads_p[i], NULL);
      if (rv != 0){
        log_err("error on thread join %d\n", i);
        exit(EXIT_FAILURE);
      }
#ifdef DEBUG
      debug("Collected thread %d.", i);
#endif
    }
  //swap t1 & t
#ifdef DEBUG
  debug("Swapping t1:%p with t:%p.", (void*)system_p->t1_p, (void*)system_p->t_p);
#endif
  ttmp_p = system_p->t1_p;
  system_p->t1_p = system_p->t_p;
  system_p->t_p = ttmp_p;

  //iteration error is maximum threads' errors.
  //use ttmp_p to reference the max error in Thread_errs.
  ttmp_p = max_err((double *)&Thread_errs, NT);
#ifdef DEBUG
  debug("Returning from step_linear_system:\t %14.6e", *ttmp_p);
#endif
  return *ttmp_p;
}
Esempio n. 2
0
/*-------------------------------------------------------*/
void NOMAD::TGP_Model::display_X_errors(const NOMAD::Display &out)
{
    if (_p == 0 || !_X)
    {
        out << "no interpolation points" << std::endl;
        return;
    }

    int               i , j , m = _bbot.size();
    NOMAD::Point      min_err(m) , max_err(m) , avg_err(m,0.0) , sd_err(m,0.0);
    NOMAD::Eval_Point x(_n , m);
    double          **err = new double * [_p];

    for (i = 0 ; i < _p ; ++i)
    {

        err[i] = new double[m];

        for (j = 0 ; j < _n ; ++j)
            x[j] = _X[i][j];

        if (predict(x , true))
        {

            for (j = 0 ; j < m ; ++j)
                if (_tgp_models[j])
                {

                    // relative error (in %) for point #i and output #j:
                    err[i][j] = (x.get_bb_outputs()[j].rel_err((_tgp_models[j]->get_Z())[i])
                                 * 100.0).value();

                    // out << "f=" << (_tgp_models[j]->get_Z())[i] << " "
                    //     << "m=" << x.get_bb_outputs()[j] << " err=" << err[i][j]
                    //     << std::endl;

                    if (!min_err[j].is_defined() || err[i][j] < min_err[j].value())
                        min_err[j] = err[i][j];

                    if (!max_err[j].is_defined() || err[i][j] > max_err[j].value())
                        max_err[j] = err[i][j];

                    avg_err[j] += err[i][j];
                }
        }
        else
        {
            for (j = 0 ; j <= i ; ++j)
                delete [] err[j];
            delete [] err;
            out << "cannot predict interpolation errors ("
                << _error_str << ")" << std::endl;
            return;
        }
    }

    for (j = 0 ; j < m ; ++j)
        if (_tgp_models[j])
        {

            // compute the median error:
            NOMAD::Double med_err;
            {
                if (_p == 1)
                    med_err = err[0][j];
                else if (_p == 2)
                    med_err = (err[0][j] + err[1][j]) / 2.0;

                else
                {
                    std::multiset<double> sorted_errors;
                    for (i = 0 ; i < _p ; ++i)
                        sorted_errors.insert(err[i][j]);
                    std::multiset<double>::const_iterator it , end = sorted_errors.end();
                    --end;
                    for (it = sorted_errors.begin() , i = 0 ; it != end ; ++it , ++i)
                    {
                        if (i == (_p+1)/2-1)
                        {
                            med_err = *it;
                            if (_p%2==0)
                            {
                                ++it;
                                med_err = (med_err + *it) / 2.0;
                            }
                            break;
                        }
                    }
                }
            }

            // compute the mean and the standard deviation:
            avg_err[j] /= _p;
            for (i = 0 ; i < _p ; ++i)
                sd_err[j] += (avg_err[j] - err[i][j]).pow2();
            sd_err[j] = (sd_err[j] / _p).sqrt();

            // display:
            if (m > 1)
            {
                std::ostringstream oss;
                oss << "output #" << j;
                if (_tgp_models[j]->is_fixed())
                    oss << " (fixed)";
                else if (_tgp_models[j]->is_binary())
                    oss << " (binary)";
                out << NOMAD::open_block(oss.str());
            }

            out << "min   : ";
            min_err[j].display(out , "%6.2f");
            out << std::endl << "max   : ";
            max_err[j].display(out , "%6.2f");
            out << std::endl << "median: ";
            med_err.display(out , "%6.2f");
            out << std::endl << "mean  : ";
            avg_err[j].display(out , "%6.2f");
            out << std::endl << "sd    : ";
            sd_err[j].display(out , "%6.2f");
            out << std::endl;

            if (m > 1)
                out.close_block();
        }

    for (i = 0 ; i < _p ; ++i)
        delete [] err[i];
    delete [] err;
}