Skip to content

Commit

Permalink
Unlock page-locked output buffer when the scheme is stopped.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjarosik committed Dec 16, 2021
1 parent 102c0db commit 491d410
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ set(ARRUS_PYTHON_INSTALL_DIR python)
set(Us4_LIB_DIR ${Us4_ROOT_DIR}/lib64)

option(ARRUS_BUILD_PY "Build python API." OFF)
option(ARRUS_PY_VERSION "Python version for which the ARRUS package should be built." 3.8)
set(ARRUS_PY_VERSION "3.8" CACHE STRING "Python version for which the ARRUS package should be built.")
option(ARRUS_BUILD_MATLAB "Build MATLAB API." OFF)
option(ARRUS_BUILD_DOCS "Build documentation." OFF)
option(ARRUS_RUN_TESTS "Run all tests builded packages." OFF)
Expand Down
3 changes: 3 additions & 0 deletions arrus/core/devices/probe/ProbeImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ void ProbeImpl::registerOutputBuffer(Us4ROutputBuffer *buffer,
bool isTriggerSync) {
adapter->registerOutputBuffer(buffer, us4rBuffer, isTriggerSync);
}
void ProbeImpl::unregisterOutputBuffer(Us4ROutputBuffer *buffer, const Us4RBuffer::Handle &us4RBuffer) {
adapter->unregisterOutputBuffer(buffer, us4RBuffer);
}

// Remaps FCM according to given rx aperture active channels mappings.
FrameChannelMapping::Handle ProbeImpl::remapFcm(
Expand Down
2 changes: 2 additions & 0 deletions arrus/core/devices/probe/ProbeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ProbeImpl : public ProbeImplBase {

void registerOutputBuffer(Us4ROutputBuffer *buffer, const Us4RBuffer::Handle &us4rBuffer, bool isTriggerSync) override;

void unregisterOutputBuffer(Us4ROutputBuffer *buffer, const Us4RBuffer::Handle &handle) override;

static FrameChannelMapping::Handle remapFcm(const FrameChannelMapping::Handle &adapterFcm,
const std::vector<std::vector<ChannelIdx>> &adapterActiveChannels,
const std::vector<ChannelIdx> &rxPaddingLeft,
Expand Down
1 change: 1 addition & 0 deletions arrus/core/devices/probe/ProbeImplBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ProbeImplBase : public Probe, public UltrasoundDevice {
uint16 rxBatchSize, std::optional<float> sri, bool triggerSync) = 0;

virtual void registerOutputBuffer(Us4ROutputBuffer *, const Us4RBuffer::Handle &, bool isTriggerSync) = 0;
virtual void unregisterOutputBuffer(Us4ROutputBuffer *, const Us4RBuffer::Handle &) = 0;
};

}
Expand Down
5 changes: 4 additions & 1 deletion arrus/core/devices/us4r/Us4RImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ Us4RImpl::upload(const ops::us4r::TxRxSequence &seq,
auto dataType = element.getDataType();
// If the output buffer already exists - remove it.
if (this->buffer) {
// The buffer should be already unregistered (after stopping the device).
this->buffer.reset();
}
// Create output buffer.
this->buffer = std::make_shared<Us4ROutputBuffer>(us4oemComponentSize, shape, dataType, hostBufferNElements);
getProbeImpl()->registerOutputBuffer(this->buffer.get(), rxBuffer, isTriggerSync);
this->us4rBuffer = std::move(rxBuffer);
getProbeImpl()->registerOutputBuffer(this->buffer.get(), this->us4rBuffer, isTriggerSync);
return {this->buffer, std::move(fcm)};
}

Expand Down Expand Up @@ -153,6 +155,7 @@ void Us4RImpl::stopDevice() {
if (this->buffer != nullptr) {
this->buffer->shutdown();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
getProbeImpl()->unregisterOutputBuffer(this->buffer.get(), this->us4rBuffer);
}
this->state = State::STOPPED;
}
Expand Down
1 change: 1 addition & 0 deletions arrus/core/devices/us4r/Us4RImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Us4RImpl : public Us4R {
std::optional<ProbeAdapterImplBase::Handle> probeAdapter;
std::optional<ProbeImplBase::Handle> probe;
std::optional<HighVoltageSupplier::Handle> hv;
std::unique_ptr<Us4RBuffer> us4rBuffer;
std::shared_ptr<Us4ROutputBuffer> buffer;
State state{State::STOPPED};
// AFE parameters.
Expand Down
53 changes: 41 additions & 12 deletions arrus/core/devices/us4r/probeadapter/ProbeAdapterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ ProbeAdapterImpl::setTxRxSequence(const std::vector<TxRxParameters> &seq,
// and has no assigned value.
ARRUS_REQUIRES_DATA_TYPE_E(
dstModuleChannel, int8,
::arrus::ArrusException(
"Invalid dstModuleChannel data type, "
"rx aperture is outise."));
::arrus::ArrusException("Invalid dstModuleChannel data type, rx aperture is outside."));
if (FrameChannelMapping::isChannelUnavailable((int8)dstModuleChannel)) {
outFcBuilder.setChannelMapping(
frameIdx, activeRxChIdx + op.getRxPadding()[0],
Expand Down Expand Up @@ -305,15 +303,7 @@ void ProbeAdapterImpl::registerOutputBuffer(Us4ROutputBuffer *buffer, const Us4R
void ProbeAdapterImpl::registerOutputBuffer(Us4ROutputBuffer *outputBuffer, const Us4OEMBuffer &us4oemBuffer,
Us4OEMImplBase::RawHandle us4oem, bool isTriggerSync) {
// Each transfer should have the same size.
std::unordered_set<size_t> sizes;
for (auto &element: us4oemBuffer.getElements()) {
sizes.insert(element.getSize());
}
if (sizes.size() > 1) {
throw ::arrus::ArrusException("Each us4oem buffer element should have the same size.");
}
// This is the size of a single element produced by this us4oem.
const size_t elementSize = *std::begin(sizes);
size_t elementSize = getUniqueUs4OEMBufferElementSize(us4oemBuffer);
if (elementSize == 0) {
// This us4oem will not transfer any data, so the buffer registration has no sense here.
return;
Expand Down Expand Up @@ -449,4 +439,43 @@ void ProbeAdapterImpl::registerOutputBuffer(Us4ROutputBuffer *outputBuffer, cons
});
}

size_t ProbeAdapterImpl::getUniqueUs4OEMBufferElementSize(const Us4OEMBuffer &us4oemBuffer) const {
std::unordered_set<size_t> sizes;
for (auto &element: us4oemBuffer.getElements()) {
sizes.insert(element.getSize());
}
if (sizes.size() > 1) {
throw ArrusException("Each us4oem buffer element should have the same size.");
}
// This is the size of a single element produced by this us4oem.
const size_t elementSize = *std::begin(sizes);
return elementSize;
}

void ProbeAdapterImpl::unregisterOutputBuffer(Us4ROutputBuffer *hostBuffer, const Us4RBuffer::Handle &us4rBuffer) {
const size_t hostBufferNElements = hostBuffer->getNumberOfElements();

for (Ordinal i = 0; i < us4oems.size(); ++i) {
auto &us4oem = us4oems[i];
const Ordinal ordinal = us4oem->getDeviceId().getOrdinal();
auto ius4oem = us4oem->getIUs4oem();

auto us4oemBuffer = us4rBuffer->getUs4oemBuffer(i);
size_t elementSize = getUniqueUs4OEMBufferElementSize(us4oemBuffer);
const auto rxBufferNElements = ARRUS_SAFE_CAST(us4oemBuffer.getNumberOfElements(), uint16);
uint16 hostElement = 0, rxElement = 0;

while (hostElement < hostBufferNElements) {
auto dstAddress = hostBuffer->getAddress(hostElement, ordinal);
auto srcAddress = us4oemBuffer.getElement(rxElement).getAddress();
logger->log(LogSeverity::DEBUG,
format("Unregistering transfer: to {} from {}, size {}",
(size_t)dstAddress, (size_t)srcAddress, elementSize));
ius4oem->ReleaseTransferRxBufferToHost(dstAddress, elementSize, srcAddress);
++hostElement;
rxElement = (rxElement + 1) % rxBufferNElements;
}
}
}

}
4 changes: 4 additions & 0 deletions arrus/core/devices/us4r/probeadapter/ProbeAdapterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class ProbeAdapterImpl : public ProbeAdapterImplBase {
const Us4RBuffer::Handle &us4rBuffer,
bool isTriggerSync);

void unregisterOutputBuffer(Us4ROutputBuffer *buffer, const Us4RBuffer::Handle &us4rBuffer);

private:
Logger::Handle logger;
ProbeAdapterModelId modelId;
Expand All @@ -65,6 +67,8 @@ class ProbeAdapterImpl : public ProbeAdapterImplBase {
Us4OEMImplBase::RawHandle getMasterUs4oem() const {
return this->us4oems[0];
}

size_t getUniqueUs4OEMBufferElementSize(const Us4OEMBuffer &us4oemBuffer) const;
};

}
Expand Down
6 changes: 3 additions & 3 deletions arrus/core/devices/us4r/probeadapter/ProbeAdapterImplBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class ProbeAdapterImplBase : public ProbeAdapter {
bool triggerSync) = 0;

virtual
void registerOutputBuffer(Us4ROutputBuffer *buffer,
const Us4RBuffer::Handle &transfers,
bool isTriggerSync) = 0;
void registerOutputBuffer(Us4ROutputBuffer *buffer, const Us4RBuffer::Handle &transfers, bool isTriggerSync) = 0;

virtual void unregisterOutputBuffer(Us4ROutputBuffer *buffer, const Us4RBuffer::Handle &handle) = 0;

virtual Ordinal getNumberOfUs4OEMs() = 0;

Expand Down

0 comments on commit 491d410

Please sign in to comment.