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

support CUDA async memory resource in JNI #9201

Merged
merged 5 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
review feedback
  • Loading branch information
rongou committed Sep 9, 2021
commit 75ac47b8ab58d1ba2d38767dddfd4c2035287c2c
6 changes: 3 additions & 3 deletions java/src/main/java/ai/rapids/cudf/Rmm.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ public static synchronized void initialize(int allocationMode, LogConf logConf,
throw new IllegalStateException("RMM is already initialized");
}
if (maxPoolSize > 0) {
if (allocationMode != RmmAllocationMode.POOL &&
allocationMode != RmmAllocationMode.ARENA &&
allocationMode != RmmAllocationMode.CUDA_ASYNC) {
if ((allocationMode & RmmAllocationMode.POOL) == 0 &&
(allocationMode & RmmAllocationMode.ARENA) == 0 &&
(allocationMode & RmmAllocationMode.CUDA_ASYNC) == 0) {
throw new IllegalArgumentException(
"Pool limit only supported in POOL or ARENA or CUDA_ASYNC allocation mode");
}
Expand Down
2 changes: 1 addition & 1 deletion java/src/main/java/ai/rapids/cudf/RmmAllocationMode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions java/src/main/native/src/RmmJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ JNIEXPORT void JNICALL Java_ai_rapids_cudf_Rmm_initializeInternal(
} else if (use_cuda_async_alloc) {
auto pool_limit = (max_pool_size > 0) ? thrust::optional<std::size_t>{max_pool_size} :
thrust::optional<std::size_t>{};
Initialized_resource = std::make_shared<rmm::mr::cuda_async_memory_resource>(
thrust::optional<std::size_t>{pool_size}, pool_limit);
Initialized_resource =
std::make_shared<rmm::mr::cuda_async_memory_resource>(pool_size, pool_limit);
abellina marked this conversation as resolved.
Show resolved Hide resolved
} else if (use_managed_mem) {
Initialized_resource = std::make_shared<rmm::mr::managed_memory_resource>();
} else {
Expand Down
47 changes: 38 additions & 9 deletions java/src/test/java/ai/rapids/cudf/RmmTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

public class RmmTest {
private static final long TOO_MUCH_MEMORY = 3L * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
Expand All @@ -51,18 +52,24 @@ public void teardown() {
}

@ParameterizedTest
@ValueSource(ints = {RmmAllocationMode.CUDA_DEFAULT, RmmAllocationMode.POOL})
@ValueSource(ints = {
RmmAllocationMode.CUDA_DEFAULT,
RmmAllocationMode.POOL,
RmmAllocationMode.ARENA})
public void testTotalAllocated(int rmmAllocMode) {
Rmm.initialize(rmmAllocMode, false, 512 * 1024 * 1024);
assertEquals(0, Rmm.getTotalBytesAllocated());
try (DeviceMemoryBuffer addr = Rmm.alloc(1024)) {
try (DeviceMemoryBuffer ignored = Rmm.alloc(1024)) {
assertEquals(1024, Rmm.getTotalBytesAllocated());
}
assertEquals(0, Rmm.getTotalBytesAllocated());
}

@ParameterizedTest
@ValueSource(ints = {RmmAllocationMode.CUDA_DEFAULT, RmmAllocationMode.POOL})
@ValueSource(ints = {
RmmAllocationMode.CUDA_DEFAULT,
RmmAllocationMode.POOL,
RmmAllocationMode.ARENA})
public void testEventHandler(int rmmAllocMode) {
AtomicInteger invokedCount = new AtomicInteger();
AtomicLong amountRequested = new AtomicLong();
Expand Down Expand Up @@ -328,7 +335,7 @@ public void onDeallocThreshold(long totalAllocSize) {

Rmm.setEventHandler(handler);
DeviceMemoryBuffer addr = Rmm.alloc(6 * 1024);
assertThrows(DeallocThresholdException.class, () -> addr.close());
assertThrows(DeallocThresholdException.class, addr::close);
assertThrows(AllocThresholdException.class, () -> Rmm.alloc(12 * 1024));
assertThrows(AllocFailException.class, () -> Rmm.alloc(TOO_MUCH_MEMORY));
}
Expand Down Expand Up @@ -356,7 +363,10 @@ public void testThreadAutoDeviceSetup() throws Exception {
}

@ParameterizedTest
@ValueSource(ints = {RmmAllocationMode.CUDA_DEFAULT, RmmAllocationMode.POOL})
@ValueSource(ints = {
RmmAllocationMode.CUDA_DEFAULT,
RmmAllocationMode.POOL,
RmmAllocationMode.ARENA})
public void testSetDeviceThrowsAfterRmmInit(int rmmAllocMode) {
Rmm.initialize(rmmAllocMode, false, 1024 * 1024);
assertThrows(CudfException.class, () -> Cuda.setDevice(Cuda.getDevice() + 1));
Expand Down Expand Up @@ -399,9 +409,28 @@ public void testPoolLimitNonPoolMode() {
() -> Rmm.initialize(RmmAllocationMode.CUDA_DEFAULT, false, 1024, 2048));
}

private static class AllocFailException extends RuntimeException {}
private static class AllocThresholdException extends RuntimeException {}
private static class DeallocThresholdException extends RuntimeException {}
@Test
public void testCudaAsyncMemoryResource() {
try {
Rmm.initialize(RmmAllocationMode.CUDA_ASYNC, false, 1024 * 1024L, 1024 * 1024L);
abellina marked this conversation as resolved.
Show resolved Hide resolved
} catch (CudfException e) {
assumeFalse(e.getMessage().contains("cudaMallocAsync not supported"));
throw e;
}
DeviceMemoryBuffer buff = Rmm.alloc(1024);
buff.close();
buff = Rmm.alloc(2048);
buff.close();
}

private static class AllocFailException extends RuntimeException {
}

private static class AllocThresholdException extends RuntimeException {
}

private static class DeallocThresholdException extends RuntimeException {
}

private static abstract class BaseRmmEventHandler implements RmmEventHandler {
@Override
Expand Down