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

Unify batch and stream API check #271

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
Next Next commit
CUFILE_CHECK_STREAM_IO: check stream error
  • Loading branch information
madsbk committed Aug 28, 2023
commit 1febfb3924702c64795dad0175662d5541ebe7b0
27 changes: 13 additions & 14 deletions cpp/examples/basic_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,30 @@ int main()
cout << "stream : " << kvikio::is_stream_available() << endl;
if (kvikio::is_stream_available()) {
{
cout << "Performing stream I/O using file handle" << endl;
off_t f_off = 0, d_off = 0;
ssize_t bytes_done;
CUstream stream;
cout << "Performing async I/O using file handle" << endl;
off_t f_off{0};
off_t d_off{0};
ssize_t bytes_done{0};
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is incorrect, per the documentation for cuFileWriteAsync:

The bytes_written pointer should be allocated with cuMemHostAlloc or registered with cuMemHostRegister, and the pointer to access that memory from the device can be obtained by using cuMemHostGetDevicePointer.

Which we are not doing here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, fixed

CUstream stream{};
check(cudaStreamCreate(&stream) == cudaSuccess);
kvikio::FileHandle f_handle("/data/test-file", "w+", kvikio::FileHandle::m644, false);
check(cudaMemcpy(a_dev, a, SIZE, cudaMemcpyHostToDevice) == cudaSuccess);

/*
* For stream based I/Os, buffer registration is not mandatory. However,
* it gives a better performance.
*/

kvikio::buffer_register(a_dev, SIZE);
check(cudaMemcpyAsync(a_dev, a, SIZE, cudaMemcpyHostToDevice, stream) == cudaSuccess);
f_handle.write_async(a_dev, &io_size, &f_off, &d_off, &bytes_done, stream);

// After synchronizing `stream`, we can read the number of bytes written
check(cudaStreamSynchronize(stream) == cudaSuccess);
// Note, `bytes_done` might be negative, which indicate an IO error thus we
// use `CUFILE_CHECK_STREAM_IO` to check for errors.
CUFILE_CHECK_STREAM_IO(bytes_done);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow, this is a garbage error reporting api.

check(bytes_done == SIZE);
cout << "File stream Write : " << bytes_done << endl;
kvikio::buffer_deregister(a_dev);
cout << "File async write : " << bytes_done << endl;

/* Read */
bytes_done = 0;
kvikio::buffer_register(c_dev, SIZE);
f_handle.read_async(c_dev, &io_size, &f_off, &d_off, &bytes_done, stream);
check(cudaStreamSynchronize(stream) == cudaSuccess);
CUFILE_CHECK_STREAM_IO(bytes_done);
check(bytes_done == SIZE);
cout << "File stream Read : " << bytes_done << endl;
kvikio::buffer_deregister(c_dev);
Expand Down
24 changes: 22 additions & 2 deletions cpp/include/kvikio/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct CUfileException : public std::runtime_error {
std::string(err_str) + ")"}; \
} \
} while (0)
#define CUDA_DRIVER_TRY_1(_call) CUDA_DRIVER_TRY_2(_call, CUfileException)
#define CUDA_DRIVER_TRY_1(_call) CUDA_DRIVER_TRY_2(_call, kvikio::CUfileException)
#endif

#ifdef KVIKIO_CUFILE_FOUND
Expand All @@ -75,7 +75,27 @@ struct CUfileException : public std::runtime_error {
cufileop_status_error(error.err)}; \
} \
} while (0)
#define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, CUfileException)
#define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, kvikio::CUfileException)
#endif
#endif

#ifdef KVIKIO_CUFILE_FOUND
#ifndef CUFILE_CHECK_STREAM_IO
#define CUFILE_CHECK_STREAM_IO(...) \
wence- marked this conversation as resolved.
Show resolved Hide resolved
GET_CUFILE_CHECK_STREAM_IO_MACRO( \
__VA_ARGS__, CUFILE_CHECK_STREAM_IO_2, CUFILE_CHECK_STREAM_IO_1) \
(__VA_ARGS__)
#define GET_CUFILE_CHECK_STREAM_IO_MACRO(_1, _2, NAME, ...) NAME
#define CUFILE_CHECK_STREAM_IO_2(_nbytes_done, _exception_type) \
do { \
int const _nbytes = (_nbytes_done); \
if (_nbytes < 0) { \
throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
KVIKIO_STRINGIFY(__LINE__) + ": " + \
cufileop_status_error((CUfileOpError)(CUFILEOP_BASE_ERR - _nbytes))}; \
Copy link
Contributor

Choose a reason for hiding this comment

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

So the documentation says that this returns:

  • The number of bytes successfully written. or;
  • -1 on IO errors. or;
  • All other errors will return a negative integer value of the CUfileOpError enum value.

But this code suggests that it returns -(some_cufile_op_error - CUFILEOP_BASE_ERR). Which can't be right given the above because it value-puns CU_FILE_DRIVER_NOT_INITIALIZED with an IO error.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think the docs are correct. I now just print the raw value of _nbytes. Let's fix this in a follow-up when we know the intended behavior :)

} \
} while (0)
#define CUFILE_CHECK_STREAM_IO_1(_call) CUFILE_CHECK_STREAM_IO_2(_call, kvikio::CUfileException)
#endif
#endif

Expand Down