CudaArray.cpp 6.22 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
 * Portions copyright (c) 2012-2022 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * Authors: Peter Eastman                                                     *
 * 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 "CudaArray.h"
28
#include "CudaContext.h"
29
#include "openmm/common/ContextSelector.h"
30
31
32
33
34
35
#include <iostream>
#include <sstream>
#include <vector>

using namespace OpenMM;

Peter Eastman's avatar
Peter Eastman committed
36
37
38
CudaArray::CudaArray() : pointer(0), ownsMemory(false) {
}

39
CudaArray::CudaArray(CudaContext& context, size_t size, int elementSize, const std::string& name) : pointer(0) {
Peter Eastman's avatar
Peter Eastman committed
40
    initialize(context, size, elementSize, name);
41
42
43
}

CudaArray::~CudaArray() {
Peter Eastman's avatar
Peter Eastman committed
44
    if (pointer != 0 && ownsMemory && context->getContextIsValid()) {
45
        ContextSelector selector(*context);
46
47
48
        CUresult result = cuMemFree(pointer);
        if (result != CUDA_SUCCESS) {
            std::stringstream str;
49
            str<<"Error deleting array "<<name<<": "<<CudaContext::getErrorString(result)<<" ("<<result<<")";
50
51
52
53
54
            throw OpenMMException(str.str());
        }
    }
}

55
void CudaArray::initialize(ComputeContext& context, size_t size, int elementSize, const std::string& name) {
Peter Eastman's avatar
Peter Eastman committed
56
57
    if (this->pointer != 0)
        throw OpenMMException("CudaArray has already been initialized");
58
    this->context = &dynamic_cast<CudaContext&>(context);
Peter Eastman's avatar
Peter Eastman committed
59
60
61
62
    this->size = size;
    this->elementSize = elementSize;
    this->name = name;
    ownsMemory = true;
63
    ContextSelector selector(*this->context);
Peter Eastman's avatar
Peter Eastman committed
64
65
66
67
68
69
70
71
    CUresult result = cuMemAlloc(&pointer, size*elementSize);
    if (result != CUDA_SUCCESS) {
        std::stringstream str;
        str<<"Error creating array "<<name<<": "<<CudaContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
}

72
void CudaArray::resize(size_t size) {
Peter Eastman's avatar
Peter Eastman committed
73
74
75
76
    if (pointer == 0)
        throw OpenMMException("CudaArray has not been initialized");
    if (!ownsMemory)
        throw OpenMMException("Cannot resize an array that does not own its storage");
77
    ContextSelector selector(*context);
Peter Eastman's avatar
Peter Eastman committed
78
79
80
81
82
83
84
85
86
87
    CUresult result = cuMemFree(pointer);
    if (result != CUDA_SUCCESS) {
        std::stringstream str;
        str<<"Error deleting array "<<name<<": "<<CudaContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
    pointer = 0;
    initialize(*context, size, elementSize, name);
}

88
89
90
91
ComputeContext& CudaArray::getContext() {
    return *context;
}

92
void CudaArray::uploadSubArray(const void* data, int offset, int elements, bool blocking) {
Peter Eastman's avatar
Peter Eastman committed
93
94
    if (pointer == 0)
        throw OpenMMException("CudaArray has not been initialized");
95
96
    if (offset < 0 || offset+elements > getSize())
        throw OpenMMException("uploadSubArray: data exceeds range of array");
97
98
    CUresult result;
    if (blocking)
99
        result = cuMemcpyHtoD(pointer+offset*elementSize, data, elements*elementSize);
100
    else
101
        result = cuMemcpyHtoDAsync(pointer+offset*elementSize, data, elements*elementSize, context->getCurrentStream());
102
103
    if (result != CUDA_SUCCESS) {
        std::stringstream str;
104
        str<<"Error uploading array "<<name<<": "<<CudaContext::getErrorString(result)<<" ("<<result<<")";
105
106
107
108
109
        throw OpenMMException(str.str());
    }
}

void CudaArray::download(void* data, bool blocking) const {
Peter Eastman's avatar
Peter Eastman committed
110
111
    if (pointer == 0)
        throw OpenMMException("CudaArray has not been initialized");
112
113
114
115
    CUresult result;
    if (blocking)
        result = cuMemcpyDtoH(data, pointer, size*elementSize);
    else
Peter Eastman's avatar
Peter Eastman committed
116
        result = cuMemcpyDtoHAsync(data, pointer, size*elementSize, context->getCurrentStream());
117
118
    if (result != CUDA_SUCCESS) {
        std::stringstream str;
119
        str<<"Error downloading array "<<name<<": "<<CudaContext::getErrorString(result)<<" ("<<result<<")";
120
121
122
        throw OpenMMException(str.str());
    }
}
123

124
void CudaArray::copyTo(ArrayInterface& dest) const {
Peter Eastman's avatar
Peter Eastman committed
125
126
    if (pointer == 0)
        throw OpenMMException("CudaArray has not been initialized");
127
128
    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");
129
130
    CudaArray& cuDest = context->unwrap(dest);
    CUresult result = cuMemcpyDtoDAsync(cuDest.getDevicePointer(), pointer, size*elementSize, context->getCurrentStream());
131
132
133
134
135
136
    if (result != CUDA_SUCCESS) {
        std::stringstream str;
        str<<"Error copying array "<<name<<" to "<<dest.getName()<<": "<<CudaContext::getErrorString(result)<<" ("<<result<<")";
        throw OpenMMException(str.str());
    }
}