示例#1
0
bool HostMatrixCOO<ValueType>::Permute(const BaseVector<int> &permutation) {

  assert(&permutation != NULL);

  // symmetric permutation only
  assert( (permutation.get_size() == this->get_nrow()) &&
          (permutation.get_size() == this->get_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->get_nnz(), this->get_nrow(), this->get_ncol());
  src.CopyFrom(*this);

  omp_set_num_threads(this->local_backend_.OpenMP_threads);  

#pragma omp parallel for      
  for (int i=0; i<this->get_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;

}
bool HostMatrixCOO<ValueType>::PermuteBackward(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_);

    // TODO
    // Is there a better way?
    int *pb = NULL;
    allocate_host(this->nrow_, &pb);

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

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

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

    }

    free_host(&pb);

    return true;

}