Exemplo n.º 1
0
 def test_arrfunc_from_instantiate_pyfunc(self):
     # Test wrapping make_assignment_ckernel as an arrfunc
     def instantiate_assignment(out_ckb, ckb_offset, dst_tp, dst_arrmeta,
                                src_tp, src_arrmeta, kernreq, ectx):
         out_ckb = _lowlevel.CKernelBuilderStruct.from_address(out_ckb)
         return _lowlevel.make_assignment_ckernel(out_ckb, ckb_offset,
                         dst_tp, dst_arrmeta,
                         src_tp[0], src_arrmeta[0],
                         kernreq, ectx)
     af = _lowlevel.arrfunc_from_instantiate_pyfunc(
                 instantiate_assignment, "(date) -> string")
     self.assertEqual(nd.as_py(af.proto), ndt.type("(date) -> string"))
     in0 = nd.array('2012-11-05', ndt.date)
     out = af(in0)
     self.assertEqual(nd.as_py(out), '2012-11-05')
     # Also test it as a lifted kernel
     af_lifted = _lowlevel.lift_arrfunc(af)
     self.assertEqual(nd.as_py(af_lifted.proto),
                      ndt.type("(Dims... * date) -> Dims... * string"))
     from datetime import date
     in0 = nd.array([['2013-03-11', date(2010, 10, 10)],
                     [date(1999, 12, 31)],
                     []], type='3 * var * date')
     out = af_lifted(in0)
     self.assertEqual(nd.as_py(out),
                     [['2013-03-11', '2010-10-10'],
                      ['1999-12-31'], []])
Exemplo n.º 2
0
    def test_ctypes_callback_deferred(self):
        # Create a deferred ckernel via a closure
        def instantiate_ckernel(out_ckb, ckb_offset, dst_tp, dst_arrmeta,
                                   src_tp, src_arrmeta, kernreq, ectx):
            out_ckb = _lowlevel.CKernelBuilder(out_ckb)
            def my_kernel_func_single(dst_ptr, src_ptr, kdp):
                dst = ctypes.c_int32.from_address(dst_ptr)
                src = ctypes.c_double.from_address(src_ptr[0])
                dst.value = int(src.value * 3.5)
            def my_kernel_func_strided(dst_ptr, dst_stride, src_ptr, src_stride, count, kdp):
                src_ptr0 = src_ptr[0]
                src_stride0 = src_stride[0]
                for i in range(count):
                    my_kernel_func_single(dst_ptr, [src_ptr0], kdp)
                    dst_ptr += dst_stride
                    src_ptr0 += src_stride0
            if kernreq == 'single':
                kfunc = _lowlevel.ExprSingleOperation(my_kernel_func_single)
            else:
                kfunc = _lowlevel.ExprStridedOperation(my_kernel_func_strided)
            return ckernel.wrap_ckernel_func(out_ckb, ckb_offset,
                            kfunc, kfunc)
        ckd = _lowlevel.arrfunc_from_instantiate_pyfunc(instantiate_ckernel,
                        "(float64) -> int32")
        # Test calling the ckd
        out = nd.empty(ndt.int32)
        in0 = nd.array(4.0, type=ndt.float64)
        ckd.execute(out, in0)
        self.assertEqual(nd.as_py(out), 14)

        # Also call it lifted
        ckd_lifted = _lowlevel.lift_arrfunc(ckd)
        out = nd.empty('2 * var * int32')
        in0 = nd.array([[1.0, 3.0, 2.5], [1.25, -1.5]], type='2 * var * float64')
        ckd_lifted.execute(out, in0)
        self.assertEqual(nd.as_py(out), [[3, 10, 8], [4, -5]])