Fix compiler warnings in OCL
[alexxy/gromacs.git] / src / gromacs / gpu_utils / ocl_caching.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2018, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  *  \brief Define infrastructure for OpenCL JIT compilation for Gromacs
37  *
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>
42  */
43
44 #include "gmxpre.h"
45
46 #include "ocl_caching.h"
47
48 #include <assert.h>
49
50 #include <cctype>
51 #include <cstdio>
52
53 #include <algorithm>
54 #include <array>
55 #include <iterator>
56 #include <string>
57 #include <vector>
58
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"
65
66 namespace gmx
67 {
68 namespace ocl
69 {
70
71 std::string makeBinaryCacheFilename(const std::string &kernelFilename,
72                                     cl_device_id       deviceId)
73 {
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)
80     {
81         GMX_THROW(InternalError(formatString("Could not get OpenCL device name, error was %s", ocl_get_error_string(cl_error).c_str())));
82     }
83
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
94        filename. */
95     std::copy_if(deviceName.begin(), deviceName.begin() + deviceNameLength, std::back_inserter(cacheFilename), isalnum);
96     cacheFilename += ".bin";
97
98     return cacheFilename;
99 }
100
101 cl_program
102 makeProgramFromCache(const std::string &filename,
103                      cl_context         context,
104                      cl_device_id       deviceId)
105 {
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);
108     if (!f)
109     {
110         GMX_THROW(FileIOError("Failed to open binary cache file " + filename));
111     }
112
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());
122
123     if (readCount != fileSize)
124     {
125         GMX_THROW(FileIOError("Failed to read binary cache file " + filename));
126     }
127
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.
132      *
133      * Also caching functionality will need full re-testing. */
134
135     /* Create program from pre-built binary */
136     cl_int     cl_error;
137     cl_program program = clCreateProgramWithBinary(context,
138                                                    1,
139                                                    &deviceId,
140                                                    &fileSize,
141                                                    const_cast<const unsigned char **>(&binary),
142                                                    nullptr,
143                                                    &cl_error);
144     if (cl_error != CL_SUCCESS)
145     {
146         GMX_THROW(InternalError("Could not create OpenCL program, error was " + ocl_get_error_string(cl_error)));
147     }
148
149     return program;
150 }
151
152 void
153 writeBinaryToCache(cl_program program, const std::string &filename)
154 {
155     size_t fileSize;
156     cl_int cl_error = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, sizeof(fileSize), &fileSize, nullptr);
157     if (cl_error != CL_SUCCESS)
158     {
159         GMX_THROW(InternalError("Could not get OpenCL program binary size, error was " + ocl_get_error_string(cl_error)));
160     }
161
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);
166
167     cl_error = clGetProgramInfo(program, CL_PROGRAM_BINARIES, sizeof(binary), &binary, nullptr);
168     if (cl_error != CL_SUCCESS)
169     {
170         GMX_THROW(InternalError("Could not get OpenCL program binary, error was " + ocl_get_error_string(cl_error)));
171     }
172
173     const auto f = create_unique_with_deleter(fopen(filename.c_str(), "wb"), fclose);
174     if (!f)
175     {
176         GMX_THROW(FileIOError("Failed to open binary cache file " + filename));
177     }
178
179     fwrite(binary, 1, fileSize, f.get());
180 }
181
182 } // namespace
183 } // namespace