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
Show file tree
Hide file tree
Changes from 4 commits
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
30 changes: 29 additions & 1 deletion devito/data/allocators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys

import cupy as cp
import numpy as np
import ctypes
from ctypes.util import find_library
Expand All @@ -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']
Copy link
Contributor

Choose a reason for hiding this comment

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

ALLOC_CUPY for homogeneity



class MemoryAllocator(object):
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

copy-paste docstring

aligned to page boundaries.
"""

is_Posix = True
Copy link
Contributor

Choose a reason for hiding this comment

The 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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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):

"""
Expand Down Expand Up @@ -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."""
Expand Down
5 changes: 3 additions & 2 deletions devito/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

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

No, you should override free instead

Copy link
Contributor

Choose a reason for hiding this comment

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

in fact, free is a no-op, so why the need to special-case here?

self._allocator.free(*self._memfree_args)
self._memfree_args = None

def __reduce__(self):
Expand Down
5 changes: 5 additions & 0 deletions devito/passes/iet/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']


Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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 _mem_spaceof the object: https://github.com/devitocodes/devito/blob/master/devito/types/basic.py#L39

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)]
Expand Down