Archived
sycl: keep the device out of plain system memory
On kernel 7.0 the xe driver can oops (drm_pagemap_zdd_destroy from __drm_pagemap_migrate_to_ram) when the CPU faults on a page its shared virtual memory had migrated into VRAM. The backend reached plain system memory in several ways; each now uses USM the runtime knows: - RAD_MEM_HOST_PINNED/_MAPPED are sycl::aligned_alloc_host, not posix_memalign + prepare_for_device_copy. O_DIRECT cannot target those pages (EFAULT), so DirectFile bounces such transfers through an 8 MiB buffer. The 27B load is not slower (10.8 s read, disk-bound). - Copies with a pageable side are staged through a host-USM bounce, synchronously (rare: startup and tests). The ranged copy stages a host range list even when its source is on the card, and refuses a pageable destination, which its kernel would write. - scatter_rows' bad-index flag is host USM, not shared. - Gates are gone (the documented state): a spinning kernel never sees a host store to host USM, and a device flag opened by a copy from another queue deadlocks behind copies queued past the gate. rad-kbench times ungated; the gate tests skip on backends without gates. Checked: device_test (sycl) 70 cases, 0 failed; rad-kbench fixture 1989/1989; 27B int4 chat output byte-identical (c4c5ba5e727c) at 30.6 tok/s; no xe messages in the kernel log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4b70a96378
commit
8931aae8b2
5 files changed
+171
-143
No files matched your search
@@ -15,8 +15,13 @@
|
|||||||
namespace rad {
|
namespace rad {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/* Bounce buffer size; allocated on first misaligned use only. */
|
/* Bounce buffer size; allocated on first use only. Large enough that a bounced extent (see
|
||||||
constexpr int64_t kBounceBytes = 1 << 20;
|
* EFAULT below) streams at close to the drive's rate. */
|
||||||
|
constexpr int64_t kBounceBytes = 8 << 20;
|
||||||
|
|
||||||
|
/* errno of the last failed transfer on this thread, so a caller can tell EFAULT from other errors
|
||||||
|
* after the RAD_E_IO the helpers return. */
|
||||||
|
thread_local int t_errno = 0;
|
||||||
|
|
||||||
int64_t align_down(int64_t v, int64_t a) { return v / a * a; }
|
int64_t align_down(int64_t v, int64_t a) { return v / a * a; }
|
||||||
|
|
||||||
@@ -27,6 +32,7 @@ int64_t pread_some(int fd, void* dst, int64_t bytes, int64_t off) {
|
|||||||
ssize_t r = ::pread(fd, (uint8_t*)dst + done, (size_t)(bytes - done), (off_t)(off + done));
|
ssize_t r = ::pread(fd, (uint8_t*)dst + done, (size_t)(bytes - done), (off_t)(off + done));
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
if (errno == EINTR) continue;
|
if (errno == EINTR) continue;
|
||||||
|
t_errno = errno;
|
||||||
return RAD_E_IO;
|
return RAD_E_IO;
|
||||||
}
|
}
|
||||||
if (r == 0) break;
|
if (r == 0) break;
|
||||||
@@ -52,6 +58,7 @@ int pwrite_exact(int fd, const void* src, int64_t bytes, int64_t off) {
|
|||||||
(off_t)(off + done));
|
(off_t)(off + done));
|
||||||
if (w <= 0) {
|
if (w <= 0) {
|
||||||
if (w < 0 && errno == EINTR) continue;
|
if (w < 0 && errno == EINTR) continue;
|
||||||
|
t_errno = w < 0 ? errno : EIO;
|
||||||
return RAD_E_IO;
|
return RAD_E_IO;
|
||||||
}
|
}
|
||||||
done += w;
|
done += w;
|
||||||
@@ -137,7 +144,15 @@ int DirectFile::read_at(void* dst, int64_t off, int64_t bytes) {
|
|||||||
|
|
||||||
/* Fast path: one pread straight into the caller's pinned buffer. Every .rad extent lands here
|
/* Fast path: one pread straight into the caller's pinned buffer. Every .rad extent lands here
|
||||||
* (movement units and rad_dev_alloc pools are both RAD_ALIGN_UNIT-aligned). */
|
* (movement units and rad_dev_alloc pools are both RAD_ALIGN_UNIT-aligned). */
|
||||||
if (aligned) return pread_exact(fd_, dst, bytes, off);
|
if (aligned) {
|
||||||
|
t_errno = 0;
|
||||||
|
const int rc = pread_exact(fd_, dst, bytes, off);
|
||||||
|
/* EFAULT: memory the kernel cannot pin for the transfer, such as pinned memory a GPU
|
||||||
|
* driver maps itself (SYCL malloc_host, core/device/sycl.cpp). Read it through the bounce,
|
||||||
|
* whole chunks at a time. */
|
||||||
|
if (rc == RAD_E_IO && t_errno == EFAULT) return bounce_read(dst, off, bytes);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
/* O_DIRECT rejects unaligned transfers with EINVAL, so read an aligned superset through the
|
/* O_DIRECT rejects unaligned transfers with EINVAL, so read an aligned superset through the
|
||||||
* bounce and copy out. This serves file tails and header/footer reads, never the mover; a
|
* bounce and copy out. This serves file tails and header/footer reads, never the mover; a
|
||||||
@@ -161,6 +176,17 @@ int DirectFile::read_at(void* dst, int64_t off, int64_t bytes) {
|
|||||||
return RAD_OK;
|
return RAD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* An aligned extent read through the bounce: the destination is memory O_DIRECT cannot target. */
|
||||||
|
int DirectFile::bounce_read(void* dst, int64_t off, int64_t bytes) {
|
||||||
|
RAD_TRY(bounce_ready());
|
||||||
|
for (int64_t done = 0; done < bytes; done += bounce_bytes_) {
|
||||||
|
const int64_t span = std::min(bounce_bytes_, bytes - done);
|
||||||
|
RAD_TRY(pread_exact(fd_, bounce_, span, off + done));
|
||||||
|
std::memcpy((uint8_t*)dst + done, bounce_, (size_t)span);
|
||||||
|
}
|
||||||
|
return RAD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ write */
|
/* ------------------------------------------------------------------ write */
|
||||||
|
|
||||||
/* Aligned writes go straight through; anything else is a read-modify-write of whole blocks. */
|
/* Aligned writes go straight through; anything else is a read-modify-write of whole blocks. */
|
||||||
@@ -172,7 +198,20 @@ int DirectFile::write_at(const void* src, int64_t off, int64_t bytes) {
|
|||||||
const int64_t A = alignment();
|
const int64_t A = alignment();
|
||||||
const bool aligned = (off % A) == 0 && (bytes % A) == 0 &&
|
const bool aligned = (off % A) == 0 && (bytes % A) == 0 &&
|
||||||
((uintptr_t)src % (uintptr_t)A) == 0;
|
((uintptr_t)src % (uintptr_t)A) == 0;
|
||||||
if (aligned) return pwrite_exact(fd_, src, bytes, off);
|
if (aligned) {
|
||||||
|
t_errno = 0;
|
||||||
|
const int rc = pwrite_exact(fd_, src, bytes, off);
|
||||||
|
if (rc == RAD_E_IO && t_errno == EFAULT) { /* as in read_at */
|
||||||
|
RAD_TRY(bounce_ready());
|
||||||
|
for (int64_t done = 0; done < bytes; done += bounce_bytes_) {
|
||||||
|
const int64_t span = std::min(bounce_bytes_, bytes - done);
|
||||||
|
std::memcpy(bounce_, (const uint8_t*)src + done, (size_t)span);
|
||||||
|
RAD_TRY(pwrite_exact(fd_, bounce_, span, off + done));
|
||||||
|
}
|
||||||
|
return RAD_OK;
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
RAD_TRY(bounce_ready());
|
RAD_TRY(bounce_ready());
|
||||||
const int64_t old_size = size();
|
const int64_t old_size = size();
|
||||||
|
|||||||
@@ -8,7 +8,9 @@
|
|||||||
*
|
*
|
||||||
* O_DIRECT requires the file offset, transfer length and buffer address to be block-aligned.
|
* O_DIRECT requires the file offset, transfer length and buffer address to be block-aligned.
|
||||||
* rad_dev_alloc memory is (host.cpp) and .rad pads movement units to RAD_ALIGN_UNIT
|
* rad_dev_alloc memory is (host.cpp) and .rad pads movement units to RAD_ALIGN_UNIT
|
||||||
* (rad_format.h), so every mover read takes the unbounced path. The bounce is for other callers.
|
* (rad_format.h), so every mover read takes the unbounced path. The bounce is for other callers,
|
||||||
|
* and for pinned memory O_DIRECT cannot target (EFAULT: a GPU driver's own mapping, such as the
|
||||||
|
* SYCL backend's malloc_host), which is bounced whole.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
@@ -63,6 +65,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
int bounce_ready();
|
int bounce_ready();
|
||||||
|
int bounce_read(void* dst, int64_t off, int64_t bytes);
|
||||||
|
|
||||||
int fd_ = -1;
|
int fd_ = -1;
|
||||||
bool direct_ = false;
|
bool direct_ = false;
|
||||||
|
|||||||
+118
-137
@@ -272,24 +272,15 @@ int d_vram_used(int device, int64_t* out) {
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------ memory
|
/* ------------------------------------------------------------------ memory
|
||||||
*
|
*
|
||||||
* Pinned host memory (RAD_MEM_HOST_PINNED and _MAPPED) is ordinary page-aligned memory registered
|
* Pinned host memory (RAD_MEM_HOST_PINNED and _MAPPED) is SYCL's malloc_host: USM host memory
|
||||||
* with the driver (prepare_for_device_copy), not SYCL's malloc_host. The driver maps malloc_host
|
* the runtime knows, which kernels and the copy engines reach through the driver's own mapping.
|
||||||
* pages itself, and O_DIRECT cannot read into them (EFAULT), while the mover reads the file tier
|
* Nothing the device touches is plain system memory. Ordinary pages registered with
|
||||||
* straight into pinned buffers (directio.h). Registered memory copies at the same rate (28.4 GB/s
|
* prepare_for_device_copy were used before, so that O_DIRECT could read into them; the device then
|
||||||
* either way on the Arc Pro B65) and kernels reach it through system USM, which the Intel driver
|
* reaches them through the xe driver's shared virtual memory, which may migrate the pages into
|
||||||
* provides (aspect usm_system_allocations). The runtime does not know such a pointer, so the
|
* VRAM, and on kernel 7.0 the CPU fault that moves such a page back can oops the kernel
|
||||||
* ranges are kept here for host_ptr, device_ptr and the memset check. */
|
* (drm_pagemap_zdd_destroy from __drm_pagemap_migrate_to_ram). O_DIRECT cannot read into
|
||||||
std::mutex g_pinned_m;
|
* malloc_host pages (EFAULT, the driver maps them itself), so DirectFile bounces those transfers
|
||||||
std::map<uintptr_t, size_t> g_pinned; /* base -> bytes */
|
* (directio.cpp). */
|
||||||
|
|
||||||
bool in_pinned(const void* p) {
|
|
||||||
const uintptr_t a = (uintptr_t)p;
|
|
||||||
std::lock_guard<std::mutex> lk(g_pinned_m);
|
|
||||||
auto it = g_pinned.upper_bound(a);
|
|
||||||
if (it == g_pinned.begin()) return false;
|
|
||||||
--it;
|
|
||||||
return a < it->first + it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Device bytes this process holds through rad_dev_alloc, for the room check's message. */
|
/* Device bytes this process holds through rad_dev_alloc, for the room check's message. */
|
||||||
std::atomic<int64_t> g_device_bytes{0};
|
std::atomic<int64_t> g_device_bytes{0};
|
||||||
@@ -341,19 +332,11 @@ void* d_alloc(int64_t bytes, int kind) {
|
|||||||
break;
|
break;
|
||||||
case RAD_MEM_HOST_PINNED:
|
case RAD_MEM_HOST_PINNED:
|
||||||
case RAD_MEM_HOST_MAPPED: {
|
case RAD_MEM_HOST_MAPPED: {
|
||||||
/* Registered page-aligned memory (see above): one address on both sides, so
|
/* USM host memory (see above): one address on both sides, so host_ptr and
|
||||||
* host_ptr and device_ptr are the pointer itself. */
|
* device_ptr are the pointer itself. Page-aligned and whole pages, as the
|
||||||
|
* RAD_ALIGN_UNIT pools and the file tier's extents expect. */
|
||||||
const size_t n = (size_t)((bytes + 4095) / 4096 * 4096);
|
const size_t n = (size_t)((bytes + 4095) / 4096 * 4096);
|
||||||
if (posix_memalign(&p, 4096, n) != 0) p = nullptr;
|
p = sycl::aligned_alloc_host(4096, n, *rt().ctx);
|
||||||
if (!p) break;
|
|
||||||
try {
|
|
||||||
syclex::prepare_for_device_copy(p, n, *rt().ctx);
|
|
||||||
} catch (...) {
|
|
||||||
std::free(p);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
std::lock_guard<std::mutex> lk(g_pinned_m);
|
|
||||||
g_pinned[(uintptr_t)p] = n;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RAD_MEM_HOST:
|
case RAD_MEM_HOST:
|
||||||
@@ -392,20 +375,21 @@ void d_free(void* p, int kind) {
|
|||||||
break;
|
break;
|
||||||
case RAD_MEM_HOST_PINNED:
|
case RAD_MEM_HOST_PINNED:
|
||||||
case RAD_MEM_HOST_MAPPED: {
|
case RAD_MEM_HOST_MAPPED: {
|
||||||
{
|
sycl::usm::alloc k = sycl::usm::alloc::unknown;
|
||||||
std::lock_guard<std::mutex> lk(g_pinned_m);
|
try {
|
||||||
if (!g_pinned.erase((uintptr_t)p)) {
|
k = sycl::get_pointer_type(p, *rt().ctx);
|
||||||
RAD_WARN("rad_dev_free(%p): not pinned memory this backend gave out; leaked "
|
} catch (const sycl::exception&) {
|
||||||
"rather than freed with the wrong allocator", p);
|
}
|
||||||
break;
|
if (k != sycl::usm::alloc::host) {
|
||||||
}
|
RAD_WARN("rad_dev_free(%p): not pinned memory this backend gave out; leaked "
|
||||||
|
"rather than freed with the wrong allocator", p);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
syclex::release_from_device_copy(p, *rt().ctx);
|
sycl::free(p, *rt().ctx);
|
||||||
} catch (const sycl::exception& e) {
|
} catch (const sycl::exception& e) {
|
||||||
RAD_WARN("device: rad_dev_free(%p): %s", p, e.what());
|
RAD_WARN("device: rad_dev_free(%p): %s", p, e.what());
|
||||||
}
|
}
|
||||||
std::free(p);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RAD_MEM_HOST:
|
case RAD_MEM_HOST:
|
||||||
@@ -418,10 +402,8 @@ void d_free(void* p, int kind) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* What `p` is to the device: USM device memory, host memory it reaches (SYCL's or this backend's
|
/* What `p` is to the device: USM device, host or shared memory, or unknown (pageable). */
|
||||||
* registered pinned ranges), or unknown (pageable). */
|
|
||||||
sycl::usm::alloc ptr_kind(const void* p) {
|
sycl::usm::alloc ptr_kind(const void* p) {
|
||||||
if (in_pinned(p)) return sycl::usm::alloc::host;
|
|
||||||
try {
|
try {
|
||||||
return sycl::get_pointer_type(p, *rt().ctx);
|
return sycl::get_pointer_type(p, *rt().ctx);
|
||||||
} catch (const sycl::exception&) {
|
} catch (const sycl::exception&) {
|
||||||
@@ -446,7 +428,6 @@ void* d_device_ptr(void* p) {
|
|||||||
struct SyclStream {
|
struct SyclStream {
|
||||||
RadSyclStream pub;
|
RadSyclStream pub;
|
||||||
sycl::queue q;
|
sycl::queue q;
|
||||||
std::atomic<int> gates{0}; /* closed gates on this stream (rad_stream_gate) */
|
|
||||||
char* stage = nullptr; /* rad_memcpy_ranges_async's device copy of host sources */
|
char* stage = nullptr; /* rad_memcpy_ranges_async's device copy of host sources */
|
||||||
size_t stage_n = 0;
|
size_t stage_n = 0;
|
||||||
SyclStream(const sycl::context& c, const sycl::device& d, const sycl::property_list& pl)
|
SyclStream(const sycl::context& c, const sycl::device& d, const sycl::property_list& pl)
|
||||||
@@ -516,11 +497,6 @@ int d_stream_sync(RadStream st) {
|
|||||||
device_set_error("rad_stream_sync: null stream");
|
device_set_error("rad_stream_sync: null stream");
|
||||||
return RAD_E_INVAL;
|
return RAD_E_INVAL;
|
||||||
}
|
}
|
||||||
if (as_stream(st)->gates.load() > 0) {
|
|
||||||
device_set_error("rad_stream_sync: the stream waits at a closed gate, which only "
|
|
||||||
"rad_gate_open releases");
|
|
||||||
return RAD_E_STATE;
|
|
||||||
}
|
|
||||||
SYCL_TRY("rad_stream_sync", as_stream(st)->q.wait_and_throw());
|
SYCL_TRY("rad_stream_sync", as_stream(st)->q.wait_and_throw());
|
||||||
return async_status("rad_stream_sync");
|
return async_status("rad_stream_sync");
|
||||||
}
|
}
|
||||||
@@ -646,6 +622,40 @@ void d_host_wrote(void) {}
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------ copies and fills */
|
/* ------------------------------------------------------------------ copies and fills */
|
||||||
|
|
||||||
|
/* A copy with a side in pageable host memory (a std::vector, the stack) goes through a bounce of
|
||||||
|
* USM host memory, and synchronously: handed such a pointer, the runtime may reach it through the
|
||||||
|
* driver's shared virtual memory, which the backend keeps out of (see memory, above). Rare -- the
|
||||||
|
* engine copies through pinned buffers -- so the wait costs nothing on the step path. `span` is
|
||||||
|
* the bytes from each base the copy can touch; `run` makes the copy with the bounce standing in
|
||||||
|
* for the pageable side(s). */
|
||||||
|
bool is_pageable(const void* p) { return ptr_kind(p) == sycl::usm::alloc::unknown; }
|
||||||
|
|
||||||
|
template <class Run>
|
||||||
|
void bounced(sycl::queue& q, void* dst, const void* src, size_t span, Run run) {
|
||||||
|
const bool pd = is_pageable(dst), ps = is_pageable(src);
|
||||||
|
char* bd = pd ? (char*)sycl::malloc_host(span, q) : nullptr;
|
||||||
|
char* bs = ps ? (char*)sycl::malloc_host(span, q) : nullptr;
|
||||||
|
if ((pd && !bd) || (ps && !bs)) {
|
||||||
|
if (bd) sycl::free(bd, q);
|
||||||
|
if (bs) sycl::free(bs, q);
|
||||||
|
throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation),
|
||||||
|
"bounce for pageable host memory");
|
||||||
|
}
|
||||||
|
if (bs) std::memcpy(bs, src, span);
|
||||||
|
if (bd) std::memcpy(bd, dst, span); /* bytes between rows of a 2-D copy stay as they were */
|
||||||
|
try {
|
||||||
|
run(bd ? (void*)bd : dst, bs ? (const void*)bs : src);
|
||||||
|
q.wait_and_throw();
|
||||||
|
} catch (...) {
|
||||||
|
if (bd) sycl::free(bd, q);
|
||||||
|
if (bs) sycl::free(bs, q);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
if (bd) std::memcpy(dst, bd, span);
|
||||||
|
if (bd) sycl::free(bd, q);
|
||||||
|
if (bs) sycl::free(bs, q);
|
||||||
|
}
|
||||||
|
|
||||||
int d_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream st) {
|
int d_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream st) {
|
||||||
RAD_TRY(check_stream(st, "rad_memcpy_async"));
|
RAD_TRY(check_stream(st, "rad_memcpy_async"));
|
||||||
if (bytes == 0) return RAD_OK;
|
if (bytes == 0) return RAD_OK;
|
||||||
@@ -654,7 +664,14 @@ int d_memcpy_async(void* dst, const void* src, int64_t bytes, RadStream st) {
|
|||||||
(long long)bytes);
|
(long long)bytes);
|
||||||
return RAD_E_INVAL;
|
return RAD_E_INVAL;
|
||||||
}
|
}
|
||||||
SYCL_TRY("rad_memcpy_async", as_stream(st)->q.memcpy(dst, src, (size_t)bytes));
|
sycl::queue& q = as_stream(st)->q;
|
||||||
|
if (is_pageable(dst) || is_pageable(src)) {
|
||||||
|
SYCL_TRY("rad_memcpy_async", bounced(q, dst, src, (size_t)bytes, [&](void* d, const void* s) {
|
||||||
|
q.memcpy(d, s, (size_t)bytes);
|
||||||
|
}));
|
||||||
|
return async_status("rad_memcpy_async");
|
||||||
|
}
|
||||||
|
SYCL_TRY("rad_memcpy_async", q.memcpy(dst, src, (size_t)bytes));
|
||||||
return RAD_OK;
|
return RAD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -668,9 +685,18 @@ int d_memcpy_2d_async(void* dst, int64_t dpitch, const void* src, int64_t spitch
|
|||||||
(long long)height);
|
(long long)height);
|
||||||
return RAD_E_INVAL;
|
return RAD_E_INVAL;
|
||||||
}
|
}
|
||||||
|
sycl::queue& q = as_stream(st)->q;
|
||||||
|
if (is_pageable(dst) || is_pageable(src)) {
|
||||||
|
/* One span for both sides, the larger pitch's. */
|
||||||
|
const size_t span = (size_t)(std::max(dpitch, spitch) * (height - 1) + width);
|
||||||
|
SYCL_TRY("rad_memcpy_2d_async", bounced(q, dst, src, span, [&](void* d, const void* s) {
|
||||||
|
q.ext_oneapi_memcpy2d(d, (size_t)dpitch, s, (size_t)spitch, (size_t)width, (size_t)height);
|
||||||
|
}));
|
||||||
|
return async_status("rad_memcpy_2d_async");
|
||||||
|
}
|
||||||
SYCL_TRY("rad_memcpy_2d_async",
|
SYCL_TRY("rad_memcpy_2d_async",
|
||||||
as_stream(st)->q.ext_oneapi_memcpy2d(dst, (size_t)dpitch, src, (size_t)spitch,
|
q.ext_oneapi_memcpy2d(dst, (size_t)dpitch, src, (size_t)spitch, (size_t)width,
|
||||||
(size_t)width, (size_t)height));
|
(size_t)height));
|
||||||
return RAD_OK;
|
return RAD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -716,24 +742,42 @@ int d_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadC
|
|||||||
dst, src);
|
dst, src);
|
||||||
return RAD_E_UNSUPPORTED;
|
return RAD_E_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
if (n > 0 && is_pageable(dst)) {
|
||||||
|
/* The ranges are written by a kernel, which pageable memory is not open to (as for
|
||||||
|
* rad_memset_async, device.h note 1). */
|
||||||
|
device_set_error("rad_memcpy_ranges_async(%p): the destination is not device-accessible "
|
||||||
|
"(pageable host memory)", dst);
|
||||||
|
return RAD_E_UNSUPPORTED;
|
||||||
|
}
|
||||||
SyclStream* ss = as_stream(st);
|
SyclStream* ss = as_stream(st);
|
||||||
sycl::queue& q = ss->q;
|
sycl::queue& q = ss->q;
|
||||||
char* d = (char*)dst;
|
char* d = (char*)dst;
|
||||||
const char* s = (const char*)src;
|
const char* s = (const char*)src;
|
||||||
SYCL_TRY("rad_memcpy_ranges_async", {
|
SYCL_TRY("rad_memcpy_ranges_async", {
|
||||||
if (prefix > 0) q.memcpy(d, s, (size_t)prefix);
|
if (prefix > 0) {
|
||||||
|
if (is_pageable(d) || is_pageable(s))
|
||||||
|
bounced(q, d, s, (size_t)prefix, [&](void* dd, const void* ff) { q.memcpy(dd, ff, (size_t)prefix); });
|
||||||
|
else
|
||||||
|
q.memcpy(d, s, (size_t)prefix);
|
||||||
|
}
|
||||||
/* Sources and ranges in host memory (the step upload's pinned staging): read by the copy
|
/* Sources and ranges in host memory (the step upload's pinned staging): read by the copy
|
||||||
* kernel they cost a link round trip a load -- 650 us a call over a PCIe x1 link -- so
|
* kernel they cost a link round trip a load -- 650 us a call over a PCIe x1 link -- so
|
||||||
* their span and the range list cross first in one transfer each, into a device buffer
|
* their span and the range list cross first in one transfer each, into a device buffer
|
||||||
* of the stream's (in order, so the next call's copy waits for this kernel). */
|
* of the stream's (in order, so the next call's copy waits for this kernel). */
|
||||||
if (n > 0 && ptr_kind(src) != sycl::usm::alloc::device && ptr_kind(ranges) != sycl::usm::alloc::device) {
|
/* Either in host memory: the source span, the range list or both cross into the stage.
|
||||||
|
* A pageable one goes through the bounce (see bounced), so the kernel only ever reads
|
||||||
|
* device memory. */
|
||||||
|
const bool host_src = n > 0 && ptr_kind(src) != sycl::usm::alloc::device;
|
||||||
|
const bool host_rng = n > 0 && ptr_kind(ranges) != sycl::usm::alloc::device;
|
||||||
|
if (host_src || host_rng) {
|
||||||
int64_t lo = INT64_MAX, hi = 0;
|
int64_t lo = INT64_MAX, hi = 0;
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) {
|
||||||
lo = std::min<int64_t>(lo, ranges[i].src);
|
lo = std::min<int64_t>(lo, ranges[i].src);
|
||||||
hi = std::max<int64_t>(hi, ranges[i].src + ranges[i].bytes);
|
hi = std::max<int64_t>(hi, ranges[i].src + ranges[i].bytes);
|
||||||
}
|
}
|
||||||
lo &= ~(int64_t)15;
|
lo &= ~(int64_t)15;
|
||||||
const size_t span = (size_t)(hi - lo + 15) / 16 * 16, rb = (size_t)n * sizeof(RadCopyRange);
|
const size_t span = host_src ? (size_t)(hi - lo + 15) / 16 * 16 : 0;
|
||||||
|
const size_t rb = host_rng ? (size_t)n * sizeof(RadCopyRange) : 0;
|
||||||
if (ss->stage_n < span + rb) {
|
if (ss->stage_n < span + rb) {
|
||||||
q.wait();
|
q.wait();
|
||||||
if (ss->stage) sycl::free(ss->stage, q);
|
if (ss->stage) sycl::free(ss->stage, q);
|
||||||
@@ -741,10 +785,20 @@ int d_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadC
|
|||||||
ss->stage = (char*)sycl::malloc_device(ss->stage_n, q);
|
ss->stage = (char*)sycl::malloc_device(ss->stage_n, q);
|
||||||
if (!ss->stage) { ss->stage_n = 0; throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation), "range staging"); }
|
if (!ss->stage) { ss->stage_n = 0; throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation), "range staging"); }
|
||||||
}
|
}
|
||||||
q.memcpy(ss->stage, s + lo, (size_t)(hi - lo));
|
auto up = [&](void* d, const void* from, size_t bytes) {
|
||||||
q.memcpy(ss->stage + span, ranges, rb);
|
if (is_pageable(from))
|
||||||
s = ss->stage - lo;
|
bounced(q, d, from, bytes, [&](void* dd, const void* ff) { q.memcpy(dd, ff, bytes); });
|
||||||
ranges = (const RadCopyRange*)(ss->stage + span);
|
else
|
||||||
|
q.memcpy(d, from, bytes);
|
||||||
|
};
|
||||||
|
if (host_src) {
|
||||||
|
up(ss->stage, s + lo, (size_t)(hi - lo));
|
||||||
|
s = ss->stage - lo;
|
||||||
|
}
|
||||||
|
if (host_rng) {
|
||||||
|
up(ss->stage + span, ranges, rb);
|
||||||
|
ranges = (const RadCopyRange*)(ss->stage + span);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
q.parallel_for(sycl::nd_range<1>{(size_t)n * kRangeWg, kRangeWg}, [=](sycl::nd_item<1> it) {
|
q.parallel_for(sycl::nd_range<1>{(size_t)n * kRangeWg, kRangeWg}, [=](sycl::nd_item<1> it) {
|
||||||
@@ -772,85 +826,13 @@ int d_memcpy_ranges_async(void* dst, const void* src, int64_t prefix, const RadC
|
|||||||
|
|
||||||
/* ------------------------------------------------------------------ gates
|
/* ------------------------------------------------------------------ gates
|
||||||
*
|
*
|
||||||
* A gate is a one-work-item kernel that spins until a flag in pinned host memory turns 1; the
|
* None (rad_stream_gate reports the backend has none; rad-kbench then times ungated). A gate's
|
||||||
* in-order queue holds everything behind it. rad_gate_open sets the flag. The flag's memory is
|
* spinning kernel needs a flag the host can flip while it runs: on the Arc Pro B65 it never sees a
|
||||||
* freed only once the kernel has finished (reaped on later gate calls), since the kernel may still
|
* store to host USM, and plain system memory, which it does see, goes through the driver's shared
|
||||||
* be reading it when the host opens. A gate held for seconds can trip the driver's job timeout;
|
* virtual memory (see memory, above). A device flag written by a copy from another queue deadlocks
|
||||||
* rad-kbench holds one for a batch. */
|
* as soon as a copy is queued behind the gate (both share the copy engine), and a kernel on another
|
||||||
struct SyclGate {
|
* queue does not run beside the spinning one. */
|
||||||
uint32_t* flag = nullptr;
|
|
||||||
SyclStream* s = nullptr;
|
|
||||||
sycl::event spin;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::mutex g_gate_m;
|
|
||||||
std::vector<std::pair<sycl::event, uint32_t*>> g_gate_graveyard;
|
|
||||||
|
|
||||||
void gate_reap() {
|
|
||||||
std::lock_guard<std::mutex> lk(g_gate_m);
|
|
||||||
for (size_t i = 0; i < g_gate_graveyard.size();) {
|
|
||||||
bool done = true;
|
|
||||||
try {
|
|
||||||
done = event_done(g_gate_graveyard[i].first);
|
|
||||||
} catch (const sycl::exception&) {
|
|
||||||
}
|
|
||||||
if (!done) { ++i; continue; }
|
|
||||||
d_free(g_gate_graveyard[i].second, RAD_MEM_HOST_PINNED);
|
|
||||||
g_gate_graveyard[i] = g_gate_graveyard.back();
|
|
||||||
g_gate_graveyard.pop_back();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int d_stream_gate(RadStream st, void** out) {
|
|
||||||
RAD_TRY(check_stream(st, "rad_stream_gate"));
|
|
||||||
if (!out) return RAD_E_INVAL;
|
|
||||||
gate_reap();
|
|
||||||
SyclStream* s = as_stream(st);
|
|
||||||
auto* g = new (std::nothrow) SyclGate();
|
|
||||||
uint32_t* flag = (uint32_t*)d_alloc(64, RAD_MEM_HOST_PINNED);
|
|
||||||
if (!g || !flag) {
|
|
||||||
delete g;
|
|
||||||
if (flag) d_free(flag, RAD_MEM_HOST_PINNED);
|
|
||||||
device_set_error("rad_stream_gate: out of memory");
|
|
||||||
return RAD_E_NOMEM;
|
|
||||||
}
|
|
||||||
*flag = 0;
|
|
||||||
try {
|
|
||||||
g->spin = s->q.single_task([=] {
|
|
||||||
sycl::atomic_ref<uint32_t, sycl::memory_order::relaxed, sycl::memory_scope::system,
|
|
||||||
sycl::access::address_space::global_space> f(*flag);
|
|
||||||
while (f.load() == 0) {
|
|
||||||
}
|
|
||||||
sycl::atomic_fence(sycl::memory_order::acquire, sycl::memory_scope::system);
|
|
||||||
});
|
|
||||||
} catch (const sycl::exception& e) {
|
|
||||||
delete g;
|
|
||||||
d_free(flag, RAD_MEM_HOST_PINNED);
|
|
||||||
return sycl_fail(e, "rad_stream_gate");
|
|
||||||
}
|
|
||||||
g->flag = flag;
|
|
||||||
g->s = s;
|
|
||||||
++s->gates;
|
|
||||||
*out = g;
|
|
||||||
return RAD_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
int d_gate_open(void* p) {
|
|
||||||
auto* g = (SyclGate*)p;
|
|
||||||
if (!g || !g->flag) return RAD_E_INVAL;
|
|
||||||
std::atomic_ref<uint32_t>(*g->flag).store(1, std::memory_order_release);
|
|
||||||
--g->s->gates;
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lk(g_gate_m);
|
|
||||||
g_gate_graveyard.emplace_back(g->spin, g->flag);
|
|
||||||
}
|
|
||||||
delete g;
|
|
||||||
return RAD_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* A Level Zero queue has no fixed depth, so nothing behind a gate waits for room; the figure only
|
|
||||||
* bounds how far a caller runs ahead. */
|
|
||||||
int64_t d_gate_room(RadStream st) { return st ? 4096 : 0; }
|
|
||||||
|
|
||||||
const DeviceBackend g_sycl = {
|
const DeviceBackend g_sycl = {
|
||||||
.name = "sycl",
|
.name = "sycl",
|
||||||
@@ -874,7 +856,6 @@ const DeviceBackend g_sycl = {
|
|||||||
.pci_address = d_pci_address,
|
.pci_address = d_pci_address,
|
||||||
.runtime_version = d_runtime_version,
|
.runtime_version = d_runtime_version,
|
||||||
/* Expert prefetch: null. */
|
/* Expert prefetch: null. */
|
||||||
.stream_gate = d_stream_gate, .gate_open = d_gate_open, .gate_room = d_gate_room,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace */
|
} /* namespace */
|
||||||
|
|||||||
@@ -146,7 +146,9 @@ int scatter_rows(const RadArgs* a, RadStream s) {
|
|||||||
if (!q) return RAD_E_DEVICE;
|
if (!q) return RAD_E_DEVICE;
|
||||||
/* libref refuses an index past x's rows before writing anything; the indices live on the card,
|
/* libref refuses an index past x's rows before writing anything; the indices live on the card,
|
||||||
* so a device flag records one and the call reports it after the copy is skipped. */
|
* so a device flag records one and the call reports it after the copy is skipped. */
|
||||||
int* bad = sycl::malloc_shared<int>(1, *q);
|
/* USM host memory, not shared: shared USM may live in the driver's shared virtual memory,
|
||||||
|
* whose page migration the backend avoids (core/device/sycl.cpp, memory). */
|
||||||
|
int* bad = sycl::malloc_host<int>(1, *q);
|
||||||
if (!bad) return refuse("scatter_rows: no flag memory");
|
if (!bad) return refuse("scatter_rows: no flag memory");
|
||||||
*bad = 0;
|
*bad = 0;
|
||||||
int rc = submit([&] {
|
int rc = submit([&] {
|
||||||
|
|||||||
@@ -1414,6 +1414,7 @@ TEST(a_host_sync_on_an_event_not_made_for_it_is_refused) {
|
|||||||
* copy land well inside 20 ms. */
|
* copy land well inside 20 ms. */
|
||||||
TEST(nothing_behind_a_closed_gate_runs_until_it_opens) {
|
TEST(nothing_behind_a_closed_gate_runs_until_it_opens) {
|
||||||
Stream st;
|
Stream st;
|
||||||
|
if (rad_gate_room(st.s) <= 0) SKIP("no gates on this backend");
|
||||||
DevBuf<uint8_t> src(4096, RAD_MEM_HOST_PINNED);
|
DevBuf<uint8_t> src(4096, RAD_MEM_HOST_PINNED);
|
||||||
DevBuf<uint8_t> dst(4096, RAD_MEM_HOST_PINNED);
|
DevBuf<uint8_t> dst(4096, RAD_MEM_HOST_PINNED);
|
||||||
REQUIRE(src && dst);
|
REQUIRE(src && dst);
|
||||||
@@ -1494,6 +1495,7 @@ TEST(a_dma_lists_mark_stays_out_of_the_pool_until_the_engine_has_read_it) {
|
|||||||
* is refused with RAD_E_STATE instead of hanging, and works again once the gate is open. */
|
* is refused with RAD_E_STATE instead of hanging, and works again once the gate is open. */
|
||||||
TEST(a_host_wait_on_a_gated_stream_is_refused_not_a_deadlock) {
|
TEST(a_host_wait_on_a_gated_stream_is_refused_not_a_deadlock) {
|
||||||
Stream st;
|
Stream st;
|
||||||
|
if (rad_gate_room(st.s) <= 0) SKIP("no gates on this backend");
|
||||||
void* gate = nullptr;
|
void* gate = nullptr;
|
||||||
REQUIRE_EQ(rad_stream_gate(st.s, &gate), RAD_OK);
|
REQUIRE_EQ(rad_stream_gate(st.s, &gate), RAD_OK);
|
||||||
CHECK_EQ(rad_stream_sync(st.s), RAD_E_STATE);
|
CHECK_EQ(rad_stream_sync(st.s), RAD_E_STATE);
|
||||||
@@ -1538,6 +1540,7 @@ TEST(a_full_gated_queue_refuses_the_next_submission_instead_of_waiting) {
|
|||||||
* with one it is not. Timing is the behaviour here, and the margins are an order of magnitude. */
|
* with one it is not. Timing is the behaviour here, and the margins are an order of magnitude. */
|
||||||
TEST(events_behind_a_gate_leave_the_hosts_submission_time_out) {
|
TEST(events_behind_a_gate_leave_the_hosts_submission_time_out) {
|
||||||
Stream st;
|
Stream st;
|
||||||
|
if (rad_gate_room(st.s) <= 0) SKIP("no gates on this backend");
|
||||||
DevBuf<uint8_t> a(4096, RAD_MEM_DEVICE);
|
DevBuf<uint8_t> a(4096, RAD_MEM_DEVICE);
|
||||||
REQUIRE(a);
|
REQUIRE(a);
|
||||||
Event e0, e1;
|
Event e0, e1;
|
||||||
|
|||||||
Reference in new issue
Block a user