Conversion from stl::vector to Ct vec

Submit New Article

March 1, 2010 11:00 PM PST


The following is a short term solution to be able to convert a stl::vector to a Ct vec.

Covering the simplest approach, one could write some helper functions. Please note, passing the vectors (a, and b in the example below) in a non-const way is not a mistake. Ct is wrapping the given memory address (or performing a copyIn/copyOut), and needs a non-const access to populate the result (e.g. after using an “rcall”). The data behind the given addresses (vector content) is then modified (copyOut) by Ct, therefore the region cannot passed constant initially. One should *not* create a std::vector to *just* build a Ct::Vec. In the latter case it would be easier to understand the Ct binding constructors instead of treating the “helper” functions as black boxes.

template<typename T>
Ct::Vec<typename Ct::Pri2CtType<T>::CtType>
make_ct_vec(std::vector<T>& a)
{
typedef typename Ct::Pri2CtType<T>::CtType ct_value_type;
typedef Ct::Vec<ct_value_type> ct_array_type;
return ct_array_type(&a[0], Ct::_const<Ct::Size>(a.size()));
}

template<typename T>
Ct::Vec<Ct::Tuple<2, typename Ct::Pri2CtType<T>::CtType> >
make_ct_vec(std::vector<T>& a, std::vector<T>& b)
{
assert(a.size() && b.size() && static_cast<const char*>("Vectors must have the same size!"));
typedef typename Ct::Pri2CtType<T>::CtType ct_value_type;
typedef Ct::Vec<Ct::Tuple<2, ct_value_type> > ct_array_type;
return ct_array_type(&a[0], Ct::_const<Ct::Size>(a.size()),
static_cast<_Size>(sizeof(T)), Ct::_const<Ct::Size>(sizeof(T)));
}


The functions are used as follows:

std::vector a, b;
make_ct_vec(a);
make_ct_vec(a, b);



Do you need more help?


This article applies to: Intel’s Ct Technology Beta Knowledge Base