98b02df93a4163a12d27d855c219e0627e7c6dc3
[alexxy/gromacs.git] / src / gromacs / gpu_utils / ocl_compiler.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020,2021, 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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \internal \file
37  *  \brief Define infrastructure for OpenCL JIT compilation for Gromacs
38  *
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>
43  */
44
45 #include "gmxpre.h"
46
47 #include "ocl_compiler.h"
48
49 #include "config.h"
50
51 #include <cstdio>
52
53 #include <algorithm>
54 #include <string>
55 #include <vector>
56
57 #include "gromacs/gpu_utils/oclutils.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/gmxassert.h"
61 #include "gromacs/utility/path.h"
62 #include "gromacs/utility/programcontext.h"
63 #include "gromacs/utility/smalloc.h"
64 #include "gromacs/utility/stringutil.h"
65 #include "gromacs/utility/textreader.h"
66 #include "gromacs/utility/unique_cptr.h"
67
68 #include "ocl_caching.h"
69
70 namespace gmx
71 {
72 namespace ocl
73 {
74
75 /*! \brief True if OpenCL binary caching is enabled.
76  *
77  *  Currently caching is disabled by default unless the env var override
78  *  is used until we resolve concurrency issues. */
79 static bool useBuildCache = getenv("GMX_OCL_GENCACHE") != nullptr;
80
81 /*! \brief Handles writing the OpenCL JIT compilation log to \c fplog.
82  *
83  * If \c fplog is non-null and either the GMX_OCL_DUMP_LOG environment
84  * variable is set or the compilation failed, then the OpenCL
85  * compilation log is written.
86  *
87  * \param fplog               Open file pointer to log file
88  * \param program             OpenCL program that was compiled
89  * \param deviceId            Id of the device for which compilation took place
90  * \param kernelFilename      File name containing the kernel
91  * \param preprocessorOptions String containing the preprocessor command-line options used for the
92  * build \param buildFailed         Whether the OpenCL build succeeded
93  *
94  * \throws std::bad_alloc if out of memory */
95 static void writeOclBuildLog(FILE*              fplog,
96                              cl_program         program,
97                              cl_device_id       deviceId,
98                              const std::string& kernelFilename,
99                              const std::string& preprocessorOptions,
100                              bool               buildFailed)
101 {
102     bool writeOutput = ((fplog != nullptr) && (buildFailed || (getenv("GMX_OCL_DUMP_LOG") != nullptr)));
103
104     if (!writeOutput)
105     {
106         return;
107     }
108
109     // Get build log string size
110     size_t buildLogSize;
111     cl_int cl_error =
112             clGetProgramBuildInfo(program, deviceId, CL_PROGRAM_BUILD_LOG, 0, nullptr, &buildLogSize);
113     if (cl_error != CL_SUCCESS)
114     {
115         GMX_THROW(InternalError("Could not get OpenCL program build log size, error was "
116                                 + ocl_get_error_string(cl_error)));
117     }
118
119     char*             buildLog = nullptr;
120     unique_cptr<char> buildLogGuard;
121     if (buildLogSize != 0)
122     {
123         /* Allocate memory to fit the build log,
124            it can be very large in case of errors */
125         snew(buildLog, buildLogSize);
126         buildLogGuard.reset(buildLog);
127
128         /* Get the actual compilation log */
129         cl_error = clGetProgramBuildInfo(
130                 program, deviceId, CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, nullptr);
131         if (cl_error != CL_SUCCESS)
132         {
133             GMX_THROW(InternalError("Could not get OpenCL program build log, error was "
134                                     + ocl_get_error_string(cl_error)));
135         }
136     }
137
138     std::string message;
139     if (buildFailed)
140     {
141         message += "Compilation of source file " + kernelFilename + " failed!\n";
142     }
143     else
144     {
145         message += "Compilation of source file " + kernelFilename + " was successful!\n";
146     }
147     message += "-- Used build options: " + preprocessorOptions + "\n";
148     message += "--------------LOG START---------------\n";
149     message += buildLog;
150     message += "---------------LOG END----------------\n";
151     ;
152
153     fputs(message.c_str(), fplog);
154 }
155
156 /*! \brief Construct compiler options string
157  *
158  * \param deviceVendor  Device vendor. Used to automatically enable some
159  *                      vendor-specific options.
160  * \return The string with the compiler options
161  */
162 static std::string selectCompilerOptions(DeviceVendor deviceVendor)
163 {
164     std::string compilerOptions;
165
166     if (getenv("GMX_OCL_NOOPT"))
167     {
168         compilerOptions += " -cl-opt-disable";
169     }
170
171     /* Fastmath improves performance on all supported arch */
172     if (getenv("GMX_OCL_DISABLE_FASTMATH") == nullptr)
173     {
174         compilerOptions += " -cl-fast-relaxed-math";
175
176         // Hint to the compiler that it can flush denorms to zero.
177         // In CUDA this is triggered by the -use_fast_math flag, equivalent with
178         // -cl-fast-relaxed-math, hence the inclusion on the conditional block.
179         compilerOptions += " -cl-denorms-are-zero";
180     }
181
182     if ((deviceVendor == DeviceVendor::Nvidia) && getenv("GMX_OCL_VERBOSE"))
183     {
184         compilerOptions += " -cl-nv-verbose";
185     }
186
187     if ((deviceVendor == DeviceVendor::Amd) && getenv("GMX_OCL_DUMP_INTERM_FILES"))
188     {
189         /* To dump OpenCL build intermediate files, caching must be off */
190         if (!useBuildCache)
191         {
192             compilerOptions += " -save-temps";
193         }
194     }
195
196     if (getenv("GMX_OCL_DEBUG"))
197     {
198         compilerOptions += " -g";
199     }
200
201     return compilerOptions;
202 }
203
204 /*! \brief Get the path to the folder storing an OpenCL source file.
205  *
206  * By default, this function constructs the full path to the OpenCL from
207  * the known location of the binary that is running, so that we handle
208  * both in-source and installed builds. The user can override this
209  * behavior by defining GMX_OCL_FILE_PATH environment variable.
210  *
211  * \param[in] sourceRelativePath    Relative path to the kernel or other file in the source tree,
212  *                                  from src, e.g. "gromacs/mdlib/nbnxn_ocl" for NB kernels.
213  * \return OS-normalized path string to the folder storing OpenCL source file
214  *
215  * \throws std::bad_alloc    if out of memory.
216  *         FileIOError  if GMX_OCL_FILE_PATH does not specify a readable path
217  */
218 static std::string getSourceRootPath(const std::string& sourceRelativePath)
219 {
220     std::string sourceRootPath;
221     /* Use GMX_OCL_FILE_PATH if the user has defined it */
222     const char* gmxOclFilePath = getenv("GMX_OCL_FILE_PATH");
223
224     if (gmxOclFilePath == nullptr)
225     {
226         /* Normal way of getting ocl_root_dir. First get the right
227            root path from the path to the binary that is running. */
228         InstallationPrefixInfo info           = getProgramContext().installationPrefix();
229         std::string            dataPathSuffix = (info.bSourceLayout ? "src" : GMX_INSTALL_OCLDIR);
230         sourceRootPath = Path::join(info.path, dataPathSuffix, sourceRelativePath);
231     }
232     else
233     {
234         if (!Directory::exists(gmxOclFilePath))
235         {
236             GMX_THROW(FileIOError(
237                     formatString("GMX_OCL_FILE_PATH must point to the directory where OpenCL"
238                                  "kernels are found, but '%s' does not exist",
239                                  gmxOclFilePath)));
240         }
241         sourceRootPath = Path::join(gmxOclFilePath, sourceRelativePath);
242     }
243
244     // Make sure we return an OS-correct path format
245     return Path::normalize(sourceRootPath);
246 }
247
248 size_t getKernelWarpSize(cl_kernel kernel, cl_device_id deviceId)
249 {
250     size_t warpSize = 0;
251     cl_int cl_error = clGetKernelWorkGroupInfo(
252             kernel, deviceId, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, sizeof(warpSize), &warpSize, nullptr);
253     if (cl_error != CL_SUCCESS)
254     {
255         GMX_THROW(InternalError("Could not query OpenCL preferred workgroup size, error was "
256                                 + ocl_get_error_string(cl_error)));
257     }
258     if (warpSize == 0)
259     {
260         GMX_THROW(InternalError(formatString("Invalid OpenCL warp size encountered")));
261     }
262     return warpSize;
263 }
264
265 size_t getDeviceWarpSize(cl_context context, cl_device_id deviceId)
266 {
267     cl_int      cl_error;
268     const char* warpSizeKernel =
269             "__kernel void test(__global int* test){test[get_local_id(0)] = 0;}";
270     cl_program program = clCreateProgramWithSource(context, 1, &warpSizeKernel, nullptr, &cl_error);
271     if (cl_error != CL_SUCCESS)
272     {
273         GMX_THROW(InternalError("Could not create OpenCL program to determine warp size, error was "
274                                 + ocl_get_error_string(cl_error)));
275     }
276
277     cl_error = clBuildProgram(program, 0, nullptr, nullptr, nullptr, nullptr);
278     if (cl_error != CL_SUCCESS)
279     {
280         GMX_THROW(InternalError("Could not build OpenCL program to determine warp size, error was "
281                                 + ocl_get_error_string(cl_error)));
282     }
283
284     cl_kernel kernel = clCreateKernel(program, "test", &cl_error);
285     if (cl_error != CL_SUCCESS)
286     {
287         GMX_THROW(InternalError("Could not create OpenCL kernel to determine warp size, error was "
288                                 + ocl_get_error_string(cl_error)));
289     }
290
291     size_t warpSize = getKernelWarpSize(kernel, deviceId);
292
293     cl_error = clReleaseKernel(kernel);
294     if (cl_error != CL_SUCCESS)
295     {
296         GMX_THROW(InternalError("Could not release OpenCL warp-size kernel, error was "
297                                 + ocl_get_error_string(cl_error)));
298     }
299     cl_error = clReleaseProgram(program);
300     if (cl_error != CL_SUCCESS)
301     {
302         GMX_THROW(InternalError("Could not release OpenCL warp-size program, error was "
303                                 + ocl_get_error_string(cl_error)));
304     }
305
306     return warpSize;
307 }
308
309 /*! \brief Select a compilation-line define for a vendor-specific kernel choice from vendor id
310  *
311  * \param[in] deviceVendor Vendor id enumerator
312  *
313  * \return The appropriate compilation-line define
314  */
315 static std::string makeVendorFlavorChoice(DeviceVendor deviceVendor)
316 {
317     switch (deviceVendor)
318     {
319         case DeviceVendor::Amd: return "-D_AMD_SOURCE_";
320         case DeviceVendor::Nvidia: return "-D_NVIDIA_SOURCE_";
321         case DeviceVendor::Intel: return "-D_INTEL_SOURCE_";
322         default: return "";
323     }
324 }
325
326 /*! \brief Create include paths for kernel sources.
327  *
328  * All OpenCL kernel files are expected to be stored in one single folder.
329  *
330  * \throws std::bad_alloc  if out of memory.
331  */
332 static std::string makeKernelIncludePathOption(const std::string& unescapedKernelRootPath)
333 {
334     std::string includePathOption;
335
336     /* Apple does not seem to accept the quoted include paths other
337      * OpenCL implementations are happy with. Since the standard still says
338      * it should be quoted, we handle Apple as a special case.
339      */
340 #ifdef __APPLE__
341     includePathOption += "-I";
342
343     // Prepend all the spaces with a backslash
344     for (std::string::size_type i = 0; i < unescapedKernelRootPath.length(); i++)
345     {
346         if (unescapedKernelRootPath[i] == ' ')
347         {
348             includePathOption.push_back('\\');
349         }
350         includePathOption.push_back(unescapedKernelRootPath[i]);
351     }
352 #else
353     includePathOption += "-I\"" + unescapedKernelRootPath + "\"";
354 #endif
355
356     return includePathOption;
357 }
358
359 /*! \brief Replace duplicated spaces with a single one in string
360  *
361  * Only the first character will be kept for multiple adjacent characters that
362  * are both identical and where the first one returns true for isspace().
363  *
364  * \param str String that will be modified.
365  */
366 static void removeExtraSpaces(std::string* str)
367 {
368     GMX_RELEASE_ASSERT(str != nullptr, "A pointer to an actual string must be provided");
369     std::string::iterator newEnd = std::unique(
370             str->begin(), str->end(), [=](char a, char b) { return isspace(a) != 0 && (a == b); });
371     str->erase(newEnd, str->end());
372 }
373
374 /*! \brief Builds a string with build options for the OpenCL kernels
375  *
376  * \throws std::bad_alloc  if out of memory. */
377 static std::string makePreprocessorOptions(const std::string& kernelRootPath,
378                                            const std::string& includeRootPath,
379                                            size_t             warpSize,
380                                            DeviceVendor       deviceVendor,
381                                            const std::string& extraDefines)
382 {
383     std::string preprocessorOptions;
384
385     /* Compose the complete build options */
386     preprocessorOptions = formatString("-DWARP_SIZE_TEST=%d", static_cast<int>(warpSize));
387     preprocessorOptions += ' ';
388     preprocessorOptions += makeVendorFlavorChoice(deviceVendor);
389     preprocessorOptions += ' ';
390     preprocessorOptions += extraDefines;
391     preprocessorOptions += ' ';
392     preprocessorOptions += selectCompilerOptions(deviceVendor);
393     preprocessorOptions += ' ';
394     preprocessorOptions += makeKernelIncludePathOption(kernelRootPath);
395     preprocessorOptions += ' ';
396     preprocessorOptions += makeKernelIncludePathOption(includeRootPath);
397
398     // Mac OS (and maybe some other implementations) does not accept double spaces in options
399     removeExtraSpaces(&preprocessorOptions);
400
401     return preprocessorOptions;
402 }
403
404 cl_program compileProgram(FILE*              fplog,
405                           const std::string& kernelRelativePath,
406                           const std::string& kernelBaseFilename,
407                           const std::string& extraDefines,
408                           cl_context         context,
409                           cl_device_id       deviceId,
410                           DeviceVendor       deviceVendor)
411 {
412     cl_int cl_error;
413     // Let the kernel find include files from its module.
414     std::string kernelRootPath = getSourceRootPath(kernelRelativePath);
415     // Let the kernel find include files from other modules.
416     std::string rootPath = getSourceRootPath("");
417
418     GMX_RELEASE_ASSERT(fplog != nullptr, "Need a valid log file for building OpenCL programs");
419
420     /* Load OpenCL source files */
421     std::string kernelFilename = Path::join(kernelRootPath, kernelBaseFilename);
422
423     /* Make the build options */
424     std::string preprocessorOptions = makePreprocessorOptions(
425             kernelRootPath, rootPath, getDeviceWarpSize(context, deviceId), deviceVendor, extraDefines);
426
427     bool buildCacheWasRead = false;
428
429     std::string cacheFilename;
430     if (useBuildCache)
431     {
432         cacheFilename = makeBinaryCacheFilename(kernelBaseFilename, deviceId);
433     }
434
435     /* Create OpenCL program */
436     cl_program program = nullptr;
437     if (useBuildCache)
438     {
439         if (File::exists(cacheFilename, File::returnFalseOnError))
440         {
441             /* Check if there's a valid cache available */
442             try
443             {
444                 program           = makeProgramFromCache(cacheFilename, context, deviceId);
445                 buildCacheWasRead = true;
446             }
447             catch (FileIOError& e)
448             {
449                 // Failing to read from the cache is not a critical error
450                 formatExceptionMessageToFile(fplog, e);
451             }
452             fprintf(fplog,
453                     "OpenCL binary cache file %s is present, will load kernels.\n",
454                     cacheFilename.c_str());
455         }
456         else
457         {
458             fprintf(fplog,
459                     "No OpenCL binary cache file was present for %s, so will compile kernels "
460                     "normally.\n",
461                     kernelBaseFilename.c_str());
462         }
463     }
464     if (program == nullptr)
465     {
466         // Compile OpenCL program from source
467         std::string kernelSource = TextReader::readFileToString(kernelFilename);
468         if (kernelSource.empty())
469         {
470             GMX_THROW(FileIOError("Error loading OpenCL code " + kernelFilename));
471         }
472         const char* kernelSourcePtr  = kernelSource.c_str();
473         size_t      kernelSourceSize = kernelSource.size();
474         /* Create program from source code */
475         program = clCreateProgramWithSource(context, 1, &kernelSourcePtr, &kernelSourceSize, &cl_error);
476         if (cl_error != CL_SUCCESS)
477         {
478             GMX_THROW(InternalError("Could not create OpenCL program, error was "
479                                     + ocl_get_error_string(cl_error)));
480         }
481     }
482
483     /* Build the OpenCL program, keeping the status to potentially
484        write to the simulation log file. */
485     cl_int buildStatus =
486             clBuildProgram(program, 0, nullptr, preprocessorOptions.c_str(), nullptr, nullptr);
487
488     /* Write log first, and then throw exception that the user know what is
489        the issue even if the build fails. */
490     writeOclBuildLog(fplog, program, deviceId, kernelFilename, preprocessorOptions, buildStatus != CL_SUCCESS);
491
492     if (buildStatus != CL_SUCCESS)
493     {
494         GMX_THROW(InternalError("Could not build OpenCL program, error was "
495                                 + ocl_get_error_string(buildStatus)));
496     }
497
498     if (useBuildCache)
499     {
500         if (!buildCacheWasRead)
501         {
502             /* If OpenCL caching is ON, but the current cache is not
503                valid => update it */
504             try
505             {
506                 writeBinaryToCache(program, cacheFilename);
507             }
508             catch (GromacsException& e)
509             {
510                 // Failing to write the cache is not a critical error
511                 formatExceptionMessageToFile(fplog, e);
512             }
513         }
514     }
515     if ((deviceVendor == DeviceVendor::Nvidia) && getenv("GMX_OCL_DUMP_INTERM_FILES"))
516     {
517         /* If dumping intermediate files has been requested and this is an NVIDIA card
518            => write PTX to file */
519         char buffer[STRLEN];
520
521         cl_error = clGetDeviceInfo(deviceId, CL_DEVICE_NAME, sizeof(buffer), buffer, nullptr);
522         if (cl_error != CL_SUCCESS)
523         {
524             GMX_THROW(InternalError("Could not get OpenCL device info, error was "
525                                     + ocl_get_error_string(cl_error)));
526         }
527         std::string ptxFilename = buffer;
528         ptxFilename += ".ptx";
529
530         try
531         {
532             writeBinaryToCache(program, ptxFilename);
533         }
534         catch (GromacsException& e)
535         {
536             // Failing to write the cache is not a critical error
537             formatExceptionMessageToFile(fplog, e);
538         }
539     }
540
541     return program;
542 }
543
544 } // namespace ocl
545 } // namespace gmx