-
Notifications
You must be signed in to change notification settings - Fork 0
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
Core PETSc objects/functions. #3
Conversation
I rebased on top of Fabio’s branch ‘sycl-init’ hence all of the other file changes. Please review these files:
I also added the function ‘dtype_to_petsctype’ inside tools/dtypes_lowering.py Also, can someone help me understand why two of the pytests are failing? |
devito/types/petsc.py
Outdated
|
||
def __init_finalize__(self, *args, **kwargs): | ||
|
||
super(PETScFunction, self).__init_finalize__(*args, **kwargs) |
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.
Can just be super().__init_finalize__(*args, **kwargs)
devito/types/petsc.py
Outdated
|
||
class PETScFunction(AbstractFunction): | ||
""" | ||
PETScFunctions. |
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.
Might want a more fleshed out docstring in due course
self._is_const = kwargs.get('is_const', False) | ||
|
||
@classmethod | ||
def __dtype_setup__(cls, **kwargs): |
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.
cls
is never used and can be dropped from the args
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.
Unless this gets called in the super in which case it might need to stay
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.
it must stay
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.
why do u need to override this method?
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.
Otherwise the dtype
for each PETScFunction
is None
and I use the dtype
inside the new function dtype_to_petsctype
. I think this works for PETScFunctions
because the petsctype will actually depend on the dtype
of the object (e.g Function
) provided by the user.
devito/types/petsc.py
Outdated
def _C_ctype(self): | ||
petsc_type = dtype_to_petsctype(self.dtype) | ||
modifier = '*' * len(self.dimensions) | ||
customtype = CustomDtype(petsc_type, modifier=modifier) |
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.
You could just return this line
tests/test_petsc.py
Outdated
grid = Grid((2, 2)) | ||
x, y = grid.dimensions | ||
|
||
ptr0 = PETScFunction(name='ptr0', dtype=np.float32, dimensions=grid.dimensions, |
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.
Would it be worth checking all the datatypes here, so np.float64
, np.int64
etc?
f8f52f6
to
7e9a5cd
Compare
self._is_const = kwargs.get('is_const', False) | ||
|
||
@classmethod | ||
def __dtype_setup__(cls, **kwargs): |
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.
it must stay
devito/types/petsc.py
Outdated
return np.float32 | ||
|
||
@classmethod | ||
def __indices_setup__(cls, *args, **kwargs): |
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.
why do u need to override this method?
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.
Otherwise the object indices are set as empty tuples because I'm inheriting from AbstractFunction
.
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.
So we only want part of DiscreteFunction
but not all of it? WHich part of inheriting directly from DiscreteFunction
creates issues?
self._is_const = kwargs.get('is_const', False) | ||
|
||
@classmethod | ||
def __dtype_setup__(cls, **kwargs): |
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.
why do u need to override this method?
devito/types/petsc.py
Outdated
return tuple(dimensions), tuple(dimensions) | ||
|
||
@property | ||
def dimensions(self): |
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.
why do u need to override this property?
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.
Oh yes, this one should not be here
7e9a5cd
to
2222435
Compare
devito/tools/dtypes_lowering.py
Outdated
np.float32: 'PetscScalar', | ||
np.int64: 'PetscInt', | ||
np.float64: 'PetscScalar' | ||
}[dtype] |
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.
Petsc use the same type for single and double precision?
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.
I believe PetscInt just represents an integer. Whether it is 32-bit or 64-bit depends on how you configured PETSc.
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.
I meant the float64 vs float32
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.
They use the same type for single and double precision but its representation depends on how the code was configured. https://petsc.org/release/manualpages/Sys/PetscScalar/
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.
So you can't have both single and double precision in the same code?
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.
No, as far as I'm aware PETSc doesn't support mixed precision
c753526
to
92bf3bc
Compare
I can achieve what I need for
but if I override the
then creating the same |
it's hacky but because we might have a technical issue in devito, which you're relying upon. So I don't have a direct answer here... But I do have a question 😂 If you created:
The former to allow custom types, the latter to allow indexing; how simpler/messy would the implementation become? If I were to design the types hierarchy now, I would maybe devise a series of base classes, each of these base classes providing a specific set of functionalities. For example ( I would actually need different names...):
Or, alternatively (I guess I'm just thinking out aloud here), I would have a common base class that in theory can do anything, and then it's up to the subclasses to specifiy what is actually needed So this is not really answering your question, I know. The truth is that I don't have an answer here about how to do things properly, and this is just food for though. I would very welcome a re-design and or improvements that help achieving what you need Sorry if not helpful this time 😄 |
I have just spent some time trying to inherit from both classes but I was having various problems. Whilst I was debugging, I came across this: https://github.com/devitocodes/devito/blob/3126fb0e7960094360ccb1b21abe577e0804f017/devito/passes/iet/definitions.py#L392C43-L392C43 |
yes I think it makes sense we could also think of moving all of that LocalObject logic ( |
3b718b4
to
4db870e
Compare
devito/types/petsc.py
Outdated
dtype = CustomDtype('KSPConvergedReason') | ||
|
||
|
||
class PETScFunction(ArrayBasic): |
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.
PETScArray since it's and Array now
devito/passes/iet/definitions.py
Outdated
@@ -219,6 +219,14 @@ def _alloc_object_array_on_low_lat_mem(self, site, obj, storage): | |||
|
|||
storage.update(obj, site, allocs=decl) | |||
|
|||
def _alloc_petsc_array_on_low_lat_mem(self, site, obj, storage): |
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.
Looks exactly the same as _alloc_object_array_on_low_lat_mem
robably only needs
elif i.is_ObjectArray or i.is_PETScArray:
below
e6ce3be
to
47e92a9
Compare
devito/passes/iet/definitions.py
Outdated
@@ -219,6 +219,17 @@ def _alloc_object_array_on_low_lat_mem(self, site, obj, storage): | |||
|
|||
storage.update(obj, site, allocs=decl) | |||
|
|||
def _alloc_special_type_on_low_lat_mem(self, site, obj, storage): |
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.
That's still the exact same one as _alloc_object_array_on_low_lat_mem
why does it needs a separate one
devito/tools/dtypes_lowering.py
Outdated
np.float32: 'PetscScalar', | ||
np.int64: 'PetscInt', | ||
np.float64: 'PetscScalar' | ||
}[dtype] |
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.
So you can't have both single and double precision in the same code?
devito/types/array.py
Outdated
__rkwargs__ = AbstractFunction.__rkwargs__ + ('is_const',) | ||
|
||
def __init_finalize__(self, *args, **kwargs): | ||
|
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.
unneeded blank line
devito/types/petsc.py
Outdated
def __dtype_setup__(cls, **kwargs): | ||
return kwargs.get('dtype', np.float32) | ||
|
||
@property |
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.
could be a cached_property
|
||
@property | ||
def _C_ctype(self): | ||
petsc_type = dtype_to_petsctype(self.dtype) |
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.
nitpicking:
PETSc is so special that I would maybe put dtype_to_petsctype
in here rather than in tools/
…he else statement
3a7fb07
to
6f375f8
Compare
No description provided.