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

Add pool memory resource to libcudf basic example #11966

Merged
merged 7 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cpp/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ libcudf examples.

Current examples:

- Basic: example that demonstrates basic use case with libcudf and building a custom application with libcudf.
- Basic: demonstrates a basic use case with libcudf and building a custom application with libcudf
- Strings: demonstrates using libcudf for accessing and creating strings columns and for building custom kernels for strings
2 changes: 1 addition & 1 deletion cpp/examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cmake -S . -B build/
# Build
cmake --build build/ --parallel $PARALLEL_LEVEL
# Execute
build/libcudf_example
build/basic_example
```

If your machine does not come with a pre-built libcudf binary, expect the
Expand Down
19 changes: 19 additions & 0 deletions cpp/examples/basic/src/process_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <cudf/io/csv.hpp>
#include <cudf/table/table.hpp>

#include <rmm/mr/device/cuda_memory_resource.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>
#include <rmm/mr/device/pool_memory_resource.hpp>

#include <memory>
#include <string>
#include <utility>
Expand Down Expand Up @@ -72,6 +76,21 @@ std::unique_ptr<cudf::table> average_closing_price(cudf::table_view stock_info_t

int main(int argc, char** argv)
{
// Construct a CUDA memory resource using RAPIDS Memory Manager (RMM)
// This is the default memory resource for libcudf for allocating device memory.
rmm::mr::cuda_memory_resource cuda_mr{};
// Construct a memory pool using the CUDA memory resource
// Using a memory pool for device memory allocations is important for good performance in libcudf.
// The pool defaults to allocating half of the available GPU memory.
rmm::mr::pool_memory_resource mr{&cuda_mr};

// Set the pool resource to be used by default for all device memory allocations
// Note: It is the user's responsibility to ensure the `mr` object stays alive for the duration of
// it being set as the default
// Also, call this before the first libcudf API call to ensure all data is allocated by the same
// memory resource.
rmm::mr::set_current_device_resource(&mr);

// Read data
auto stock_table_with_metadata = read_csv("4stock_5day.csv");

Expand Down
37 changes: 37 additions & 0 deletions cpp/examples/strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# libcudf C++ examples using strings columns

This C++ example demonstrates using libcudf APIs to access and create
strings columns.

The example source code loads a csv file and produces a redacted strings
column from the names column using the values from the visibilities column.

Four examples are included:
1. Using libcudf APIs to build the output
2. Using a simple custom kernel with dynamic memory
3. Using a custom kernel with pre-allocated device memory
4. Using a two-pass approach to improve performance

These examples are described in more detail in
https://developer.nvidia.com/blog/mastering-string-transformations-in-rapids-libcudf/

## Compile and execute

```bash
# Configure project
cmake -S . -B build/
# Build
cmake --build build/ --parallel $PARALLEL_LEVEL
# Execute
build/libcudf_apis names.csv
--OR--
build/custom_with_malloc names.csv
--OR--
build/custom_prealloc names.csv
--OR--
build/custom_optimized names.csv
```

If your machine does not come with a pre-built libcudf binary, expect the
first build to take some time, as it would build libcudf on the host machine.
It may be sped up by configuring the proper `PARALLEL_LEVEL` number.