-
Notifications
You must be signed in to change notification settings - Fork 370
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
parallel_rng_types: refactoring and "objectification" #482
Conversation
f27f2c4
to
c63a99c
Compare
some observations which may be important to reduce the usage of pointers in CP2K:
Example for the problem of passing non-pointer objects to functions expecting pointers or allocatables: module mymod
type old_type
integer :: val
end type old_type
type new_type
integer :: val
contains
procedure, pass(self) :: print_me => print_new_type
end type new_type
contains
subroutine print_new_type(self)
class(new_type) :: self
print *, self%val
end subroutine
subroutine print_old_type(type_instance)
type(old_type) :: type_instance
print *, type_instance%val
end subroutine
subroutine print_old_type_by_ptr(type_instance)
type(old_type), pointer :: type_instance
print *, type_instance%val
end subroutine
subroutine print_old_type_by_alloc(type_instance)
type(old_type), allocatable :: type_instance
print *, type_instance%val
end subroutine
end module mymod
program main
use mymod
type(old_type) :: ot
type(old_type), pointer :: ot_ptr
type(old_type), allocatable :: ot_alloc
ot%val = 10
allocate (ot_ptr, ot_alloc)
ot_ptr%val = 11
ot_alloc%val = 12
call print_old_type(ot)
call print_old_type(ot_ptr) ! can pass a pointer to a procedure not expecting a pointer
call print_old_type(ot_alloc) ! can pass an allocatable to a procedure not expecting a allocatable
call print_old_type_by_ptr(ot_ptr)
!call print_old_type_by_ptr(ot) ! DOES NOT WORK, MUST BE A POINTER
!call print_old_type_by_ptr(ot_alloc) ! DOES NOT WORK, MUST BE A POINTER
call print_old_type_by_alloc(ot_alloc)
!call print_old_type_by_alloc(ot) ! DOES NOT WORK, MUST BE AN ALLOCATABLE
!call print_old_type_by_alloc(ot_ptr) ! DOES NOT WORK, MUST BE AN ALLOCATABLE
deallocate (ot_ptr)
end program main Some observations wrt our prettifier:
... wrt conventions checker:
|
c63a99c
to
2322df5
Compare
@juerghutter this PR and my remarks above are essentially a follow-up on our discussion about our usage of pointers and possibly moving to allocatables (or plain objects). The conversion of |
Yes, the pointer attribute should be removed from pretty much all dummy arguments. The good news is that we can work our way through the "foodchain" one type at a time. Obviously, we have to remove a type's reference counting first, but that shouldn't be too difficulty. Our objects hardly ever change ownership, ie. all those pointers are just weak references. I also agree that the types that actually own an object should have it as plain object or allocatable. In the past we always ran into compiler issues (e.g. 1284d6c and f1f51e8), but we just have to keep trying. |
From what I've seen one must be careful about the the reference counting removal. And I think it is not even necessary to start there: first remove the pointer attribute on the non-allocation-related methods of a type, then you can remove the pointer on many of the functions which get a pointer to that type just because they're using its methods. The only thing I haven't thought about yet is how to handle the case where you're doing a type :: foo
end type
type :: bar
type(foo), pointer :: my_bar
end type
type(bar), pointer, intent(in) :: b
type(foo), pointer :: f
bar_get(b, foo=f)
foo_do_something(f, 1, 2, 3) This pattern will be difficult with non-pointers.
Thanks for the references. 2016 isn't too long ago :-( |
64141ec
to
bf0079b
Compare
this PR now only depends on |
00c9594
to
40d408d
Compare
d823162
to
b30f570
Compare
the following is valid Fortran: mystr = "abc& &def" and is equivalent to: mystr = "abcdef" but the current version of the `doxify.sh` script strips it since `fixcomments.pl` assumed that all ampersands are line continuations.
0e5df13
to
cde5872
Compare
Fortran does pass-by-reference, but `POINTER` was needed for the procedures acting on the `rng_stream_type`. With those refactored as type-bound procedures, almost all uses of pointers can be avoided. One of the two remaining cases (in `helium_methods.F`) could likely be avoided as well by using an allocatable and `move_alloc`, but this would need another nested type like `rng_stream_p_type` but with an allocatable member instead of a pointer (or a complete redesign of the RNG setup in that part).
cde5872
to
1e6d217
Compare
This
rng_stream_type
is one of the most low-level/basic objects I found. To see how a complete OO approach could look like, I converted it to Modern Fortran.Points to discuss:
init(...)
instead.final
routine..._TEST
program to test this part without most of the other infrastructureTo build and test:
There is not much here yet, except for the syntactic sugar. More to come when adapting the code to the changes and when going up the "foodchain" (tackling the globenv).