/******************************************************************************** * Copyright (c) 2023 CEA-List * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 * ********************************************************************************/ #include "aidge/data/Tensor.hpp" #include "aidge/utils/Types.h" #include "aidge/utils/ErrorHandling.hpp" void Aidge::Tensor::copyCastFrom(const Tensor& src, std::shared_ptr<Tensor>& convertedSrcPtr) { if (src == *this) { return; } const Tensor& convertedSrc = src.refCast(convertedSrcPtr, dataType()); getImpl()->copyFrom(*(convertedSrc.getImpl()), convertedSrc.size()); } Aidge::Tensor& Aidge::Tensor::refCast(std::shared_ptr<Tensor>& fallback, const Aidge::DataType& dt) { // Scott Meyers' solution to avoid code duplication return const_cast<Tensor&>(static_cast<const Tensor&>(*this).refCast(fallback, dt)); } const Aidge::Tensor& Aidge::Tensor::refCast(std::shared_ptr<Tensor>& fallback, const Aidge::DataType& dt) const { if (dt == dataType()) { return *this; } else { if (!fallback) { fallback = std::make_shared<Tensor>(dt); } else { fallback->setDataType(dt); } const auto device = getImpl()->device(); fallback->setBackend(device.first, device.second); fallback->resize(dims()); fallback->getImpl()->copyCast(getImpl()->rawPtr(), size(), dataType()); return *fallback; } } Aidge::Tensor& Aidge::Tensor::ref(std::shared_ptr<Tensor>& fallback, const std::string &backend, int device) { // Scott Meyers' solution to avoid code duplication return const_cast<Tensor&>(static_cast<const Tensor&>(*this).ref(fallback, backend, device)); } const Aidge::Tensor& Aidge::Tensor::ref(std::shared_ptr<Tensor>& fallback, const std::string &backend, int device) const { if (std::make_pair(backend, device) == getImpl()->device()) { return *this; } else { if (!fallback) { fallback = std::make_shared<Tensor>(dataType()); } else { fallback->setDataType(dataType()); } fallback->setBackend(backend, device); fallback->resize(dims()); fallback->getImpl()->copyFrom(*getImpl(), size()); return *fallback; } }