void HostMatrixCOO<ValueType>::Apply(const BaseVector<ValueType> &in, BaseVector<ValueType> *out) const {

    assert(in.  get_size() >= 0);
    assert(out->get_size() >= 0);
    assert(in.  get_size() == this->ncol_);
    assert(out->get_size() == this->nrow_);

    const HostVector<ValueType> *cast_in = dynamic_cast<const HostVector<ValueType>*> (&in) ;
    HostVector<ValueType> *cast_out      = dynamic_cast<      HostVector<ValueType>*> (out) ;

    assert(cast_in != NULL);
    assert(cast_out!= NULL);

    _set_omp_backend_threads(this->local_backend_, this->nnz_);

    #pragma omp parallel for



    for (int i=0; i<this->nrow_; ++i) {
        cast_out->vec_[i] = ValueType(0.0);
        if (i == 0) {
            int nthreads = omp_get_num_threads();
            std::cout << "variable nthreads IN COO is " << nthreads << std::endl;
        }
    }

    for (int i=0; i<this->nnz_; ++i)
        cast_out->vec_[this->mat_.row[i] ] += this->mat_.val[i] * cast_in->vec_[ this->mat_.col[i] ];

}
Exemplo n.º 2
0
void HostVector<ValueType>::CopyFrom(const BaseVector<ValueType> &vec) {

  if (this != &vec)  {

    if (const HostVector<ValueType> *cast_vec = dynamic_cast<const HostVector<ValueType>*> (&vec)) {

      if (this->size_ == 0)
        this->Allocate(cast_vec->size_);

      assert(cast_vec->size_ == this->size_);

      _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
      for (int i=0; i<this->size_; ++i)
        this->vec_[i] = cast_vec->vec_[i];

    } else {

      // non-host type

    vec.CopyTo(this);

    }

  }

}
Exemplo n.º 3
0
std::complex<double> HostVector<std::complex<double> >::DotNonConj(const BaseVector<std::complex<double> > &x) const {

  assert(&x != NULL);

  const HostVector<std::complex<double> > *cast_x = dynamic_cast<const HostVector<std::complex<double> >*> (&x);

  assert(cast_x != NULL);
  assert(this->size_ == cast_x->size_);

  double dot_real = double(0.0);
  double dot_imag = double(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:dot_real, dot_imag)
  for (int i=0; i<this->size_; ++i) {

    dot_real += this->vec_[i].real() * cast_x->vec_[i].real() - this->vec_[i].imag() * cast_x->vec_[i].imag();
    dot_imag += this->vec_[i].real() * cast_x->vec_[i].imag() + this->vec_[i].imag() * cast_x->vec_[i].real();

  }

  return std::complex<double>(dot_real, dot_imag);

}
bool HostMatrixCOO<ValueType>::Permute(const BaseVector<int> &permutation) {

    assert(&permutation != NULL);

    // symmetric permutation only
    assert( (permutation.get_size() == this->nrow_) &&
            (permutation.get_size() == this->ncol_) );

    const HostVector<int> *cast_perm = dynamic_cast<const HostVector<int>*> (&permutation) ;
    assert(cast_perm != NULL);

    HostMatrixCOO<ValueType> src(this->local_backend_);
    src.AllocateCOO(this->nnz_, this->nrow_, this->ncol_);
    src.CopyFrom(*this);

    _set_omp_backend_threads(this->local_backend_, this->nnz_);

    #pragma omp parallel for
    for (int i=0; i<this->nnz_; ++i) {

        this->mat_.row[i] = cast_perm->vec_[ src.mat_.row[i] ];
        this->mat_.col[i] = cast_perm->vec_[ src.mat_.col[i] ];

    }

    return true;

}
Exemplo n.º 5
0
void HostVector<ValueType>::CopyFrom(const BaseVector<ValueType> &src,
                                     const int src_offset,
                                     const int dst_offset,
                                     const int size) {

  assert(&src != NULL);

  const HostVector<ValueType> *cast_src = dynamic_cast<const HostVector<ValueType>*> (&src);

  assert(cast_src != NULL);
  //TOOD check always for == this?
  assert(&src != this);
  assert(this->size_     > 0);
  assert(cast_src->size_ > 0);
  assert(size            > 0);
  assert(src_offset + size <= cast_src->size_);
  assert(dst_offset + size <= this->size_);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<size; ++i)
    this->vec_[i+dst_offset] = cast_src->vec_[i+src_offset];

}
Exemplo n.º 6
0
void HostVector<ValueType>::Scale(const ValueType alpha) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] *= alpha;

}
Exemplo n.º 7
0
void HostVector<std::complex<float> >::Power(const double power) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = pow(this->vec_[i], std::complex<float>(float(power)));

}
Exemplo n.º 8
0
void HostVector<ValueType>::Power(const double power) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = pow(this->vec_[i], ValueType(power));

}
Exemplo n.º 9
0
void HostVector<ValueType>::SetValues(const ValueType val) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = val;

}
Exemplo n.º 10
0
void HostVector<ValueType>::Ones(void) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = ValueType(1.0);

}
Exemplo n.º 11
0
bool HostMatrixCOO<ValueType>::AddScalar(const ValueType alpha) {

    _set_omp_backend_threads(this->local_backend_, this->nnz_);

    #pragma omp parallel for
    for (int i=0; i<this->nnz_; ++i)
        this->mat_.val[i] += alpha;

    return true;

}
Exemplo n.º 12
0
void HostVector<ValueType>::SetRandom(const ValueType a, const ValueType b, const int seed) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

  // Fill this with random data from interval [a,b]
  srand(seed);
#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = a + (ValueType)rand() / (ValueType)RAND_MAX * (b - a);

}
Exemplo n.º 13
0
void HostVector<std::complex<float> >::SetRandom(const std::complex<float> a, const std::complex<float> b,
                                                 const int seed) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

  // Fill this with random data from interval [a,b]
  srand(seed);
#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = a + (float)rand() / (float)RAND_MAX * (b - a);

}
Exemplo n.º 14
0
bool HostMatrixCOO<ValueType>::ScaleOffDiagonal(const ValueType alpha) {

    _set_omp_backend_threads(this->local_backend_, this->nnz_);

    #pragma omp parallel for
    for (int i=0; i<this->nnz_; ++i)
        if (this->mat_.row[i] != this->mat_.col[i])
            this->mat_.val[i] *= alpha;

    return true;

}
Exemplo n.º 15
0
void HostVector<ValueType>::CopyToData(ValueType *data) const {

  if (this->size_ > 0) {

    _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
    for (int i=0; i<this->size_; ++i)
      data[i] = this->vec_[i];

  }

}
Exemplo n.º 16
0
ValueType HostVector<ValueType>::Reduce(void) const {

  ValueType reduce = ValueType(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:reduce)
  for (int i=0; i<this->size_; ++i)
    reduce += this->vec_[i];

  return reduce;

}
Exemplo n.º 17
0
ValueType HostVector<ValueType>::Asum(void) const {

  ValueType asum = ValueType(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:asum)
  for (int i=0; i<this->size_; ++i)
    asum += paralution_abs(this->vec_[i]);

  return asum;

}
Exemplo n.º 18
0
ValueType HostVector<ValueType>::Norm(void) const {

  ValueType norm2 = ValueType(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:norm2)
  for (int i=0; i<this->size_; ++i)
    norm2 += this->vec_[i] * this->vec_[i];

  return sqrt(norm2);

}
Exemplo n.º 19
0
void HostVector<int>::Power(const double power) {

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i) {

    int value = 1;
    for (int j=0; j<power; ++j)
      value *= this->vec_[i];
    
    this->vec_[i] = value;
  }

}
Exemplo n.º 20
0
std::complex<double> HostVector<std::complex<double> >::Norm(void) const {

  double norm2 = double(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:norm2)
  for (int i=0; i<this->size_; ++i)
    norm2 += this->vec_[i].real() * this->vec_[i].real() + this->vec_[i].imag() * this->vec_[i].imag();

  std::complex<double> res(sqrt(norm2), double(0.0));

  return res;

}
Exemplo n.º 21
0
void HostVector<ValueType>::ScaleAddScale(const ValueType alpha, const BaseVector<ValueType> &x, const ValueType beta) {

  assert(&x != NULL);

  const HostVector<ValueType> *cast_x = dynamic_cast<const HostVector<ValueType>*> (&x);

  assert(cast_x != NULL);
  assert(this->size_ == cast_x->size_);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = alpha*this->vec_[i] + beta*cast_x->vec_[i];

}
Exemplo n.º 22
0
void HostVector<ValueType>::PointWiseMult(const BaseVector<ValueType> &x) {

  assert(&x != NULL);

  const HostVector<ValueType> *cast_x = dynamic_cast<const HostVector<ValueType>*> (&x);

  assert(cast_x != NULL);
  assert(this->size_ == cast_x->size_);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = this->vec_[i]*cast_x->vec_[i];

}
Exemplo n.º 23
0
std::complex<double> HostVector<std::complex<double> >::Reduce(void) const {

  double reduce_real = double(0.0);
  double reduce_imag = double(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:reduce_real, reduce_imag)
  for (int i=0; i<this->size_; ++i) {

    reduce_real += this->vec_[i].real();
    reduce_imag += this->vec_[i].imag();

  }

  return  std::complex<double>(reduce_real, reduce_imag);

}
Exemplo n.º 24
0
std::complex<double> HostVector<std::complex<double> >::Asum(void) const {

  double asum_real = double(0.0);
  double asum_imag = double(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:asum_real, asum_imag)
  for (int i=0; i<this->size_; ++i) {

    asum_real += paralution_abs(this->vec_[i].real());
    asum_imag += paralution_abs(this->vec_[i].imag());

  }

  return std::complex<double>(asum_real, asum_imag);

}
Exemplo n.º 25
0
void HostVector<ValueType>::CopyFromPermuteBackward(const BaseVector<ValueType> &src,
                                                    const BaseVector<int> &permutation) {

  assert(this != &src);

  const HostVector<ValueType> *cast_vec = dynamic_cast<const HostVector<ValueType>*> (&src);
  const HostVector<int> *cast_perm      = dynamic_cast<const HostVector<int>*> (&permutation);
  assert(cast_perm != NULL);
  assert(cast_vec  != NULL);

  assert(cast_vec ->size_ == this->size_);
  assert(cast_perm->size_ == this->size_);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i)
    this->vec_[i] = cast_vec->vec_[ cast_perm->vec_[i] ];

}
Exemplo n.º 26
0
ValueType HostVector<ValueType>::Dot(const BaseVector<ValueType> &x) const {

  assert(&x != NULL);

  const HostVector<ValueType> *cast_x = dynamic_cast<const HostVector<ValueType>*> (&x);

  assert(cast_x != NULL);
  assert(this->size_ == cast_x->size_);

  ValueType dot = ValueType(0.0);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for reduction(+:dot)
  for (int i=0; i<this->size_; ++i)
    dot += this->vec_[i]*cast_x->vec_[i];

  return dot;

}
Exemplo n.º 27
0
void HostMatrixCOO<ValueType>::CopyFrom(const BaseMatrix<ValueType> &mat) {

    // copy only in the same format
    assert(this->get_mat_format() == mat.get_mat_format());

    if (const HostMatrixCOO<ValueType> *cast_mat = dynamic_cast<const HostMatrixCOO<ValueType>*> (&mat)) {

        if (this->nnz_ == 0)
            this->AllocateCOO(cast_mat->nnz_, cast_mat->nrow_, cast_mat->ncol_ );

        assert((this->nnz_  == cast_mat->nnz_)  &&
               (this->nrow_ == cast_mat->nrow_) &&
               (this->ncol_ == cast_mat->ncol_) );

        if (this->nnz_ > 0) {

            _set_omp_backend_threads(this->local_backend_, this->nnz_);

            #pragma omp parallel for
            for (int j=0; j<this->nnz_; ++j)
                this->mat_.row[j] = cast_mat->mat_.row[j];

            #pragma omp parallel for
            for (int j=0; j<this->nnz_; ++j)
                this->mat_.col[j] = cast_mat->mat_.col[j];

            #pragma omp parallel for
            for (int j=0; j<this->nnz_; ++j)
                this->mat_.val[j] = cast_mat->mat_.val[j];

        }

    } else {

        // Host matrix knows only host matrices
        // -> dispatching
        mat.CopyTo(this);

    }

}
Exemplo n.º 28
0
void HostVector<ValueType>::ScaleAddScale(const ValueType alpha, const BaseVector<ValueType> &x, const ValueType beta,
                                          const int src_offset, const int dst_offset,const int size) {

  assert(&x != NULL);

  const HostVector<ValueType> *cast_x = dynamic_cast<const HostVector<ValueType>*> (&x);

  assert(cast_x != NULL);
  assert(this->size_   > 0);
  assert(cast_x->size_ > 0);
  assert(size          > 0);
  assert(src_offset + size <= cast_x->size_);
  assert(dst_offset + size <= this->size_);

  _set_omp_backend_threads(this->local_backend_, size);

#pragma omp parallel for
  for (int i=0; i<size; ++i)
    this->vec_[i+dst_offset] = alpha*this->vec_[i+dst_offset] + beta*cast_x->vec_[i+src_offset];

}
void HostMatrixELL<ValueType>::Apply(const BaseVector<ValueType> &in, BaseVector<ValueType> *out) const {

  if (this->nnz_ > 0) {

    assert(in.  get_size() >= 0);
    assert(out->get_size() >= 0);
    assert(in.  get_size() == this->ncol_);
    assert(out->get_size() == this->nrow_);

    const HostVector<ValueType> *cast_in = dynamic_cast<const HostVector<ValueType>*> (&in);
    HostVector<ValueType> *cast_out      = dynamic_cast<      HostVector<ValueType>*> (out);

    assert(cast_in != NULL);
    assert(cast_out!= NULL);

    _set_omp_backend_threads(this->local_backend_, this->nrow_);

#pragma omp parallel for
    for (int ai=0; ai<this->nrow_; ++ai) {
      ValueType sum = ValueType(0.0);

      for (int n=0; n<this->mat_.max_row; ++n) {

        int aj = ELL_IND(ai, n, this->nrow_, this->mat_.max_row);
        int col_aj = this->mat_.col[aj];

        if (col_aj >= 0)
          sum += this->mat_.val[aj] * cast_in->vec_[col_aj];
        else
          break;

      }

      cast_out->vec_[ai] = sum;

    }

  }

}
Exemplo n.º 30
0
void HostVector<ValueType>::PermuteBackward(const BaseVector<int> &permutation) {

  assert(&permutation != NULL);

  const HostVector<int> *cast_perm = dynamic_cast<const HostVector<int>*> (&permutation);

  assert(cast_perm != NULL);
  assert(this->size_ == cast_perm->size_);

  HostVector<ValueType> vec_tmp(this->local_backend_);
  vec_tmp.Allocate(this->size_);
  vec_tmp.CopyFrom(*this);

  _set_omp_backend_threads(this->local_backend_, this->size_);

#pragma omp parallel for
  for (int i=0; i<this->size_; ++i) {
    assert_dbg(cast_perm->vec_[i] >= 0);
    assert_dbg(cast_perm->vec_[i] < this->size_);
    this->vec_[i] = vec_tmp.vec_[ cast_perm->vec_[i] ];
  }
}