/* -------------------------------------------------------------------------- * * OpenMM * * -------------------------------------------------------------------------- * * This is part of the OpenMM molecular simulation toolkit originating from * * Simbios, the NIH National Center for Physics-Based Simulation of * * Biological Structures at Stanford, funded under the NIH Roadmap for * * Medical Research, grant U54 GM072970. See https://simtk.org. * * * * Portions copyright (c) 2012-2019 Stanford University and the Authors. * * Portions copyright (c) 2020 Advanced Micro Devices, Inc. * * Authors: Peter Eastman, Nicholas Curtis * * Contributors: * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as published * * by the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public License * * along with this program. If not, see . * * -------------------------------------------------------------------------- */ #include "HipArray.h" #include "HipContext.h" #include #include #include using namespace OpenMM; HipArray::HipArray() : pointer(0), ownsMemory(false) { } HipArray::HipArray(HipContext& context, int size, int elementSize, const std::string& name) : pointer(0) { initialize(context, size, elementSize, name); } HipArray::~HipArray() { if (pointer != 0 && ownsMemory && context->getContextIsValid()) { context->setAsCurrent(); hipError_t result = hipFree(pointer); if (result != hipSuccess) { std::stringstream str; str<<"Error deleting array "<pointer != 0) throw OpenMMException("HipArray has already been initialized"); this->context = &dynamic_cast(context); this->size = size; this->elementSize = elementSize; this->name = name; ownsMemory = true; hipError_t result = hipMalloc(&pointer, size*elementSize); if (result != hipSuccess) { std::stringstream str; str<<"Error creating array "< getSize()) throw OpenMMException("uploadSubArray: data exceeds range of array"); hipError_t result; if (blocking) result = hipMemcpyHtoD(reinterpret_cast(pointer)+offset*elementSize, const_cast(data), elements*elementSize); else result = hipMemcpyHtoDAsync(reinterpret_cast(pointer)+offset*elementSize, const_cast(data), elements*elementSize, context->getCurrentStream()); if (result != hipSuccess) { std::stringstream str; str<<"Error uploading array "<getCurrentStream()); if (result != hipSuccess) { std::stringstream str; str<<"Error downloading array "<unwrap(dest); hipError_t result = hipMemcpyDtoDAsync(cuDest.getDevicePointer(), pointer, size*elementSize, context->getCurrentStream()); if (result != hipSuccess) { std::stringstream str; str<<"Error copying array "<