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