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

Non-static bounce buffer option #454

Merged
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
test_bounce_buffer_clear
  • Loading branch information
madsbk committed Sep 2, 2024
commit 7039bef7704f4d712d094c02e89ef6c86bd74f78
4 changes: 2 additions & 2 deletions cpp/include/kvikio/bounce_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ class AllocRetain {
*/
std::size_t _clear()
{
std::size_t num_allocs = 0;
std::size_t ret = _free_allocs.size() * _size;
while (!_free_allocs.empty()) {
CUDA_DRIVER_TRY(cudaAPI::instance().MemFreeHost(_free_allocs.top()));
_free_allocs.pop();
}
return num_allocs * _size;
return ret;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion python/kvikio/kvikio/_lib/buffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from kvikio._lib.arr cimport Array


cdef extern from "<kvikio/buffer.hpp>" namespace "kvikio" nogil:
cdef extern from "<kvikio/buffer.hpp>" nogil:
void cpp_memory_register "kvikio::memory_register"(const void* devPtr) except +
void cpp_memory_deregister "kvikio::memory_deregister"(const void* devPtr) except +

Expand All @@ -25,3 +25,11 @@ def memory_deregister(buf) -> None:
buf = Array(buf)
cdef Array arr = buf
cpp_memory_deregister(<void*>arr.ptr)


cdef extern from "<kvikio/bounce_buffer.hpp>" nogil:
size_t cpp_alloc_retain_clear "kvikio::AllocRetain::instance().clear"() except +


def bounce_buffer_clear() -> int:
return cpp_alloc_retain_clear()
4 changes: 4 additions & 0 deletions python/kvikio/kvikio/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ def memory_deregister(buf) -> None:
Device buffer to deregister .
"""
buffer.memory_deregister(buf)


def bounce_buffer_clear() -> int:
return buffer.bounce_buffer_clear()
16 changes: 16 additions & 0 deletions python/kvikio/tests/test_basic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import kvikio
import kvikio.buffer
import kvikio.defaults

cupy = pytest.importorskip("cupy")
Expand Down Expand Up @@ -273,3 +274,18 @@ def test_different_bounce_buffer_sizes(tmp_path, size, tasksize, buffer_size):
f.write(a)
assert f.read(b) == b.nbytes
cupy.testing.assert_array_equal(a, b)


def test_bounce_buffer_clear(tmp_path):
"""Test clearing the bounce buffer"""
filename = tmp_path / "test-file"
with kvikio.defaults.set_compat_mode(True), kvikio.defaults.set_num_threads(1):
with kvikio.CuFile(filename, "w") as f:
with kvikio.defaults.set_bounce_buffer_size(1024):
f.write(cupy.arange(10)) # populate the bounce buffer
assert kvikio.buffer.bounce_buffer_clear() == 1024
f.write(cupy.arange(10)) # populate the bounce buffer
with kvikio.defaults.set_bounce_buffer_size(2048):
with kvikio.CuFile(filename, "w") as f:
f.write(cupy.arange(10)) # populate the bounce buffer
assert kvikio.buffer.bounce_buffer_clear() == 2048
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Is this indentation right? I thought that the context manager on line 288 would reset the bounce buffer size to the default (16MiB) size, and so this clear would return 0 (because when we exited the block we had to clear the bounce buffers).

Copy link
Member Author

Choose a reason for hiding this comment

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

The bounce buffer isn't cleared on context exit, added the following comment:

            # Notice, since the bounce buffer size is only checked when the buffer
            # is used, we populate the bounce buffer in between we clear it.

Loading