text:
#include "truncated-svd-solver/linear-algebra-helpers.h"

#include <algorithm>
#include <cmath>

#include <cholmod.h>
#include <Eigen/Dense>
#include <glog/logging.h>
#include <spqr.hpp>
#include <SuiteSparseQR.hpp>

namespace truncated_svd_solver {

cholmod_sparse* columnSubmatrix(cholmod_sparse* A, std::ptrdiff_t
    col_start_idx, std::ptrdiff_t col_end_idx, cholmod_common* cholmod) {
  CHECK_NOTNULL(A);
  CHECK_NOTNULL(cholmod);

  CHECK_GE(col_end_idx, col_start_idx);
  CHECK_LT(col_end_idx, static_cast<std::ptrdiff_t>(A->ncol));
  CHECK_GE(col_start_idx, 0);

  const std::ptrdiff_t num_indices = col_end_idx - col_start_idx + 1;
  std::ptrdiff_t* col_indices = new std::ptrdiff_t[num_indices];
  for (std::ptrdiff_t j = col_start_idx; j <= col_end_idx; ++j) {
    col_indices[j - col_start_idx] = j;
  }

  cholmod_sparse* A_sub = cholmod_l_submatrix(A, nullptr, -1, col_indices,
    num_indices, 1, 1, cholmod);
  delete [] col_indices;
  CHECK(A_sub != nullptr) << "cholmod_l_submatrix failed.";

  return A_sub;
}

cholmod_sparse* rowSubmatrix(cholmod_sparse* A, std::ptrdiff_t row_start_idx,
    std::ptrdiff_t row_end_idx, cholmod_common* cholmod) {
  CHECK_NOTNULL(A);
  CHECK_NOTNULL(cholmod);

  CHECK_GE(row_end_idx, row_start_idx);
  CHECK_LT(row_end_idx, static_cast<std::ptrdiff_t>(A->nrow));
  CHECK_GE(row_start_idx, 0);

  const std::ptrdiff_t num_indices = row_end_idx - row_start_idx + 1;
  std::ptrdiff_t* row_indices = new std::ptrdiff_t[num_indices];
  for (std::ptrdiff_t i = row_start_idx; i <= row_end_idx; ++i){
    row_indices[i - row_start_idx] = i;
  }

  cholmod_sparse* A_sub = cholmod_l_submatrix(A, row_indices, num_indices,
    nullptr, -1, 1, 1, cholmod);
  delete [] row_indices;
  CHECK(A_sub != nullptr) << "cholmod_l_submatrix failed.";

  return A_sub;
}

double colNorm(const cholmod_sparse* A, std::ptrdiff_t col_idx) {
  CHECK_NOTNULL(A);
  CHECK_GE(col_idx, 0);
  CHECK_LT(col_idx, static_cast<std::ptrdiff_t>(A->ncol));

  const std::ptrdiff_t* col_ptr =
    reinterpret_cast<const std::ptrdiff_t*>(A->p);
  const double* values = reinterpret_cast<const double*>(A->x);
  const std::ptrdiff_t p = col_ptr[col_idx];
  const std::ptrdiff_t num_elements = col_ptr[col_idx + 1] - p;

  double norm = 0.0;
  for (std::ptrdiff_t i = 0; i < num_elements; ++i) {
    norm += values[p + i] * values[p + i];
  }
  return std::sqrt(norm);
}

cholmod_dense* columnScalingMatrix(cholmod_sparse* A, cholmod_common*
    cholmod, double eps) {
  CHECK_NOTNULL(A);
  CHECK_NOTNULL(cholmod);
  CHECK_GT(eps, 0.0);

  cholmod_dense* G = cholmod_l_allocate_dense(A->ncol, 1, A->ncol,
    CHOLMOD_REAL, cholmod);
  CHECK(G != nullptr) << "cholmod_l_allocate_dense failed.";

  const double norm_tolerance = std::sqrt(A->nrow * eps);
  double* values = reinterpret_cast<double*>(G->x);
  for (std::ptrdiff_t col_idx = 0;
      col_idx < static_cast<std::ptrdiff_t>(A->ncol);
      ++col_idx) {
    const double norm = colNorm(A, col_idx);
    if (norm < norm_tolerance) {
      values[col_idx] = 0.0;
    } else {
      values[col_idx] = 1.0 / norm;
    }
  }
  return G;
}

void cholmodSparseToEigenDenseCopy(const cholmod_sparse* in,
    Eigen::MatrixXd& out) {
  CHECK_NOTNULL(in);
  out.setZero(in->nrow, in->ncol);

  const std::ptrdiff_t* row_ind =
    reinterpret_cast<const std::ptrdiff_t*>(in->i);
  const std::ptrdiff_t* col_ptr =
    reinterpret_cast<const std::ptrdiff_t*>(in->p);
  const double* values = reinterpret_cast<const double*>(in->x);
  for (std::ptrdiff_t col_idx = 0;
       col_idx < static_cast<std::ptrdiff_t>(in->ncol); ++col_idx)
    for (std::ptrdiff_t val_idx = col_ptr[col_idx];
         val_idx < col_ptr[col_idx + 1]; ++val_idx) {
      out(row_ind[val_idx], col_idx) = values[val_idx];
      if (in->stype && col_idx != row_ind[val_idx]) {
        out(col_idx, row_ind[val_idx]) = values[val_idx];
      }
    }
}

void eigenDenseToCholmodDenseView(const Eigen::VectorXd& in, cholmod_dense*
    out) {
  CHECK_NOTNULL(out);

  out->nrow = in.size();
  out->ncol = 1;
  out->nzmax = in.size();
  out->d = in.size();
  out->x = reinterpret_cast<void*>(const_cast<double*>(in.data()));
  out->z = nullptr;
  out->xtype = CHOLMOD_REAL;
  out->dtype = CHOLMOD_DOUBLE;
}

cholmod_dense* eigenDenseToCholmodDenseCopy(const Eigen::VectorXd& in,
    cholmod_common* cholmod) {
  CHECK_NOTNULL(cholmod);
  cholmod_dense* out = cholmod_l_allocate_dense(in.size(), 1, in.size(),
    CHOLMOD_REAL, cholmod);
  CHECK(out != nullptr) << "cholmod_l_allocate_dense failed.";

  double* out_val = reinterpret_cast<double*>(out->x);
  const double* in_val = in.data();
  std::copy(in_val, in_val + in.size(), out_val);
  return out;
}

void cholmodDenseToEigenDenseCopy(const cholmod_dense* in, Eigen::VectorXd&
    out) {
  CHECK_NOTNULL(in);
  out.resize(in->nrow);
  const double* in_val = reinterpret_cast<const double*>(in->x);
  std::copy(in_val, in_val + in->nrow, out.data());
}

cholmod_sparse* eigenDenseToCholmodSparseCopy(const Eigen::MatrixXd& A,
    cholmod_common* cholmod, double eps) {
  CHECK_NOTNULL(cholmod);
  CHECK_GT(eps, 0.0);

  size_t nzmax = 0;
  for (std::ptrdiff_t i = 0; i < A.rows(); ++i) {
    for (std::ptrdiff_t j = 0; j < A.cols(); ++j) {
      if (std::fabs(A(i, j)) > eps) {
        nzmax++;
      }
    }
  }

  cholmod_sparse* A_cholmod = cholmod_l_allocate_sparse(A.rows(), A.cols(),
    nzmax, 1, 1, 0, CHOLMOD_REAL, cholmod);
  CHECK(A_cholmod != nullptr) << "cholmod_l_allocate_sparse failed.";

  std::ptrdiff_t* row_ind = reinterpret_cast<std::ptrdiff_t*>(A_cholmod->i);
  std::ptrdiff_t* col_ptr = reinterpret_cast<std::ptrdiff_t*>(A_cholmod->p);
  double* values = reinterpret_cast<double*>(A_cholmod->x);
  std::ptrdiff_t row_it = 0;
  std::ptrdiff_t col_it = 1;
  for (std::ptrdiff_t c = 0; c < A.cols(); ++c) {
    for (std::ptrdiff_t r = 0; r < A.rows(); ++r)
      if (std::fabs(A(r, c)) > eps) {
        values[row_it] = A(r, c);
        row_ind[row_it] = r;
        row_it++;
      }
    col_ptr[col_it] = row_it;
    col_it++;
  }
  return A_cholmod;
}

std::ptrdiff_t estimateNumericalRank(const Eigen::VectorXd& singular_values,
                                     double tolerance) {
  CHECK_GE(tolerance, 0.0);

  std::ptrdiff_t numerical_rank = singular_values.size();
  for (std::ptrdiff_t i = singular_values.size() - 1; i >= 0; --i) {
    if (singular_values(i) > tolerance) {
      // Assumes that the singular values are ordered.
      break;
    } else {
      --numerical_rank;
    }
  }
  return numerical_rank;
}

double rankTol(const Eigen::VectorXd& singular_values, double eps) {
  CHECK_GT(singular_values.rows(), 0) << "Empty singular values vector.";
  CHECK_GT(eps, 0.0);
  return singular_values(0) * eps * singular_values.size();
}

double qrTol(cholmod_sparse* A, cholmod_common* cholmod, double eps) {
  CHECK_NOTNULL(A);
  CHECK_NOTNULL(cholmod);
  CHECK_GT(eps, 0.0);
  return 20.0 * static_cast<double>(A->nrow + A->ncol) * eps *
    spqr_maxcolnorm<double>(A, cholmod);
}

double svGap(const Eigen::VectorXd& singular_values, std::ptrdiff_t rank) {
  CHECK_LE(rank, singular_values.rows());
  CHECK_GE(rank, 0);

  if (rank == 0) {
    return 0.0;
  } else if (rank < singular_values.size()) {
    return singular_values(rank - 1) / singular_values(rank);
  }
  return std::numeric_limits<double>::infinity();
}

void reduceLeftHandSide(SuiteSparseQR_factorization<double>* factor,
    cholmod_sparse* A_rt, cholmod_sparse** Omega, cholmod_sparse** A_rtQ,
    cholmod_common* cholmod) {
  CHECK_NOTNULL(A_rt);
  CHECK_NOTNULL(cholmod);
  CHECK_NOTNULL(Omega);

  // Handle the special case where the QR part is empty.
  if (factor == nullptr) {
    // NO QR part!
    *Omega = cholmod_l_aat(A_rt, nullptr, 0, 1, cholmod);
    return;
  } else {
    CHECK(factor->QRsym != nullptr) << "Run symbolic factorization first.";
    CHECK(factor->QRnum != nullptr) << "Run QR decomposition first.";
  }
  CHECK_NOTNULL(factor);
  CHECK_NOTNULL(A_rtQ);

  cholmod_sparse* A_rtQFull = SuiteSparseQR_qmult<double>(SPQR_XQ, factor,
    A_rt, cholmod);
  CHECK(A_rtQFull != nullptr) << "SuiteSparseQR_qmult failed.";

  *A_rtQ = columnSubmatrix(A_rtQFull, 0, factor->QRsym->n - 1, cholmod);
  cholmod_l_free_sparse(&A_rtQFull, cholmod);

  cholmod_sparse* A_rtQ2 = cholmod_l_aat(*A_rtQ, nullptr, 0, 1, cholmod);
  CHECK(A_rtQ2 != nullptr) << "cholmod_l_aat failed.";
  A_rtQ2->stype = 1;

  cholmod_sparse* A_rt2 = cholmod_l_aat(A_rt, nullptr, 0, 1, cholmod);
  CHECK(A_rt2 != nullptr) << "cholmod_l_aat failed.";
  A_rt2->stype = 1;

  double alpha[2];
  alpha[0] = 1.0;
  double beta[2];
  beta[0] = -1.0;

  *Omega = cholmod_l_add(A_rt2, A_rtQ2, alpha, beta, 1, 1, cholmod);
  CHECK(*Omega != nullptr) << "cholmod_l_add failed.";

  cholmod_l_free_sparse(&A_rt2, cholmod);
  cholmod_l_free_sparse(&A_rtQ2, cholmod);
}

cholmod_dense* reduceRightHandSide(SuiteSparseQR_factorization<double>*
    factor, cholmod_sparse* A_rt, cholmod_sparse* A_rtQ, cholmod_dense* b,
    cholmod_common* cholmod) {
  CHECK_NOTNULL(A_rt);
  CHECK_NOTNULL(b);
  CHECK_NOTNULL(cholmod);

  // Handle the special case where the QR part is empty.
  if (factor == nullptr) {
    // NO QR part!
    double one = 1, zero = 0;
    cholmod_dense* b_reduced = cholmod_l_allocate_dense(
        A_rt->nrow, 1, A_rt->nrow, CHOLMOD_REAL, cholmod);
    CHECK(b_reduced != nullptr) << "cholmod_l_allocate_dense failed.";

    const int status = cholmod_l_sdmult(A_rt, 0, &one, &zero, b, b_reduced,
                                        cholmod);
    CHECK(status) << "cholmod_l_sdmult failed";
    return b_reduced;
  } else {
    CHECK(factor->QRsym != nullptr) << "Run symbolic factorization first.";
    CHECK(factor->QRnum != nullptr) << "Run QR decomposition first.";
  }
  CHECK_NOTNULL(factor);
  CHECK_NOTNULL(A_rtQ);

  cholmod_sparse* b_sparse = cholmod_l_dense_to_sparse(b, 1, cholmod);
  CHECK(b_sparse != nullptr) << "cholmod_l_dense_to_sparse failed.";

  cholmod_sparse* A_rtb = cholmod_l_ssmult(A_rt, b_sparse, 0, 1, 1, cholmod);
  CHECK(A_rtb != nullptr) << "cholmod_l_ssmult failed.";

  cholmod_sparse* QtbFull = SuiteSparseQR_qmult<double>(SPQR_QTX, factor,
    b_sparse, cholmod);
  CHECK(QtbFull != nullptr) << "SuiteSparseQR_qmult failed.";
  cholmod_l_free_sparse(&b_sparse, cholmod);

  cholmod_sparse* Qtb;
  Qtb = rowSubmatrix(QtbFull, 0, factor->QRsym->n - 1, cholmod);
  cholmod_l_free_sparse(&QtbFull, cholmod);

  cholmod_sparse* A_rtQQtb = cholmod_l_ssmult(A_rtQ, Qtb, 0, 1, 1, cholmod);
  CHECK(A_rtQQtb != nullptr) << "cholmod_l_ssmult failed.";
  cholmod_l_free_sparse(&Qtb, cholmod);

  double alpha[2];
  alpha[0] = 1.0;
  double beta[2];
  beta[0] = -1.0;

  cholmod_sparse* b_reduced_sparse = cholmod_l_add(A_rtb, A_rtQQtb, alpha,
    beta, 1, 1, cholmod);
  CHECK(b_reduced_sparse != nullptr) << "cholmod_l_add failed.";
  cholmod_l_free_sparse(&A_rtb, cholmod);
  cholmod_l_free_sparse(&A_rtQQtb, cholmod);

  cholmod_dense* b_reduced = cholmod_l_sparse_to_dense(b_reduced_sparse,
    cholmod);
  CHECK(b_reduced != nullptr) << "cholmod_l_sparse_to_dense failed.";
  cholmod_l_free_sparse(&b_reduced_sparse, cholmod);
  return b_reduced;
}

void analyzeSVD(const cholmod_sparse* Omega, Eigen::VectorXd& sv,
    Eigen::MatrixXd& U, Eigen::MatrixXd& V) {
  CHECK_NOTNULL(Omega);

  Eigen::MatrixXd OmegaDense;
  cholmodSparseToEigenDenseCopy(Omega, OmegaDense);
  const Eigen::JacobiSVD<Eigen::MatrixXd> svd(OmegaDense,
    Eigen::ComputeThinU | Eigen::ComputeThinV);
  U = svd.matrixU();
  V = svd.matrixV();
  sv = svd.singularValues();
}

void solveSVD(const cholmod_dense* b, const Eigen::VectorXd& sv, const
    Eigen::MatrixXd& U, const Eigen::MatrixXd& V, std::ptrdiff_t rank,
    Eigen::VectorXd& x) {
  CHECK_NOTNULL(b);
  CHECK_LE(rank, V.cols());
  CHECK_EQ(V.cols(), sv.rows());
  CHECK_EQ(U.rows(), static_cast<std::ptrdiff_t>(b->nrow));

  Eigen::Map<const Eigen::VectorXd> b_eigen(
    reinterpret_cast<const double*>(b->x), b->nrow);
  x = V.leftCols(rank) * sv.head(rank).asDiagonal().inverse() *
    U.leftCols(rank).adjoint() * b_eigen;
}

cholmod_dense* solveQR(SuiteSparseQR_factorization<double>* factor,
    cholmod_dense* b, cholmod_sparse* A_r, const Eigen::VectorXd& x_r,
    cholmod_common* cholmod) {
  CHECK_NOTNULL(factor);
  CHECK(factor->QRsym != nullptr) << "Run symbolic factorization first.";
  CHECK(factor->QRnum != nullptr) << "Run QR decomposition first.";
  CHECK_NOTNULL(b);
  CHECK_NOTNULL(cholmod);

  cholmod_dense* QtbmA_rx_r = nullptr;
  if (A_r != nullptr) {
    CHECK_EQ(x_r.size(), static_cast<int>(A_r->ncol))
      << "Dimension mismatch between x_r and A_r.";

    cholmod_dense x_r_cholmod;
    eigenDenseToCholmodDenseView(x_r, &x_r_cholmod);
    cholmod_dense* A_rx_r = cholmod_l_allocate_dense(A_r->nrow, 1, A_r->nrow,
      CHOLMOD_REAL, cholmod);
    CHECK(A_rx_r != nullptr) << "cholmod_l_allocate_dense failed.";

    double alpha[2];
    alpha[0] = 1.0;
    double beta[2];
    beta[0] = 0.0;
    const bool success = cholmod_l_sdmult(A_r, 0, alpha, beta, &x_r_cholmod,
                                          A_rx_r, cholmod);
    CHECK(success) << "cholmod_l_sdmult failed.";

    Eigen::Map<const Eigen::VectorXd> bEigen(
      reinterpret_cast<const double*>(b->x), b->nrow);
    Eigen::Map<const Eigen::VectorXd> A_rx_rEigen(
      reinterpret_cast<const double*>(A_rx_r->x), A_rx_r->nrow);
    const Eigen::VectorXd bmA_rx_rEigen = bEigen - A_rx_rEigen;
    cholmod_l_free_dense(&A_rx_r, cholmod);
    cholmod_dense bmA_rx_r;
    eigenDenseToCholmodDenseView(bmA_rx_rEigen, &bmA_rx_r);
    QtbmA_rx_r = SuiteSparseQR_qmult<double>(SPQR_QTX, factor, &bmA_rx_r,
                                             cholmod);
  } else {
    QtbmA_rx_r = SuiteSparseQR_qmult<double>(SPQR_QTX, factor, b, cholmod);
  }
  CHECK(QtbmA_rx_r != nullptr) << "SuiteSparseQR_qmult failed.";

  cholmod_dense* x_l = SuiteSparseQR_solve<double>(SPQR_RETX_EQUALS_B,
    factor, QtbmA_rx_r, cholmod);
  CHECK(x_l != nullptr) << "SuiteSparseQR_solve failed.";
  cholmod_l_free_dense(&QtbmA_rx_r, cholmod);
  return x_l;
}

}  // namespace truncated_svd_solver

ext: cc


text:
#include "FMIndexManager.h"
#include <document-manager/DocumentManager.h>
#include <boost/filesystem.hpp>
#include <glog/logging.h>
#include <icma/icma.h>
#include <la-manager/LAPool.h>
#include <mining-manager/util/split_ustr.h>
#include <mining-manager/group-manager/DateStrFormat.h>
#include "FilterManager.h"
#include "../product-tokenizer/FuzzyNormalizer.h"

using namespace cma;
using namespace izenelib::util;

namespace sf1r
{
using namespace faceted;

FMIndexManager::FMIndexManager(
        const std::string& homePath,
        boost::shared_ptr<DocumentManager>& document_manager,
        boost::shared_ptr<FilterManager>& filter_manager,
        FuzzyNormalizer* fuzzyNormalizer)
    : data_root_path_(homePath)
    , document_manager_(document_manager)
    , filter_manager_(filter_manager)
    , doc_count_(0)
    , fuzzyNormalizer_(fuzzyNormalizer)
{
}

FMIndexManager::~FMIndexManager()
{
}

bool FMIndexManager::isStartFromLocalFM() const
{
    return doc_count_ > 0;
}

void FMIndexManager::addProperties(const std::vector<std::string>& properties, PropertyFMType type)
{
    //property
    for (size_t i = 0; i < properties.size(); ++i)
    {
        const std::string& index_prop = properties[i];
        std::pair<FMIndexIter, bool> insert_ret = all_fmi_.insert(std::make_pair(index_prop, PropertyFMIndex()));
        if (!insert_ret.second)
        {
            LOG(ERROR) << "duplicate fm index property : " << index_prop;
            continue;
        }
        insert_ret.first->second.type = type;
    }
}

void FMIndexManager::getProperties(std::vector<std::string>& properties, PropertyFMType type) const
{
    FMIndexConstIter cit = all_fmi_.begin();
    while (cit != all_fmi_.end())
    {
        if (cit->second.type == type)
            properties.push_back(cit->first);
        ++cit;
    }
}

void FMIndexManager::clearFMIData()
{
    for (FMIndexIter it = all_fmi_.begin(); it != all_fmi_.end() ; ++it)
    {
        it->second.fmi.reset();
        it->second.docarray_mgr_index = (size_t)(-1);
    }
    docarray_mgr_.clear();
    doc_count_ = 0;
    LOG(INFO) << "fmi data cleared!";
}

bool FMIndexManager::loadAll()
{
    for (FMIndexIter fmit = all_fmi_.begin(); fmit != all_fmi_.end() ; ++fmit)
    {
        std::ifstream ifs((data_root_path_ + "/" + fmit->first + ".fm_idx").c_str());
        if (ifs)
        {
            LOG(INFO) << "loading fmindex for property : " << fmit->first;
            fmit->second.fmi.reset(new FMIndexType);
            ifs.read((char*)&fmit->second.docarray_mgr_index, sizeof(fmit->second.docarray_mgr_index));
            fmit->second.fmi->load(ifs);
            if (fmit->second.docarray_mgr_index == (size_t)-1)
            {
                // the LESS_DV property hold its own doc_array data.
                if (fmit->second.type != LESS_DV)
                {
                    LOG(ERROR) << "the doc array should be stored to doc array manager for Not LESS_DV property: " << fmit->first;
                    clearFMIData();
                    return false;
                }
                if (fmit->second.fmi->docCount() == 0)
                {
                    LOG(ERROR) << "the LESS_DV property's doc array count is zero!!";
                }
            }
            else
            {
                if (fmit->second.type == LESS_DV)
                {
                    LOG(ERROR) << "Doc array should not be stored to doc array manager for LESS_DV property: " << fmit->first;
                    clearFMIData();
                    return false;
                }
                if (fmit->second.fmi->docCount() != 0 )
                {
                    LOG(ERROR) << "the COMMON property's doc array count must be zero!!";
                    clearFMIData();
                    return false;
                }
            }
        }
        else
        {
            if (fmit->second.type == LESS_DV)
                filter_manager_->setRebuildFlag();
            LOG(INFO) << "loading fmindex for property failed: " << fmit->first;
        }
    }

    try
    {
        std::ifstream ifs((data_root_path_ + "/" + "AllDocArray.doc_array").c_str());
        if (ifs)
        {
            docarray_mgr_.load(ifs);
            doc_count_ = docarray_mgr_.getDocCount();
            LOG(INFO) << data_root_path_ << " loading all doc array : " << doc_count_;
        }
    }
    catch (...)
    {
        LOG(ERROR) << "load the doc array of fmindex error.";
        clearFMIData();
        return false;
    }
    return true;
}

void FMIndexManager::swapUnchangedFilter(FMIndexManager* old_fmi_manager)
{
    const std::set<std::string>& unchanged_props = filter_manager_->getUnchangedProperties();

    std::set<std::string>::const_iterator unchanged_cit = unchanged_props.begin();
    for (; unchanged_cit != unchanged_props.end(); ++unchanged_cit)
    {
        FMIndexIter fmit = all_fmi_.find(*unchanged_cit);
        if (fmit != all_fmi_.end())
        {
            if (fmit->second.type == LESS_DV)
            {
                LOG(INFO) << "LESS_DV property fm-index swapped: " << *unchanged_cit;
                fmit->second.fmi.swap(old_fmi_manager->all_fmi_[*unchanged_cit].fmi);
            }
        }
        size_t prop_id = filter_manager_->getPropertyId(*unchanged_cit);
        if (prop_id == (size_t)-1)
        {
            LOG(ERROR) << "swap unchanged property failed for non-exist property: " << *unchanged_cit;
            continue;
        }
        LOG(INFO) << "LESS_DV property doc array swapped: " << *unchanged_cit;
        docarray_mgr_.swapFilterDocArray(prop_id, old_fmi_manager->docarray_mgr_);
    }
}

void FMIndexManager::buildExternalFilter()
{
    if (doc_count_ == 0)
    {
        LOG(ERROR) << "the doc count should not be 0 before building the external filter.";
        return;
    }
    docarray_mgr_.buildFilter();
}

void FMIndexManager::setFilterList(std::vector<std::vector<FMDocArrayMgrType::FilterItemT> > &filter_list)
{
    docarray_mgr_.setFilterList(filter_list);
}

bool FMIndexManager::getFilterRange(size_t prop_id, const RangeT &filter_id_range, RangeT &match_range) const
{
    return docarray_mgr_.getFilterRange(prop_id, filter_id_range, match_range);
}

void FMIndexManager::reconstructText(
        const std::string& prop_name,
        const std::vector<uint32_t>& del_docid_list,
        std::vector<uint16_t>& orig_text) const
{
    if (doc_count_ == 0) return;
    FMIndexConstIter cit = all_fmi_.find(prop_name);
    if (cit == all_fmi_.end()) return;
    if (cit->second.type != COMMON) return;
    docarray_mgr_.reconstructText(cit->second.docarray_mgr_index, del_docid_list, orig_text);
}

bool FMIndexManager::initAndLoadOldDocs(const FMIndexManager* old_fmi_manager)
{
    std::vector<uint32_t> del_docid_list;
    document_manager_->getDeletedDocIdList(del_docid_list);

    int has_orig_prop = 0;
    if (old_fmi_manager)
    {
        doc_count_ = old_fmi_manager->docCount();
    }
    for (FMIndexIter it = all_fmi_.begin(); it != all_fmi_.end(); ++it)
    {
        if (it->second.type != COMMON)
            continue;

        it->second.fmi.reset(new FMIndexType());
        if (doc_count_ == 0 || !old_fmi_manager)
            continue;

        const std::string& prop_name = it->first;
        FMIndexType* new_fmi = it->second.fmi.get();
        LOG(INFO) << "start loading orig_text in fm-index for property : " << prop_name;
        std::ifstream ifs((data_root_path_ + "/" + prop_name + ".orig_txt").c_str());
        if (ifs)
        {
            new_fmi->loadOriginalText(ifs);
            std::vector<uint16_t> orig_text;
            new_fmi->swapOrigText(orig_text);
            old_fmi_manager->reconstructText(prop_name, del_docid_list, orig_text);
            new_fmi->swapOrigText(orig_text);
            has_orig_prop++;
        }
        else
        {
            LOG(ERROR) << "one of fmindex's orig_txt is empty, must rebuilding from 0";
            doc_count_ = 0;
            // all properties should have orig_txt or none property have orig_txt.
            // if not, it should be some wrong and the build need stop.
            if (has_orig_prop > 0)
                return false;
        }
    }
    return true;
}

size_t FMIndexManager::putFMIndexToDocArrayMgr(FMIndexType* fmi)
{
    // the doc array added to manager should all be the same doc size.
    size_t mgr_index = docarray_mgr_.mainDocArrayNum();
    FMDocArrayMgrType::DocArrayItemT& doc_array_item = docarray_mgr_.addMainDocArrayItem();
    // take the owner of data to avoid duplication.
    LOG(INFO) << "add the doc array to manager, docCount: " << fmi->docCount();
    doc_array_item.doc_delim.swap(fmi->getDocDelim());
    doc_array_item.doc_array_ptr.swap(fmi->getDocArray());
    return mgr_index;
}

void FMIndexManager::buildLessDVProperties()
{
    // Indexing: the LESS_DV property docid list will be stored in the doc array manager.
    // The docid list wavelet tree can be generated from the inverted docid list in the
    // FilterManager. In FMIndex, only store the distinct property value id for
    // each distinct property value string.
    // Searching: from distinct value string we can get the distinct property value id
    // by searching the fmindex, and using the distinct property value id we can
    // get the filter range in the docarray of docid. Using the range we can
    // get topk docid list by doing union/intersect with other ranges.
    if (!filter_manager_)
    {
        LOG(INFO) << "filter manager is empty, LESS_DV property will not build.";
        return;
    }
    for (FMIndexIter it = all_fmi_.begin(); it != all_fmi_.end(); ++it)
    {
        if (it->second.type != LESS_DV)
            continue;
        it->second.fmi.reset(new FMIndexType());
        size_t prop_id = filter_manager_->getPropertyId(it->first);
        if (prop_id == (size_t)-1)
        {
            LOG(ERROR) << "LESS_DV property not found in filter manager : " << it->first;
            continue;
        }
        if (filter_manager_->isUnchangedProperty(it->first))
        {
            LOG(INFO) << "unchanged property do not need rebuild the fm-index. " << it->first;
            continue;
        }
        // read all property distinct string from filter manager.
        size_t max_filterstr_id = filter_manager_->getMaxPropFilterStrId(prop_id);
        for (size_t i = 1; i <= max_filterstr_id; ++i)
        {
            UString text = filter_manager_->getPropFilterString(prop_id, i);
            Algorithm<UString>::to_lower(text);
            fuzzyNormalizer_->normalizeText(text);
            for(size_t c_i = 0; c_i < text.length(); ++c_i)
            {
                if(text[c_i] == succinct::fm_index::DOC_DELIM)
                {
                    LOG(WARNING) << "find a DOC_DELIM char in the document data.";
                    text[c_i] = ' ';
                }
            }
            it->second.fmi->addDoc(text.data(), text.length());
        }
        LOG(INFO) << "LESS_DV for property count is: " << max_filterstr_id ;
        LOG(INFO) << "building fm-index for property: " << it->first << " ....";
        it->second.fmi->build();
        LOG(INFO) << "building fm-index for property: " << it->first << " finished, docCount: " << doc_count_;
    }
}

void FMIndexManager::swapCommonProperties(FMIndexManager* old_fmi_manager)
{
    std::swap(doc_count_, old_fmi_manager->doc_count_);
    LOG(INFO) << "swap common fmindex data, doc count: " << doc_count_;
    docarray_mgr_.swapMainDocArray(old_fmi_manager->docarray_mgr_);

    FMIndexIter it_start = all_fmi_.begin();
    FMIndexIter it_end = all_fmi_.end();
    FMIndexIter old_it_start = old_fmi_manager->all_fmi_.begin();
    FMIndexIter old_it_end = old_fmi_manager->all_fmi_.end();
    for (; it_start != it_end && old_it_start != old_it_end; ++it_start, ++old_it_start)
    {
        if (it_start->second.type != COMMON)
            continue;
        it_start->second.fmi.swap(old_it_start->second.fmi);
        std::swap(it_start->second.docarray_mgr_index, old_it_start->second.docarray_mgr_index);
    }
}

void FMIndexManager::useOldDocCount(const FMIndexManager* old_fmi_manager)
{
    doc_count_ = old_fmi_manager->doc_count_;
    docarray_mgr_.setDocCount(doc_count_);
}

bool FMIndexManager::buildCommonProperties(const FMIndexManager* old_fmi_manager)
{
    if (!initAndLoadOldDocs(old_fmi_manager))
    {
        LOG(ERROR) << "fmindex init building failed, must stop.";
        clearFMIData();
        return false;
    }

    appendDocs(doc_count_);

    return buildCollectionAfter();
}

void FMIndexManager::appendDocs(size_t last_docid)
{
    // add new document data to fm index.
    for (size_t i = last_docid + 1; i <= document_manager_->getMaxDocId(); ++i)
    {
        if (i % 100000 == 0)
        {
            LOG(INFO) << "inserted docs: " << i;
        }
        Document doc;
        bool failed = !document_manager_->getDocument(i, doc);
        appendDocsAfter(failed, doc);
    }
    LOG(INFO) << "inserted docs: " << document_manager_->getMaxDocId();
}

void FMIndexManager::appendDocsAfter(bool failed, const Document& doc)
{
    for (FMIndexIter it = all_fmi_.begin(); it != all_fmi_.end(); ++it)
    {
        if (it->second.type == COMMON)
        {
            if (failed)
            {
                it->second.fmi->addDoc(NULL, 0);
            }
            else
            {
                const std::string& prop_name = it->first;
                Document::property_const_iterator dit = doc.findProperty(prop_name);
                if (prop_name != virtualProperty_.virtualName && dit == doc.propertyEnd() )
                {
                    it->second.fmi->addDoc(NULL, 0);
                }
                else
                {
                    UString totalText;
                    UString space(" ", izenelib::util::UString::UTF_8);
                    if (prop_name == virtualProperty_.virtualName)
                    {
                        for (std::vector<std::string>::iterator i = virtualProperty_.virtual_properties.begin();
                             i != virtualProperty_.virtual_properties.end(); ++i)
                        {
                            Document::property_const_iterator vdit = doc.findProperty(*i);
                            if (vdit != doc.propertyEnd())
                            {
                                UString text = propstr_to_ustr(vdit->second.getPropertyStrValue());
                                totalText += text;
                                totalText += space;
                            }
                        }
                    }
                    else
                        totalText = propstr_to_ustr(dit->second.getPropertyStrValue());

                    Algorithm<UString>::to_lower(totalText);
                    fuzzyNormalizer_->normalizeText(totalText);

                    for(size_t c_i = 0; c_i < totalText.length(); ++c_i)
                    {
                        if(totalText[c_i] == succinct::fm_index::DOC_DELIM)
                        {
                            LOG(WARNING) << "find a DOC_DELIM char in the document data.";
                            totalText[c_i] = ' ';
                        }
                    }

                    it->second.fmi->addDoc(totalText.data(), totalText.length());
                }
            }
        }
    }
    //LOG(INFO) << "inserted docs: " << document_manager_->getMaxDocId();
}

bool FMIndexManager::buildCollectionAfter()
{
    size_t new_doc_cnt = 0;
    doc_count_ = 0;
    docarray_mgr_.clearMainDocArray();
    for (FMIndexIter it = all_fmi_.begin(); it != all_fmi_.end(); ++it)
    {
        if (it->second.type != COMMON)
            continue;
        std::ofstream ofs((data_root_path_ + "/" + it->first + ".orig_txt").c_str());
        it->second.fmi->saveOriginalText(ofs);
        ofs.close();
        LOG(INFO) << "building fm-index for property: " << it->first << " ....";
        it->second.fmi->build();
        new_doc_cnt = it->second.fmi->docCount();
        if (doc_count_ == 0)
        {
            doc_count_ = new_doc_cnt;
            docarray_mgr_.setDocCount(doc_count_);
        }
        else if (new_doc_cnt != doc_count_)
        {
            LOG(ERROR) << "docCount is different in common property: " << it->first;
            clearFMIData();
            return false;
        }
        it->second.docarray_mgr_index = putFMIndexToDocArrayMgr(it->second.fmi.get());
        LOG(INFO) << "building fm-index for property : " << it->first << " finished, docCount:" << doc_count_;
    }
    return true;
}

void FMIndexManager::getMatchedDocIdList(
        const std::string& property,
        const RangeListT& raw_range_list,
        size_t max_docs,
        std::vector<uint32_t>& docid_list,
        std::vector<size_t>& doclen_list) const
{
    if (doc_count_ == 0)
        return;
    // if the property is LESS_DV type, find in the filter docid list
    // otherwise find in the fm index.
    FMIndexConstIter cit = all_fmi_.find(property);
    if (cit == all_fmi_.end())
    {
        return;
    }
    if (cit->second.type == COMMON)
    {
        if (cit->second.docarray_mgr_index == (size_t)-1)
        {
            LOG(ERROR) << "the common property: " << property << " not found in doc array.";
            return;
        }
        docarray_mgr_.getMatchedDocIdList(cit->second.docarray_mgr_index, false, raw_range_list,
            max_docs, docid_list, doclen_list);
    }
    else if (cit->second.type == LESS_DV)
    {
        if (!filter_manager_)
            return;
        size_t match_filter_index = filter_manager_->getPropertyId(property);
        if (match_filter_index == (size_t)-1)
        {
            LOG(ERROR) << "the LESS_DV property: " << property << " not found in filter.";
            return;
        }
        docarray_mgr_.getMatchedDocIdList(match_filter_index, true, raw_range_list,
            max_docs, docid_list, doclen_list);
        // Since doclen is not available in doc array manager for LESS_DV property,
        // we need recompute doclen from common property.
        getDocLenList(docid_list, doclen_list);
    }
    else
    {
        assert(false);
    }

}

void FMIndexManager::convertMatchRanges(
        const std::string& property,
        size_t max_docs,
        RangeListT& raw_range_list,
        std::vector<double>& score_list) const
{
    if (doc_count_ == 0) return;
    FMIndexConstIter cit = all_fmi_.find(property);
    if (cit == all_fmi_.end())
    {
        return;
    }
    if (cit->second.type != LESS_DV || !filter_manager_)
        return;
    if (!cit->second.fmi)
    {
        raw_range_list.clear();
        score_list.clear();
        return;
    }
    std::vector<uint32_t> tmp_docid_list;
    std::vector<size_t> tmp_doclen_list;
    RangeListT converted_match_ranges;
    std::vector<double> converted_score_list;
    LOG(INFO) << "convert range for property : " << property;
    for (size_t i = 0; i < raw_range_list.size(); ++i)
    {
        LOG(INFO) << "orig range : " << raw_range_list[i].first << ", " << raw_range_list[i].second;
        cit->second.fmi->getMatchedDocIdList(raw_range_list[i], max_docs, tmp_docid_list, tmp_doclen_list);
        // for LESS_DV, the docid is the distinct value id, we need get all the docid belong to the distinct
        // value from the docarray_mgr_.
        size_t oldsize = converted_match_ranges.size();
        converted_match_ranges.reserve(oldsize + tmp_docid_list.size());
        converted_score_list.reserve(oldsize + tmp_docid_list.size());
        std::sort(tmp_docid_list.begin(), tmp_docid_list.end(), std::less<uint32_t>());
        for (size_t j = 0; j < tmp_docid_list.size(); ++j)
        {
            size_t prop_id = filter_manager_->getPropertyId(property);
            if (prop_id == (size_t)-1)
            {
                LOG(ERROR) << "property id not found in FilterManager, " << property;
                continue;
            }
            UString match_filter_string = filter_manager_->getPropFilterString(prop_id, tmp_docid_list[j]);
            if (match_filter_string.empty())
                continue;
            FilterManager::FilterIdRange range = filter_manager_->getStrFilterIdRangeExact(
                    prop_id, match_filter_string);
            if (range.start >= range.end)
                continue;
            RangeT doc_array_filterrange;
            getFilterRange(prop_id, std::make_pair(range.start, range.end), doc_array_filterrange);
            LOG(INFO) << "( id: " << tmp_docid_list[j] << ", converted match range: " << doc_array_filterrange.first <<
                "-" << doc_array_filterrange.second << ")";
            if (converted_match_ranges.size() > oldsize)
            {
                RangeT& prev_range = converted_match_ranges.back();
                if (doc_array_filterrange.first < prev_range.first)
                {
                    LOG(ERROR) << "range should be after.!! " << doc_array_filterrange.first <<
                        "-" << doc_array_filterrange.second << ", prev_range: " <<
                        prev_range.first << "-" << prev_range.second;
                }
                // merge the overlap range.
                if (doc_array_filterrange.first <= prev_range.second)
                {
                    prev_range.second = max(doc_array_filterrange.second, prev_range.second);
                    continue;
                }
            }
            converted_match_ranges.push_back(doc_array_filterrange);
            converted_score_list.push_back(score_list[i]);
        }
        tmp_docid_list.clear();
        tmp_doclen_list.clear();
    }
    converted_match_ranges.swap(raw_range_list);
    converted_score_list.swap(score_list);
}

size_t FMIndexManager::longestSuffixMatch(
        const std::string& property,
        const UString& pattern,
        RangeListT& raw_range_list) const
{
    if (doc_count_ == 0)
        return 0;
    FMIndexConstIter cit = all_fmi_.find(property);
    if (cit == all_fmi_.end() || !cit->second.fmi)
    {
        return 0;
    }
    return cit->second.fmi->longestSuffixMatch(pattern.data(), pattern.length(), raw_range_list);
}

size_t FMIndexManager::backwardSearch(const std::string& prop, const UString& pattern, RangeT& match_range) const
{
    if (doc_count_ == 0)
        return 0;

    FMIndexConstIter cit = all_fmi_.find(prop);
    if (cit == all_fmi_.end() || !cit->second.fmi)
    {
        return 0;
    }
    return cit->second.fmi->backwardSearch(pattern.data(), pattern.length(), match_range);
}

void FMIndexManager::getTopKDocIdListByFilter(
        const std::string& property,
        const std::vector<size_t> &prop_id_list,
        const std::vector<RangeListT> &filter_ranges,
        const RangeListT &raw_range_list,
        const std::vector<double> &score_list,
        size_t thres,
        size_t max_docs,
        std::vector<std::pair<double, uint32_t> > &res_list) const
{
    if (doc_count_ == 0)
        return;

    FMIndexConstIter cit = all_fmi_.find(property);
    if (cit == all_fmi_.end())
    {
        LOG(INFO) << "get topk failed for not exist property : " << property;
        return;
    }
    if (cit->second.type == COMMON)
    {
        LOG(INFO) << "get topk in common property : " << property;
        if (cit->second.docarray_mgr_index == (size_t)-1)
        {
            LOG(ERROR) << "the common property: " << property << " not found in doc array.";
            return;
        }
        docarray_mgr_.getTopKDocIdListByFilter(
                prop_id_list,
                filter_ranges,
                cit->second.docarray_mgr_index,
                false,
                raw_range_list,
                score_list,
                thres,
                max_docs,
                res_list);
    }
    else if (cit->second.type == LESS_DV)
    {
        if (!filter_manager_) return;

        LOG(INFO) << "get topk in LESS_DV property : " << property;
        size_t match_filter_index = filter_manager_->getPropertyId(property);
        if (match_filter_index == (size_t)-1)
        {
            LOG(ERROR) << "the LESS_DV property: " << property << " not found in filter.";
            return;
        }
        docarray_mgr_.getTopKDocIdListByFilter(
                prop_id_list,
                filter_ranges,
                match_filter_index,
                true,
                raw_range_list,
                score_list,
                thres,
                max_docs,
                res_list);
    }
    else
    {
        assert(false);
    }
}

//yy
void FMIndexManager::getTopKDocIdListByFilter(
        const std::string& property,
        const std::vector<size_t> &prop_id_list,
        const std::vector<RangeListT> &filter_ranges,
        const std::vector<std::vector<boost::tuple<size_t, size_t, double> > > &synonym_range_list,
        size_t thres,
        size_t max_docs,
        std::vector<std::pair<double, uint32_t> > &res_list) const
{
    if (doc_count_ == 0)
        return;

    FMIndexConstIter cit = all_fmi_.find(property);
    if (cit == all_fmi_.end())
    {
        LOG(INFO) << "get topk failed for not exist property : " << property;
        return;
    }
    if (cit->second.type == COMMON)
    {
        LOG(INFO) << "get topk in common property : " << property;
        if (cit->second.docarray_mgr_index == (size_t)-1)
        {
            LOG(ERROR) << "the common property: " << property << " not found in doc array.";
            return;
        }
        docarray_mgr_.getTopKDocIdListByFilter(
                prop_id_list,
                filter_ranges,
                cit->second.docarray_mgr_index,
                false,
                synonym_range_list,
                thres,
                max_docs,
                res_list);
    }
    else if (cit->second.type == LESS_DV)
    {
        if (!filter_manager_) return;

        LOG(INFO) << "get topk in LESS_DV property : " << property;
        size_t match_filter_index = filter_manager_->getPropertyId(property);
        if (match_filter_index == (size_t)-1)
        {
            LOG(ERROR) << "the LESS_DV property: " << property << " not found in filter.";
            return;
        }
        docarray_mgr_.getTopKDocIdListByFilter(
                prop_id_list,
                filter_ranges,
                match_filter_index,
                true,
                synonym_range_list,
                thres,
                max_docs,
                res_list);
    }
    else
    {
        assert(false);
    }
}

void FMIndexManager::getDocLenList(const std::vector<uint32_t>& docid_list, std::vector<size_t>& doclen_list) const
{
    // the doclen is total length for common property only.
    docarray_mgr_.getDocLenList(docid_list, doclen_list);
}

void FMIndexManager::getLessDVStrLenList(const std::string& property, const std::vector<uint32_t>& dvid_list, std::vector<size_t>& dvlen_list) const
{
    dvlen_list.resize(dvid_list.size(), 0);
    if (doc_count_ == 0)
        return;
    FMIndexConstIter cit = all_fmi_.find(property);
    if (cit == all_fmi_.end() || !cit->second.fmi)
    {
        LOG(INFO) << "get LESS_DV string length failed for not exist property : " << property;
        return;
    }
    if (cit->second.type != LESS_DV)
    {
        LOG(INFO) << "get LESS_DV string length failed for not LESS_DV property : " << property;
        return;
    }

    // for LESS_DV properties, we can use the distinct value id to get the length of the distinct value string.
    cit->second.fmi->getDocLenList(dvid_list, dvlen_list);
}

void FMIndexManager::saveAll()
{
    FMIndexConstIter fmit = all_fmi_.begin();
    while (fmit != all_fmi_.end())
    {
        std::ofstream ofs;
        ofs.open((data_root_path_ + "/" + fmit->first + ".fm_idx").c_str());
        ofs.write((const char*)&fmit->second.docarray_mgr_index, sizeof(fmit->second.docarray_mgr_index));
        if (fmit->second.fmi)
            fmit->second.fmi->save(ofs);
        ++fmit;
    }
    try
    {
        std::ofstream ofs;
        ofs.open((data_root_path_ + "/" + "AllDocArray.doc_array").c_str());
        docarray_mgr_.save(ofs);
    }
    catch (...)
    {
        LOG(ERROR) << "saving doc array of fmindex error. must clean crashed data.";
        clearFMIData();
        boost::filesystem::remove_all(data_root_path_);
    }
}

}

ext: cpp


text:
/**
    copyright  (C) 2004
    the icecube collaboration
    $Id: LexicalCasts.cxx 168448 2012-11-04 16:34:42Z nwhitehorn $

    @version $Revision: 168448 $
    @date $Date: 2012-11-04 09:34:42 -0700 (Sun, 04 Nov 2012) $
    @author troy d. straszheim <troy@resophonic.com>
*/

#include <I3Test.h>
#include <icetray/I3Logging.h>
#include <icetray/I3Context.h>
#include <icetray/OMKey.h>
#include <boost/lexical_cast.hpp>
#include <string>
using namespace std;

TEST_GROUP(LexicalCasts);

TEST(OMKey_roundtrip)
{
  // OMKey goes round-trip
  OMKey k(3,14);
  string s = boost::lexical_cast<string>(k);
  cout << s << endl;
  OMKey k2 = boost::lexical_cast<OMKey>(s);
  ENSURE_EQUAL(k, k2);

  // OMKey goes round-trip (with PMT number)
  k = OMKey(3,14,15);
  s = boost::lexical_cast<string>(k);
  cout << s << endl;
  k2 = boost::lexical_cast<OMKey>(s);
  ENSURE_EQUAL(k, k2);

  k = OMKey(-13,0);
  s = boost::lexical_cast<string>(k);
  k2 = boost::lexical_cast<OMKey>(s);
  ENSURE_EQUAL(k, k2);
}

ext: cxx


text:
#include <iostream>
#include "mod/set.hpp"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE( set_suite )

BOOST_AUTO_TEST_CASE( mod_set_contains_true ) {
  mod::set<int> s1 = {1, 2, 3};

  BOOST_TEST(s1.contains(1));
  BOOST_TEST(s1.contains(2));
  BOOST_TEST(s1.contains(3));
}

BOOST_AUTO_TEST_CASE( mod_set_contains_false ) {
  mod::set<int> s1 = {1, 2, 3};

  BOOST_TEST(!s1.contains(4));
  BOOST_TEST(!s1.contains(-1));
  BOOST_TEST(!s1.contains(6));
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: rationalis-petra/modulus

max_issues_repo_name: rationalis-petra/modulus

max_forks_repo_name: rationalis-petra/modulus


text:
class HomogeneousElectronGas
{
public:
  HomogeneousElectronGas()
    : _rs(1.0)
  {}

  double get_rs() const { return _rs; }
  void set_rs(double rs)
  {
    _rs = rs;
  }

private:
  double _rs;
};

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(heg)
{
    class_<HomogeneousElectronGas>("HomogeneousElectronGas")
        .add_property("rs",
          &HomogeneousElectronGas::get_rs,
          &HomogeneousElectronGas::set_rs
        )
    ;
}

max_stars_repo_name: Paul-St-Young/QMC

max_issues_repo_name: Paul-St-Young/QMC

max_forks_repo_name: Paul-St-Young/QMC


text:
// Copyright (c) 2020 Bitcoin Association
// Distributed under the Open BSV software license, see the accompanying file
// LICENSE.

#include "config.h"
#include "mempooltxdb.h"
#include "mining/journal_change_set.h"

#include "mempool_test_access.h"

#include "test/test_bitcoin.h"

#include <boost/test/unit_test.hpp>

#include <vector>

namespace {
    mining::CJournalChangeSetPtr nullChangeSet{nullptr};

    std::vector<CTxMemPoolEntry> GetABunchOfEntries(int howMany, bool expired=false)
    {
        TestMemPoolEntryHelper entry;
        if (!expired) {
            entry.Time(GetTime());
        }

        std::vector<CTxMemPoolEntry> result;
        for (int i = 0; i < howMany; i++) {
            CMutableTransaction mtx;
            mtx.vin.resize(1);
            mtx.vin[0].scriptSig = CScript() << OP_11;
            mtx.vout.resize(1);
            mtx.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
            mtx.vout[0].nValue = Amount(33000LL + i);
            result.emplace_back(entry.FromTx(mtx));
        }
        return result;
    }

    struct Validator
    {
        explicit Validator(CTxMemPool& pool_)
            : pool(pool_)
        {}

        CValidationState operator()(const TxInputDataSPtr& txInputData,
                                    const mining::CJournalChangeSetPtr& changeSet,
                                    bool limitMempoolSize)
        {
            auto entry = std::make_shared<CTxMemPoolEntry>(
                helper.Time(txInputData->GetAcceptTime())
                .FromTx(*txInputData->GetTxnPtr()));
            pool.AddUnchecked(entry->GetTxId(), *entry,
                              txInputData->GetTxStorage(),
                              changeSet);
            return CValidationState();
        }

    private:
        CTxMemPool& pool;
        TestMemPoolEntryHelper helper;
    };

    bool LoadMempool(CTxMemPoolTestAccess& poolAccess, Config& testConfig)
    {
        auto token = task::CCancellationSource::Make()->GetToken();
        auto validator = Validator(poolAccess.mempool);
        const auto validate =
            [&validator](const TxInputDataSPtr& txInputData,
                         const mining::CJournalChangeSetPtr& changeSet,
                         bool limitMempoolSize) -> CValidationState
            {
                return validator(txInputData, changeSet, limitMempoolSize);
            };
        return poolAccess.LoadMempool(testConfig, token, validate);
    }

    uint64_t PrepareMempoolDat(const std::vector<CTxMemPoolEntry>& entries,
                               const uint64_t version, const bool save,
                               int* uniqueTxdbSuffix)
    {
        uint64_t count = 0;
        CTxMemPool testPool;
        CTxMemPoolTestAccess testPoolAccess(testPool);

        // Add transactions:
        testPoolAccess.InitUniqueMempoolTxDB();
        *uniqueTxdbSuffix = testPoolAccess.GetMempoolTxDBUniqueSuffix();
        for (auto& entry : entries) {
            testPool.AddUnchecked(entry.GetTxId(), entry, TxStorage::memory, nullChangeSet);
        }
        BOOST_CHECK_EQUAL(testPool.Size(), entries.size());
        BOOST_CHECK_EQUAL(testPool.GetDiskTxCount(), 0);

        if (save)
        {
            // Save half of the transactions to disk.
            size_t size = 0;
            count = entries.size() / 2;
            for (uint64_t i = 0; i < count; ++i)
            {
                size += entries[i].GetTxSize();
            }
            testPool.SaveTxsToDisk(size);
            testPoolAccess.SyncWithMempoolTxDB();
            BOOST_CHECK_EQUAL(testPool.GetDiskTxCount(), count);
        }

        // Dump the mempool and forget about it.
        testPoolAccess.DumpMempool(version);
        BOOST_CHECK_EQUAL(testPool.GetDiskTxCount(), count);
        return count;
    }

    void LoadMempoolDat(const std::vector<CTxMemPoolEntry>& entries, Config& testConfig,
                        const uint64_t count, const bool expired,
                        int uniqueTxdbSuffix)
    {
        CTxMemPool testPool;
        CTxMemPoolTestAccess testPoolAccess(testPool);

        testPool.SuspendSanityCheck();
        testPoolAccess.SetMempoolTxDBUniqueSuffix(uniqueTxdbSuffix);
        testPoolAccess.InitUniqueMempoolTxDB();
        BOOST_CHECK(LoadMempool(testPoolAccess, testConfig));
        testPool.ResumeSanityCheck();

        BOOST_CHECK(testPoolAccess.CheckMempoolTxDB());
        if (!expired)
        {
            BOOST_CHECK_EQUAL(testPool.Size(), entries.size());
            BOOST_CHECK_EQUAL(testPool.GetDiskTxCount(), count);
            for (const auto& entry : entries)
            {
                BOOST_CHECK(testPool.Exists(entry.GetTxId()));
            }
        }
        else
        {
            BOOST_CHECK_EQUAL(testPool.Size(), 0);
            BOOST_CHECK_EQUAL(testPool.GetDiskTxCount(), 0);
            for (const auto& entry : entries)
            {
                BOOST_CHECK(!testPool.Exists(entry.GetTxId()));
            }
        }
    }
}

BOOST_FIXTURE_TEST_SUITE(mempool_dump_load_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(DumpLoadFormat1)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6);
    const auto count = PrepareMempoolDat(entries, 1, false, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, 0);
    LoadMempoolDat(entries, testConfig, count, false, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat1Empty)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(0);
    const auto count = PrepareMempoolDat(entries, 1, false, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, 0);
    LoadMempoolDat(entries, testConfig, count, false, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat1WithOnDiskTxs)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6);
    const auto count = PrepareMempoolDat(entries, 1, true, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, entries.size() / 2);
    LoadMempoolDat(entries, testConfig, 0, false, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat1Expired)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6, true);
    const auto count = PrepareMempoolDat(entries, 1, false, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, 0);
    LoadMempoolDat(entries, testConfig, 0, true, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat2)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6);
    const auto count = PrepareMempoolDat(entries, 2, false, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, 0);
    LoadMempoolDat(entries, testConfig, count, false, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat2Empty)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(0);
    const auto count = PrepareMempoolDat(entries, 2, false, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, 0);
    LoadMempoolDat(entries, testConfig, count, false, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat2WithOnDiskTxs)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6);
    const auto count = PrepareMempoolDat(entries, 2, true, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, entries.size() / 2);
    LoadMempoolDat(entries, testConfig, count, false, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat2Expired)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6, true);
    const auto count = PrepareMempoolDat(entries, 2, false, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, 0);
    LoadMempoolDat(entries, testConfig, 0, true, uniqueSuffix);
}

BOOST_AUTO_TEST_CASE(DumpLoadFormat2WithOnDiskTxsExpired)
{
    int uniqueSuffix = -1;
    gArgs.ForceSetBoolArg("-persistmempool", true);
    const auto entries = GetABunchOfEntries(6, true);
    const auto count = PrepareMempoolDat(entries, 2, true, &uniqueSuffix);
    BOOST_CHECK_EQUAL(count, entries.size() / 2);
    LoadMempoolDat(entries, testConfig, 0, true, uniqueSuffix);
}
BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: AustEcon/bitcoin-sv


text:
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

#include "stdafx.h"

#include <boost/test/unit_test.hpp>
#include "Common/boost-taef.h"

using namespace ktl;
using namespace Data::StateManager;
using namespace Data::Utilities;
using namespace StateManagerTests;

SerializableMetadata::CSPtr TestCheckpointFileReadWrite::ReadMetadata(
    __in PartitionedReplicaId const & traceId,
    __in BinaryReader & reader,
    __in KAllocator & allocator)
{
    SerializableMetadata::CSPtr serializableMetadataCSPtr = nullptr;
    NTSTATUS status = CheckpointFileAsyncEnumerator::ReadMetadata(traceId, reader, allocator, serializableMetadataCSPtr);
    VERIFY_IS_TRUE(NT_SUCCESS(status));

    return serializableMetadataCSPtr;
}

ULONG32 TestCheckpointFileReadWrite::WriteMetadata(
    __in PartitionedReplicaId const & traceId,
    __in BinaryWriter & writer,
    __in SerializableMetadata const & metadata,
    __in SerializationMode::Enum serailizationMode)
{
    return CheckpointFile::WriteMetadata(traceId, writer, metadata, serailizationMode);
}

max_stars_repo_name: gridgentoo/ServiceFabricAzure

max_issues_repo_name: gridgentoo/ServiceFabricAzure

max_forks_repo_name: gridgentoo/ServiceFabricAzure


text:
// Copyright 2014-2015 SDL plc
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/** \file

    thread specific objects (and their pointers) must not be passed to other
    threads. if they exist on the thread stack then they're already safe to write/read
    for just that thread. if they exist on the heap then they're like a map
    keyed on thread-id - each thread has its own slot
*/

#ifndef THREADSPECIFIC_JG2012917_HPP
#define THREADSPECIFIC_JG2012917_HPP
#pragma once

#ifndef SDL_USE_PTHREAD_THREAD_SPECIFIC
#if defined(_MSC_VER)
#define SDL_USE_PTHREAD_THREAD_SPECIFIC 0
#elif SDL_VALGRIND
#define SDL_USE_PTHREAD_THREAD_SPECIFIC 0
#else
/**
   uninit warning if 1:
~ThreadSpecificInt (ThreadSpecific.hpp:100) - Util::ThreadSpecificBool originalAlreadyIncluded
~IWordToPhrase (WordToPhrase.hpp:125)
~IStringToString (StringToString.hpp:37)
~Recase (Lowercase.hpp:26)
~configure_info_for (configure_is.hpp:100)
std::string configure::configure_usage<sdl::Lowercase::Recase>() (configure_is.hpp:123)
*/
#define SDL_USE_PTHREAD_THREAD_SPECIFIC 0
#endif
#endif

#include <sdl/Util/Delete.hpp>
#include <sdl/Util/Errno.hpp>
#include <sdl/Util/ThreadId.hpp>
#include <sdl/IntTypes.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread/tss.hpp>
#include <memory>
#include <string>
#include <type_traits>
#include <stdint.h>
// warning: std::uintptr_t is in C++11 only. most compilers' stdint.h will have uintptr_t, though
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
#include <pthread.h>
#else
#include <boost/shared_ptr.hpp>
#endif

namespace sdl {
namespace Util {

using boost::thread_specific_ptr;

namespace impl {
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
using ::pthread_getspecific;
using ::pthread_setspecific;
using ::pthread_key_create;
using ::pthread_key_delete;
using ::pthread_key_t;
#else
using boost::detail::get_tss_data;
using boost::detail::set_tss_data;
using boost::detail::tss_cleanup_function;
#endif
}

inline bool isThreadStackAddress(void const* p) {
  return false;
  // TODO: pthread_attr_getstack can tell us precisely, but is it faster than using tss?
}

template <class Int>
struct ThreadSpecificInt : boost::noncopyable {
  typedef uintptr PtrInt;
  union KeyOrInt {
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
    ::pthread_key_t key;
#endif
    Int i;
  };
  KeyOrInt ki;
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
  void check(int pthread_errno, char const* callname = "ThreadSpecificInt pthread error") {
    if (pthread_errno) throw_errno(callname);
  }
#endif
  enum { isThreadStack = false };
  bool alreadyThreadSpecific() const { return isSingleThreadProgram() || isThreadStack; }

  ThreadSpecificInt() { init(Int()); }
  ThreadSpecificInt(ThreadSpecificInt const& o) { init(o); }
  ThreadSpecificInt(Int i) { init(i); }
  ~ThreadSpecificInt() {
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
    check(::pthread_key_delete(ki.key), "pthread_key_delete");
#endif
  }
  operator Int() const { return get(); }
  void operator=(Int i) { set(i); }
  void operator=(ThreadSpecificInt const& i) { set(i); }
  Int get() const {
    if (alreadyThreadSpecific()) return ki.i;
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
    return static_cast<Int>((PtrInt)::pthread_getspecific(ki.key));
// will return NULL=0 if val not set on this thread
#else
    return static_cast<Int>((PtrInt)impl::get_tss_data(this));
#endif
  }
  static_assert(sizeof(Int) <= sizeof(PtrInt), "ThreadSpecificInt must be no larger than void*");
  void set(Int i) {
    if (alreadyThreadSpecific())
      ki.i = i;
    else
      setThreadSpecific(i);
  }
  void setThreadSpecific(Int i) {
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
    (void)::pthread_setspecific(ki.key, (void const*)static_cast<PtrInt>(i));
#else
    // TODO: why not sdl::shared_ptr (std)
    impl::set_tss_data(this, boost::shared_ptr<impl::tss_cleanup_function>(), (void*)static_cast<PtrInt>(i),
                       false);
// Boost (pre- 1.52) bug on Windows: must past pointer to do-nothing tss_cleanup_function instead of nullptr
#endif
  }
  typedef ThreadSpecificInt<Int> Self;

 private:
  void init(Int i) {
    maybeCheckSingleThread();
    // isThreadStack = isThreadStackAddress(this); //TODO
    if (alreadyThreadSpecific())
      ki.i = i;
    else {
#if SDL_USE_PTHREAD_THREAD_SPECIFIC
      check(::pthread_key_create(&ki.key, NULL), "pthread_key_create");
// Upon key creation, the value NULL shall be associated with the new key in all active threads. Upon thread
// creation, the value NULL shall be associated with all defined keys in the new thread.
#endif
      setThreadSpecific(i);
    }
  }
};

typedef ThreadSpecificInt<bool> ThreadSpecificBool;
typedef ThreadSpecificInt<unsigned> ThreadSpecificUnsigned;
typedef ThreadSpecificInt<std::size_t> ThreadSpecificSize;

/** new Value() per thread. */
template <class Value>
struct ThreadSpecific : boost::noncopyable {
  Value* local_;
  enum { isThreadStack = false };
  bool alreadyThreadSpecific() const { return isSingleThreadProgram() || isThreadStack; }
  typedef boost::thread_specific_ptr<Value> PVal;
  static void cleanFun(Value* p) {
    if (p) {
      p->~Value();
      std::free(p);
    }
  }
  ThreadSpecific() : pVal(cleanFun), local_() {}
  ~ThreadSpecific() {
    if (alreadyThreadSpecific() && local_) {
      local_->~Value();
      std::free(local_);
    } else {
      assert(!local_);
    }
  }
  PVal pVal;
  operator Value&() { return get(); }

  Value& get() {
    maybeCheckSingleThread();
    if (alreadyThreadSpecific()) {
      if (!local_) {
        local_ = alloc();
        new (local_) Value();
      }
      return *local_;
    } else {
      Value* v = pVal.get();
      if (!v) {
        v = alloc();
        new (v) Value();
        pVal.reset(v);
      }
      return *v;
    }
  }

  /// \return whether v needs to be constructed (will only be true once per
  /// thread) - sets v to point at thread specific value
  bool isUnconstructed(Value*& v) {
    if (alreadyThreadSpecific()) {
      if (local_) {
        v = local_;
        return false;
      } else {
        local_ = v = alloc();
        return true;
      }
    } else {
      v = pVal.get();
      if (v)
        return false;
      else {
        pVal.reset(v = alloc());
        return true;
      }
    }
  }

  Value* getPtr() const { return alreadyThreadSpecific() ? local_ : pVal.get(); }
  Value const& get() const {
    Value* v = getPtr();
    assert(v != NULL);
    return *v;
  }
  bool got() const { return getPtr(); }
  void maybeAssign(ThreadSpecific const& o) {
    if (o.got()) get() = o.get();
  }

 private:
  ThreadSpecific(ThreadSpecific const& o);
  static Value* alloc() { return (Value*)std::malloc(sizeof(Value)); }
};

}}

#endif

ext: hpp

max_stars_repo_name: sdl-research/hyp

max_issues_repo_name: hypergraphs/hyp

max_forks_repo_name: hypergraphs/hyp


text:
#include <Resource/cJsonManager.h>
#include <Utility/cString.h>
#include <Utility/cSearchSystem.h>
#include <cinder/ObjLoader.h>
#include <cinder/app/App.h>
#include <boost/optional.hpp>
namespace Resource
{
cJsonManager::cJsonManager( )
{
	reload( );
}
boost::optional<Json::Value const&> cJsonManager::find( std::string const & underAssetsUnderJSONUnderPath )const noexcept
{
	auto findItr = mJsons.find( underAssetsUnderJSONUnderPath );
	if ( findItr != mJsons.end( ) )
	{
		return findItr->second;
	}
	else
	{
		return boost::none;
	}
}
void cJsonManager::reload( )
{
	mJsons.clear( );
	Utility::cSearchSystem search;
	search.search( Utility::cString::getAssetPath( ) + "JSON\\" );
	auto& files = search.unixNotationFullPaths( );
	for ( int i = 0, size = files.size( ); i < size; ++i )
	{
		auto const& fileName = files[i];
		auto extension = Utility::cString::getExtensionName( fileName );
		if ( extension == "json" )
		{
			auto underPos = fileName.find( "JSON/" ) + sizeof( "JSON/" ) - sizeof( '\0' );
			auto unferName = fileName.substr( underPos );
			std::stringstream ss;
			auto asset = cinder::app::loadAsset( "JSON/" + unferName );
			auto buffer = asset->getBuffer( );
			ss.write( static_cast<char const*>( buffer->getData( ) ), buffer->getSize( ) );
			Json::Value root;
			if ( Json::Reader( ).parse( ss, root ) )
			{
				mJsons.insert( std::make_pair( unferName, root ) );
			}
		}
	}
}
JsonSuppoter const JSON;
}

max_stars_repo_name: desspert/mogupro

max_issues_repo_name: desspert/mogupro

max_forks_repo_name: desspert/mogupro


text:
#include "RengaPyModule.h"
#include "PyScriptRuntimeContext.h"

#include <boost/python.hpp>

#include <RengaScript/IParameters.h>

using namespace boost::python;
using namespace py; // local namespace

renga_script::MetricParameter* Parameters::createMetric(std::wstring name, double defaultValue)
{
  auto pParametersBackend = ScriptRuntimeContext::getContext()->getParameters();
  auto parameter = renga_script::MetricParameter(name, defaultValue);
  auto parameterWasSet = pParametersBackend->setParameter(parameter);
  if (parameterWasSet == false)
    throw std::wstring(L"Failed to set parameter: ") + name; // TODO: message duplicate
  return new renga_script::MetricParameter(name, defaultValue);
}

BOOST_PYTHON_MODULE(renga)
{
  class_<renga_script::MetricParameter>("MetricParameter", no_init)
    ;

  class_<Parameters>("params")
    .def("create_metric", &Parameters::createMetric, return_value_policy<manage_new_object>())
    ;
}

max_stars_repo_name: tyan/RengaScript

max_issues_repo_name: tyan/RengaScript

max_forks_repo_name: tyan/RengaScript


text:
//==============================================================================
//         Copyright 2003 - 2012   LASMEA UMR 6602 CNRS/Univ. Clermont II
//         Copyright 2009 - 2012   LRI    UMR 8623 CNRS/Univ Paris Sud XI
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#include <nt2/table.hpp>
#include <nt2/include/functions/cons.hpp>
#include <nt2/include/functions/of_size.hpp>
#include <boost/array.hpp>

#include <nt2/sdk/unit/module.hpp>
#include <nt2/sdk/unit/tests/relation.hpp>
#include <nt2/sdk/unit/tests/exceptions.hpp>
#include <nt2/sdk/unit/tests/type_expr.hpp>

template<typename Expr> void inline check_properties(Expr const& )
{
  using nt2::meta::option;
  typedef typename option<Expr,nt2::tag::storage_duration_>::type sd_t;
  NT2_TEST_TYPE_IS( typename sd_t::storage_duration_type, nt2::automatic_ );
}

NT2_TEST_CASE_TPL( cons, NT2_TYPES )
{
  using nt2::table;
  using nt2::cons;

  boost::array<T, 8> ref  = {{  T(1.), T(2.), T(3.), T(4.)
                              , T(5.), T(6.), T(7.), T(8.)
                            }};

  NT2_TEST_EQUAL( ref, cons ( T(1.), T(2.), T(3.), T(4.)
                            , T(5.), T(6.), T(7.), T(8.)
                            )
                );

  check_properties( cons( T(1.), T(2.), T(3.), T(4.)
                        , T(5.), T(6.), T(7.), T(8.)
                        )
                  );
}

NT2_TEST_CASE_TPL( sized_cons, NT2_TYPES )
{
  using nt2::table;
  using nt2::cons;
  using nt2::of_size;

  boost::array<T, 8> ref  = {{  T(1.), T(2.), T(3.), T(4.)
                              , T(5.), T(6.), T(7.), T(8.)
                            }};

  NT2_TEST_EQUAL( ref, cons ( of_size(4,2)
                            , T(1.), T(2.), T(3.), T(4.)
                            , T(5.), T(6.), T(7.), T(8.)
                            )
                );

  check_properties( cons( of_size(4,2)
                        , T(1.), T(2.), T(3.), T(4.)
                        , T(5.), T(6.), T(7.), T(8.)
                        )
                  );

  NT2_TEST_ASSERT ( cons ( of_size(5,3)
                        , T(1.), T(2.), T(3.), T(4.)
                        , T(5.), T(6.), T(7.), T(8.)
                        )
                  );
}


text: #include <boost/test/included/test_exec_monitor.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
/* =========================================================================
   Copyright (c) 2010-2012, Institute for Microelectronics,
                            Institute for Analysis and Scientific Computing,
                            TU Wien.
   Portions of this software are copyright by UChicago Argonne, LLC.

                            -----------------
                  ViennaCL - The Vienna Computing Library
                            -----------------

   Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at

   (A list of authors and contributors can be found in the PDF manual)

   License:         MIT (X11), see file LICENSE in the base directory
============================================================================= */

//#define VIENNACL_DEBUG_ALL
//#define VIENNACL_DEBUG_BUILD
//#define VIENNACL_DEBUG_BUILD
//
// *** System
//
#include <iostream>

//
// *** Boost
//
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/foreach.hpp>

//
// *** ViennaCL
//
//#define VIENNACL_DEBUG_BUILD
#define VIENNACL_WITH_UBLAS
//#define VIENNACL_DEBUG_ALL

#include "viennacl/matrix.hpp"
#include "viennacl/vector.hpp"
#include "viennacl/linalg/inner_prod.hpp"
#include "viennacl/linalg/reduce.hpp"
#include "viennacl/linalg/vector_operations.hpp"
//#include "viennacl/linalg/norm_1.hpp"
//#include "viennacl/linalg/norm_2.hpp"
//#include "viennacl/linalg/norm_inf.hpp"
#include "viennacl/scheduler/io.hpp"

#define CHECK_RESULT(cpu,gpu, op) \
    if ( float delta = fabs ( diff ( cpu, gpu) ) > epsilon ) {\
        std::cout << "# Error at operation: " #op << std::endl;\
        std::cout << "  diff: " << delta << std::endl;\
        retval = EXIT_FAILURE;\
    }\

using namespace boost::numeric;
using namespace viennacl;

template<typename ScalarType, typename VCLMatrixType>
ScalarType diff(ublas::matrix<ScalarType> & mat1, VCLMatrixType & mat2)
{
    ublas::matrix<ScalarType> mat2_cpu(mat2.size1(), mat2.size2());
    viennacl::backend::finish();
    viennacl::copy(mat2, mat2_cpu);
    double ret = 0;
    double act = 0;
    for (unsigned int i = 0; i < mat2_cpu.size1(); ++i)
    {
      for (unsigned int j = 0; j < mat2_cpu.size2(); ++j)
      {
         act = fabs(mat2_cpu(i,j) - mat1(i,j)) / std::max( fabs(mat2_cpu(i, j)), fabs(mat1(i,j)) );
         if (act > ret)
           ret = act;
      }
    }
    //std::cout << ret << std::endl;
    return ret;
}

template<typename ScalarType, unsigned int Alignment>
ScalarType diff ( ublas::vector<ScalarType> & v1, viennacl::vector<ScalarType,Alignment> & v2 ) {
    ublas::vector<ScalarType> v2_cpu ( v2.size() );
    viennacl::copy( v2.begin(), v2.end(), v2_cpu.begin() );
    for ( unsigned int i=0; i<v1.size(); ++i ) {
        if ( std::max ( fabs ( v2_cpu[i] ), fabs ( v1[i] ) ) > 0 )
            v2_cpu[i] = fabs ( v2_cpu[i] - v1[i] ) / std::max ( fabs ( v2_cpu[i] ), fabs ( v1[i] ) );
        else
            v2_cpu[i] = 0.0;
    }
    return norm_inf ( v2_cpu );
}

template<typename ScalarType>
ScalarType diff(ScalarType s, viennacl::scalar<ScalarType> & gs){
  ScalarType other = gs;
  return (s - other) / std::max(s, other);
}

template< typename NumericT, typename Epsilon >
int test_vector ( Epsilon const& epsilon) {
    using namespace viennacl::device_specific;
    int retval = EXIT_SUCCESS;

    unsigned int size = 1024*32;
    ublas::vector<NumericT> cw(size);
    ublas::vector<NumericT> cx(size);
    ublas::vector<NumericT> cy(size);
    ublas::vector<NumericT> cz(size);

//    NumericT s;

    for (unsigned int i=0; i<cw.size(); ++i){
      cw[i]=std::rand()/(NumericT)RAND_MAX;
    }

    std::cout << "Running tests for vector of size " << cw.size() << std::endl;
    viennacl::vector<NumericT> w (size);
    viennacl::vector<NumericT> x (size);
    viennacl::vector<NumericT> y (size);
    viennacl::vector<NumericT> z (size);
    NumericT s = 0;
    viennacl::scalar<NumericT> gs(0);

    cx = 2.0f*cw;
    cy = 3.0f*cw;
    cz = 4.0f*cw;
    viennacl::copy (cw, w);
    viennacl::copy (cx, x);
    viennacl::copy (cy, y);
    viennacl::copy (cz, z);

    NumericT alpha = 3.14;
    NumericT beta = 1;

    // --------------------------------------------------------------------------

//    {
//        std::cout << "w = scalar_vector(alpha) ..." << std::endl;
//        for (unsigned int i = 0; i < size; ++i)
//          cw[i] = alpha;
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::scalar_vector<NumericT>(size,alpha));
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = scalar_vector(alpha));
//    }

//    {
//        std::cout << "w = x ..." << std::endl;
//        cw = cx;
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), x);
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x);
//    }

//    {
//        std::cout << "w = -x ..." << std::endl;
//        viennacl::scalar<NumericT> s0(1);
//        cw =  -cx;
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), -s0*x);
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = -x);
//    }

//    {
//        std::cout << "w = alpha*x + beta*y ..." << std::endl;
//        cw = alpha*cx + beta*cy;
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), alpha*x + beta*y);
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = alpha*x + beta*y);
//    }

//    {
//        std::cout << "x = alpha*w + beta*y ..." << std::endl;
//        cx = alpha*cw + beta*cy;
//        viennacl::scheduler::statement statement(x, viennacl::op_assign(), alpha*w + beta*y);
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = alpha*x + beta*y);
//    }

//    {
//        std::cout << "[Independent] x = alpha*w + beta*y, y = alpha*x + beta*w ..." << std::endl;
//        for (std::size_t i = 0; i < size; ++i)
//        {
//          NumericT tmpcx = cx[i];
//          NumericT tmpcy = cy[i];

//          cx[i] = alpha*cw[i] + beta*tmpcy;
//          cy[i] = alpha*tmpcx + beta*cw[i];
//        }
//        viennacl::scheduler::statement s1(x, viennacl::op_assign(), alpha*w + beta*y);
//        viennacl::scheduler::statement s2(y, viennacl::op_assign(), alpha*x + beta*w);
//        device_specific::execute(database::get<NumericT>(database::axpy), statements_container(s1, s2, statements_container::INDEPENDENT));
//        viennacl::backend::finish();
//        CHECK_RESULT(cx, x, (x = alpha*w + beta*y, y = alpha*x + beta*w));
//        CHECK_RESULT(cy, y, (x = alpha*w + beta*y, y = alpha*x + beta*w));
//    }

//    {
//        std::cout << "[Sequential] x = alpha*w + beta*y, y = alpha*x + beta*w ..." << std::endl;
//        cx = alpha*cw + beta*cy;
//        cy = alpha*cx + beta*cw;
//        viennacl::scheduler::statement s1(x, viennacl::op_assign(), alpha*w + beta*y);
//        viennacl::scheduler::statement s2(y, viennacl::op_assign(), alpha*x + beta*w);
//        device_specific::execute(database::get<NumericT>(database::axpy), statements_container(s1, s2, statements_container::SEQUENTIAL));
//        viennacl::backend::finish();
//        CHECK_RESULT(cx, x, (x = alpha*w + beta*y, y = alpha*x + beta*w));
//        CHECK_RESULT(cy, y, (x = alpha*w + beta*y, y = alpha*x + beta*w));
//    }

//    {
//        std::cout << "w = exp(y) ..." << std::endl;
//        for (std::size_t i = 0; i < size; ++i)
//          cw[i] = std::exp(y[i]);
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_exp(y));
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = alpha*x + beta*y);
//    }

//    {
//        std::cout << "w = element_prod(x,y) ..." << std::endl;
//        for (std::size_t i = 0; i < size; ++i)
//          cw[i] = x[i]*y[i];
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_prod(x,y));
//        device_specific::execute(database::get<NumericT>(database::axpy), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = element_prod(x,y));
//    }

//    {
//        std::cout << "w = x == x" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = (cx(i) == cx(i));
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_eq(x,x));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = (x == x))
//    }

//    {
//        std::cout << "w = x != x" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = cx(i) != cx(i);
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_neq(x,x));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x != x)
//    }

//    {
//        std::cout << "w = x > y" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = cx(i) > cy(i);
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_greater(x,y));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x > y)
//    }

//    {
//        std::cout << "w = x >= y" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = cx(i) >= cy(i);
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_geq(x,y));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x > y)
//    }

//    {
//        std::cout << "w = x < y" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = cx(i) < cy(i);
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_less(x,y));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x > y)
//    }

//    {
//        std::cout << "w = x <= y" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = cx(i) <= cy(i);
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_leq(x,y));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x > y)
//    }

//    {
//        std::cout << "w = x.^y" << std::endl;
//        for (unsigned int i=0; i < size; ++i){
//            cw(i) = std::pow(cx(i),cy(i));
//        }
//        viennacl::scheduler::statement statement(w, viennacl::op_assign(), viennacl::linalg::element_pow(x,y));
//        generator::execute(statement, statement.array()[0]);
//        viennacl::backend::finish();
//        CHECK_RESULT(cw, w, w = x.^y)
//    }

//    {
//        std::cout << "s = inner_prod(x,y)..." << std::endl;
//        s = 0;
//        for (unsigned int i=0; i<size; ++i)  s+=cx[i]*cy[i];
//        viennacl::scheduler::statement statement(gs, viennacl::op_assign(), viennacl::linalg::inner_prod(x,y));
//        device_specific::execute(database::get<NumericT>(database::reduction), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(s, gs, s = inner_prod(x,y));
//    }

//    {
//        std::cout << "s = reduce<add>(x)..." << std::endl;
//        s = 0;
//        for (unsigned int i=0; i<size; ++i)  s+=cx[i];
//        viennacl::scheduler::statement statement(gs, viennacl::op_assign(), viennacl::linalg::reduce<viennacl::op_add>(x));
//        device_specific::execute(profiles::get(REDUCTION_TYPE, NUMERIC_TYPE), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(s, gs, s = reduce<add>(x));
//    }

//    {
//        std::cout << "s = reduce<fmax>(x)..." << std::endl;
//        s = cx[0];
//        for (unsigned int i=1; i<size; ++i)  s=std::max(s,cx[i]);
//        viennacl::scheduler::statement statement(gs, viennacl::op_assign(), viennacl::linalg::reduce<iennacl::op_fmax>(x));
//        device_specific::execute(database::get<NumericT>(database::reduction), statement);
//        viennacl::backend::finish();
//        CHECK_RESULT(s, gs, s = reduce<mult>(x));
//    }

    {
        std::cout << "s = reduce<argmax>(x)..." << std::endl;
        NumericT s = 0;
        NumericT val = cx[0];
        for (unsigned int i=1; i<size; ++i)
        {
            if (cx[i] > val)
            {
                s = i;
                val = cx[i];
            }
        }
        viennacl::scheduler::statement statement(gs, viennacl::op_assign(), viennacl::linalg::reduce<viennacl::op_argmax>(x));
        device_specific::execute(database::get<NumericT>(database::reduction), statement);
        viennacl::backend::finish();
        CHECK_RESULT(s, gs, s = reduce<argmax>(x));
    }

    return retval;
}

template< typename NumericT, class Layout, typename Epsilon >
int test_matrix ( Epsilon const& epsilon) {
    int retval = EXIT_SUCCESS;

    unsigned int size1 = 1024;
    unsigned int size2 = 1024;

    unsigned int pattern_size1 = 256;
    unsigned int pattern_size2 = 128;

//    unsigned int n_rep1 = size1/pattern_size1;
//    unsigned int n_rep2 = size2/pattern_size2;

    ublas::matrix<NumericT> cA(size1,size2);
    ublas::matrix<NumericT> cB(size1,size2);
    ublas::matrix<NumericT> cC(size1,size2);

    ublas::matrix<NumericT> cPattern(pattern_size1,pattern_size2);

    ublas::vector<NumericT> cx(size1);
    ublas::vector<NumericT> cy(size2);

    for (unsigned int i=0; i<size1; ++i)
        for (unsigned int j=0; j<size2; ++j)
            cA(i,j)=(NumericT)std::rand()/RAND_MAX;

    for (unsigned int i = 0; i < pattern_size1; ++i)
        for (unsigned int j = 0; j < pattern_size2; ++j)
            cPattern(i,j) = (NumericT)std::rand()/RAND_MAX;

    for (unsigned int i=0; i<size2; ++i){
        cx(i) = (NumericT)std::rand()/RAND_MAX;
        cy(i) = (NumericT)std::rand()/RAND_MAX;
    }

//    std::cout << "Running tests for matrix of size " << cA.size1() << "," << cA.size2() << std::endl;

    viennacl::matrix<NumericT,Layout> A (size1, size2);
    viennacl::matrix<NumericT,Layout> B (size1, size2);
    viennacl::matrix<NumericT,Layout> C (size1, size2);

    viennacl::matrix<NumericT, Layout> pattern(pattern_size1, pattern_size2);

    viennacl::vector<NumericT> x(size1);
    viennacl::vector<NumericT> y(size2);

    cB = cA;
    cC = cA;
    viennacl::copy(cA,A);
    viennacl::copy(cB,B);
    viennacl::copy(cC,C);

    viennacl::copy(cx,x);
    viennacl::copy(cy,y);
    viennacl::copy(cPattern,pattern);

//    {
//      std::cout << "C = A + B ..." << std::endl;
//      cC     = ( cA + cB );
//      viennacl::scheduler::statement statement(C, viennacl::op_assign(), A + B);
//      device_specific::execute<device_specific::matrix_axpy_template>(device_specific::database::get<NumericT>(device_specific::database::matrix_axpy), statement);
//      viennacl::backend::finish();
//      CHECK_RESULT(cC, C, C=A+B)
//    }

    {
      std::cout << "C = diag(x) ..." << std::endl;
      for (unsigned int i = 0; i < size1; ++i)
        for (unsigned int j = 0; j < size2; ++j)
          cC(i,j) = (i==j)?cx[i]:0;
      viennacl::scheduler::statement statement(C, viennacl::op_assign(), viennacl::diag(x));
      device_specific::execute<device_specific::matrix_axpy_template>(device_specific::database::get<NumericT>(device_specific::database::matrix_axpy), statement);
      viennacl::backend::finish();
      CHECK_RESULT(cC, C, C=diag(x))
    }

    {
      std::cout << "x = diag(C) ..." << std::endl;
      for (unsigned int i = 0; i < std::min(size1, size2); ++i)
        cx[i] = cC(i,i);
      viennacl::scheduler::statement statement(x, viennacl::op_assign(), viennacl::diag(C));
      device_specific::execute<device_specific::vector_axpy_template>(device_specific::database::get<NumericT>(device_specific::database::vector_axpy), statement);
      viennacl::backend::finish();
      CHECK_RESULT(cx, x, x=diag(C))
    }

    {
      std::cout << "y = row(C, 7) ..." << std::endl;
      for (unsigned int j = 0; j < size2; ++j)
        cy[j] = cC(7,j);
      viennacl::scheduler::statement statement(y, viennacl::op_assign(), viennacl::row(C, 7));
      device_specific::execute<device_specific::vector_axpy_template>(device_specific::database::get<NumericT>(device_specific::database::vector_axpy), statement);
      viennacl::backend::finish();
      CHECK_RESULT(cy, y, y=row(C, 7))
    }

//    {
//      std::cout << "x = diag(C) ..." << std::endl;
//      for (unsigned int i = 0; i < std::min(size1, size2); ++i)
//        cx[i] = cC(i,i);
//      viennacl::scheduler::statement statement(x, viennacl::op_assign(), viennacl::diag(C));
//      device_specific::execute<device_specific::vector_axpy_template>(device_specific::database::get<NumericT>(device_specific::database::vector_axpy), statement);
//      viennacl::backend::finish();
//      CHECK_RESULT(cx, x, x=diag(C))
//    }

//    {
//        std::cout << "C = diag(x) ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i){
//          for (unsigned int j = 0; j < size2; ++j){
//            cC(i,j) = (i==j)?cx[i]:0;
//          }
//        }
//        generator::custom_operation op;
//        op.add(mat(C) = generator::diag(vec(x)));
//        op.execute();
//        viennacl::backend::finish();
//        CHECK_RESULT(cC, C, C = diag(x))
//    }

//    {
//        std::cout << "x = diag(C) ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i){
//            cx(i) = cA(i,i);
//        }
//        generator::custom_operation op;
//        op.add(vec(x) = generator::diag(mat(A)));
//        op.execute();
//        viennacl::backend::finish();
//        CHECK_RESULT(cx,x, x = diag(A));
//    }

//    {
//        std::cout << "C = repmat(P, M, N) ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i)
//            for (unsigned int j = 0; j < size2; ++j)
//                cC(i,j) = cPattern(i%pattern_size1, j%pattern_size2);
//        generator::custom_operation op;
//        op.add(mat(C) = generator::repmat(mat(pattern),n_rep1,n_rep2));
//        op.execute();
//        viennacl::backend::finish();
//        CHECK_RESULT(cC, C, C = repmat(P, M, N))
//    }

//    {
//        std::cout << "C = repmat(x, 1, N) ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i)
//            for (unsigned int j = 0; j < size2; ++j)
//                cC(i,j) = cx(i);
//        generator::custom_operation op;
//        op.add(mat(C) = generator::repmat(vec(x),1, C.size2()));
//        op.execute();
//        viennacl::backend::finish();
//        CHECK_RESULT(cC, C, C = repmat(x, 1, N))
//    }

//    {
//        std::cout << "C = trans(repmat(x, 1, N)) ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i)
//            for (unsigned int j = 0; j < size2; ++j)
//                cC(i,j) = cx(j);
//        generator::custom_operation op;
//        op.add(mat(C) = generator::trans(generator::repmat(vec(x),1,C.size2())));
//        op.execute();
//        viennacl::backend::finish();
//        CHECK_RESULT(cC, C, C = repmat(x, 1, N))
//    }

//    {
//        std::cout << "C = -A ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i)
//            for (unsigned int j = 0; j < size2; ++j)
//                cC(i,j) = -cA(i,j);
//        generator::custom_operation op;
//        op.add(mat(C) = -mat(A));
//        op.execute();
//        viennacl::backend::finish();

//        CHECK_RESULT(cC, C, C = -A)
//    }

//    {
//        std::cout << "C = 1/(1+EXP(-A)) ..." << std::endl;
//        for (unsigned int i = 0; i < size1; ++i)
//            for (unsigned int j = 0; j < size2; ++j)
//                cC(i,j) = 1.0f/(1.0f+std::exp(-cA(i,j)));
//        generator::custom_operation op;
//        op.add(mat(C) = 1.0f/(1.0f+generator::exp(-mat(A))));
//        op.execute();
//        viennacl::backend::finish();
//        CHECK_RESULT(cC, C, C = 1/(1+EXP(-A)))
//    }

    return retval;
}

int main(int argc, char* argv[]){
    std::vector<std::string> args(argv,argv+argc);
    unsigned int requested_device;
    if (argc!=2){
        requested_device=0;
    }
    else{
        requested_device = atoi(args[1].c_str());
    }
    int retval = EXIT_SUCCESS;

    typedef std::vector< viennacl::ocl::platform > platforms_type;
    typedef std::vector<viennacl::ocl::device> devices_type;

    platforms_type platforms = viennacl::ocl::get_platforms();
    size_t num_platforms = platforms.size();

    unsigned int current_device = 0;

    for (unsigned int k=0; k < num_platforms; ++k)
    {
        viennacl::ocl::platform pf(k);
        viennacl::ocl::set_context_device_type(k,CL_DEVICE_TYPE_ALL);
        viennacl::ocl::set_context_platform_index(k,k);
        viennacl::ocl::switch_context(k);
        devices_type dev = viennacl::ocl::current_context().devices();
        for (devices_type::iterator it = dev.begin(); it != dev.end(); ++it){

            if (current_device++ == requested_device ){
                viennacl::ocl::switch_device(*it);
                std::cout << std::endl;
                std::cout << "----------------------------------------------" << std::endl;
                std::cout << "               Device Info" << std::endl;
                std::cout << "----------------------------------------------" << std::endl;
                std::cout << viennacl::ocl::current_device().info() << std::endl;

//                std::cout << std::endl;
//                std::cout << "----------------------------------------------" << std::endl;
//                std::cout << "----------------------------------------------" << std::endl;
//                std::cout << "## Test :: Vector" << std::endl;
//                std::cout << "----------------------------------------------" << std::endl;

//                {
//                    double epsilon = 1.0E-4;

//                    std::cout << "# Testing setup:" << std::endl;
//                    std::cout << "  numeric: float" << std::endl;
//                    retval = test_vector<float> (epsilon);

//                    std::cout << std::endl;

//                    std::cout << "# Testing setup:" << std::endl;
//                    std::cout << "  numeric: double" << std::endl;
//                    retval = test_vector<double> (epsilon);

//                    if ( retval == EXIT_SUCCESS )
//                        std::cout << "# Test passed" << std::endl;
//                    else
//                        return retval;
//              }

              std::cout << std::endl;
              std::cout << "----------------------------------------------" << std::endl;
              std::cout << "----------------------------------------------" << std::endl;
              std::cout << "## Test :: Matrix" << std::endl;
              std::cout << "----------------------------------------------" << std::endl;

              {
                  double epsilon = 1.0E-4;
                  std::cout << "# Testing setup:" << std::endl;

                  std::cout << "  numeric: float" << std::endl;
                  std::cout << "  --------------" << std::endl;
                  std::cout << "  Row-Major"      << std::endl;
                  std::cout << "  --------------" << std::endl;
                  retval = test_matrix<float, viennacl::row_major> (epsilon);

                  std::cout << "  --------------" << std::endl;
                  std::cout << "  Column-Major"      << std::endl;
                  std::cout << "  --------------" << std::endl;
                  retval &= test_matrix<float, viennacl::column_major> (epsilon);

                  std::cout << "  numeric: double" << std::endl;
                  std::cout << "  --------------" << std::endl;
                  std::cout << "  Row-Major"      << std::endl;
                  std::cout << "  --------------" << std::endl;
                  retval = test_matrix<double, viennacl::row_major> (epsilon);

                  std::cout << "  --------------" << std::endl;
                  std::cout << "  Column-Major"      << std::endl;
                  std::cout << "  --------------" << std::endl;
                  retval &= test_matrix<double, viennacl::column_major> (epsilon);

                  if ( retval == EXIT_SUCCESS )
                      std::cout << "# Test passed" << std::endl;
                  else
                      return retval;
              }

            }
        }
    }
}


text:
//==================================================================================================
/*!
  @file

  @copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================
#ifndef BOOST_SIMD_ARCH_X86_AVX2_SIMD_FUNCTION_MAKE_HPP_INCLUDED
#define BOOST_SIMD_ARCH_X86_AVX2_SIMD_FUNCTION_MAKE_HPP_INCLUDED

#include <boost/simd/meta/hierarchy/simd.hpp>
#include <boost/dispatch/function/overload.hpp>
#include <boost/config.hpp>

namespace boost { namespace simd { namespace ext
{
  namespace bd = ::boost::dispatch;
  namespace bs = ::boost::simd;

  //------------------------------------------------------------------------------------------------
  // make a pack of int64
  BOOST_DISPATCH_OVERLOAD ( make_
                          , (typename Target, typename V0, typename V1, typename V2, typename V3)
                          , bs::avx2_
                          , bd::target_<bs::pack_<bd::ints64_<Target>,bs::avx_>>
                          , bd::scalar_<bd::unspecified_<V0>>
                          , bd::scalar_<bd::unspecified_<V1>>
                          , bd::scalar_<bd::unspecified_<V2>>
                          , bd::scalar_<bd::unspecified_<V3>>
                          )
  {
    using target_t  = typename Target::type;

    BOOST_FORCEINLINE target_t
    operator()(Target const&, V0 const& v0, V1 const& v1
                            , V2 const& v2, V3 const& v3) const BOOST_NOEXCEPT
    {
      using value_t = typename target_t::value_type;
      return _mm256_set_epi64x( static_cast<value_t>(v3), static_cast<value_t>(v2)
                              , static_cast<value_t>(v1), static_cast<value_t>(v0)
                              );
    }
  };

  //------------------------------------------------------------------------------------------------
  // make a pack of int32
  BOOST_DISPATCH_OVERLOAD ( make_
                          , (typename Target, typename... Values)
                          , bs::avx2_
                          , bd::target_<bs::pack_<bd::ints32_<Target>,bs::avx_>>
                          , bd::scalar_<bd::unspecified_<Values>>...
                          )
  {
    using target_t  = typename Target::type;

    static_assert ( sizeof...(Values) == 8
                  , "boost::simd::make - Invalid number of parameters"
                  );

    BOOST_FORCEINLINE target_t operator()(Target const&, Values const&... vs) const BOOST_NOEXCEPT
    {
      return _mm256_setr_epi32(static_cast<typename target_t::value_type>(vs)...);
    }
  };

  //------------------------------------------------------------------------------------------------
  // make a pack of int16
  BOOST_DISPATCH_OVERLOAD ( make_
                          , (typename Target, typename... Values)
                          , bs::avx2_
                          , bd::target_<bs::pack_<bd::ints16_<Target>,bs::avx_>>
                          , bd::scalar_<bd::unspecified_<Values>>...
                          )
  {
    using target_t  = typename Target::type;

    static_assert ( sizeof...(Values) == 16
                  , "boost::simd::make - Invalid number of parameters"
                  );

    BOOST_FORCEINLINE target_t operator()(Target const&, Values const&... vs) const BOOST_NOEXCEPT
    {
      return _mm256_setr_epi16(static_cast<typename target_t::value_type>(vs)...);
    }
  };

  //------------------------------------------------------------------------------------------------
  // make a pack of int8
  BOOST_DISPATCH_OVERLOAD ( make_
                          , (typename Target, typename... Values)
                          , bs::avx2_
                          , bd::target_<bs::pack_<bd::ints8_<Target>,bs::avx_>>
                          , bd::scalar_<bd::unspecified_<Values>>...
                          )
  {
    using target_t  = typename Target::type;

    static_assert ( sizeof...(Values) == 32
                  , "boost::simd::make - Invalid number of parameters"
                  );

    BOOST_FORCEINLINE target_t operator()(Target const&, Values const&... vs) const BOOST_NOEXCEPT
    {
      return _mm256_setr_epi8(static_cast<typename target_t::value_type>(vs)...);
    }
  };
} } }

#endif