Example #1
0
 def buffered_ptr(self, idx):
     if len(idx) != self.nindex:
         raise IndexError('Incorrect number of indices (got %d, require %d)' %
                        (len(idx), self.nindex))
     dst_arr = self.dyndarr[idx]
     buf_arr = dst_arr.cast(self._c_dtype).eval()
     buf_ptr = _lowlevel.data_address_of(buf_arr)
     if buf_ptr == _lowlevel.data_address_of(dst_arr):
         # If no buffering is needed, just return the pointer
         return buffered_ptr_ctxmgr(buf_ptr, None)
     else:
         def buffer_flush():
             dst_arr[...] = buf_arr
         return buffered_ptr_ctxmgr(buf_ptr, buffer_flush)
Example #2
0
 def __next__(self):
     # Copy the previous element to the array if it is buffered
     self.flush()
     if self._index < self._len:
         i = self._index
         self._index = i + 1
         if self._usebuffer:
             if self._buffer is None:
                 self._buffer = nd.empty(self._c_dtype)
             self._buffer_index = i
             return _lowlevel.data_address_of(self._buffer)
         else:
             return _lowlevel.data_address_of(self._dyndarr[i])
     else:
         raise StopIteration
 def test_array_from_ptr(self):
     # cfixed_dim arrmeta is redundant so this is ok
     a = (ctypes.c_int32 * 3)()
     a[0] = 3
     a[1] = 6
     a[2] = 9
     # Readwrite version using cfixed
     b = _lowlevel.array_from_ptr('cfixed[3] * int32',
                                  ctypes.addressof(a), a, 'readwrite')
     self.assertEqual(_lowlevel.data_address_of(b), ctypes.addressof(a))
     self.assertEqual(nd.dshape_of(b), '3 * int32')
     self.assertEqual(nd.as_py(b), [3, 6, 9])
     b[1] = 10
     self.assertEqual(a[1], 10)
     # Readonly version using cfixed
     b = _lowlevel.array_from_ptr('cfixed[3] * int32',
                                  ctypes.addressof(a), a, 'readonly')
     self.assertEqual(nd.as_py(b), [3, 10, 9])
     def assign_to(b):
         b[1] = 100
     self.assertRaises(RuntimeError, assign_to, b)
     # Using a fixed dim default-constructs the arrmeta, so works too
     b = _lowlevel.array_from_ptr('3 * int32', ctypes.addressof(a),
                                  a, 'readonly')
     self.assertEqual(nd.as_py(b), [3, 10, 9])
     # Should get an error if we try strided, because the size is unknown
     self.assertRaises(RuntimeError,
                       lambda: _lowlevel.array_from_ptr('strided * int32',
                                                        ctypes.addressof(a),
                                                        a, 'readonly'))
Example #4
0
 def read_single(self, idx, count=1):
     idxlen = len(idx)
     if idxlen != self.nindex:
         raise IndexError('Incorrect number of indices (got %d, require %d)' %
                        (len(idx), self.nindex))
     idx = tuple(operator.index(i) for i in idx)
     if count == 1:
         x = self._dyndarr[idx]
         # Make it C-contiguous and in native byte order
         x = x.cast(self._c_dtype).eval()
         self._tmpbuffer = x
         return _lowlevel.data_address_of(x)
     else:
         idx = idx[:-1] + (slice(idx[-1], idx[-1]+count),)
         x = self._dyndarr[idx]
         # Make it C-contiguous and in native byte order
         x = x.cast(ndt.make_fixed_dim(count, self._c_dtype)).eval()
         self._tmpbuffer = x
         return _lowlevel.data_address_of(x)
Example #5
0
 def read_single(self, idx):
     if len(idx) != self.nindex:
         raise IndexError('Incorrect number of indices (got %d, require %d)' %
                        (len(idx), self.nindex))
     idx = tuple([operator.index(i) for i in idx])
     x = self.dyndarr[idx]
     # Make it C-contiguous and in native byte order
     x = x.cast(self._c_dtype).eval()
     self._tmpbuffer = x
     return _lowlevel.data_address_of(x)
Example #6
0
 def __next__(self):
     if self._index < self._len:
         i = self._index
         self._index = i + 1
         x = self._dyndarr[i]
         # Make it C-contiguous and in native byte order
         x = x.cast(self._c_dtype).eval()
         self._tmpbuffer = x
         return _lowlevel.data_address_of(x)
     else:
         raise StopIteration
Example #7
0
    def buffered_ptr(self, idx, count=1):
        if len(idx) != self.nindex:
            raise IndexError('Incorrect number of indices (got %d, require %d)' %
                           (len(idx), self.nindex))

        idx = tuple(operator.index(i) for i in idx)
        c_dtype = self._c_dtype

        if count != 1:
            idx = idx[:-1] + (slice(idx[-1], idx[-1]+count),)
            c_dtype = ndt.make_fixed_dim(count, c_dtype)

        dst_arr = self._dyndarr[idx]
        buf_arr = dst_arr.cast(c_dtype).eval()
        buf_ptr = _lowlevel.data_address_of(buf_arr)
        if buf_ptr == _lowlevel.data_address_of(dst_arr):
            # If no buffering is needed, just return the pointer
            return buffered_ptr_ctxmgr(buf_ptr, None)
        else:
            def buffer_flush():
                dst_arr[...] = buf_arr
            return buffered_ptr_ctxmgr(buf_ptr, buffer_flush)
Example #8
0
 def test_array_from_ptr(self):
     a = (ctypes.c_int32 * 3)()
     a[0] = 3
     a[1] = 6
     a[2] = 9
     # Readwrite version
     b = _lowlevel.array_from_ptr(ndt.type('3 * int32'), ctypes.addressof(a),
                     a, 'readwrite')
     self.assertEqual(_lowlevel.data_address_of(b), ctypes.addressof(a))
     self.assertEqual(nd.dshape_of(b), '3 * int32')
     self.assertEqual(nd.as_py(b), [3, 6, 9])
     b[1] = 10
     self.assertEqual(a[1], 10)
     # Readonly version
     b = _lowlevel.array_from_ptr(ndt.type('3 * int32'), ctypes.addressof(a),
                     a, 'readonly')
     self.assertEqual(nd.as_py(b), [3, 10, 9])
     def assign_to(b):
         b[1] = 100
     self.assertRaises(RuntimeError, assign_to, b)