From 8e4f11368f12839369fc032f0e3ab9c0262fb6d4 Mon Sep 17 00:00:00 2001
From: JM Etancelin <jean-matthieu.etancelin@univ-pau.fr>
Date: Wed, 17 Mar 2021 16:41:04 +0100
Subject: [PATCH] fix numpy deprecation and allocation alignment

---
 hysop/backend/host/host_buffer.py | 29 +++++++++++++++++------------
 hysop/core/memory/allocator.py    | 25 +++++++++++++------------
 2 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/hysop/backend/host/host_buffer.py b/hysop/backend/host/host_buffer.py
index 9d343d21b..7e44b22ba 100644
--- a/hysop/backend/host/host_buffer.py
+++ b/hysop/backend/host/host_buffer.py
@@ -1,9 +1,11 @@
 import numpy as np
+import ctypes as C
 
 from hysop.constants import MemoryOrdering, default_order
 from hysop.tools.types import check_instance
 from hysop.core.memory.buffer import Buffer, PooledBuffer
 
+
 class HostBuffer(np.ndarray, Buffer):
     """
     Host buffer class.
@@ -11,22 +13,22 @@ class HostBuffer(np.ndarray, Buffer):
     __array_priority__ = -1.0
 
     def __new__(cls, size,
-            shape=None, dtype=np.uint8, order=None,
-            buffer=None, offset=0, strides=None):
+                shape=None, dtype=np.uint8, order=None,
+                buffer=None, offset=0, strides=None):
 
         from_buffer = False
         if isinstance(buffer, Buffer):
             __buffer = buffer
-            buffer   = buffer.buf
+            buffer = buffer.buf
             from_buffer = True
 
-        obj = super(HostBuffer,cls).__new__(cls,
-                shape=shape or (size,), dtype=dtype, order=order,
-                buffer=buffer, offset=offset, strides=strides)
+        obj = super(HostBuffer, cls).__new__(cls,
+                                             shape=shape or (size,), dtype=dtype, order=order,
+                                             buffer=buffer, offset=offset, strides=strides)
 
-        #keep a reference to the buffer (usefull for pooled buffers)
-        #such that buffer.__del__ will only be called when all views
-        #on this HostBuffer have been destroyed.
+        # keep a reference to the buffer (usefull for pooled buffers)
+        # such that buffer.__del__ will only be called when all views
+        # on this HostBuffer have been destroyed.
         if from_buffer and isinstance(__buffer, HostPooledBuffer):
             obj._hysop_base_data = __buffer
 
@@ -38,11 +40,13 @@ class HostBuffer(np.ndarray, Buffer):
 
     def __str__(self):
         return self.view(np.ndarray).__str__()
+
     def __repr__(self):
         return self.view(np.ndarray).__repr__()
 
     def get_int_ptr(self):
         return self.ctypes.data
+
     def release(self):
         pass
 
@@ -51,7 +55,7 @@ class HostBuffer(np.ndarray, Buffer):
         """
         Given int ptr should never be freed, numpy take ownership.
         """
-        buf = np.core.multiarray.int_asbuffer(int_ptr_value, size)
+        buf = np.ctypeslib.as_array(C.cast(int_ptr_value, C.POINTER(C.c_uint8)), (size,))
         return cls.from_buffer(buf)
 
     @classmethod
@@ -62,7 +66,7 @@ class HostBuffer(np.ndarray, Buffer):
     def aligned_view(self, alignment, size=None):
         assert self.ndim == 1
         assert self.dtype == np.uint8
-        assert alignment>0
+        assert alignment > 0
         assert not (alignment & (alignment-1)), 'alignment is not a power of 2.'
         ptr = self.get_int_ptr()
         offset = -ptr % alignment
@@ -70,7 +74,7 @@ class HostBuffer(np.ndarray, Buffer):
             size = self.size-offset
         else:
             assert self.size >= (offset+size)
-        buf = self.__getitem__(slice(offset,offset+size))
+        buf = self.__getitem__(slice(offset, offset+size))
         return buf
 
     @classmethod
@@ -82,6 +86,7 @@ class HostBuffer(np.ndarray, Buffer):
 
     int_ptr = property(get_int_ptr)
 
+
 class HostPooledBuffer(PooledBuffer):
     def get_array(self):
         return self._bufview
diff --git a/hysop/core/memory/allocator.py b/hysop/core/memory/allocator.py
index 46616a61f..4f93e42c0 100644
--- a/hysop/core/memory/allocator.py
+++ b/hysop/core/memory/allocator.py
@@ -1,4 +1,3 @@
-
 import traceback
 from abc import ABCMeta, abstractmethod
 from hysop import __VERBOSE__, __TRACE_MEMALLOCS__, __BACKTRACE_BIG_MEMALLOCS__
@@ -7,12 +6,13 @@ from hysop.tools.units import bytes2str
 from hysop.tools.types import first_not_None
 from hysop.tools.handle import TaggedObject
 
+
 class AllocatorBase(TaggedObject, metaclass=ABCMeta):
     """
     Base class for allocators.
     """
 
-    is_deferred=False
+    is_deferred = False
 
     def __new__(cls, verbose, **kwds):
         return super(AllocatorBase, cls).__new__(cls, tag_prefix='al', **kwds)
@@ -24,8 +24,10 @@ class AllocatorBase(TaggedObject, metaclass=ABCMeta):
 
     def __eq__(self, other):
         return (self is other)
+
     def __ne__(self, other):
         return (self is not other)
+
     def __hash__(self):
         return id(self)
 
@@ -39,12 +41,12 @@ class AllocatorBase(TaggedObject, metaclass=ABCMeta):
         try_count = 0
         while try_count < 2:
             try:
-                if (alignment > 1):
-                    buf = self.allocate_aligned(size=size, alignment=alignment)
-                else:
+                if alignment is None or alignment == 1:
                     buf = self.allocate(nbytes=size)
+                else:
+                    buf = self.allocate_aligned(size=size, alignment=alignment)
                 if (buf is None):
-                    msg='{}.allocate(): returned allocation is None.'.format(self.__class__)
+                    msg = '{}.allocate(): returned allocation is None.'.format(self.__class__)
                     raise ValueError(msg)
                 return buf
             except MemoryError:
@@ -80,8 +82,8 @@ class AllocatorBase(TaggedObject, metaclass=ABCMeta):
         """
         if (self._verbose or __BACKTRACE_BIG_MEMALLOCS__) and verbose:
             print('{}allocating block of size {}.'.format(
-                    self.prefix(), bytes2str(nbytes)))
-        if __BACKTRACE_BIG_MEMALLOCS__ and nbytes>64*1024*1024:
+                self.prefix(), bytes2str(nbytes)))
+        if __BACKTRACE_BIG_MEMALLOCS__ and nbytes > 64*1024*1024:
             print('[BIG ALLOCATION BACKTRACE]')
             print(''.join(traceback.format_stack()))
             print('[END OF TRACE]')
@@ -91,12 +93,12 @@ class AllocatorBase(TaggedObject, metaclass=ABCMeta):
         """
         Allocate size bytes aligned on alignment.
         """
-        assert alignment>0
-        assert (alignment & (alignment-1))==0, 'alignment is not a power of 2.'
+        assert alignment > 0
+        assert (alignment & (alignment-1)) == 0, 'alignment is not a power of 2.'
         nbytes = size + alignment - 1
         if self._verbose:
             print('{}allocating block of size {}, to satisfy {} aligned on {} bytes.'.format(
-                    self.prefix(), bytes2str(nbytes), bytes2str(size), alignment))
+                self.prefix(), bytes2str(nbytes), bytes2str(size), alignment))
         return self.allocate(nbytes, verbose=False).aligned_view(alignment=alignment, size=size)
 
     def try_release_blocks(self):
@@ -111,4 +113,3 @@ class AllocatorBase(TaggedObject, metaclass=ABCMeta):
         Release the allocated buffer.
         """
         buf.release()
-
-- 
GitLab