Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: Unified Memory Allocator #2023

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0befd4e
dsl: Creates CupyAllocator class
Oct 19, 2022
db87362
misc: Fix indentation
Oct 19, 2022
ef1f368
dsl: Fix del method allowing the dealocation of the Cupy data
Oct 19, 2022
ca806b3
dsl: Changes that exclude copyin and copyout pragmas from source code…
Oct 19, 2022
50cd534
dsl: Remove the part of the code that makes the source code be genera…
Oct 26, 2022
539254c
dsl: Change from CUPY_ALLOC to ALLOC_CUPY
Oct 26, 2022
ddb5991
dsl: Update CupyAllocator's mem_free_args as a tuple, allowing remova…
Oct 27, 2022
d337ac8
misc: Fix indentation and comments
Oct 27, 2022
6511b06
dsl: Update free method inside CupyAllocator
Nov 4, 2022
ce12f56
tests: Add test to unified memory allocator
Feb 7, 2023
3ce03ba
dsl: Add conditional import for Cupy module
Mar 8, 2023
f4231e2
test: Update tests adding a class responsible for test external and …
Mar 8, 2023
c4444a1
dsl: Changing import cupy from init() to initialize()
Apr 13, 2023
f3f90c1
dsl: Update to fix the problem when ALLOC_CUPY tries to alloc data w…
Apr 24, 2023
41838ae
dsl: Update CupyAllocator to run at multiples nodes using MPI
May 11, 2023
241e444
dsl: Fix CupyAllocator to properly support MPI execution.
May 17, 2023
e724ffb
misc: Fix indentation
May 17, 2023
9379b31
misc: Removes unwanted leftover comments.
Mar 8, 2024
7814a46
dsl: Update the way MPI is imported at CupyAllocator
Mar 8, 2024
6df7a06
misc: Add explanatory comment
Mar 8, 2024
76dcdb1
dsl: Update "except" to "except ImportError". Other errors should be …
Mar 8, 2024
6ad6611
tests: Update memory allocator test to use skipif('nodevice')
Mar 8, 2024
92ba35c
dsl: Update of the way data type allocation is defined
Mar 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
misc: Fix indentation
Gustavo Coelho committed May 17, 2023
commit e724ffb5e74506ea12e6514a3cdd7f86ce748df4
10 changes: 5 additions & 5 deletions devito/data/allocators.py
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ def alloc(self, shape, dtype):
buf = ctypes.cast(c_pointer, ctypes.POINTER(ctype_1d)).contents
pointer = np.frombuffer(buf, dtype=dtype)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we end up here? the c_pointer is None case is already above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During the execution in MPI, domain splitting can generate a situation where the allocated data size is zero, as we have observed with Sparse Functions. When this occurs, Cupy returns a pointer with a value of zero. This conditional statement was defined for this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment noting this, until some better solution is around?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will push it, George.

pointer = np.empty(shape = (0), dtype=dtype)
pointer = np.empty(shape=(0), dtype=dtype)
# pointer.reshape should not be used here because it may introduce a copy
# From https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html:
# It is not always possible to change the shape of an array without copying the
@@ -343,12 +343,12 @@ def initialize(cls):
try:
from mpi4py import MPI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reimport and not import it from devito.mpi?

cls.MPI = MPI
cls._set_device_for_mpi()
cls._set_device_for_mpi()
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except ImportError other errors should be caught

cls.MPI = None
except:
cls.lib = None

@classmethod
def _initialize_shared_memory(cls):
cls._mempool = cls.lib.cuda.MemoryPool(cls.lib.cuda.malloc_managed)
@@ -358,8 +358,8 @@ def _initialize_shared_memory(cls):
def _set_device_for_mpi(cls):
if cls.MPI.Is_initialized():
n_gpu = cls.lib.cuda.runtime.getDeviceCount()
rank_local = cls.MPI.COMM_WORLD.Split_type(cls.MPI.COMM_TYPE_SHARED).Get_rank()
cls.lib.cuda.runtime.setDevice(rank_local % n_gpu)
rank_l = cls.MPI.COMM_WORLD.Split_type(cls.MPI.COMM_TYPE_SHARED).Get_rank()
cls.lib.cuda.runtime.setDevice(rank_l % n_gpu)

def _alloc_C_libcall(self, size, ctype):
if not self.available():
9 changes: 5 additions & 4 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
import cupy as cp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to be added somehow to the test requirements and this step should be decoratred with a skipif(device)


from devito import (Grid, Function, TimeFunction, SparseTimeFunction, Dimension, # noqa
Eq, Operator, ALLOC_GUARD, ALLOC_FLAT, ALLOC_CUPY, configuration, switchconfig)
Eq, Operator, ALLOC_GUARD, ALLOC_FLAT, ALLOC_CUPY,
configuration, switchconfig)
from devito.data import LEFT, RIGHT, Decomposition, loc_data_idx, convert_index
from devito.tools import as_tuple
from devito.types import Scalar
@@ -1483,13 +1484,13 @@ def test_uma_allocation(self):
nt = 5
grid = Grid(shape=(4, 4, 4))

u = Function(name='u', grid=grid, allocator=ALLOC_CUPY )
u = Function(name='u', grid=grid, allocator=ALLOC_CUPY)
u.data[:] = 5
address = u.data.ctypes.data
pointerAttr = cp.cuda.runtime.pointerGetAttributes(address)
assert pointerAttr.devicePointer == pointerAttr.hostPointer

v = TimeFunction(name='v', grid=grid, save=nt, allocator=ALLOC_CUPY )
v = TimeFunction(name='v', grid=grid, save=nt, allocator=ALLOC_CUPY)
v.data[:] = 5
address = v.data.ctypes.data
pointerAttr = cp.cuda.runtime.pointerGetAttributes(address)
@@ -1501,7 +1502,7 @@ def test_external_allocator(self):
numpy_array = np.ones(shape, dtype=np.float32)
g = Grid(shape)
f = Function(name='f', space_order=space_order, grid=g,
allocator=ExternalAllocator(numpy_array), initializer=lambda x: None)
allocator=ExternalAllocator(numpy_array), initializer=lambda x: None)

# Ensure the two arrays have the same value
assert(np.array_equal(f.data, numpy_array))