Questions about shallow and deep copy

Because PGI OpenACC doesn’t yet support deep copy for allocatable and pointer elements in derived type variables, we should do more work.

For example,

TYPE DERIVEDTYPE
REAL, POINTER :: PointerElement(:)
END TYPE

TYPE(DERIVEDTYPE) :: A
ALLOCATE(A%PointerElement(100))

!$ACC ENTER_DATA COPYIN(A)
!$ACC ENTER_DATA COPYIN(A%PointerElement)

I know this actually works, but my question is that how does this work?

!$ACC ENTER_DATA COPYIN(A)

When A is copied in by this line, A%PointerElement still targets some locations in host memory, which is invalid on the device. (This is shallow copy, if I understood correctly.)

!$ACC ENTER_DATA COPYIN(A%PointerElement)

By this line, the data that is being pointed by A%PointerElement is copied in to the device. (This is deep copy).

But, A%PointerElement still targets host memory. Data has been copied in to the device, but we didn’t do anything to the pointer.

How are the pointers linked with the data on the device?

I saw a sentence from starting guide book:

If the derived type is not present on the device when the allocatable array members are copied, the accesses to the allocatable members on the device will be invalid, because the hidden pointer and descriptor values in the derived type variable will not get updated.

Can you explain to me more about ‘hidden pointer’ and ‘descriptor value’? I suppose my question is related with these.

!$ACC ENTER_DATA COPYIN(A)

A shallow copy of “A” is copied to the device. Any pointers or allocatables will be the host address.

!$ACC ENTER_DATA COPYIN(A%PointerElement)

Here the PointerElement array is created on the device and then it’s elements and descriptor are copied over.

Next, PointerElement gets “attached” to A. “attached” meaning that it’s device address is set in the device copy of A. This is a PGI extension.

Order matters. If you copied in A after copying in A%PointerElement, then the “attach” wouldn’t occur.

I presented a talk at GTC2015 (https://www.youtube.com/watch?v=rWLmZt_u5u4) which in one segment discusses deep copy. Granted the focus is on C++ Classes but the same ideas apply here.

Does this help?

  • Mat

Thanks for the resource.

So your point is that compiler automatically handles “attaching” process without my command and I don’t have to worry about malfunctions, right?

So your point is that compiler automatically handles “attaching” process without my command

So long as you maintain the proper order.

and I don’t have to worry about malfunctions, right?

You shouldn’t but there’s always the possibility of bugs.

  • Mat