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

Feature/add freeOnDevice() method #298

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

XL64
Copy link
Contributor

@XL64 XL64 commented Jul 24, 2023

Add freeOnDevice() method to allow freeing memory from GPU before the object destruction.
This method is useful to remove temporary working data from GPU space, freeing GPU memory.
Add logger using callback for free/allocate when activated.

/**
* Move data to host if not valid and register host as the last valid space and free data on device.
*/
void freeOnDevice() const
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be in Array not ArrayView.

Copy link

@sframba sframba Aug 29, 2023

Choose a reason for hiding this comment

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

Hi @corbett5 , I think this should be available from ArrayView, for two reasons:

  • All the device memory manipulations (move, registerTouch, ...) are located in ArrayView. They do not modify the underlying array data, only its availability on device;
  • from the application side, sometime we only have an ArrayView, but we still want to be able to free memory on device. This is our use case in GEOS: we use the (read-only for us) maps, but we wish to free them from GPU after use

Could we instead keep this in ArrayView, but overload the move method to allow (optionally) freeing the device memory when moving data to host?
What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

free can change the array in ways that move and registerTouch cannot. Specifically if the array only exists in the memory space we are freeing it from. And as a counter point all the explicitly allocating functions live in Array, and it makes sense to me that the deallocating functions should live there too.

I also think that adding free to ArrayView would add more ways you could shoot yourself in the foot, since the parent Array would have to do some kind of "update" to know that the data was free'd.

My suggestion would be to re-think how you're planning on doing it in GEOS. Either pass an Array to the functions where you want to free the data on the GPU or just free the data after calling the functions.

TLDR: I think your suggestion makes more sense if the functionality was "free the array on device but only if it also has a copy on host". But that seems super specific and I would much rather have general "free in the provided memory space".

Copy link

Choose a reason for hiding this comment

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

Thanks for the feedback, I'll try to move it to Array as suggested then. I'll see if we can use in this way from GEOS. Thanks !

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry that this will require some changes in GEOS, but if you have any trouble there feel free to let me know.

Copy link
Member

Choose a reason for hiding this comment

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

@corbett5 I think a freeInSpace(memorySpace) is what they need here. Perhaps where such a function should live is a deeper design question. The allocation of the device memory space (or any space other than the original allocation) via a RAJA capture is done using an ArrayView. Thus an ArrayView has provided the interface to perform the allocation.

Should the ArrayView be allowed to provide an interface function to deallocate the allocation it triggered? If not, then the user has to get a reference to the actual Array, create an ArrayView, capture the ArrayView, then deallocate using the Array. I think we might shield access to the underlying Array in many cases so this could be problematic. (i.e. we get an ArrayView from the repo, not an Array)

Alternatively, we can just make a hard copy of a host array on device, and delete that copy after use, but that seems like the wrong way to go.

Copy link
Contributor

@wrtobin wrtobin Mar 28, 2024

Choose a reason for hiding this comment

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

Per discussions in the meeting today, we're thinking instead of labeling this functionality as a free and moving into Array, it should instead be thought of as a reduction of the valid set of memory spaces the ArrayView can be moved to / accessed from, and any potential free resulting from that space reduction is a byproduct of the reduction. Additionally, one should not be allowed to remove the default memory space, and one should not be able to dynamically add back a removed memory space, instead requiring the user to create/copy into a new array with the correct memory spaces.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alternatively, we can just make a hard copy of a host array on device, and delete that copy after use, but that seems like the wrong way to go.

@rrsettgast if you know for a fact that the existing array does not exist on device and you just want to move it there, do some computation and then free the device memory using this pattern of copying to a new device-only array seems fairly natural to me. I forget what the specific use case is here, but if you're working in a CPU only environment then this pattern would be more wasteful (allocate a copy on the CPU, then free it) than just accessing the existing array and a no-op freeOnDevice.

Additionally, one should not be allowed to remove the default memory space, and one should not be able to dynamically add back a removed memory space, instead requiring the user to create/copy into a new array with the correct memory spaces.

@wrtobin I'm not sure what you mean by "one should not be able to dynamically add back a removed memory space". Do you mean that if I free a view on device that operation forbids the view from being automatically moved to device next time it is captured?

Whatever the case may be, y'all have put much more thought into this than I have so I'm fine doing whatever you think works best.

LVARRAY_LOG( "Allocated " << paddedSize << " to the " << spaceStr << ": " << typeString << " " << name << " Free memory on device: " << size2 );
}

if( act == chai::ACTION_FREE )
Copy link
Collaborator

Choose a reason for hiding this comment

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

I definitely like the added message here.

Copy link
Collaborator

@corbett5 corbett5 left a comment

Choose a reason for hiding this comment

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

Great, we definitely need this functionality! My thought though is that this new functionality should be accommodated by adding a space parameter to (or overload of) bufferManipulation::free.

void free( BUFFER & buf, std::ptrdiff_t const size )

Then Array should have a method free( MemorySpace space ) or something like that.

@sframba sframba self-assigned this Aug 30, 2023
@rrsettgast rrsettgast requested a review from wrtobin April 25, 2024 14:43
@rrsettgast
Copy link
Member

@wrtobin Are you ok with this in its current form?

@wrtobin
Copy link
Contributor

wrtobin commented Apr 25, 2024

@corbett5 Randy and I discussed that particular point and IIRC we decided it wasn't a necessary restriction. My concern was primarily preventing users from erroneously de- and re-allocating on device and losing performance as a result.

@sframba sframba assigned jiemeng-total and unassigned sframba May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants