本文整理汇总了C++中nd::array::get_data_memblock方法的典型用法代码示例。如果您正苦于以下问题:C++ array::get_data_memblock方法的具体用法?C++ array::get_data_memblock怎么用?C++ array::get_data_memblock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nd::array
的用法示例。
在下文中一共展示了array::get_data_memblock方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: view
nd::array nd::view(const nd::array& arr, const ndt::type& tp)
{
// If the types match exactly, simply return 'arr'
if (arr.get_type() == tp) {
return arr;
} else if (arr.get_ndim() == tp.get_ndim()) {
// Allocate a result array to attempt the view in it
array result(make_array_memory_block(tp.get_metadata_size()));
// Copy the fields
result.get_ndo()->m_data_pointer = arr.get_ndo()->m_data_pointer;
if (arr.get_ndo()->m_data_reference == NULL) {
// Embedded data, need reference to the array
result.get_ndo()->m_data_reference = arr.get_memblock().release();
} else {
// Use the same data reference, avoid producing a chain
result.get_ndo()->m_data_reference = arr.get_data_memblock().release();
}
result.get_ndo()->m_type = ndt::type(tp).release();
result.get_ndo()->m_flags = arr.get_ndo()->m_flags;
// Now try to copy the metadata as a view
if (try_view(arr.get_type(), arr.get_ndo_meta(), tp,
result.get_ndo_meta(), arr.get_memblock().get())) {
// If it succeeded, return it
return result;
}
// Otherwise fall through, let it get destructed, and raise an error
}
stringstream ss;
ss << "Unable to view nd::array of type " << arr.get_type();
ss << "as type " << tp;
throw type_error(ss.str());
}
示例2: result
static nd::array view_concrete(const nd::array &arr, const ndt::type &tp)
{
// Allocate a result array to attempt the view in it
nd::array result(make_array_memory_block(tp.get_arrmeta_size()));
// Copy the fields
result.get_ndo()->data.ptr = arr.get_ndo()->data.ptr;
if (arr.get_ndo()->data.ref == NULL) {
// Embedded data, need reference to the array
result.get_ndo()->data.ref = arr.get_memblock().release();
} else {
// Use the same data reference, avoid producing a chain
result.get_ndo()->data.ref = arr.get_data_memblock().release();
}
result.get_ndo()->m_type = ndt::type(tp).release();
result.get_ndo()->m_flags = arr.get_ndo()->m_flags;
// First handle a special case of viewing outermost "var" as "fixed[#]"
if (arr.get_type().get_type_id() == var_dim_type_id && tp.get_type_id() == fixed_dim_type_id) {
const var_dim_type_arrmeta *in_am = reinterpret_cast<const var_dim_type_arrmeta *>(arr.get_arrmeta());
const var_dim_type_data *in_dat = reinterpret_cast<const var_dim_type_data *>(arr.get_readonly_originptr());
fixed_dim_type_arrmeta *out_am = reinterpret_cast<fixed_dim_type_arrmeta *>(result.get_arrmeta());
out_am->dim_size = tp.extended<ndt::fixed_dim_type>()->get_fixed_dim_size();
out_am->stride = in_am->stride;
if ((intptr_t)in_dat->size == out_am->dim_size) {
// Use the more specific data reference from the var arrmeta if possible
if (in_am->blockref != NULL) {
memory_block_decref(result.get_ndo()->data.ref);
memory_block_incref(in_am->blockref);
result.get_ndo()->data.ref = in_am->blockref;
}
result.get_ndo()->data.ptr = in_dat->begin + in_am->offset;
// Try to copy the rest of the arrmeta as a view
if (try_view(arr.get_type().extended<ndt::base_dim_type>()->get_element_type(),
arr.get_arrmeta() + sizeof(var_dim_type_arrmeta),
tp.extended<ndt::base_dim_type>()->get_element_type(),
result.get_arrmeta() + sizeof(fixed_dim_type_arrmeta), arr.get_memblock().get())) {
return result;
}
}
}
// Otherwise try to copy the arrmeta as a view
else if (try_view(arr.get_type(), arr.get_arrmeta(), tp, result.get_arrmeta(), arr.get_memblock().get())) {
// If it succeeded, return it
return result;
}
stringstream ss;
ss << "Unable to view nd::array of type " << arr.get_type();
ss << " as type " << tp;
throw type_error(ss.str());
}
示例3: array
static nd::array view_as_bytes(const nd::array &arr, const ndt::type &tp)
{
if (arr.get_type().get_flags() & type_flag_destructor) {
// Can't view arrays of object type
return nd::array();
}
// Get the essential components of the array to analyze
memory_block_ptr data_ref = arr.get_data_memblock();
char *data_ptr = arr.get_ndo()->data.ptr;
ndt::type data_tp = arr.get_type();
const char *data_meta = arr.get_arrmeta();
intptr_t data_dim_size = -1, data_stride = 0;
// Repeatedly refine the data
while (data_tp.get_type_id() != uninitialized_type_id) {
refine_bytes_view(data_ref, data_ptr, data_tp, data_meta, data_dim_size, data_stride);
}
// Check that it worked, and that the resulting data pointer is aligned
if (data_dim_size < 0 ||
!offset_is_aligned(reinterpret_cast<size_t>(data_ptr), tp.extended<ndt::bytes_type>()->get_target_alignment())) {
// This signals we could not view the data as a
// contiguous chunk of bytes
return nd::array();
}
char *result_data_ptr = NULL;
nd::array result(make_array_memory_block(tp.extended()->get_arrmeta_size(), tp.get_data_size(),
tp.get_data_alignment(), &result_data_ptr));
// Set the bytes extents
((char **)result_data_ptr)[0] = data_ptr;
((char **)result_data_ptr)[1] = data_ptr + data_dim_size;
// Set the array arrmeta
array_preamble *ndo = result.get_ndo();
ndo->m_type = ndt::type(tp).release();
ndo->data.ptr = result_data_ptr;
ndo->data.ref = NULL;
ndo->m_flags = arr.get_flags();
// Set the bytes arrmeta
bytes_type_arrmeta *ndo_meta = reinterpret_cast<bytes_type_arrmeta *>(result.get_arrmeta());
ndo_meta->blockref = data_ref.release();
return result;
}
示例4: struct_concat
nd::array dynd::struct_concat(nd::array lhs, nd::array rhs)
{
nd::array res;
if (lhs.is_null()) {
res = rhs;
return res;
}
if (rhs.is_null()) {
res = lhs;
return res;
}
const ndt::type &lhs_tp = lhs.get_type(), &rhs_tp = rhs.get_type();
if (lhs_tp.get_kind() != struct_kind) {
stringstream ss;
ss << "Cannot concatenate array with type " << lhs_tp << " as a struct";
throw invalid_argument(ss.str());
}
if (rhs_tp.get_kind() != struct_kind) {
stringstream ss;
ss << "Cannot concatenate array with type " << rhs_tp << " as a struct";
throw invalid_argument(ss.str());
}
// Make an empty shell struct by concatenating the fields together
intptr_t lhs_n = lhs_tp.extended<ndt::base_struct_type>()->get_field_count();
intptr_t rhs_n = rhs_tp.extended<ndt::base_struct_type>()->get_field_count();
intptr_t res_n = lhs_n + rhs_n;
nd::array res_field_names = nd::empty(res_n, ndt::string_type::make());
nd::array res_field_types = nd::empty(res_n, ndt::make_type());
res_field_names(irange(0, lhs_n)).vals() = lhs_tp.extended<ndt::base_struct_type>()->get_field_names();
res_field_names(irange(lhs_n, res_n)).vals() = rhs_tp.extended<ndt::base_struct_type>()->get_field_names();
res_field_types(irange(0, lhs_n)).vals() = lhs_tp.extended<ndt::base_struct_type>()->get_field_types();
res_field_types(irange(lhs_n, res_n)).vals() = rhs_tp.extended<ndt::base_struct_type>()->get_field_types();
ndt::type res_tp = ndt::struct_type::make(res_field_names, res_field_types);
const ndt::type *res_field_tps = res_tp.extended<ndt::base_struct_type>()->get_field_types_raw();
res = nd::empty_shell(res_tp);
// Initialize the default data offsets for the struct arrmeta
ndt::struct_type::fill_default_data_offsets(res_n, res_tp.extended<ndt::base_struct_type>()->get_field_types_raw(),
reinterpret_cast<uintptr_t *>(res.get_arrmeta()));
// Get information about the arrmeta layout of the input and res
const uintptr_t *lhs_arrmeta_offsets = lhs_tp.extended<ndt::base_struct_type>()->get_arrmeta_offsets_raw();
const uintptr_t *rhs_arrmeta_offsets = rhs_tp.extended<ndt::base_struct_type>()->get_arrmeta_offsets_raw();
const uintptr_t *res_arrmeta_offsets = res_tp.extended<ndt::base_struct_type>()->get_arrmeta_offsets_raw();
const char *lhs_arrmeta = lhs.get_arrmeta();
const char *rhs_arrmeta = rhs.get_arrmeta();
char *res_arrmeta = res.get_arrmeta();
// Copy the arrmeta from the input arrays
for (intptr_t i = 0; i < lhs_n; ++i) {
const ndt::type &tp = res_field_tps[i];
if (!tp.is_builtin()) {
tp.extended()->arrmeta_copy_construct(res_arrmeta + res_arrmeta_offsets[i], lhs_arrmeta + lhs_arrmeta_offsets[i],
lhs.get_data_memblock().get());
}
}
for (intptr_t i = 0; i < rhs_n; ++i) {
const ndt::type &tp = res_field_tps[i + lhs_n];
if (!tp.is_builtin()) {
tp.extended()->arrmeta_copy_construct(res_arrmeta + res_arrmeta_offsets[i + lhs_n],
rhs_arrmeta + rhs_arrmeta_offsets[i], rhs.get_data_memblock().get());
}
}
// Get information about the data layout of the input and res
const uintptr_t *lhs_data_offsets = lhs_tp.extended<ndt::base_struct_type>()->get_data_offsets(lhs.get_arrmeta());
const uintptr_t *rhs_data_offsets = rhs_tp.extended<ndt::base_struct_type>()->get_data_offsets(rhs.get_arrmeta());
const uintptr_t *res_data_offsets = res_tp.extended<ndt::base_struct_type>()->get_data_offsets(res.get_arrmeta());
const char *lhs_data = lhs.get_readonly_originptr();
const char *rhs_data = rhs.get_readonly_originptr();
char *res_data = res.get_readwrite_originptr();
// Copy the data from the input arrays
for (intptr_t i = 0; i < lhs_n; ++i) {
const ndt::type &tp = res_field_tps[i];
typed_data_copy(tp, res_arrmeta + res_arrmeta_offsets[i], res_data + res_data_offsets[i],
lhs_arrmeta + lhs_arrmeta_offsets[i], lhs_data + lhs_data_offsets[i]);
}
for (intptr_t i = 0; i < rhs_n; ++i) {
const ndt::type &tp = res_field_tps[i + lhs_n];
typed_data_copy(tp, res_arrmeta + res_arrmeta_offsets[i + lhs_n], res_data + res_data_offsets[i + lhs_n],
rhs_arrmeta + rhs_arrmeta_offsets[i], rhs_data + rhs_data_offsets[i]);
}
return res;
}