mmdeploy/csrc/core/utils/device_utils.cpp
lzhangzz 73cf3b5feb
[Fix] Optimize preprocess & fix pontential use-after-free (#229)
* hold async data and wait only at the end of the pipeline

* fix use-after-free bugs

* fix wording

* bypass trivial cases for Pad to avoid ppl.cv's bug

* fix pad

* fix lint

* cleanup

* fix DefaultFormatBundle

* fix all cpu preprocess impl

* suppress log

* fix dynamic library build & add comments for SyncOnScopeExit
2022-03-28 17:29:22 +08:00

43 lines
1.0 KiB
C++

// Copyright (c) OpenMMLab. All rights reserved.
#include "device_utils.h"
#include "core/logger.h"
namespace mmdeploy {
Result<Mat> MakeAvailableOnDevice(const Mat& src, const Device& device, Stream& stream) {
if (src.device() == device) {
return src;
}
Mat dst{src.height(), src.width(), src.pixel_format(), src.type(), device};
OUTCOME_TRY(stream.Copy(src.buffer(), dst.buffer(), dst.byte_size()));
return dst;
}
Result<Tensor> MakeAvailableOnDevice(const Tensor& src, const Device& device, Stream& stream) {
if (src.device() == device) {
return src;
}
TensorDesc desc{device, src.data_type(), src.shape(), src.name()};
Tensor dst(desc);
OUTCOME_TRY(stream.Copy(src.buffer(), dst.buffer(), src.byte_size()));
return dst;
}
SyncOnScopeExit::~SyncOnScopeExit() {
if (active_ && stream_) {
if (!stream_.Wait()) {
MMDEPLOY_ERROR("Implicit stream synchronization failed.");
} else {
MMDEPLOY_DEBUG("Implicit stream synchronization succeeded.");
}
}
}
} // namespace mmdeploy