Repository navigation
Expand file tree
/
Copy pathhost_stream_flag.cpp
More file actions
53 lines (41 loc) · 1.53 KB
/
Copy pathhost_stream_flag.cpp
File metadata and controls
53 lines (41 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "host_stream_flag.h"
#if defined(LUCE_BACKEND_HIP)
#include <hip/hip_runtime.h>
#endif
namespace luce::common {
#if defined(LUCE_BACKEND_HIP)
HostStreamFlag::~HostStreamFlag() {
if (host_) (void) hipHostFree(host_);
}
bool HostStreamFlag::init() {
if (host_) return true;
uint32_t * host = nullptr;
if (hipHostMalloc((void **) &host, 64, hipHostMallocDefault) != hipSuccess) return false;
*host = 0;
uint32_t * dev = nullptr;
if (hipHostGetDevicePointer((void **) &dev, host, 0) != hipSuccess) {
(void) hipHostFree(host);
return false;
}
host_ = host;
dev_ = dev;
return true;
}
bool HostStreamFlag::enqueue_wait(void * stream, uint32_t value) const {
return dev_ && hipStreamWaitValue32((hipStream_t) stream, dev_, value,
hipStreamWaitValueEq, 0xffffffffu) == hipSuccess;
}
void HostStreamFlag::signal(uint32_t value) const {
if (host_) __atomic_store_n(host_, value, __ATOMIC_RELEASE);
}
bool HostStreamFlag::copy_to_device(void * stream, void * dst, const void * src, size_t bytes) {
return hipMemcpyAsync(dst, src, bytes, hipMemcpyHostToDevice, (hipStream_t) stream) == hipSuccess;
}
#else
HostStreamFlag::~HostStreamFlag() = default;
bool HostStreamFlag::init() { return false; }
bool HostStreamFlag::enqueue_wait(void *, uint32_t) const { return false; }
void HostStreamFlag::signal(uint32_t) const {}
bool HostStreamFlag::copy_to_device(void *, void *, const void *, size_t) { return false; }
#endif
} // namespace luce::common