Skip to content

[REVIEW] Memory resource design - #96

Merged
harrism merged 18 commits into
rapidsai:branch-0.9from
jrhemstad:fea-ext-memory-resource
Jul 3, 2019
Merged

harrism merged 18 commits into
rapidsai:branch-0.9from
jrhemstad:fea-ext-memory-resource

Conversation

@jrhemstad

@jrhemstad jrhemstad commented Jun 17, 2019 •

Copy link
Copy Markdown
Contributor

This is a precursor to the cudf::column redesign: NVIDIA/cudf#1443

Based on std::pmr::memory_resource and thrust::mr::memory_resource.

Key differences:

  • Doesn't have an alignment argument. It will be assumed that all implementations of device_memory_resource will provide at least 256B alignment.
  • Adds a stream argument for optional allocation/deallocation within a CUDA stream (e.g., CNMEM supports streams in alloc/free)
  • Adds a supports_streams pure virtual function to query if the resource supports streams

In short, the device_memory_resource is a base class that has two public functions: allocate and deallocate. These functions call corresponding private, pure virtual functions do_allocate and do_deallocate.

To define a custom memory resource, all that need be done is define a new class, derive from device_memory_resource, and implement do_allocate and do_deallocate to perform whatever custom behavior is desired. To see an example, look at how I've implemented cuda_memory_resource, which just uses cudaMalloc and cudaFree for alloc/free.

Example usage:

class my_custom_resource : public device_memory_resource { 

private:

void * do_allocate(size_t ptr, cudaStream_t stream) override { ... };

void do_deallocate(void * ptr, cudaStream_t stream) override {...};
};

device_memory_resource * base_resource_ptr = new my_custom_resource();

// `output` will be allocated using the custom memory resource
column output = cudf::some_function(..., base_resource_ptr);

By using runtime polymorphism via virtual functions in device_memory_resource, this design allows us to keep APIs template-free while still providing the option to the user to customize memory allocation behavior.

This PR adds 3 memory resources based on existing RMM functionality:

  1. cuda_memory_resource - allocates with cudaMalloc/cudaFree
  2. managed_memory_resource - uses managed memory
  3. cnmem_memory_resource - uses CNMEM pool suballocator
    TODO: Add a cnmem_managed_resource for a CNMEM pool allocated w/ managed memory

It also adds a default_memory_resource that can be used when a memory resource is not explicitly specified. Initially, the default memory resource is set to a cuda_memory_resource.

Tests were added and expanded based on existing RMM tests.

  • Add a thrust_allocator_adaptor to enable using a rmm::memory_resource for Thrust allocation.

Future tasks:

  • Put RMM's logging functionality in the device_memory_resource base class (or create a logging_device_memory_resource base class)
  • Refactor existing RMM initialization logic to simply set the default memory resource accordingly
  • Refactor existing functions rmm::alloc and rmm::free to simply forward to calling the default memory resource
  • In time, I think the rmm::alloc and rmm::free calls should be deprecated in favor of using the memory resources directly

@jrhemstad
jrhemstad requested a review from harrism June 17, 2019 01:44
@jrhemstad
jrhemstad force-pushed the fea-ext-memory-resource branch from 5b82703 to aa1cf79 Compare June 17, 2019 15:10
@jrhemstad

Copy link
Copy Markdown
Contributor Author

rerun tests

@jirikraus jirikraus left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good to me. I think it would be helpful to add examples how to use this with thrust and stl containers.

/**---------------------------------------------------------------------------*
* @brief Allocates memory of size at least \p bytes.
*
* The returned pointer will have at minimum 256 byte alignment.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe add an assert checking for this alignment on the pointer returned by do_allocate?

@harrism

harrism commented Jun 19, 2019

Copy link
Copy Markdown
Member

@jrhemstad Should this move to branch-0.9?

@jrhemstad

Copy link
Copy Markdown
Contributor Author

@jrhemstad Should this move to branch-0.9?

Sure, unless you want to try and review and merge for 0.8. Doesn't affect me either way.

@harrism

harrism commented Jun 19, 2019

Copy link
Copy Markdown
Member

I'll leave it here until end of burndown in case I get time to review.

@harrism
harrism changed the base branch from branch-0.8 to branch-0.9 June 21, 2019 03:59
@harrism harrism added the 3 - Ready for review Ready for review by team label Jun 21, 2019

@harrism harrism left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall excellent. Main concern is having to provide a size in order to deallocate -- definitely non-standard, right?

Also one about naming cnmem_memory_resource and the right level of abstraction.

Some questions and documentation requests.

Comment thread src/mr/default_memory_resource.cpp Resolved
Comment thread src/mr/default_memory_resource.cpp Resolved
Comment thread tests/mr/mr_tests.cpp Resolved
Comment thread tests/mr/thrust_allocator_tests.cpp Resolved
@harrism
harrism merged commit bf26308 into rapidsai:branch-0.9 Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 - Ready for review Ready for review by team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants