2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief Define infrastructure for OpenCL JIT compilation for Gromacs
39 * \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
40 * \author Anca Hamuraru <anca@streamcomputing.eu>
41 * \author Teemu Virolainen <teemu@streamcomputing.eu>
42 * \author Mark Abraham <mark.j.abraham@gmail.com>
47 #include "ocl_caching.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/programcontext.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/stringutil.h"
64 #include "gromacs/utility/textreader.h"
65 #include "gromacs/utility/unique_cptr.h"
72 std::string makeBinaryCacheFilename(const std::string& kernelFilename, cl_device_id deviceId)
74 // Note that the OpenCL API is defined in terms of bytes, and we
75 // assume that sizeof(char) is one byte.
76 std::array<char, 1024> deviceName;
77 size_t deviceNameLength;
78 cl_int cl_error = clGetDeviceInfo(
79 deviceId, CL_DEVICE_NAME, deviceName.size(), deviceName.data(), &deviceNameLength);
80 if (cl_error != CL_SUCCESS)
82 GMX_THROW(InternalError(formatString("Could not get OpenCL device name, error was %s",
83 ocl_get_error_string(cl_error).c_str())));
86 std::string cacheFilename = "OCL-cache";
87 /* remove the kernel source suffix */
88 cacheFilename += "_" + stripSuffixIfPresent(kernelFilename, ".cl") + "_";
89 /* We want a cache filename that's somewhat human readable, and
90 describes the device because it's based on the vendor's
91 information, but also always works as a filename. So we remove
92 characters that are commonly illegal in filenames (dot, slash),
93 or sometimes inconvenient (whitespace), or perhaps problematic
94 (symbols), by permitting only alphanumeric characters from the
95 current locale. We assume these work well enough in a
97 std::copy_if(deviceName.begin(),
98 deviceName.begin() + deviceNameLength,
99 std::back_inserter(cacheFilename),
101 cacheFilename += ".bin";
103 return cacheFilename;
106 cl_program makeProgramFromCache(const std::string& filename, cl_context context, cl_device_id deviceId)
108 // TODO all this file reading stuff should become gmx::BinaryReader
109 const auto f = create_unique_with_deleter(fopen(filename.c_str(), "rb"), fclose);
112 GMX_THROW(FileIOError("Failed to open binary cache file " + filename));
115 // TODO more stdio error handling
116 fseek(f.get(), 0, SEEK_END);
117 unsigned char* binary;
118 unique_cptr<unsigned char> binaryGuard;
119 size_t fileSize = ftell(f.get());
120 snew(binary, fileSize);
121 binaryGuard.reset(binary);
122 fseek(f.get(), 0, SEEK_SET);
123 size_t readCount = fread(binary, 1, fileSize, f.get());
125 if (readCount != fileSize)
127 GMX_THROW(FileIOError("Failed to read binary cache file " + filename));
130 /* TODO If/when caching is re-enabled, compare current build
131 * options and code against the build options and the code
132 * corresponding to the cache. If any change is detected then the
133 * cache cannot be used.
135 * Also caching functionality will need full re-testing. */
137 /* Create program from pre-built binary */
139 cl_program program = clCreateProgramWithBinary(
140 context, 1, &deviceId, &fileSize, const_cast<const unsigned char**>(&binary), nullptr, &cl_error);
141 if (cl_error != CL_SUCCESS)
143 GMX_THROW(InternalError("Could not create OpenCL program from the cache file " + filename
144 + ", error was " + ocl_get_error_string(cl_error)));
150 void writeBinaryToCache(cl_program program, const std::string& filename)
154 clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(fileSize), &fileSize, nullptr);
155 if (cl_error != CL_SUCCESS)
157 GMX_THROW(InternalError("Could not get OpenCL program binary size, error was "
158 + ocl_get_error_string(cl_error)));
161 // TODO all this file writing stuff should become gmx::BinaryWriter
162 unsigned char* binary;
163 snew(binary, fileSize);
164 const unique_cptr<unsigned char> binaryGuard(binary);
166 cl_error = clGetProgramInfo(program, CL_PROGRAM_BINARIES, sizeof(binary), &binary, nullptr);
167 if (cl_error != CL_SUCCESS)
169 GMX_THROW(InternalError("Could not get OpenCL program binary, error was "
170 + ocl_get_error_string(cl_error)));
173 const auto f = create_unique_with_deleter(fopen(filename.c_str(), "wb"), fclose);
176 GMX_THROW(FileIOError("Failed to open binary cache file " + filename));
179 fwrite(binary, 1, fileSize, f.get());