HipArray.cpp 6.36 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
10
 * Portions copyright (c) 2012-2022 Stanford University and the Authors.      *
 * Portions copyright (c) 2020-2022 Advanced Micro Devices, Inc.              *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * 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 <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include "HipArray.h"
#include "HipContext.h"
30
#include "openmm/common/ContextSelector.h"
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <sstream>
#include <vector>

using namespace OpenMM;

HipArray::HipArray() : pointer(0), ownsMemory(false) {
}

40
HipArray::HipArray(HipContext& context, size_t size, int elementSize, const std::string& name) : pointer(0) {
41
42
43
44
45
    initialize(context, size, elementSize, name);
}

HipArray::~HipArray() {
    if (pointer != 0 && ownsMemory && context->getContextIsValid()) {
46
        ContextSelector selector(*context);
47
48
49
50
51
52
53
54
55
        hipError_t result = hipFree(pointer);
        if (result != hipSuccess) {
            std::stringstream str;
            str<<"Error deleting array "<<name<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")";
            throw OpenMMException(str.str());
        }
    }
}

56
void HipArray::initialize(ComputeContext& context, size_t size, int elementSize, const std::string& name) {
57
58
59
60
61
62
63
    if (this->pointer != 0)
        throw OpenMMException("HipArray has already been initialized");
    this->context = &dynamic_cast<HipContext&>(context);
    this->size = size;
    this->elementSize = elementSize;
    this->name = name;
    ownsMemory = true;
64
    ContextSelector selector(*this->context);
65
66
67
68
69
70
71
72
    hipError_t result = hipMalloc(&pointer, size*elementSize);
    if (result != hipSuccess) {
        std::stringstream str;
        str<<"Error creating array "<<name<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
}

73
void HipArray::resize(size_t size) {
74
75
76
77
    if (pointer == 0)
        throw OpenMMException("HipArray has not been initialized");
    if (!ownsMemory)
        throw OpenMMException("Cannot resize an array that does not own its storage");
78
    ContextSelector selector(*context);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    hipError_t result = hipFree(pointer);
    if (result != hipSuccess) {
        std::stringstream str;
        str<<"Error deleting array "<<name<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
    pointer = 0;
    initialize(*context, size, elementSize, name);
}

ComputeContext& HipArray::getContext() {
    return *context;
}

void HipArray::uploadSubArray(const void* data, int offset, int elements, bool blocking) {
    if (pointer == 0)
        throw OpenMMException("HipArray has not been initialized");
    if (offset < 0 || offset+elements > getSize())
        throw OpenMMException("uploadSubArray: data exceeds range of array");
    hipError_t result;
    if (blocking)
        result = hipMemcpyHtoD(reinterpret_cast<char*>(pointer)+offset*elementSize, const_cast<void*>(data), elements*elementSize);
    else
        result = hipMemcpyHtoDAsync(reinterpret_cast<char*>(pointer)+offset*elementSize, const_cast<void*>(data), elements*elementSize, context->getCurrentStream());
    if (result != hipSuccess) {
        std::stringstream str;
        str<<"Error uploading array "<<name<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
}

void HipArray::download(void* data, bool blocking) const {
    if (pointer == 0)
        throw OpenMMException("HipArray has not been initialized");
    hipError_t result;
    if (blocking)
        result = hipMemcpyDtoH(data, pointer, size*elementSize);
    else
        result = hipMemcpyDtoHAsync(data, pointer, size*elementSize, context->getCurrentStream());
    if (result != hipSuccess) {
        std::stringstream str;
        str<<"Error downloading array "<<name<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
}

void HipArray::copyTo(ArrayInterface& dest) const {
    if (pointer == 0)
        throw OpenMMException("HipArray has not been initialized");
    if (dest.getSize() != size || dest.getElementSize() != elementSize)
        throw OpenMMException("Error copying array "+name+" to "+dest.getName()+": The destination array does not match the size of the array");
    HipArray& cuDest = context->unwrap(dest);
    hipError_t result = hipMemcpyDtoDAsync(cuDest.getDevicePointer(), pointer, size*elementSize, context->getCurrentStream());
    if (result != hipSuccess) {
        std::stringstream str;
        str<<"Error copying array "<<name<<" to "<<dest.getName()<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
}