"platforms/vscode:/vscode.git/clone" did not exist on "72f2205ab2c4bce851dc1ff46cded0cdc84bfa39"
OpenCLContext.h 4.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef OPENMM_OPENCLCONTEXT_H_
#define OPENMM_OPENCLCONTEXT_H_

/* -------------------------------------------------------------------------- *
 *                                   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) 2009 Stanford University and the Authors.           *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

#define __CL_ENABLE_EXCEPTIONS
31
#include <cl.hpp>
32
33
34
35
36
37

namespace OpenMM {

template <class T>
class OpenCLArray;

38
39
40
41
42
43
44
45
46
47
/**
 * We can't use cl_float4, since different OpenCL implementations currently define it in
 * incompatible ways.  Hopefully that will be fixed in the future.  In the mean time, we
 * define our own type to represent float4 on the host.
 */

typedef struct {
    cl_float x, y, z, w;
} mm_float4;

48
49
50
51
52
53
/**
 * This class contains the information associated with a Context by the OpenCL Platform.
 */

class OpenCLContext {
public:
54
55
    static const int ThreadBlockSize = 64;
    static const int TileSize = 32;
56
57
58
59
60
61
    OpenCLContext(int numParticles, int platformIndex, int deviceIndex);
    ~OpenCLContext();
    /**
     * Get the cl::Context associated with this object.
     */
    cl::Context& getContext() {
62
        return context;
63
64
65
66
67
    }
    /**
     * Get the cl::CommandQueue associated with this object.
     */
    cl::CommandQueue& getQueue() {
68
        return queue;
69
70
71
72
    }
    /**
     * Get the array which contains the position and charge of each atom.
     */
73
    OpenCLArray<mm_float4>& getPosq() {
74
75
76
77
78
        return *posq;
    }
    /**
     * Get the array which contains the velocity and massof each atom.
     */
79
    OpenCLArray<mm_float4>& getVelm() {
80
81
82
83
84
        return *velm;
    }
    /**
     * Get the array which contains the force on each atom.
     */
85
    OpenCLArray<mm_float4>& getForce() {
86
87
        return *force;
    }
88
89
90
    /**
     * Get the array which contains the buffers in which forces are computed.
     */
91
    OpenCLArray<mm_float4>& getForceBuffers() {
92
93
        return *forceBuffers;
    }
94
95
96
97
98
99
    /**
     * Get the array which contains the index of each atom.
     */
    OpenCLArray<cl_int>& getAtomIndex() {
        return *atomIndex;
    }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    /**
     * Load OpenCL source code from a file in the kernels directory.
     */
    std::string loadSourceFromFile(const std::string& filename) const;
    /**
     * Create an OpenCL Program from source code.
     */
    cl::Program createProgram(const std::string source);
    /**
     * Set all elements of an array to 0.
     */
    void clearBuffer(OpenCLArray<float>& array);
    /**
     * Set all elements of an array to 0.
     */
115
    void clearBuffer(OpenCLArray<mm_float4>& array);
116
117
118
119
120
121
122
    int numAtoms;
    int paddedNumAtoms;
    int numAtomBlocks;
    int numTiles;
    int numThreadBlocks;
    int numForceBuffers;
    bool forceBufferPerWarp;
123
private:
124
125
126
127
128
    cl::Context context;
    cl::Device device;
    cl::CommandQueue queue;
    cl::Program utilities;
    cl::Kernel clearBufferKernel;
129
130
131
132
    OpenCLArray<mm_float4>* posq;
    OpenCLArray<mm_float4>* velm;
    OpenCLArray<mm_float4>* force;
    OpenCLArray<mm_float4>* forceBuffers;
133
134
135
136
137
138
    OpenCLArray<cl_int>* atomIndex;
};

} // namespace OpenMM

#endif /*OPENMM_OPENCLCONTEXT_H_*/