forked from devitocodes/devito
-
Notifications
You must be signed in to change notification settings - Fork 0
PETSc Memory Allocator #69
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
Draft
ZoeLeibowitz
wants to merge
9
commits into
master
Choose a base branch
from
memory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f3bc8b
some progress
ZoeLeibowitz 076405a
add example
ZoeLeibowitz e017d1c
no malloc_debug errors now but need to inspect dag size assertion whe…
ZoeLeibowitz 273da13
fcqw
ZoeLeibowitz 474b8ac
fix dag assertion problem
ZoeLeibowitz 3f3539a
switch to place array
ZoeLeibowitz be17616
switch to use placearray
ZoeLeibowitz f670170
dcaec
ZoeLeibowitz ce4ddd7
fwer
ZoeLeibowitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import mmap | ||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
@@ -13,7 +14,7 @@ | |
|
|
||
| __all__ = ['ALLOC_ALIGNED', 'ALLOC_NUMA_LOCAL', 'ALLOC_NUMA_ANY', | ||
| 'ALLOC_KNL_MCDRAM', 'ALLOC_KNL_DRAM', 'ALLOC_GUARD', | ||
| 'default_allocator'] | ||
| 'ALLOC_PETSC', 'default_allocator'] | ||
|
|
||
|
|
||
| class AbstractMemoryAllocator: | ||
|
|
@@ -334,6 +335,31 @@ def put_local(self): | |
| return self._node == 'local' | ||
|
|
||
|
|
||
| class PetscMemoryAllocator(MemoryAllocator): | ||
| """ | ||
| """ | ||
| @classmethod | ||
| def initialize(cls): | ||
| from devito.petsc.utils import core_metadata | ||
| metadata = core_metadata() | ||
| lib_dir = Path(metadata['lib_dirs'][-1]) | ||
|
|
||
| try: | ||
| cls.lib = ctypes.CDLL(lib_dir/'libpetsc.so') | ||
| except OSError: | ||
| cls.lib = None | ||
|
|
||
| def _alloc_C_libcall(self, size, ctype): | ||
| if not self.available(): | ||
| raise RuntimeError("...") | ||
|
Collaborator
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. Missing error message? |
||
| c_bytesize = ctypes.c_ulong(size * ctypes.sizeof(ctype)) | ||
| c_pointer = ctypes.cast(ctypes.c_void_p(), ctypes.c_void_p) | ||
|
|
||
| from devito.petsc.memory import op_memory | ||
|
Collaborator
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. There are a couple of imports within functions here, which probably means things need moving around to prevent circular imports |
||
| op_memory(m=c_bytesize, result=ctypes.byref(c_pointer)) | ||
| return c_pointer, (c_pointer, ) | ||
|
|
||
|
|
||
| class DataReference(MemoryAllocator): | ||
|
|
||
| """ | ||
|
|
@@ -393,6 +419,7 @@ def alloc(self, shape, dtype, padding=0): | |
| ALLOC_KNL_MCDRAM = NumaAllocator(1) | ||
| ALLOC_NUMA_ANY = NumaAllocator('any') | ||
| ALLOC_NUMA_LOCAL = NumaAllocator('local') | ||
| ALLOC_PETSC = PetscMemoryAllocator() | ||
|
|
||
| custom_allocators = { | ||
| 'fallback': ALLOC_ALIGNED, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from devito import Operator, switchconfig | ||
| from devito.types import Symbol | ||
| from devito.types.equation import PetscEq | ||
| from devito.petsc.types import AllocateMemory | ||
|
|
||
|
|
||
| dummy = Symbol(name='d') | ||
|
|
||
| with switchconfig(language='petsc'): | ||
| op_memory = Operator( | ||
| [PetscEq(dummy, AllocateMemory(dummy))], | ||
| name='kernel_allocate_memory', opt='noop' | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import os | ||
| import numpy as np | ||
|
|
||
| from devito import (Grid, Function, Eq, Operator, configuration, | ||
| switchconfig) | ||
| from devito.petsc import PETScSolve | ||
| from devito.petsc.initialize import PetscInitialize | ||
| configuration['compiler'] = 'custom' | ||
| os.environ['CC'] = 'mpicc' | ||
|
|
||
| PetscInitialize() | ||
|
|
||
| nx = 81 | ||
| ny = 81 | ||
|
|
||
| grid = Grid(shape=(nx, ny), extent=(2., 2.), dtype=np.float64) | ||
|
|
||
| u = Function(name='u', grid=grid, dtype=np.float64, space_order=2) | ||
| v = Function(name='v', grid=grid, dtype=np.float64, space_order=2) | ||
|
|
||
| v.data[:] = 5.0 | ||
|
|
||
| eq = Eq(v, u.laplace, subdomain=grid.interior) | ||
|
|
||
| petsc = PETScSolve([eq], target=u) | ||
|
|
||
| with switchconfig(language='petsc'): | ||
| op = Operator(petsc) | ||
| op.apply() | ||
|
|
||
| print(op.ccode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this correct? Looks like division by a string unless I'm missing something
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.
lib_dir is a Path so maybe it overrides division to allow that