-
Notifications
You must be signed in to change notification settings - Fork 229
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
0befd4e
db87362
ef1f368
ca806b3
50cd534
539254c
ddb5991
d337ac8
6511b06
ce12f56
3ce03ba
f4231e2
c4444a1
f3f90c1
41838ae
241e444
e724ffb
9379b31
7814a46
6df7a06
76dcdb1
6ad6611
92ba35c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import os | ||
import sys | ||
|
||
import cupy as cp | ||
import numpy as np | ||
import ctypes | ||
from ctypes.util import find_library | ||
|
@@ -15,7 +16,7 @@ | |
|
||
__all__ = ['ALLOC_FLAT', 'ALLOC_NUMA_LOCAL', 'ALLOC_NUMA_ANY', | ||
'ALLOC_KNL_MCDRAM', 'ALLOC_KNL_DRAM', 'ALLOC_GUARD', | ||
'default_allocator'] | ||
'CUPY_ALLOC', 'default_allocator'] | ||
|
||
|
||
class MemoryAllocator(object): | ||
|
@@ -317,6 +318,32 @@ def put_local(self): | |
return self._node == 'local' | ||
|
||
|
||
class CupyAllocator(MemoryAllocator): | ||
|
||
""" | ||
Memory allocator based on ``posix`` functions. The allocated memory is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copy-paste docstring |
||
aligned to page boundaries. | ||
""" | ||
|
||
is_Posix = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leftover..... |
||
|
||
def __init__(self): | ||
cp.cuda.set_allocator(cp.cuda.MemoryPool(cp.cuda.malloc_managed).malloc) | ||
|
||
@classmethod | ||
def initialize(cls): | ||
pass | ||
|
||
|
||
def _alloc_C_libcall(self, size, ctype): | ||
|
||
mem_obj = cp.zeros(size, dtype=cp.float64) | ||
return mem_obj.data.ptr, mem_obj | ||
|
||
def free(self, c_pointer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CuPy frees the memory right? unless we explicitly tell it at some point? |
||
pass | ||
|
||
|
||
class ExternalAllocator(MemoryAllocator): | ||
|
||
""" | ||
|
@@ -373,6 +400,7 @@ def alloc(self, shape, dtype): | |
ALLOC_KNL_MCDRAM = NumaAllocator(1) | ||
ALLOC_NUMA_ANY = NumaAllocator('any') | ||
ALLOC_NUMA_LOCAL = NumaAllocator('local') | ||
CUPY_ALLOC = CupyAllocator() | ||
|
||
custom_allocators = {} | ||
"""User-defined allocators.""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
import numpy as np | ||
|
||
from devito.data.allocators import ALLOC_FLAT | ||
from devito.data.allocators import ALLOC_FLAT, CUPY_ALLOC | ||
from devito.data.utils import * | ||
from devito.logger import warning | ||
from devito.parameters import configuration | ||
|
@@ -82,7 +82,8 @@ def __del__(self): | |
# Dask/Distributed context), which may (re)create a Data object | ||
# without going through `__array_finalize__` | ||
return | ||
self._allocator.free(*self._memfree_args) | ||
if self._allocator is not CUPY_ALLOC: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, you should override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in fact, |
||
self._allocator.free(*self._memfree_args) | ||
self._memfree_args = None | ||
|
||
def __reduce__(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
from devito.tools import as_mapper, as_list, as_tuple, filter_sorted, flatten | ||
from devito.types import DeviceRM, Symbol | ||
|
||
from devito.data.allocators import CUPY_ALLOC | ||
|
||
__all__ = ['DataManager', 'DeviceAwareDataManager', 'Storage'] | ||
|
||
|
||
|
@@ -435,6 +437,9 @@ def _map_function_on_high_bw_mem(self, site, obj, storage, devicerm, read_only=F | |
""" | ||
mmap = self.lang._map_to(obj) | ||
|
||
if obj._allocator is CUPY_ALLOC: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this breaks the abstraction in all sort of ways, unfortunately You can't access the allocator here to steer compilation what really matters at this point is the you shouldn't actually end up here, because GPU-allocated functions shuold have a local mem-space, which in turns naturally prevents them ever enter this point |
||
return | ||
|
||
if read_only is False: | ||
unmap = [self.lang._map_update(obj), | ||
self.lang._map_release(obj, devicerm=devicerm)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ALLOC_CUPY for homogeneity