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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
// Copyright 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "DeviceRTImpl_ispc.h"
#include "common/FeatureFlagsEnum.h"
#include "rkcommon/memory/malloc.h"
#include <cstring> // for memcpy
namespace ospray {
namespace devicert {
/////////////////////////////////////////////////////////////////////
// AsyncEvent implementation
void AsyncEventImpl::jobScheduled()
{
std::lock_guard<std::mutex> lock(mtx);
ready = false;
}
void AsyncEventImpl::jobStarted()
{
std::lock_guard<std::mutex> lock(mtx);
timer.start();
}
void AsyncEventImpl::jobFinished()
{
{
std::lock_guard<std::mutex> lock(mtx);
timer.stop();
ready = true;
}
cv.notify_one();
}
void AsyncEventImpl::wait() const
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return ready; });
}
bool AsyncEventImpl::finished() const
{
std::lock_guard<std::mutex> lock(mtx);
return ready;
}
float AsyncEventImpl::getDuration() const
{
std::lock_guard<std::mutex> lock(mtx);
return timer.seconds();
}
void *AsyncEventImpl::getSyclEventPtr()
{
return nullptr;
}
/////////////////////////////////////////////////////////////////////
// Device implementation
// Base command class
struct DeviceImpl::Command
{
Command() = default;
Command(std::shared_ptr<AsyncEventImpl> event) : event(event) {}
virtual ~Command() = default;
virtual bool execute() = 0;
friend struct DeviceImpl;
protected:
std::shared_ptr<AsyncEventImpl> event;
};
// Shutdown device thread command
struct DeviceImpl::CommandShutdown : public Command
{
bool execute() override
{
// Just return false for loop exit
return false;
}
};
// MemCpy command
struct DeviceImpl::CommandMemCpy : public Command
{
CommandMemCpy(void *dest,
const void *src,
std::size_t size,
std::shared_ptr<AsyncEventImpl> event)
: Command(event), dest(dest), src(src), size(size)
{}
bool execute() override
{
// Do memory copying
event->jobStarted();
std::memcpy(dest, src, size);
event->jobFinished();
// Continue commands processing
return true;
}
private:
void *const dest;
const void *const src;
const std::size_t size;
};
// Launch render kernel command
struct DeviceImpl::CommandLaunchRenderKernel : public Command
{
CommandLaunchRenderKernel(const vec3ui &itemDims,
RendererKernel kernel,
ispc::Renderer *renderer,
ispc::FrameBuffer *fb,
ispc::Camera *camera,
ispc::World *world,
const uint32_t *taskIDs,
const FeatureFlags &ff,
std::shared_ptr<AsyncEventImpl> event)
: Command(event),
itemDims(itemDims),
rendererKernel(kernel),
renderer(renderer),
fb(fb),
camera(camera),
world(world),
taskIDs(taskIDs),
ff(ff)
{}
bool execute() override
{
// Run kernel
event->jobStarted();
rendererKernel(
nullptr, nullptr, itemDims, renderer, fb, camera, world, taskIDs, ff);
event->jobFinished();
// Continue commands processing
return true;
}
private:
// Domain and kernel pointer
const vec3ui itemDims;
RendererKernel rendererKernel;
// Kernel parameters
ispc::Renderer *renderer;
ispc::FrameBuffer *fb;
ispc::Camera *camera;
ispc::World *world;
const uint32_t *taskIDs;
const FeatureFlags ff;
};
// Launch FrameOp kernel command
struct DeviceImpl::CommandLaunchFrameOpKernel : public Command
{
CommandLaunchFrameOpKernel(const vec2ui &itemDims,
FrameOpKernel kernel,
const ispc::FrameBufferView *fbv,
std::shared_ptr<AsyncEventImpl> event)
: Command(event), itemDims(itemDims), frameOpKernel(kernel), fbv(fbv)
{}
bool execute() override
{
// Run kernel
event->jobStarted();
frameOpKernel(nullptr, nullptr, itemDims, fbv);
event->jobFinished();
// Continue commands processing
return true;
}
private:
// Domain and kernel pointer
const vec2ui itemDims;
FrameOpKernel frameOpKernel;
// Kernel parameters
const ispc::FrameBufferView *fbv;
};
// Run host task command
struct DeviceImpl::CommandRunHostTask : public Command
{
CommandRunHostTask(
const std::function<void()> task, std::shared_ptr<AsyncEventImpl> event)
: Command(event), hostTask(task)
{}
bool execute() override
{
// Run task
event->jobStarted();
hostTask();
event->jobFinished();
// Continue commands processing
return true;
}
private:
const std::function<void()> hostTask;
};
DeviceImpl::DeviceImpl(bool debug)
: Device(debug), workerThread([this]() {
// Main worker thread loop
CommandPtr cmd;
do {
// Lock the mutex
std::unique_lock<std::mutex> lock(queueMtx);
// Atomically release the mutex and wait. At this point, the mutex
// is released, allowing main thread to acquire it
queueCV.wait(lock, [this] { return !commandQueue.empty(); });
// When notified by the main thread and the condition is true, the
// mutex is re-acquired by the wait function before returning so we
// can safely retrieve command from the queue
cmd = std::move(commandQueue.front());
commandQueue.pop();
// Process the command
} while (cmd->execute());
})
{}
DeviceImpl::DeviceImpl(uint32_t, bool debug) : DeviceImpl(debug) {}
DeviceImpl::DeviceImpl(void *, void *, bool debug) : DeviceImpl(debug) {}
DeviceImpl::~DeviceImpl()
{
// Signal the worker thread to terminate by adding shutdown command
scheduleCommand(std::unique_ptr<Command>(new CommandShutdown()));
// And finish worker thread
workerThread.join();
}
void DeviceImpl::scheduleCommand(CommandPtr cmd)
{
{
std::lock_guard<std::mutex> lock(queueMtx);
if (cmd->event)
cmd->event->jobScheduled();
commandQueue.push(std::move(cmd));
}
queueCV.notify_one();
}
void *DeviceImpl::deviceMalloc(std::size_t size)
{
return rkcommon::memory::alignedMalloc(size);
}
void *DeviceImpl::sharedMalloc(std::size_t size)
{
return rkcommon::memory::alignedMalloc(size);
}
void *DeviceImpl::hostMalloc(std::size_t size)
{
return rkcommon::memory::alignedMalloc(size);
}
void DeviceImpl::free(void *ptr)
{
rkcommon::memory::alignedFree(ptr);
}
Alloc DeviceImpl::getPointerType(void *) const
{
return Alloc::Host;
}
void DeviceImpl::wait()
{
// Get the last event if exists
std::shared_ptr<AsyncEventImpl> lastEvent;
{
std::lock_guard<std::mutex> lock(queueMtx);
if (commandQueue.empty())
return;
lastEvent = commandQueue.back()->event;
}
// And wait for its completion
lastEvent->wait();
}
AsyncEvent DeviceImpl::createAsyncEvent()
{
std::shared_ptr<AsyncEventImpl> eventImpl =
std::make_shared<AsyncEventImpl>();
return AsyncEvent(eventImpl);
}
AsyncEvent DeviceImpl::memcpy(void *dest, const void *src, std::size_t size)
{
// Create a command with event
auto eventImpl = std::make_shared<AsyncEventImpl>();
CommandPtr cmd =
std::unique_ptr<Command>(new CommandMemCpy(dest, src, size, eventImpl));
// Put the command into the queue and signal the worker thread
scheduleCommand(std::move(cmd));
return AsyncEvent(eventImpl);
}
AsyncEvent DeviceImpl::launchRendererKernel(const vec3ui &itemDims,
RendererKernel kernel,
ispc::Renderer *renderer,
ispc::FrameBuffer *fb,
ispc::Camera *camera,
ispc::World *world,
const uint32_t *taskIDs,
const FeatureFlags &ff)
{
// Create a command with event
auto eventImpl = std::make_shared<AsyncEventImpl>();
CommandPtr cmd = std::unique_ptr<Command>(new CommandLaunchRenderKernel(
itemDims, kernel, renderer, fb, camera, world, taskIDs, ff, eventImpl));
// Put the command into the queue and signal the worker thread
scheduleCommand(std::move(cmd));
return AsyncEvent(eventImpl);
}
AsyncEvent DeviceImpl::launchFrameOpKernel(const vec2ui &itemDims,
FrameOpKernel kernel,
const ispc::FrameBufferView *fbv)
{
// Create a command with event
auto eventImpl = std::make_shared<AsyncEventImpl>();
CommandPtr cmd = std::unique_ptr<Command>(
new CommandLaunchFrameOpKernel(itemDims, kernel, fbv, eventImpl));
// Put the command into the queue and signal the worker thread
scheduleCommand(std::move(cmd));
return AsyncEvent(eventImpl);
}
AsyncEvent DeviceImpl::launchHostTask(const std::function<void()> &task)
{
// Create a command with event
auto eventImpl = std::make_shared<AsyncEventImpl>();
CommandPtr cmd =
std::unique_ptr<Command>(new CommandRunHostTask(task, eventImpl));
// Put the command into the queue and signal the worker thread
scheduleCommand(std::move(cmd));
return AsyncEvent(eventImpl);
}
void *DeviceImpl::getSyclDevicePtr()
{
// SYCL not used
return nullptr;
}
void *DeviceImpl::getSyclContextPtr()
{
// SYCL not used
return nullptr;
}
void *DeviceImpl::getSyclQueuePtr()
{
// SYCL not used
return nullptr;
}
} // namespace devicert
} // namespace ospray
|