def get_array_read(self, memtype=MEM_HOST): """Get read-only access to a Vector via the specified memory type. Args: **memtype: memory type of the array being passed, default CEED_MEM_HOST Returns: *array: Numpy or Numba array""" # Retrieve the length of the array length_pointer = ffi.new("CeedInt *") err_code = lib.CeedVectorGetLength(self._pointer[0], length_pointer) self._ceed._check_error(err_code) # Setup the pointer's pointer array_pointer = ffi.new("CeedScalar **") # libCEED call err_code = lib.CeedVectorGetArrayRead(self._pointer[0], memtype, array_pointer) self._ceed._check_error(err_code) # Return array created from buffer if memtype == MEM_HOST: # Create buffer object from returned pointer buff = ffi.buffer(array_pointer[0], ffi.sizeof("CeedScalar") * length_pointer[0]) # return read only Numpy array ret = np.frombuffer(buff, dtype="float64") ret.flags['WRITEABLE'] = False return ret else: # CUDA array interface # https://numba.pydata.org/numba-doc/latest/cuda/cuda_array_interface.html import numba.cuda as nbcuda desc = { 'shape': (length_pointer[0]), 'typestr': '>f8', 'data': (int(ffi.cast("intptr_t", array_pointer[0])), False), 'version': 2 } # return read only Numba array return nbcuda.from_cuda_array_interface(desc)
def get_array_write(self, memtype=MEM_HOST): """Get write-only access to a Vector via the specified memory type. All old values should be considered invalid. Args: **memtype: memory type of the array being passed, default CEED_MEM_HOST Returns: *array: Numpy or Numba array""" # Retrieve the length of the array length_pointer = ffi.new("CeedSize *") err_code = lib.CeedVectorGetLength(self._pointer[0], length_pointer) self._ceed._check_error(err_code) # Setup the pointer's pointer array_pointer = ffi.new("CeedScalar **") # libCEED call err_code = lib.CeedVectorGetArrayWrite(self._pointer[0], memtype, array_pointer) self._ceed._check_error(err_code) # Return array created from buffer if memtype == MEM_HOST: # Create buffer object from returned pointer buff = ffi.buffer(array_pointer[0], ffi.sizeof("CeedScalar") * length_pointer[0]) # return Numpy array return np.frombuffer(buff, dtype=scalar_types[lib.CEED_SCALAR_TYPE]) else: # CUDA array interface # https://numba.pydata.org/numba-doc/latest/cuda/cuda_array_interface.html import numba.cuda as nbcuda desc = { 'shape': (length_pointer[0]), 'typestr': '>f8', 'data': (int(ffi.cast("intptr_t", array_pointer[0])), False), 'version': 2 } # return Numba array return nbcuda.from_cuda_array_interface(desc)