"vscode:/vscode.git/clone" did not exist on "37ab709f156aa1733e55b712d1fe838793da8582"
OpenCLSort.cpp 9.89 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.               *
 *                                                                            *
peastman's avatar
peastman committed
9
 * Portions copyright (c) 2010-2018 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * 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/>.      *
 * -------------------------------------------------------------------------- */

peastman's avatar
peastman committed
27
28
29
30
#ifdef _MSC_VER
    // Prevent Windows from defining macros that interfere with other code.
    #define NOMINMAX
#endif
31
32
#include "OpenCLSort.h"
#include "OpenCLKernelSources.h"
peastman's avatar
peastman committed
33
#include <algorithm>
34
#include <map>
peastman's avatar
peastman committed
35
#include <string>
36
37
38
39

using namespace OpenMM;
using namespace std;

peastman's avatar
peastman committed
40
OpenCLSort::OpenCLSort(OpenCLContext& context, SortTrait* trait, unsigned int length) : context(context), trait(trait), dataLength(length) {
41
42
43
44
45
46
47
48
49
50
51
    // Create kernels.

    std::map<std::string, std::string> replacements;
    replacements["DATA_TYPE"] = trait->getDataType();
    replacements["KEY_TYPE"] =  trait->getKeyType();
    replacements["SORT_KEY"] = trait->getSortKey();
    replacements["MIN_KEY"] = trait->getMinKey();
    replacements["MAX_KEY"] = trait->getMaxKey();
    replacements["MAX_VALUE"] = trait->getMaxValue();
    cl::Program program = context.createProgram(context.replaceStrings(OpenCLKernelSources::sort, replacements));
    shortListKernel = cl::Kernel(program, "sortShortList");
peastman's avatar
peastman committed
52
    shortList2Kernel = cl::Kernel(program, "sortShortList2");
53
54
55
56
57
58
59
60
61
62
    computeRangeKernel = cl::Kernel(program, "computeRange");
    assignElementsKernel = cl::Kernel(program, "assignElementsToBuckets");
    computeBucketPositionsKernel = cl::Kernel(program, "computeBucketPositions");
    copyToBucketsKernel = cl::Kernel(program, "copyDataToBuckets");
    sortBucketsKernel = cl::Kernel(program, "sortBuckets");

    // Work out the work group sizes for various kernels.

    unsigned int maxGroupSize = std::min(256, (int) context.getDevice().getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>());
    int maxSharedMem = context.getDevice().getInfo<CL_DEVICE_LOCAL_MEM_SIZE>();
63
64
    unsigned int maxRangeSize = std::min(maxGroupSize, (unsigned int) computeRangeKernel.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(context.getDevice()));
    unsigned int maxPositionsSize = std::min(maxGroupSize, (unsigned int) computeBucketPositionsKernel.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(context.getDevice()));
peastman's avatar
peastman committed
65
66
67
    int maxLocalBuffer = (maxSharedMem/trait->getDataSize())/2;
    unsigned int maxShortList = min(8192, max(maxLocalBuffer, (int) OpenCLContext::ThreadBlockSize*context.getNumThreadBlocks()));
    // On Qualcomm's OpenCL, it's essential to check against CL_KERNEL_WORK_GROUP_SIZE.  Otherwise you get a crash.
68
69
70
    // But AMD's OpenCL returns an inappropriately small value for it that is much shorter than the actual
    // maximum, so including the check hurts performance.  For the moment I'm going to just comment it out.
    // If we officially support Qualcomm in the future, we'll need to do something better.
peastman's avatar
peastman committed
71
72
    //maxShortList = min(maxShortList, shortListKernel.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(context.getDevice()));
    isShortList = (length <= maxShortList);
peastman's avatar
Bug fix  
peastman committed
73
74
75
76
77
    string vendor = context.getDevice().getInfo<CL_DEVICE_VENDOR>();
    if (vendor.size() >= 6 && vendor.substr(0, 6) == "NVIDIA")
        useShortList2 = (dataLength <= OpenCLContext::ThreadBlockSize*context.getNumThreadBlocks());
    else
        useShortList2 = false;
78
    for (rangeKernelSize = 1; rangeKernelSize*2 <= maxRangeSize; rangeKernelSize *= 2)
79
        ;
80
    positionsKernelSize = std::min(rangeKernelSize, maxPositionsSize);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    sortKernelSize = (isShortList ? rangeKernelSize : rangeKernelSize/2);
    if (rangeKernelSize > length)
        rangeKernelSize = length;
    if (sortKernelSize > maxLocalBuffer)
        sortKernelSize = maxLocalBuffer;
    unsigned int targetBucketSize = sortKernelSize/2;
    unsigned int numBuckets = length/targetBucketSize;
    if (numBuckets < 1)
        numBuckets = 1;
    if (positionsKernelSize > numBuckets)
        positionsKernelSize = numBuckets;

    // Create workspace arrays.

    if (!isShortList) {
peastman's avatar
peastman committed
96
97
98
99
        dataRange.initialize(context, 2, trait->getKeySize(), "sortDataRange");
        bucketOffset.initialize<cl_uint>(context, numBuckets, "bucketOffset");
        bucketOfElement.initialize<cl_uint>(context, length, "bucketOfElement");
        offsetInBucket.initialize<cl_uint>(context, length, "offsetInBucket");
100
    }
peastman's avatar
peastman committed
101
    buckets.initialize(context, length, trait->getDataSize(), "buckets");
102
103
104
105
106
107
108
109
110
111
112
113
}

OpenCLSort::~OpenCLSort() {
    delete trait;
}

void OpenCLSort::sort(OpenCLArray& data) {
    if (data.getSize() != dataLength || data.getElementSize() != trait->getDataSize())
        throw OpenMMException("OpenCLSort called with different data size");
    if (data.getSize() == 0)
        return;
    if (isShortList) {
peastman's avatar
peastman committed
114
        // We can use a simpler sort kernel that does the entire operation in one kernel.
115
        
peastman's avatar
peastman committed
116
        if (useShortList2) {
peastman's avatar
peastman committed
117
118
119
120
121
122
123
124
125
126
127
128
            shortList2Kernel.setArg<cl::Buffer>(0, data.getDeviceBuffer());
            shortList2Kernel.setArg<cl::Buffer>(1, buckets.getDeviceBuffer());
            shortList2Kernel.setArg<cl_int>(2, dataLength);
            context.executeKernel(shortList2Kernel, dataLength);
            buckets.copyTo(data);
        }
        else {
            shortListKernel.setArg<cl::Buffer>(0, data.getDeviceBuffer());
            shortListKernel.setArg<cl_uint>(1, dataLength);
            shortListKernel.setArg(2, dataLength*trait->getDataSize(), NULL);
            context.executeKernel(shortListKernel, sortKernelSize, sortKernelSize);
        }
129
130
131
132
    }
    else {
        // Compute the range of data values.

peastman's avatar
peastman committed
133
        unsigned int numBuckets = bucketOffset.getSize();
134
135
        computeRangeKernel.setArg<cl::Buffer>(0, data.getDeviceBuffer());
        computeRangeKernel.setArg<cl_uint>(1, data.getSize());
peastman's avatar
peastman committed
136
        computeRangeKernel.setArg<cl::Buffer>(2, dataRange.getDeviceBuffer());
137
        computeRangeKernel.setArg(3, rangeKernelSize*trait->getKeySize(), NULL);
Peter Eastman's avatar
Peter Eastman committed
138
139
        computeRangeKernel.setArg(4, rangeKernelSize*trait->getKeySize(), NULL);
        computeRangeKernel.setArg<cl_int>(5, numBuckets);
peastman's avatar
peastman committed
140
        computeRangeKernel.setArg<cl::Buffer>(6, bucketOffset.getDeviceBuffer());
141
142
143
144
145
146
147
        context.executeKernel(computeRangeKernel, rangeKernelSize, rangeKernelSize);

        // Assign array elements to buckets.

        assignElementsKernel.setArg<cl::Buffer>(0, data.getDeviceBuffer());
        assignElementsKernel.setArg<cl_int>(1, data.getSize());
        assignElementsKernel.setArg<cl_int>(2, numBuckets);
peastman's avatar
peastman committed
148
149
150
151
        assignElementsKernel.setArg<cl::Buffer>(3, dataRange.getDeviceBuffer());
        assignElementsKernel.setArg<cl::Buffer>(4, bucketOffset.getDeviceBuffer());
        assignElementsKernel.setArg<cl::Buffer>(5, bucketOfElement.getDeviceBuffer());
        assignElementsKernel.setArg<cl::Buffer>(6, offsetInBucket.getDeviceBuffer());
152
153
154
155
156
        context.executeKernel(assignElementsKernel, data.getSize());

        // Compute the position of each bucket.

        computeBucketPositionsKernel.setArg<cl_int>(0, numBuckets);
peastman's avatar
peastman committed
157
        computeBucketPositionsKernel.setArg<cl::Buffer>(1, bucketOffset.getDeviceBuffer());
158
159
160
161
162
163
        computeBucketPositionsKernel.setArg(2, positionsKernelSize*sizeof(cl_int), NULL);
        context.executeKernel(computeBucketPositionsKernel, positionsKernelSize, positionsKernelSize);

        // Copy the data into the buckets.

        copyToBucketsKernel.setArg<cl::Buffer>(0, data.getDeviceBuffer());
peastman's avatar
peastman committed
164
        copyToBucketsKernel.setArg<cl::Buffer>(1, buckets.getDeviceBuffer());
165
        copyToBucketsKernel.setArg<cl_int>(2, data.getSize());
peastman's avatar
peastman committed
166
167
168
        copyToBucketsKernel.setArg<cl::Buffer>(3, bucketOffset.getDeviceBuffer());
        copyToBucketsKernel.setArg<cl::Buffer>(4, bucketOfElement.getDeviceBuffer());
        copyToBucketsKernel.setArg<cl::Buffer>(5, offsetInBucket.getDeviceBuffer());
169
170
171
172
173
        context.executeKernel(copyToBucketsKernel, data.getSize());

        // Sort each bucket.

        sortBucketsKernel.setArg<cl::Buffer>(0, data.getDeviceBuffer());
peastman's avatar
peastman committed
174
        sortBucketsKernel.setArg<cl::Buffer>(1, buckets.getDeviceBuffer());
175
        sortBucketsKernel.setArg<cl_int>(2, numBuckets);
peastman's avatar
peastman committed
176
        sortBucketsKernel.setArg<cl::Buffer>(3, bucketOffset.getDeviceBuffer());
177
178
179
180
        sortBucketsKernel.setArg(4, sortKernelSize*trait->getDataSize(), NULL);
        context.executeKernel(sortBucketsKernel, ((data.getSize()+sortKernelSize-1)/sortKernelSize)*sortKernelSize, sortKernelSize);
    }
}