2025-11-01 00:06:42 -04:00
|
|
|
#include <config.h>
|
2025-10-25 19:28:18 -04:00
|
|
|
#include <opts.h>
|
|
|
|
|
#include <algorithm>
|
2025-11-01 00:06:42 -04:00
|
|
|
#include <unistd.h>
|
2025-11-10 01:02:06 -04:00
|
|
|
#include <iomanip>
|
2025-11-01 00:06:42 -04:00
|
|
|
#include <user/spMcRingBuffer.h>
|
2025-10-31 12:22:07 -04:00
|
|
|
#include <componentThread.h>
|
2025-11-10 01:02:06 -04:00
|
|
|
#include <asynchronousLoop.h>
|
|
|
|
|
#include <user/stimulusFrame.h>
|
2025-10-31 12:22:07 -04:00
|
|
|
#include "pcloudStimulusBuffer.h"
|
2025-11-10 01:02:06 -04:00
|
|
|
#include "frameAssemblyDesc.h"
|
2025-10-25 19:28:18 -04:00
|
|
|
|
|
|
|
|
namespace smo {
|
|
|
|
|
namespace stim_buff {
|
|
|
|
|
|
2025-11-04 00:46:07 -04:00
|
|
|
extern const SmoCallbacks* smoHooksPtr;
|
|
|
|
|
|
2025-11-01 21:33:35 -04:00
|
|
|
// OpenCL kernels are used to collate and produce our StimFrames.
|
|
|
|
|
static SpMcRingBuffer::InputEngineConstraints openClInputConstraints(
|
|
|
|
|
static_cast<size_t>(sysconf(_SC_PAGE_SIZE)), sizeof(void *));
|
|
|
|
|
|
|
|
|
|
PcloudStimulusBuffer::PcloudStimulusBuffer(
|
2025-11-04 00:46:07 -04:00
|
|
|
const std::shared_ptr<device::DeviceAttachmentSpec> &deviceAttachmentSpec,
|
2025-11-01 21:33:35 -04:00
|
|
|
std::shared_ptr<livoxProto1::Device> &device,
|
|
|
|
|
const PcloudFormatDesc& formatDesc,
|
|
|
|
|
int histbuffMs,
|
|
|
|
|
size_t nDgramsPerStagingBufferFrame)
|
|
|
|
|
: StimulusBuffer(
|
|
|
|
|
deviceAttachmentSpec,
|
|
|
|
|
static_cast<size_t>(histbuffMs / CONFIG_STIMBUFF_FRAME_PERIOD_MS),
|
|
|
|
|
openClInputConstraints,
|
|
|
|
|
device->componentThread->getIoService()),
|
2025-11-04 00:46:07 -04:00
|
|
|
device(device),
|
2025-11-07 22:07:27 -04:00
|
|
|
formatDesc(formatDesc),
|
2025-11-08 12:20:21 -04:00
|
|
|
openClCollatingAndMeshingEngine(*this),
|
2025-11-07 22:07:27 -04:00
|
|
|
assemblyBuffer(
|
|
|
|
|
StagingBuffer::IOEngineConstraints::ioUringConstraints,
|
|
|
|
|
StagingBuffer::IOEngineConstraints::openClInputConstraints,
|
|
|
|
|
nDgramsPerStagingBufferFrame),
|
2025-11-09 00:55:58 -04:00
|
|
|
ioUringAssemblyEngine(*this, nDgramsPerStagingBufferFrame),
|
2025-11-07 22:07:27 -04:00
|
|
|
collationBuffer(
|
|
|
|
|
StagingBuffer::IOEngineConstraints::openClInputConstraints,
|
|
|
|
|
StagingBuffer::IOEngineConstraints::openClInputConstraints,
|
|
|
|
|
nDgramsPerStagingBufferFrame)
|
2025-11-01 21:33:35 -04:00
|
|
|
{
|
2025-11-08 01:45:47 -04:00
|
|
|
if (smoHooksPtr->OptionParser_getOptions().verbose)
|
|
|
|
|
{
|
|
|
|
|
std::cout << __func__ << ": assembly buffer : "
|
|
|
|
|
<< assemblyBuffer.stringify()
|
|
|
|
|
<< "\n\tcollation buffer : " << collationBuffer.stringify()
|
|
|
|
|
<< "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 00:56:46 -04:00
|
|
|
std::cout << __func__ << ": Device's component thread is "
|
|
|
|
|
<< device->componentThread->name << std::endl;
|
|
|
|
|
|
|
|
|
|
#ifndef CONFIG_WORLD_USE_BODY_THREAD
|
|
|
|
|
if (smoHooksPtr->ComponentThread_getSelf()->id != ComponentThread::WORLD)
|
|
|
|
|
#else
|
|
|
|
|
if (smoHooksPtr->ComponentThread_getSelf()->id != ComponentThread::BODY)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
std::string errMsg = std::string(__func__) +
|
|
|
|
|
": PcloudStimulusBuffer constructor called on non-world/body thread " +
|
|
|
|
|
smoHooksPtr->ComponentThread_getSelf()->name;
|
|
|
|
|
|
|
|
|
|
std::cout << errMsg << std::endl;
|
|
|
|
|
// throw std::runtime_error(errMsg);
|
|
|
|
|
}
|
2025-11-01 21:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 22:03:28 -04:00
|
|
|
void PcloudStimulusBuffer::start()
|
|
|
|
|
{
|
|
|
|
|
// Call ioUringAssemblyEngine setup() as the final step
|
2025-11-04 15:33:55 -04:00
|
|
|
ioUringAssemblyEngine.setup();
|
2025-11-08 12:22:11 -04:00
|
|
|
openClCollatingAndMeshingEngine.setup();
|
2025-11-01 22:03:28 -04:00
|
|
|
// Call base class start() as the final step
|
|
|
|
|
StimulusBuffer::start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PcloudStimulusBuffer::stop()
|
|
|
|
|
{
|
|
|
|
|
// Call base class stop() as the first step
|
|
|
|
|
StimulusBuffer::stop();
|
|
|
|
|
// Call ioUringAssemblyEngine stop() as the final step
|
2025-11-08 12:22:11 -04:00
|
|
|
openClCollatingAndMeshingEngine.finalize();
|
2025-11-04 15:33:55 -04:00
|
|
|
ioUringAssemblyEngine.finalize();
|
2025-11-01 22:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-10 01:02:06 -04:00
|
|
|
void produceStimFrameAck(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 21:33:35 -04:00
|
|
|
void PcloudStimulusBuffer::stimFrameProductionTimesliceInd()
|
|
|
|
|
{
|
2025-11-10 01:02:06 -04:00
|
|
|
produceFrameReq({nullptr, nullptr});
|
2025-11-01 21:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-10 01:02:06 -04:00
|
|
|
class PcloudStimulusBuffer::ProduceFrameReq
|
|
|
|
|
: public PostedAsynchronousContinuation<produceFrameReqCbFn>
|
2025-11-01 04:14:07 -04:00
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
PcloudStimulusBuffer& stimBuff;
|
2025-11-10 01:02:06 -04:00
|
|
|
AsynchronousLoop frameAssemblyResult;
|
|
|
|
|
StimulusFrame& stimulusFrame;
|
2025-11-01 04:14:07 -04:00
|
|
|
|
|
|
|
|
public:
|
2025-11-10 01:02:06 -04:00
|
|
|
ProduceFrameReq(
|
2025-11-01 04:14:07 -04:00
|
|
|
PcloudStimulusBuffer& buffer,
|
2025-11-10 01:02:06 -04:00
|
|
|
const std::shared_ptr<ComponentThread>& caller,
|
|
|
|
|
Callback<produceFrameReqCbFn> cb)
|
|
|
|
|
: PostedAsynchronousContinuation<produceFrameReqCbFn>(caller, cb),
|
|
|
|
|
stimBuff(buffer),
|
|
|
|
|
frameAssemblyResult(0),
|
|
|
|
|
stimulusFrame(buffer.frames_[0])
|
2025-11-01 04:14:07 -04:00
|
|
|
{}
|
|
|
|
|
|
2025-11-10 01:02:06 -04:00
|
|
|
public:
|
2025-11-01 04:14:07 -04:00
|
|
|
void callOriginalCallback()
|
|
|
|
|
{
|
2025-11-10 01:02:06 -04:00
|
|
|
stimBuff.allowNextStimulusFrame();
|
|
|
|
|
callOriginalCb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
void produceFrameReq1_doAssemble_posted(
|
|
|
|
|
std::shared_ptr<ProduceFrameReq> context)
|
|
|
|
|
{
|
|
|
|
|
stimBuff.ioUringAssemblyEngine.assembleFrameReq(
|
|
|
|
|
{context, std::bind(
|
|
|
|
|
&ProduceFrameReq::produceFrameReq2_assembleDone,
|
|
|
|
|
context.get(), context,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void produceFrameReq2_assembleDone(
|
|
|
|
|
std::shared_ptr<ProduceFrameReq> context,
|
|
|
|
|
bool success, AsynchronousLoop loop)
|
|
|
|
|
{
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << __func__ << ": Failed to assemble frame" << std::endl;
|
|
|
|
|
callOriginalCallback();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context->frameAssemblyResult = loop;
|
|
|
|
|
|
|
|
|
|
stimBuff.openClCollatingAndMeshingEngine.compactCollateAndMeshFrameReq(
|
|
|
|
|
loop, stimulusFrame,
|
|
|
|
|
{context, std::bind(
|
|
|
|
|
&ProduceFrameReq::produceFrameReq3_compactCollateDone,
|
|
|
|
|
context.get(), context,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2)});
|
2025-11-01 04:14:07 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-10 01:02:06 -04:00
|
|
|
void produceFrameReq3_compactCollateDone(
|
|
|
|
|
[[maybe_unused]] std::shared_ptr<ProduceFrameReq> context,
|
|
|
|
|
bool success, StimulusFrame& /*stimulusFrame*/)
|
2025-11-01 04:14:07 -04:00
|
|
|
{
|
2025-11-10 01:02:06 -04:00
|
|
|
if (!success) {
|
|
|
|
|
std::cerr << __func__ << ": Failed to compact and collate frame" << std::endl;
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << __func__ << ": Successfully compacted and collated frame" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-12 13:03:39 -04:00
|
|
|
// Print kernel execution durations
|
|
|
|
|
auto compactDuration = stimBuff.openClCollatingAndMeshingEngine.getCompactKernelDuration();
|
|
|
|
|
auto collateDuration = stimBuff.openClCollatingAndMeshingEngine.getCollateKernelDuration();
|
|
|
|
|
std::cout << __func__ << ": compactKernelDuration=" << compactDuration.count()
|
|
|
|
|
<< "ms, collateKernelDuration=" << collateDuration.count() << "ms" << std::endl;
|
|
|
|
|
|
2025-11-10 01:02:06 -04:00
|
|
|
callOriginalCallback();
|
2025-11-01 04:14:07 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-10 01:02:06 -04:00
|
|
|
void PcloudStimulusBuffer::produceFrameReq(
|
|
|
|
|
smo::Callback<produceFrameReqCbFn> callback)
|
2025-11-01 04:14:07 -04:00
|
|
|
{
|
2025-11-10 01:02:06 -04:00
|
|
|
auto caller = smoHooksPtr->ComponentThread_getSelf();
|
|
|
|
|
auto request = std::make_shared<ProduceFrameReq>(
|
|
|
|
|
*this, caller, std::move(callback));
|
|
|
|
|
|
|
|
|
|
// Post the doAssemble method to the component thread
|
|
|
|
|
device->componentThread->getIoService().post(
|
|
|
|
|
STC(std::bind(
|
|
|
|
|
&ProduceFrameReq::produceFrameReq1_doAssemble_posted,
|
|
|
|
|
request.get(), request)));
|
2025-11-01 04:14:07 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-25 19:28:18 -04:00
|
|
|
} // namespace stim_buff
|
|
|
|
|
} // namespace smo
|