Exemplo n.º 1
0
size_t string_type::make_assignment_kernel(
                ckernel_builder *out, size_t offset_out,
                const ndt::type& dst_tp, const char *dst_metadata,
                const ndt::type& src_tp, const char *src_metadata,
                kernel_request_t kernreq, assign_error_mode errmode,
                const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        switch (src_tp.get_type_id()) {
            case string_type_id: {
                return make_blockref_string_assignment_kernel(out, offset_out,
                                dst_metadata, get_encoding(),
                                src_metadata, static_cast<const base_string_type *>(src_tp.extended())->get_encoding(),
                                kernreq, errmode, ectx);
            }
            case fixedstring_type_id: {
                return make_fixedstring_to_blockref_string_assignment_kernel(out, offset_out,
                                dst_metadata, get_encoding(),
                                src_tp.get_data_size(),
                                static_cast<const base_string_type *>(src_tp.extended())->get_encoding(),
                                kernreq, errmode, ectx);
            }
            default: {
                if (!src_tp.is_builtin()) {
                    return src_tp.extended()->make_assignment_kernel(out, offset_out,
                                    dst_tp, dst_metadata,
                                    src_tp, src_metadata,
                                    kernreq, errmode, ectx);
                } else {
                    return make_builtin_to_string_assignment_kernel(out, offset_out,
                                dst_tp, dst_metadata,
                                src_tp.get_type_id(),
                                kernreq, errmode, ectx);
                }
            }
        }
    } else {
        if (dst_tp.is_builtin()) {
            return make_string_to_builtin_assignment_kernel(out, offset_out,
                            dst_tp.get_type_id(),
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        } else {
            stringstream ss;
            ss << "Cannot assign from " << src_tp << " to " << dst_tp;
            throw dynd::type_error(ss.str());
        }
    }
}
Exemplo n.º 2
0
size_t bytes_type::make_assignment_kernel(
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        switch (src_tp.get_type_id()) {
            case bytes_type_id: {
                return make_blockref_bytes_assignment_kernel(ckb, ckb_offset,
                                get_data_alignment(), dst_arrmeta,
                                src_tp.get_data_alignment(), src_arrmeta,
                                kernreq, ectx);
            }
            case fixedbytes_type_id: {
                return make_fixedbytes_to_blockref_bytes_assignment_kernel(ckb, ckb_offset,
                                get_data_alignment(), dst_arrmeta,
                                src_tp.get_data_size(), src_tp.get_data_alignment(),
                                kernreq, ectx);
            }
            default: {
                if (!src_tp.is_builtin()) {
                    src_tp.extended()->make_assignment_kernel(
                        ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp,
                        src_arrmeta, kernreq, ectx);
                }
                break;
            }
        }
    }

    stringstream ss;
    ss << "Cannot assign from " << src_tp << " to " << dst_tp;
    throw runtime_error(ss.str());
}
Exemplo n.º 3
0
size_t cstruct_type::make_assignment_kernel(
                ckernel_builder *out, size_t offset_out,
                const ndt::type& dst_tp, const char *dst_metadata,
                const ndt::type& src_tp, const char *src_metadata,
                kernel_request_t kernreq, assign_error_mode errmode,
                const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        if (this == src_tp.extended()) {
            return make_struct_identical_assignment_kernel(out, offset_out,
                            dst_tp,
                            dst_metadata, src_metadata,
                            kernreq, errmode, ectx);
        } else if (src_tp.get_kind() == struct_kind) {
            return make_struct_assignment_kernel(out, offset_out,
                            dst_tp, dst_metadata,
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        } else if (!src_tp.is_builtin()) {
            return src_tp.extended()->make_assignment_kernel(out, offset_out,
                            dst_tp, dst_metadata,
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        }
    }

    stringstream ss;
    ss << "Cannot assign from " << src_tp << " to " << dst_tp;
    throw runtime_error(ss.str());
}
Exemplo n.º 4
0
size_t fixedstring_type::make_comparison_kernel(
    void *ckb, intptr_t ckb_offset, const ndt::type &src0_dt,
    const char *src0_arrmeta, const ndt::type &src1_dt,
    const char *src1_arrmeta, comparison_type_t comptype,
    const eval::eval_context *ectx) const
{
    if (this == src0_dt.extended()) {
        if (*this == *src1_dt.extended()) {
            return make_fixedstring_comparison_kernel(ckb, ckb_offset,
                            m_stringsize, m_encoding,
                            comptype, ectx);
        } else if (src1_dt.get_kind() == string_kind) {
            return make_general_string_comparison_kernel(ckb, ckb_offset,
                            src0_dt, src0_arrmeta,
                            src1_dt, src1_arrmeta,
                            comptype, ectx);
        } else if (!src1_dt.is_builtin()) {
            return src1_dt.extended()->make_comparison_kernel(ckb, ckb_offset,
                            src0_dt, src0_arrmeta,
                            src1_dt, src1_arrmeta,
                            comptype, ectx);
        }
    }

    throw not_comparable_error(src0_dt, src1_dt, comptype);
}
Exemplo n.º 5
0
adapt_type::adapt_type(const ndt::type &operand_type,
                       const ndt::type &value_type, const nd::string &op)
    : base_expr_type(
          adapt_type_id, expr_kind, operand_type.get_data_size(),
          operand_type.get_data_alignment(),
          inherited_flags(value_type.get_flags(), operand_type.get_flags()), 0),
      m_value_type(value_type), m_operand_type(operand_type), m_op(op)
{
  if (!value_type.is_builtin() &&
      value_type.extended()->adapt_type(operand_type.value_type(), op,
                                        m_forward, m_reverse)) {
  } else if (!operand_type.value_type().is_builtin() &&
             operand_type.value_type().extended()->reverse_adapt_type(
                 value_type, op, m_forward, m_reverse)) {
  } else {
    stringstream ss;
    ss << "Cannot create type ";
    print_type(ss);
    throw type_error(ss.str());
  }

  // If the operand is an expression, make a buffering arrfunc
  if (m_operand_type.get_kind() == expr_kind && !m_forward.is_null() &&
      m_operand_type != m_forward.get_type()->get_arg_type(0)) {
    m_forward = make_chain_arrfunc(
        make_arrfunc_from_assignment(m_forward.get_type()->get_arg_type(0),
                                     m_operand_type, assign_error_default),
        m_forward, m_forward.get_type()->get_arg_type(0));
  }
}
Exemplo n.º 6
0
size_t string_type::make_comparison_kernel(
                ckernel_builder *out, size_t offset_out,
                const ndt::type& src0_dt, const char *src0_metadata,
                const ndt::type& src1_dt, const char *src1_metadata,
                comparison_type_t comptype,
                const eval::eval_context *ectx) const
{
    if (this == src0_dt.extended()) {
        if (*this == *src1_dt.extended()) {
            return make_string_comparison_kernel(out, offset_out,
                            m_encoding,
                            comptype, ectx);
        } else if (src1_dt.get_kind() == string_kind) {
            return make_general_string_comparison_kernel(out, offset_out,
                            src0_dt, src0_metadata,
                            src1_dt, src1_metadata,
                            comptype, ectx);
        } else if (!src1_dt.is_builtin()) {
            return src1_dt.extended()->make_comparison_kernel(out, offset_out,
                            src0_dt, src0_metadata,
                            src1_dt, src1_metadata,
                            comptype, ectx);
        }
    }

    throw not_comparable_error(src0_dt, src1_dt, comptype);
}
Exemplo n.º 7
0
intptr_t dynd::make_cuda_from_device_builtin_type_assignment_kernel(
    const callable_type_data *DYND_UNUSED(self),
    const ndt::callable_type *DYND_UNUSED(af_tp), char *DYND_UNUSED(data),
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *DYND_UNUSED(dst_arrmeta), intptr_t DYND_UNUSED(nsrc),
    const ndt::type *src_tp, const char *const *DYND_UNUSED(src_arrmeta),
    kernel_request_t kernreq, const eval::eval_context *ectx,
    const nd::array &DYND_UNUSED(kwds),
    const std::map<std::string, ndt::type> &DYND_UNUSED(tp_vars))
{
  assign_error_mode errmode = ectx->errmode;

  if (errmode != assign_error_nocheck &&
      is_lossless_assignment(dst_tp, *src_tp)) {
    errmode = assign_error_nocheck;
  }

  if (!dst_tp.is_builtin() || !src_tp->is_builtin() ||
      errmode == assign_error_default) {
    stringstream ss;
    ss << "cannot assign from CUDA device with types " << *src_tp << " to "
       << dst_tp;
    throw type_error(ss.str());
  }

  nd::cuda_device_to_host_assign_ck::make(ckb, kernreq, ckb_offset,
                                          src_tp->get_data_size());
  return make_builtin_type_assignment_kernel(
      ckb, ckb_offset, dst_tp.without_memory_type().get_type_id(),
      src_tp->without_memory_type().get_type_id(), kernel_request_single,
      errmode);
}
Exemplo n.º 8
0
size_t cfixed_dim_type::make_assignment_kernel(
    ckernel_builder *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx) const
{
  if (this == dst_tp.extended()) {
    intptr_t src_size, src_stride;
    ndt::type src_el_tp;
    const char *src_el_arrmeta;

    if (src_tp.get_ndim() < dst_tp.get_ndim()) {
      kernels::strided_assign_ck *self =
          kernels::strided_assign_ck::create(ckb, kernreq, ckb_offset);
      self->m_size = get_fixed_dim_size();
      self->m_dst_stride = get_fixed_stride();
      // If the src has fewer dimensions, broadcast it across this one
      self->m_src_stride = 0;
      return ::make_assignment_kernel(
          ckb, ckb_offset, m_element_tp,
          dst_arrmeta + sizeof(cfixed_dim_type_arrmeta), src_tp, src_arrmeta,
          kernel_request_strided, ectx);
    } else if (src_tp.get_as_strided(src_arrmeta, &src_size, &src_stride,
                                         &src_el_tp, &src_el_arrmeta)) {
      kernels::strided_assign_ck *self =
          kernels::strided_assign_ck::create(ckb, kernreq, ckb_offset);
      self->m_size = get_fixed_dim_size();
      self->m_dst_stride = get_fixed_stride();
      self->m_src_stride = src_stride;
      // Check for a broadcasting error
      if (src_size != 1 && get_fixed_dim_size() != src_size) {
        throw broadcast_error(dst_tp, dst_arrmeta, src_tp, src_arrmeta);
      }

      return ::make_assignment_kernel(
          ckb, ckb_offset, m_element_tp,
          dst_arrmeta + sizeof(cfixed_dim_type_arrmeta), src_el_tp,
          src_el_arrmeta, kernel_request_strided, ectx);
    } else if (!src_tp.is_builtin()) {
      // Give the src type a chance to make a kernel
      return src_tp.extended()->make_assignment_kernel(
          ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, src_arrmeta,
          kernreq, ectx);
    } else {
      stringstream ss;
      ss << "Cannot assign from " << src_tp << " to " << dst_tp;
      throw dynd::type_error(ss.str());
    }
  } else if (dst_tp.get_kind() == string_kind) {
    return make_any_to_string_assignment_kernel(ckb, ckb_offset, dst_tp,
                                                dst_arrmeta, src_tp,
                                                src_arrmeta, kernreq, ectx);
  } else if (dst_tp.get_ndim() < src_tp.get_ndim()) {
    throw broadcast_error(dst_tp, dst_arrmeta, src_tp, src_arrmeta);
  } else {
    stringstream ss;
    ss << "Cannot assign from " << src_tp << " to " << dst_tp;
    throw dynd::type_error(ss.str());
  }
}
Exemplo n.º 9
0
 void get_dynamic_array_functions(
                 const std::pair<std::string, gfunc::callable> **out_functions,
                 size_t *out_count) const
 {
     if (!m_value_type.is_builtin()) {
         m_value_type.extended()->get_dynamic_array_functions(out_functions, out_count);
     }
 }
Exemplo n.º 10
0
byteswap_type::byteswap_type(const ndt::type& value_type)
    : base_expr_type(byteswap_type_id, expr_kind, value_type.get_data_size(),
                            value_type.get_data_alignment(), type_flag_scalar, 0),
                    m_value_type(value_type),
                    m_operand_type(ndt::make_fixedbytes(value_type.get_data_size(), value_type.get_data_alignment()))
{
    if (!value_type.is_builtin()) {
        throw dynd::type_error("byteswap_type: Only built-in types are supported presently");
    }
}
Exemplo n.º 11
0
size_t fixedstring_type::make_assignment_kernel(
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        switch (src_tp.get_type_id()) {
            case fixedstring_type_id: {
                const fixedstring_type *src_fs = src_tp.extended<fixedstring_type>();
                return make_fixedstring_assignment_kernel(
                    ckb, ckb_offset, get_data_size(), m_encoding,
                    src_fs->get_data_size(), src_fs->m_encoding, kernreq, ectx);
            }
            case string_type_id: {
                const base_string_type *src_fs = src_tp.extended<base_string_type>();
                return make_blockref_string_to_fixedstring_assignment_kernel(
                    ckb, ckb_offset, get_data_size(), m_encoding,
                    src_fs->get_encoding(), kernreq, ectx);
            }
            default: {
                if (!src_tp.is_builtin()) {
                    return src_tp.extended()->make_assignment_kernel(
                        ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp,
                        src_arrmeta, kernreq, ectx);
                } else {
                    return make_builtin_to_string_assignment_kernel(
                        ckb, ckb_offset, dst_tp, dst_arrmeta,
                        src_tp.get_type_id(), kernreq, ectx);
                }
            }
        }
    } else {
        if (dst_tp.is_builtin()) {
            return make_string_to_builtin_assignment_kernel(
                ckb, ckb_offset, dst_tp.get_type_id(), src_tp, src_arrmeta,
                kernreq, ectx);
        } else {
            stringstream ss;
            ss << "Cannot assign from " << src_tp << " to " << dst_tp;
            throw dynd::type_error(ss.str());
        }
    }
}
Exemplo n.º 12
0
std::string pydynd::make_pep3118_format(intptr_t &out_itemsize, const ndt::type &tp, const char *arrmeta)
{
  std::stringstream result;
  // Specify native alignment/storage if it's a builtin scalar type
  if (tp.is_builtin()) {
    result << "@";
  }
  append_pep3118_format(out_itemsize, tp, arrmeta, result);
  return result.str();
}
Exemplo n.º 13
0
nd::array dynd::parse_json(const ndt::type &tp, const char *json_begin, const char *json_end,
                           const eval::eval_context *ectx)
{
  nd::array result;
  result = nd::empty(tp);
  parse_json(result, json_begin, json_end, ectx);
  if (!tp.is_builtin()) {
    tp.extended()->arrmeta_finalize_buffers(result.get()->metadata());
  }
  return result;
}
Exemplo n.º 14
0
size_t dynd::make_assignment_kernel(
                ckernel_builder *out, size_t offset_out,
                const ndt::type& dst_tp, const char *dst_metadata,
                const ndt::type& src_tp, const char *src_metadata,
                kernel_request_t kernreq, assign_error_mode errmode,
                const eval::eval_context *ectx)
{
    if (errmode == assign_error_default && ectx != NULL) {
        errmode = ectx->default_errmode;
    }

    if (dst_tp.is_builtin()) {
        if (src_tp.is_builtin()) {
            // If the casting can be done losslessly, disable the error check to find faster code paths
            if (errmode != assign_error_none && is_lossless_assignment(dst_tp, src_tp)) {
                errmode = assign_error_none;
            }

            if (dst_tp.extended() == src_tp.extended()) {
                return make_pod_typed_data_assignment_kernel(out, offset_out,
                                dst_tp.get_data_size(), dst_tp.get_data_alignment(),
                                kernreq);
            } else {
                return make_builtin_type_assignment_kernel(out, offset_out,
                                dst_tp.get_type_id(), src_tp.get_type_id(),
                                kernreq, errmode);
            }
        } else {
            return src_tp.extended()->make_assignment_kernel(out, offset_out,
                            dst_tp, dst_metadata,
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        }
    } else {
        return dst_tp.extended()->make_assignment_kernel(out, offset_out,
                        dst_tp, dst_metadata,
                        src_tp, src_metadata,
                        kernreq, errmode, ectx);
    }
}
Exemplo n.º 15
0
size_t dynd::make_value_to_pointer_assignment_kernel(void *ckb,
        intptr_t ckb_offset,
        const ndt::type &tp,
        kernel_request_t kernreq)
{
    if (tp.is_builtin()) {
        return make_builtin_value_to_pointer_assignment_kernel(
                   ckb, ckb_offset, tp.get_type_id(), kernreq);
    }

    stringstream ss;
    ss << "make_value_to_pointer_assignment_kernel: unrecognized type " << tp;
    throw runtime_error(ss.str());
}
Exemplo n.º 16
0
intptr_t dynd::make_assignment_kernel(
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx)
{
  if (dst_tp.is_builtin()) {
    if (src_tp.is_builtin()) {
      nd::callable &child = nd::assign::overload(dst_tp, src_tp);
      return child.get()->instantiate(NULL, 0, NULL, ckb, ckb_offset, dst_tp,
                                      dst_arrmeta, 1, &src_tp, &src_arrmeta,
                                      kernreq, ectx, 0, NULL,
                                      std::map<std::string, ndt::type>());
    } else {
      return src_tp.extended()->make_assignment_kernel(
          ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, src_arrmeta, kernreq,
          ectx);
    }
  } else {
    return dst_tp.extended()->make_assignment_kernel(
        ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, src_arrmeta, kernreq,
        ectx);
  }
}
Exemplo n.º 17
0
ndt::type ndt::make_pointer(const ndt::type& target_tp)
{
    // Static instances of the type, which have a reference
    // count > 0 for the lifetime of the program. This static
    // construction is inside a function to ensure correct creation
    // order during startup.
    static static_pointer sp;

    if (target_tp.is_builtin()) {
        return sp.static_builtins_instance[target_tp.get_type_id()];
    } else {
        return ndt::type(new pointer_type(target_tp), false);
    }
}
Exemplo n.º 18
0
ndt::type ndt::make_strided_dim(const ndt::type& element_tp)
{
    // Static instances of the types, which have a reference
    // count > 0 for the lifetime of the program. This static
    // construction is inside a function to ensure correct creation
    // order during startup.
    static static_strided_dims ssd;

    if (element_tp.is_builtin()) {
        return ssd.static_builtins_instance[element_tp.get_type_id()];
    } else {
        return ndt::type(new strided_dim_type(element_tp), false);
    }
}
Exemplo n.º 19
0
size_t datetime_type::make_assignment_kernel(
                ckernel_builder *out, size_t offset_out,
                const ndt::type& dst_tp, const char *dst_metadata,
                const ndt::type& src_tp, const char *src_metadata,
                kernel_request_t kernreq, assign_error_mode errmode,
                const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        if (src_tp == dst_tp) {
            return make_pod_typed_data_assignment_kernel(out, offset_out,
                            get_data_size(), get_data_alignment(), kernreq);
        } else if (src_tp.get_kind() == string_kind) {
            // Assignment from strings
            return make_string_to_datetime_assignment_kernel(out, offset_out,
                            dst_tp, dst_metadata,
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        } else if (src_tp.get_kind() == struct_kind) {
            // Convert to struct using the "struct" property
            return ::make_assignment_kernel(out, offset_out,
                ndt::make_property(dst_tp, "struct"), dst_metadata,
                src_tp, src_metadata,
                kernreq, errmode, ectx);
        } else if (!src_tp.is_builtin()) {
            return src_tp.extended()->make_assignment_kernel(out, offset_out,
                            dst_tp, dst_metadata,
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        }
    } else {
        if (dst_tp.get_kind() == string_kind) {
            // Assignment to strings
            return make_datetime_to_string_assignment_kernel(out, offset_out,
                            dst_tp, dst_metadata,
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        } else if (dst_tp.get_kind() == struct_kind) {
            // Convert to struct using the "struct" property
            return ::make_assignment_kernel(out, offset_out,
                dst_tp, dst_metadata,
                ndt::make_property(src_tp, "struct"), src_metadata,
                kernreq, errmode, ectx);
        }
        // TODO
    }

    stringstream ss;
    ss << "Cannot assign from " << src_tp << " to " << dst_tp;
    throw runtime_error(ss.str());
}
Exemplo n.º 20
0
nd::array dynd::parse_json(const ndt::type &tp, const char *json_begin,
                           const char *json_end, const eval::eval_context *ectx)
{
    nd::array result;
    if (tp.get_data_size() != 0) {
        result = nd::empty(tp);
        parse_json(result, json_begin, json_end, ectx);
        if (!tp.is_builtin()) {
            tp.extended()->metadata_finalize_buffers(result.get_ndo_meta());
        }
        result.flag_as_immutable();
        return result;
    } else {
        stringstream ss;
        ss << "The dynd type provided to parse_json, " << tp << ", cannot be used because it requires additional shape information";
        throw runtime_error(ss.str());
    }
}
Exemplo n.º 21
0
size_t time_type::make_assignment_kernel(
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        if (src_tp.get_type_id() == time_type_id) {
            return make_pod_typed_data_assignment_kernel(ckb, ckb_offset,
                            get_data_size(), get_data_alignment(), kernreq);
        } else if (src_tp.get_kind() == string_kind) {
            // Assignment from strings
            return make_string_to_time_assignment_kernel(
                ckb, ckb_offset, dst_tp, src_tp, src_arrmeta, kernreq, ectx);
        } else if (src_tp.get_kind() == struct_kind) {
            // Convert to struct using the "struct" property
            return ::make_assignment_kernel(
                ckb, ckb_offset, ndt::make_property(dst_tp, "struct"),
                dst_arrmeta, src_tp, src_arrmeta, kernreq, ectx);
        } else if (!src_tp.is_builtin()) {
            return src_tp.extended()->make_assignment_kernel(
                ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, src_arrmeta,
                kernreq, ectx);
        }
    } else {
        if (dst_tp.get_kind() == string_kind) {
            // Assignment to strings
            return make_time_to_string_assignment_kernel(
                ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, kernreq, ectx);
        } else if (dst_tp.get_kind() == struct_kind) {
            // Convert to struct using the "struct" property
            return ::make_assignment_kernel(
                ckb, ckb_offset, dst_tp, dst_arrmeta,
                ndt::make_property(src_tp, "struct"), src_arrmeta, kernreq,
                ectx);
        }
        // TODO
    }

    stringstream ss;
    ss << "Cannot assign from " << src_tp << " to " << dst_tp;
    throw dynd::type_error(ss.str());
}
Exemplo n.º 22
0
size_t void_pointer_type::make_assignment_kernel(
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *dst_arrmeta, const ndt::type &src_tp, const char *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        if (src_tp.get_type_id() == void_pointer_type_id) {
            return ::make_pod_typed_data_assignment_kernel(ckb, ckb_offset,
                    get_data_size(), get_data_alignment(),
                    kernreq);
        } else if (!src_tp.is_builtin()) {
            src_tp.extended()->make_assignment_kernel(
                ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp, src_arrmeta,
                kernreq, ectx);
        }
    }

    stringstream ss;
    ss << "Cannot assign from " << src_tp << " to " << dst_tp;
    throw dynd::type_error(ss.str());
}
Exemplo n.º 23
0
 void internal_allocate()
 {
   m_stride = m_type.get_data_size();
   m_storage = new char[DYND_BUFFER_CHUNK_SIZE * m_stride];
   m_arrmeta = NULL;
   size_t metasize =
       m_type.is_builtin() ? 0 : m_type.extended()->get_arrmeta_size();
   if (metasize != 0) {
     try
     {
       m_arrmeta = new char[metasize];
       m_type.extended()->arrmeta_default_construct(m_arrmeta, 0, NULL, true);
     }
     catch (const std::exception &)
     {
       delete[] m_storage;
       delete[] m_arrmeta;
       throw;
     }
   }
 }
Exemplo n.º 24
0
size_t time_type::make_comparison_kernel(
                void *ckb, intptr_t ckb_offset,
                const ndt::type& src0_tp, const char *src0_arrmeta,
                const ndt::type& src1_tp, const char *src1_arrmeta,
                comparison_type_t comptype,
                const eval::eval_context *ectx) const
{
    if (this == src0_tp.extended()) {
        if (*this == *src1_tp.extended()) {
            return make_builtin_type_comparison_kernel(ckb, ckb_offset,
                            int64_type_id, int64_type_id, comptype);
        } else if (!src1_tp.is_builtin()) {
            return src1_tp.extended()->make_comparison_kernel(ckb, ckb_offset,
                            src0_tp, src0_arrmeta,
                            src1_tp, src1_arrmeta,
                            comptype, ectx);
        }
    }

    throw not_comparable_error(src0_tp, src1_tp, comptype);
}
Exemplo n.º 25
0
size_t date_type::make_comparison_kernel(
    ckernel_builder *out, size_t offset_out,
    const ndt::type& src0_tp, const char *src0_metadata,
    const ndt::type& src1_tp, const char *src1_metadata,
    comparison_type_t comptype,
    const eval::eval_context *ectx) const
{
    if (this == src0_tp.extended()) {
        if (*this == *src1_tp.extended()) {
            return make_builtin_type_comparison_kernel(out, offset_out,
                    int32_type_id, int32_type_id, comptype);
        } else if (!src1_tp.is_builtin()) {
            return src1_tp.extended()->make_comparison_kernel(out, offset_out,
                    src0_tp, src0_metadata,
                    src1_tp, src1_metadata,
                    comptype, ectx);
        }
    }

    throw not_comparable_error(src0_tp, src1_tp, comptype);
}
Exemplo n.º 26
0
intptr_t dynd::make_cuda_device_builtin_type_assignment_kernel(
    const callable_type_data *DYND_UNUSED(self),
    const ndt::callable_type *DYND_UNUSED(af_tp), char *DYND_UNUSED(data),
    void *ckb, intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *DYND_UNUSED(dst_arrmeta), intptr_t DYND_UNUSED(nsrc),
    const ndt::type *src_tp, const char *const *DYND_UNUSED(src_arrmeta),
    kernel_request_t kernreq, const eval::eval_context *ectx,
    const nd::array &kwds, const std::map<std::string, ndt::type> &tp_vars)
{
  assign_error_mode errmode = ectx->cuda_device_errmode;

  if (errmode != assign_error_nocheck &&
      is_lossless_assignment(dst_tp, *src_tp)) {
    errmode = assign_error_nocheck;
  }

  if (!dst_tp.is_builtin() || !src_tp->is_builtin() ||
      errmode == assign_error_default) {
    stringstream ss;
    ss << "cannot do cuda device assignment from " << *src_tp << " to "
       << dst_tp;
    throw runtime_error(ss.str());
  }

  if ((kernreq & kernel_request_cuda_device) == 0) {
    // Create a trampoline ckernel from host to device
    nd::cuda_launch_ck<1> *self =
        nd::cuda_launch_ck<1>::make(ckb, kernreq, ckb_offset, 1, 1);
    // Make the assignment on the device
    make_cuda_device_builtin_type_assignment_kernel(
        NULL, NULL, NULL, &self->ckb, 0, dst_tp, NULL, 1, src_tp, NULL,
        kernreq | kernel_request_cuda_device, ectx, kwds, tp_vars);
    // Return the offset for the original ckb
    return ckb_offset;
  }

  return make_builtin_type_assignment_kernel(
      ckb, ckb_offset, dst_tp.get_type_id(), src_tp->get_type_id(), kernreq,
      errmode);
}
Exemplo n.º 27
0
intptr_t nd::copy_ck::instantiate(
    char *DYND_UNUSED(static_data), size_t DYND_UNUSED(data_size),
    char *DYND_UNUSED(data), void *ckb, intptr_t ckb_offset,
    const ndt::type &dst_tp, const char *dst_arrmeta,
    intptr_t DYND_UNUSED(nsrc), const ndt::type *src_tp,
    const char *const *src_arrmeta, kernel_request_t kernreq,
    const eval::eval_context *ectx, intptr_t DYND_UNUSED(nkwd),
    const nd::array *DYND_UNUSED(kwds),
    const std::map<std::string, ndt::type> &DYND_UNUSED(tp_vars))
{
  if (dst_tp.is_builtin()) {
    if (src_tp[0].is_builtin()) {
      if (dst_tp.extended() == src_tp[0].extended()) {
        return make_pod_typed_data_assignment_kernel(
            ckb, ckb_offset, dynd::ndt::detail::builtin_data_sizes
                                 [dst_tp.unchecked_get_builtin_type_id()],
            dynd::ndt::detail::builtin_data_alignments
                [dst_tp.unchecked_get_builtin_type_id()],
            kernreq);
      } else {
        nd::callable &child = nd::assign::overload(dst_tp, src_tp[0]);
        return child.get()->instantiate(NULL, 0, NULL, ckb, ckb_offset, dst_tp,
                                        dst_arrmeta, 1, src_tp, src_arrmeta,
                                        kernreq, ectx, 0, NULL,
                                        std::map<std::string, ndt::type>());
      }
    } else {
      return src_tp[0].extended()->make_assignment_kernel(
          ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp[0], src_arrmeta[0],
          kernreq, ectx);
    }
  } else {
    return dst_tp.extended()->make_assignment_kernel(
        ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp[0], src_arrmeta[0],
        kernreq, ectx);
  }
}
Exemplo n.º 28
0
static intptr_t instantiate_copy(
    const arrfunc_type_data *self,
    const arrfunc_type *af_tp, void *ckb,
    intptr_t ckb_offset, const ndt::type &dst_tp, const char *dst_arrmeta,
    const ndt::type *src_tp, const char *const *src_arrmeta,
    kernel_request_t kernreq, const eval::eval_context *ectx,
    const nd::array &kwds)
{
  if (dst_tp.is_builtin()) {
    if (src_tp[0].is_builtin()) {
      if (dst_tp.extended() == src_tp[0].extended()) {
        return make_pod_typed_data_assignment_kernel(
            ckb, ckb_offset,
            detail::builtin_data_sizes[dst_tp.unchecked_get_builtin_type_id()],
            detail::builtin_data_alignments
                [dst_tp.unchecked_get_builtin_type_id()],
            kernreq);
      }
      else {
        return make_builtin_type_assignment_kernel(
            ckb, ckb_offset, dst_tp.get_type_id(), src_tp[0].get_type_id(),
            kernreq, ectx->errmode);
      }
    }
    else {
      return src_tp[0].extended()->make_assignment_kernel(
          self, af_tp, ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp[0], src_arrmeta[0],
          kernreq, ectx, kwds);
    }
  }
  else {
    return dst_tp.extended()->make_assignment_kernel(
        self, af_tp, ckb, ckb_offset, dst_tp, dst_arrmeta, src_tp[0], src_arrmeta[0],
        kernreq, ectx, kwds);
  }
}
Exemplo n.º 29
0
// Returns true if the destination type can represent *all* the values
// of the source type, false otherwise. This is used, for example,
// to skip any overflow checks when doing value assignments between differing
// types.
bool dynd::is_lossless_assignment(const ndt::type &dst_tp,
                                  const ndt::type &src_tp)
{
  if (dst_tp.is_builtin() && src_tp.is_builtin()) {
    switch (src_tp.get_kind()) {
    case kind_kind: // TODO: raise an error?
      return true;
    case pattern_kind: // TODO: raise an error?
      return true;
    case bool_kind:
      switch (dst_tp.get_kind()) {
      case bool_kind:
      case sint_kind:
      case uint_kind:
      case real_kind:
      case complex_kind:
        return true;
      case bytes_kind:
        return false;
      default:
        break;
      }
      break;
    case sint_kind:
      switch (dst_tp.get_kind()) {
      case bool_kind:
        return false;
      case sint_kind:
        return dst_tp.get_data_size() >= src_tp.get_data_size();
      case uint_kind:
        return false;
      case real_kind:
        return dst_tp.get_data_size() > src_tp.get_data_size();
      case complex_kind:
        return dst_tp.get_data_size() > 2 * src_tp.get_data_size();
      case bytes_kind:
        return false;
      default:
        break;
      }
      break;
    case uint_kind:
      switch (dst_tp.get_kind()) {
      case bool_kind:
        return false;
      case sint_kind:
        return dst_tp.get_data_size() > src_tp.get_data_size();
      case uint_kind:
        return dst_tp.get_data_size() >= src_tp.get_data_size();
      case real_kind:
        return dst_tp.get_data_size() > src_tp.get_data_size();
      case complex_kind:
        return dst_tp.get_data_size() > 2 * src_tp.get_data_size();
      case bytes_kind:
        return false;
      default:
        break;
      }
      break;
    case real_kind:
      switch (dst_tp.get_kind()) {
      case bool_kind:
      case sint_kind:
      case uint_kind:
        return false;
      case real_kind:
        return dst_tp.get_data_size() >= src_tp.get_data_size();
      case complex_kind:
        return dst_tp.get_data_size() >= 2 * src_tp.get_data_size();
      case bytes_kind:
        return false;
      default:
        break;
      }
    case complex_kind:
      switch (dst_tp.get_kind()) {
      case bool_kind:
      case sint_kind:
      case uint_kind:
      case real_kind:
        return false;
      case complex_kind:
        return dst_tp.get_data_size() >= src_tp.get_data_size();
      case bytes_kind:
        return false;
      default:
        break;
      }
    case string_kind:
      switch (dst_tp.get_kind()) {
      case bool_kind:
      case sint_kind:
      case uint_kind:
      case real_kind:
      case complex_kind:
        return false;
      case bytes_kind:
        return false;
      default:
        break;
      }
    case bytes_kind:
      return dst_tp.get_kind() == bytes_kind &&
             dst_tp.get_data_size() == src_tp.get_data_size();
    default:
      break;
    }

    throw std::runtime_error(
        "unhandled built-in case in is_lossless_assignmently");
  }

  // Use the available base_type to check the casting
  if (!dst_tp.is_builtin()) {
    // Call with dst_dt (the first parameter) first
    return dst_tp.extended()->is_lossless_assignment(dst_tp, src_tp);
  } else {
    // Fall back to src_dt if the dst's extended is NULL
    return src_tp.extended()->is_lossless_assignment(dst_tp, src_tp);
  }
}
Exemplo n.º 30
0
size_t json_type::make_assignment_kernel(
                ckernel_builder *out, size_t offset_out,
                const ndt::type& dst_tp, const char *dst_metadata,
                const ndt::type& src_tp, const char *src_metadata,
                kernel_request_t kernreq, assign_error_mode errmode,
                const eval::eval_context *ectx) const
{
    if (this == dst_tp.extended()) {
        switch (src_tp.get_type_id()) {
            case json_type_id: {
                // Assume the input is valid JSON when copying from json to json types
                return make_blockref_string_assignment_kernel(out, offset_out,
                                dst_metadata, string_encoding_utf_8,
                                src_metadata, string_encoding_utf_8,
                                kernreq, errmode, ectx);
            }
            case string_type_id:
            case fixedstring_type_id: {
                offset_out = make_kernreq_to_single_kernel_adapter(out, offset_out, kernreq);
                out->ensure_capacity(offset_out + sizeof(string_to_json_kernel_extra));
                string_to_json_kernel_extra *e = out->get_at<string_to_json_kernel_extra>(offset_out);
                e->base.set_function<unary_single_operation_t>(&string_to_json_kernel_extra::single);
                e->base.destructor = &string_to_json_kernel_extra::destruct;
                e->dst_metadata = dst_metadata;
                e->validate = (errmode != assign_error_none);
                if (src_tp.get_type_id() == string_type_id) {
                    return make_blockref_string_assignment_kernel(
                                    out, offset_out + sizeof(string_to_json_kernel_extra),
                                    dst_metadata, string_encoding_utf_8,
                                    src_metadata,
                                    static_cast<const base_string_type *>(src_tp.extended())->get_encoding(),
                                    kernel_request_single, errmode, ectx);
                } else {
                    return make_fixedstring_to_blockref_string_assignment_kernel(
                                    out, offset_out + sizeof(string_to_json_kernel_extra),
                                    dst_metadata, string_encoding_utf_8,
                                    src_tp.get_data_size(),
                                    static_cast<const base_string_type *>(src_tp.extended())->get_encoding(),
                                    kernel_request_single, errmode, ectx);
                }
            }
            default: {
                if (!src_tp.is_builtin()) {
                    return src_tp.extended()->make_assignment_kernel(out, offset_out,
                                    dst_tp, dst_metadata,
                                    src_tp, src_metadata,
                                    kernreq, errmode, ectx);
                } else {
                    return make_builtin_to_string_assignment_kernel(out, offset_out,
                                dst_tp, dst_metadata,
                                src_tp.get_type_id(),
                                kernreq, errmode, ectx);
                }
            }
        }
    } else {
        if (dst_tp.is_builtin()) {
            return make_string_to_builtin_assignment_kernel(out, offset_out,
                            dst_tp.get_type_id(),
                            src_tp, src_metadata,
                            kernreq, errmode, ectx);
        } else {
            stringstream ss;
            ss << "Cannot assign from " << src_tp << " to " << dst_tp;
            throw runtime_error(ss.str());
        }
    }
}