Skip to content

Commit

Permalink
Address reviewer comment. Also minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcrimsontianyu committed Feb 10, 2025
1 parent 25d8850 commit 1691bd0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
35 changes: 34 additions & 1 deletion cpp/include/kvikio/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,43 @@ class PushAndPopContext {
std::tuple<void*, std::size_t, std::size_t> get_alloc_info(void const* devPtr,
CUcontext* ctx = nullptr);

/**
* @brief Create a shared state in a future object that is immediately ready.
*
* A partial implementation of the namesake function from the concurrency TS
* (https://en.cppreference.com/w/cpp/experimental/make_ready_future). The cases of
* std::reference_wrapper and void are not implemented.
*
* @tparam T Type of the value provided.
* @param t Object provided.
* @return A future holding a decayed copy of the object provided.
*/
template <typename T>
std::future<std::decay_t<T>> make_ready_future(T&& t)
{
std::promise<std::decay_t<T>> p;
auto fut = p.get_future();
p.set_value(std::forward<T>(t));
return fut;
}

/**
* @brief Check the status of the future object. True indicates that the result is available in the
* future's shared state. False otherwise.
*
* The future shall not be created using `std::async(std::launch::deferred)`. Otherwise, this
* function always returns true.
*
* @tparam T Type of the future.
* @param future Instance of the future.
* @return Boolean answer indicating if the future is ready or not.
*/
template <typename T>
bool is_future_done(T const& future)
{
assert(future.valid());
if (!future.valid()) {
throw std::invalid_argument("The future object does not refer to a valid shared state.");
}
return future.wait_for(std::chrono::seconds(0)) != std::future_status::timeout;
}

Expand Down
15 changes: 7 additions & 8 deletions cpp/src/file_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <kvikio/defaults.hpp>
#include <kvikio/file_handle.hpp>
#include <kvikio/file_utils.hpp>
#include "kvikio/utils.hpp"

namespace kvikio {

Expand Down Expand Up @@ -212,10 +213,9 @@ std::future<std::size_t> FileHandle::pread(void* buf,
if (size < gds_threshold) {
PushAndPopContext c(ctx);
auto bytes_read = detail::posix_device_read(_fd_direct_off.fd(), buf, size, file_offset, 0);
std::promise<std::size_t> read_promise;
auto read_future = read_promise.get_future();
read_promise.set_value(bytes_read);
return read_future;
// Maintain API consistency while making this trivial case synchronous.
// The result in the future is immediately available after the call.
return make_ready_future(bytes_read);
}

// Let's synchronize once instead of in each task.
Expand Down Expand Up @@ -263,10 +263,9 @@ std::future<std::size_t> FileHandle::pwrite(void const* buf,
if (size < gds_threshold) {
PushAndPopContext c(ctx);
auto bytes_write = detail::posix_device_write(_fd_direct_off.fd(), buf, size, file_offset, 0);
std::promise<std::size_t> write_promise;
auto write_future = write_promise.get_future();
write_promise.set_value(bytes_write);
return write_future;
// Maintain API consistency while making this trivial case synchronous.
// The result in the future is immediately available after the call.
return make_ready_future(bytes_write);
}

// Let's synchronize once instead of in each task.
Expand Down

0 comments on commit 1691bd0

Please sign in to comment.