OpenCLArray.cpp 6.28 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-2025 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 "OpenCLArray.h"
peastman's avatar
peastman committed
28
#include "OpenCLContext.h"
29
#include "OpenCLQueue.h"
30
31
32
33
34
35
#include <iostream>
#include <sstream>
#include <vector>

using namespace OpenMM;

peastman's avatar
peastman committed
36
37
38
OpenCLArray::OpenCLArray() : buffer(NULL), ownsBuffer(false) {
}

39
OpenCLArray::OpenCLArray(OpenCLContext& context, size_t size, int elementSize, const std::string& name, cl_int flags) : buffer(NULL) {
peastman's avatar
peastman committed
40
41
42
    initialize(context, size, elementSize, name, flags);
}

43
OpenCLArray::OpenCLArray(OpenCLContext& context, cl::Buffer* buffer, size_t size, int elementSize, const std::string& name) : buffer(NULL) {
peastman's avatar
peastman committed
44
45
46
47
48
49
50
51
    initialize(context, buffer, size, elementSize, name);
}

OpenCLArray::~OpenCLArray() {
    if (buffer != NULL && ownsBuffer)
        delete buffer;
}

52
void OpenCLArray::initialize(ComputeContext& context, size_t size, int elementSize, const std::string& name) {
53
54
55
    initialize(dynamic_cast<OpenCLContext&>(context), size, elementSize, name, CL_MEM_READ_WRITE);
}

56
void OpenCLArray::initialize(OpenCLContext& context, size_t size, int elementSize, const std::string& name, cl_int flags) {
peastman's avatar
peastman committed
57
58
59
60
61
62
63
64
    if (buffer != NULL)
        throw OpenMMException("OpenCLArray has already been initialized");
    this->context = &context;
    this->size = size;
    this->elementSize = elementSize;
    this->name = name;
    this->flags = flags;
    ownsBuffer = true;
65
66
67
68
69
70
71
72
73
74
    try {
        buffer = new cl::Buffer(context.getContext(), flags, size*elementSize);
    }
    catch (cl::Error err) {
        std::stringstream str;
        str<<"Error creating array "<<name<<": "<<err.what()<<" ("<<err.err()<<")";
        throw OpenMMException(str.str());
    }
}

75
void OpenCLArray::initialize(OpenCLContext& context, cl::Buffer* buffer, size_t size, int elementSize, const std::string& name) {
peastman's avatar
peastman committed
76
77
78
79
80
81
82
83
    if (this->buffer != NULL)
        throw OpenMMException("OpenCLArray has already been initialized");
    this->context = &context;
    this->buffer = buffer;
    this->size = size;
    this->elementSize = elementSize;
    this->name = name;
    ownsBuffer = false;
84
85
}

86
void OpenCLArray::resize(size_t size) {
peastman's avatar
peastman committed
87
88
89
90
91
92
93
    if (buffer == NULL)
        throw OpenMMException("OpenCLArray has not been initialized");
    if (!ownsBuffer)
        throw OpenMMException("Cannot resize an array that does not own its storage");
    delete buffer;
    buffer = NULL;
    initialize(*context, size, elementSize, name, flags);
94
95
}

96
97
98
99
ComputeContext& OpenCLArray::getContext() {
    return *context;
}

100
101
102
103
cl::CommandQueue OpenCLArray::getQueue() const {
    return dynamic_cast<OpenCLQueue*>(context->getCurrentQueue().get())->getQueue();
}

104
void OpenCLArray::uploadSubArray(const void* data, int offset, int elements, bool blocking) {
peastman's avatar
peastman committed
105
106
    if (buffer == NULL)
        throw OpenMMException("OpenCLArray has not been initialized");
107
108
    if (offset < 0 || offset+elements > getSize())
        throw OpenMMException("uploadSubArray: data exceeds range of array");
109
    try {
110
        getQueue().enqueueWriteBuffer(*buffer, blocking ? CL_TRUE : CL_FALSE, offset*elementSize, elements*elementSize, data);
111
112
113
114
115
116
117
118
119
    }
    catch (cl::Error err) {
        std::stringstream str;
        str<<"Error uploading array "<<name<<": "<<err.what()<<" ("<<err.err()<<")";
        throw OpenMMException(str.str());
    }
}

void OpenCLArray::download(void* data, bool blocking) const {
peastman's avatar
peastman committed
120
121
    if (buffer == NULL)
        throw OpenMMException("OpenCLArray has not been initialized");
122
    try {
123
        getQueue().enqueueReadBuffer(*buffer, blocking ? CL_TRUE : CL_FALSE, 0, size*elementSize, data);
124
125
126
127
128
129
130
131
    }
    catch (cl::Error err) {
        std::stringstream str;
        str<<"Error downloading array "<<name<<": "<<err.what()<<" ("<<err.err()<<")";
        throw OpenMMException(str.str());
    }
}

132
void OpenCLArray::copyTo(ArrayInterface& dest) const {
peastman's avatar
peastman committed
133
134
    if (buffer == NULL)
        throw OpenMMException("OpenCLArray has not been initialized");
135
136
    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");
137
    OpenCLArray& clDest = context->unwrap(dest);
138
    try {
139
        getQueue().enqueueCopyBuffer(*buffer, clDest.getDeviceBuffer(), 0, 0, size*elementSize);
140
141
142
143
144
145
146
    }
    catch (cl::Error err) {
        std::stringstream str;
        str<<"Error copying array "<<name<<" to "<<dest.getName()<<": "<<err.what()<<" ("<<err.err()<<")";
        throw OpenMMException(str.str());
    }
}