|
| 1 | +/* |
| 2 | + * ****************************************************************************** |
| 3 | + * * |
| 4 | + * * |
| 5 | + * * This program and the accompanying materials are made available under the |
| 6 | + * * terms of the Apache License, Version 2.0 which is available at |
| 7 | + * * https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * * |
| 9 | + * * See the NOTICE file distributed with this work for additional |
| 10 | + * * information regarding copyright ownership. |
| 11 | + * * Unless required by applicable law or agreed to in writing, software |
| 12 | + * * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * * License for the specific language governing permissions and limitations |
| 15 | + * * under the License. |
| 16 | + * * |
| 17 | + * * SPDX-License-Identifier: Apache-2.0 |
| 18 | + * ***************************************************************************** |
| 19 | + */ |
| 20 | + |
| 21 | +#include <helpers/ConstantHelper.h> |
| 22 | +#include <helpers/cuda/CudaShapeBufferCreator.h> |
| 23 | + |
| 24 | +#include "array/CudaPointerDeallocator.h" |
| 25 | +#include "array/PrimaryPointerDeallocator.h" |
| 26 | + |
| 27 | +namespace sd { |
| 28 | + |
| 29 | +ConstantShapeBuffer* CudaShapeBufferCreator::create(const LongType* shapeInfo, int rank) { |
| 30 | + const int shapeInfoLength = shape::shapeInfoLength(rank); |
| 31 | + LongType* shapeCopy = new LongType[shapeInfoLength]; |
| 32 | + std::memcpy(shapeCopy, shapeInfo, shapeInfoLength * sizeof(LongType)); |
| 33 | + |
| 34 | + auto deallocator = std::shared_ptr<PrimaryPointerDeallocator>( |
| 35 | + new PrimaryPointerDeallocator(), |
| 36 | + [] (PrimaryPointerDeallocator* ptr) { delete ptr; } |
| 37 | + ); |
| 38 | + |
| 39 | + auto hPtr = std::make_shared<PointerWrapper>(shapeCopy, deallocator); |
| 40 | + |
| 41 | + // Create device pointer for CUDA |
| 42 | + auto dPtr = std::make_shared<PointerWrapper>( |
| 43 | + ConstantHelper::getInstance().replicatePointer(hPtr->pointer(), |
| 44 | + shape::shapeInfoByteLength(hPtr->pointerAsT<sd::LongType>())), |
| 45 | + std::make_shared<CudaPointerDeallocator>()); |
| 46 | + |
| 47 | + ConstantShapeBuffer *buffer = new ConstantShapeBuffer(hPtr, dPtr); |
| 48 | + |
| 49 | + return buffer; |
| 50 | +} |
| 51 | + |
| 52 | +CudaShapeBufferCreator& CudaShapeBufferCreator::getInstance() { |
| 53 | + static CudaShapeBufferCreator instance; |
| 54 | + return instance; |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace sd |
0 commit comments