Exemplo n.º 1
0
void dynd::typed_data_assign(const ndt::type& dst_tp, const char *dst_metadata, char *dst_data,
                const ndt::type& src_tp, const char *src_metadata, const char *src_data,
                assign_error_mode errmode, const eval::eval_context *ectx)
{
    DYND_ASSERT_ALIGNED(dst, 0, dst_tp.get_data_alignment(), "dst type: " << dst_tp << ", src type: " << src_tp);
    DYND_ASSERT_ALIGNED(src, 0, src_dt.get_data_alignment(), "src type: " << src_tp << ", dst type: " << dst_tp);

    if (errmode == assign_error_default) {
        if (ectx != NULL) {
            if (dst_tp.get_dtype().get_type_id() == cuda_device_type_id && src_tp.get_dtype().get_type_id() == cuda_device_type_id) {
                errmode = ectx->default_cuda_device_errmode;
            } else {
                errmode = ectx->default_errmode;
            }
        } else if (dst_tp == src_tp) {
            errmode = assign_error_none;
        } else {
            stringstream ss;
            ss << "assignment from " << src_tp << " to " << dst_tp << " with default error mode requires an eval_context";
            throw dynd::type_error(ss.str());
        }
    }

    assignment_ckernel_builder k;
    make_assignment_kernel(&k, 0, dst_tp, dst_metadata,
                    src_tp, src_metadata,
                    kernel_request_single,
                    errmode, ectx);
    k(dst_data, src_data);
}
Exemplo n.º 2
0
nd::array nd::typed_rand(intptr_t ndim, const intptr_t *shape, const ndt::type &tp) {
    type_id_t dtp_id = tp.get_dtype().get_type_id();

    nd::array res = nd::typed_empty(ndim, shape, tp);

    try {
        array_iter<1, 0> iter(res);
        do {
            switch (dtp_id) {
                case float32_type_id:
                    *reinterpret_cast<float *>(iter.data())
                        = rand() / ((float) RAND_MAX);
                    break;
                case float64_type_id:
                    *reinterpret_cast<double *>(iter.data())
                        = rand() / ((double) RAND_MAX);
                    break;
                case complex_float32_type_id:
                    *reinterpret_cast<dynd_complex<float> *>(iter.data())
                        = dynd_complex<float>(rand() / ((float) RAND_MAX), rand() / ((float) RAND_MAX));
                    break;
                case complex_float64_type_id:
                    *reinterpret_cast<dynd_complex<double> *>(iter.data())
                        = dynd_complex<double>(rand() / ((double) RAND_MAX), rand() / ((double) RAND_MAX));
                    break;
                default:
                    throw std::runtime_error("rand: unsupported dtype");
            }
        } while (iter.next());
    } catch (...) {
        ndt::type strided_tp = tp.get_dtype();
        for (intptr_t i = 0; i < ndim; ++i) {
            strided_tp = ndt::make_strided_dim(strided_tp);
        }

        res.vals() = nd::typed_rand(ndim, shape, strided_tp);
    }

    return res.ucast(tp.get_dtype()).eval();
}
Exemplo n.º 3
0
intptr_t kernels::fixed_dim_assign_na_ck::instantiate(
    const arrfunc_type_data *DYND_UNUSED(self),
    const arrfunc_type *DYND_UNUSED(af_tp), void *ckb,
    intptr_t ckb_offset, const ndt::type &dst_tp,
    const char *DYND_UNUSED(dst_arrmeta), const ndt::type *DYND_UNUSED(src_tp),
    const char *const *DYND_UNUSED(src_arrmeta), kernel_request_t kernreq,
    const eval::eval_context *DYND_UNUSED(ectx),
    const nd::array &DYND_UNUSED(kwds))
{
  ckernel_prefix *ckp = reinterpret_cast<ckernel_builder<kernel_request_host> *>(ckb)->alloc_ck_leaf<ckernel_prefix>(ckb_offset);
  switch (dst_tp.get_dtype().get_type_id()) {
  case bool_type_id:
    ckp->set_expr_function<assign_na<dynd_bool> >(kernreq);
    break;
  case int8_type_id:
    ckp->set_expr_function<assign_na<int8_t> >(kernreq);
    break;
  case int16_type_id:
    ckp->set_expr_function<assign_na<int16_t> >(kernreq);
    break;
  case int32_type_id:
    ckp->set_expr_function<assign_na<int32_t> >(kernreq);
    break;
  case int64_type_id:
    ckp->set_expr_function<assign_na<int64_t> >(kernreq);
    break;
  case int128_type_id:
    ckp->set_expr_function<assign_na<dynd_int128> >(kernreq);
    break;
  case float32_type_id:
    ckp->set_expr_function<assign_na<float> >(kernreq);
    break;
  case float64_type_id:
    ckp->set_expr_function<assign_na<double> >(kernreq);
    break;
  case complex_float32_type_id:
    ckp->set_expr_function<assign_na<dynd_complex<float> > >(kernreq);
    break;
  case complex_float64_type_id:
    ckp->set_expr_function<assign_na<dynd_complex<double> > >(kernreq);
    break;
  default:
    throw type_error("fixed_dim_assign_na: expected built-in type");
    break;
  }
  return ckb_offset;
}
Exemplo n.º 4
0
 static intptr_t instantiate(char *DYND_UNUSED(static_data), 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 *DYND_UNUSED(src_tp),
                             const char *const *DYND_UNUSED(src_arrmeta), kernel_request_t kernreq,
                             const eval::eval_context *DYND_UNUSED(ectx), intptr_t DYND_UNUSED(nkwd),
                             const nd::array *DYND_UNUSED(kwds),
                             const std::map<std::string, ndt::type> &DYND_UNUSED(tp_vars))
 {
   switch (dst_tp.get_dtype().get_type_id()) {
   case bool_type_id:
     assign_na_kernel<bool_type_id, bool_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case int8_type_id:
     assign_na_kernel<int8_type_id, sint_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case int16_type_id:
     assign_na_kernel<int16_type_id, sint_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case int32_type_id:
     assign_na_kernel<int32_type_id, sint_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case int64_type_id:
     assign_na_kernel<int64_type_id, sint_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case int128_type_id:
     assign_na_kernel<int128_type_id, sint_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case float32_type_id:
     assign_na_kernel<float32_type_id, real_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case float64_type_id:
     assign_na_kernel<float64_type_id, real_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case complex_float32_type_id:
     assign_na_kernel<complex_float32_type_id, complex_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   case complex_float64_type_id:
     assign_na_kernel<complex_float64_type_id, complex_kind>::make(ckb, kernreq, ckb_offset);
     return ckb_offset;
   default:
     throw type_error("fixed_dim_assign_na: expected built-in type");
   }
 }
Exemplo n.º 5
0
 static void instantiate(char *DYND_UNUSED(static_data), char *DYND_UNUSED(data), kernel_builder *ckb,
                         const ndt::type &dst_tp, const char *DYND_UNUSED(dst_arrmeta), intptr_t DYND_UNUSED(nsrc),
                         const ndt::type *DYND_UNUSED(src_tp), const char *const *DYND_UNUSED(src_arrmeta),
                         kernel_request_t kernreq, intptr_t DYND_UNUSED(nkwd), const nd::array *DYND_UNUSED(kwds),
                         const std::map<std::string, ndt::type> &DYND_UNUSED(tp_vars))
 {
   switch (dst_tp.get_dtype().get_id()) {
   case bool_id:
     ckb->emplace_back<assign_na_kernel<bool_id, bool_kind_id>>(kernreq);
     break;
   case int8_id:
     ckb->emplace_back<assign_na_kernel<int8_id, int_kind_id>>(kernreq);
     break;
   case int16_id:
     ckb->emplace_back<assign_na_kernel<int16_id, int_kind_id>>(kernreq);
     break;
   case int32_id:
     ckb->emplace_back<assign_na_kernel<int32_id, int_kind_id>>(kernreq);
     break;
   case int64_id:
     ckb->emplace_back<assign_na_kernel<int64_id, int_kind_id>>(kernreq);
     break;
   case int128_id:
     ckb->emplace_back<assign_na_kernel<int128_id, int_kind_id>>(kernreq);
     break;
   case float32_id:
     ckb->emplace_back<assign_na_kernel<float32_id, float_kind_id>>(kernreq);
     break;
   case float64_id:
     ckb->emplace_back<assign_na_kernel<float64_id, float_kind_id>>(kernreq);
     break;
   case complex_float32_id:
     ckb->emplace_back<assign_na_kernel<complex_float32_id, complex_kind_id>>(kernreq);
     break;
   case complex_float64_id:
     ckb->emplace_back<assign_na_kernel<complex_float64_id, complex_kind_id>>(kernreq);
     break;
   default:
     throw type_error("fixed_dim_assign_na: expected built-in type");
   }
 }