def test_element_iter_write(self):
        a = np.array([1, 2, 3, 4, 5], dtype=np.int32)
        dd = NumPyDataDescriptor(a)

        self.assertEqual(dd.dshape, datashape.dshape('5, int32'))
        with dd.element_write_iter() as ge:
            self.assertTrue(isinstance(ge, IElementWriteIter))
            for val, ptr in izip([5,7,4,5,3], ge):
                x = ctypes.c_int32(val)
                ctypes.memmove(ptr, ctypes.addressof(x), 4)
        self.assertEqual(dd_as_py(dd), [5,7,4,5,3])
    def test_element_iter_types(self):
        a = np.arange(6).reshape(2,3)
        dd = NumPyDataDescriptor(a)

        # Requesting element iteration should produce an
        # IElementReadIter object
        ei = dd.element_read_iter()
        self.assertTrue(isinstance(ei, IElementReadIter))
        # Iteration over the IElementReadIter object should produce
        # raw ints which are pointers
        for ptr in ei:
            self.assertTrue(isinstance(ptr, _inttypes))
    def test_element_write(self):
        a = np.array([1, 2, 3, 4, 5], dtype=np.int32)
        dd = NumPyDataDescriptor(a)

        self.assertEqual(dd.dshape, datashape.dshape('5, int32'))
        ge = dd.element_writer(1)
        self.assertTrue(isinstance(ge, IElementWriter))

        x = ctypes.c_int32(123)
        ge.write_single((1,), ctypes.addressof(x))
        self.assertEqual(dd_as_py(dd), [1,123,3,4,5])

        with ge.buffered_ptr((3,)) as dst_ptr:
            x = ctypes.c_int32(456)
            ctypes.memmove(dst_ptr, ctypes.addressof(x), 4)
        self.assertEqual(dd_as_py(dd), [1,123,3,456,5])
Exemplo n.º 4
0
def execute_datadescriptor(dd):
    # make a lifted fused func...
    lifted = dd.kerneltree._fused.kernel.lift(2,'C')
    cf = lifted.ctypes_func
    # the actual ctypes function to call
    args = [(ct._type_,
             arr.arr._data.element_reader(0).read_single(()),
             arr.arr.dshape.shape)
            for ct, arr in izip(cf.argtypes[:-1], dd.args)]

    res_dd = NumPyDataDescriptor(np.empty(*to_numpy(dd.dshape)))
    with res_dd.element_writer(0).buffered_ptr(()) as dst_ptr:
        args.append((cf.argtypes[-1]._type_, dst_ptr, res_dd.shape))
        cf_args = [_convert(*foo) for foo in args]
        cf(*[ctypes.byref(x) for x in cf_args])

    return blaze.Array(res_dd)
    def test_element_getitem_types(self):
        a = np.arange(6).reshape(2,3)
        dd = NumPyDataDescriptor(a)

        # Requesting get_element with one index should produce an
        # IElementReader object
        ge = dd.element_reader(1)
        self.assertTrue(isinstance(ge, IElementReader))
        # Iteration over the IElementReadIter object should produce
        # raw ints which are pointers
        self.assertTrue(isinstance(ge.read_single((1,)), _inttypes))

        # Requesting element reader with two indices should produce an
        # IElementReader object
        ge = dd.element_reader(2)
        self.assertTrue(isinstance(ge, IElementReader))
        # Iteration over the IElementReadIter object should produce
        # raw ints which are pointers
        self.assertTrue(isinstance(ge.read_single((1,2)), _inttypes))
Exemplo n.º 6
0
def execute_datadescriptor_outerdim(dd):
    # only lift by one
    lifted = dd.kerneltree._fused.kernel.lift(1,'C')
    cf = lifted.ctypes_func
    print(dir(cf))
    # element readers for operands
    args = [(ct._type_,
             arr.arr._data.element_reader(1),
             arr.arr.dshape.shape[1:])
            for ct, arr in izip(cf.argtypes[:-1], dd.args)]

    res_dd = NumPyDataDescriptor(np.empty(*to_numpy(dd.dshape)))
    outer_dimension = res_dd.shape[0]
    dst = res_dd.element_writer(1)

    for i in xrange(outer_dimension):
        args_i = [(t, er.read_single((i,)), sh) for t, er, sh in args]
        with dst.buffered_ptr((i,)) as dst_ptr:
            args_i.append((cf.argtypes[-1]._type_, dst_ptr, res_dd.shape[1:]))
            cf_args = [_convert(*foo) for foo in args_i]
            cf(*[ctypes.byref(x) for x in cf_args])

    return blaze.Array(res_dd)