Remove the CPU hardware context from the general list of test hardware contexts
[alexxy/gromacs.git] / src / gromacs / gpu_utils / tests / device_availability.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, 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
37  * Check if GPUs are available when they should be.
38  *
39  * This is to test that CI can detect and use GPUs, when they are available.
40  * Driver and CUDA mismatch can lead to the tests falling back to the CPU
41  * code path quietly, leaving the GPU path untested. This is designed to
42  * fail when GPUs should be available, indicated by setting the environment
43  * variable \c GMX_TEST_REQUIRED_NUMBER_OF_DEVICES to 1 or greater.
44  *
45  * \author Artem Zhmurov <zhmurov@gmail.com>
46  * \author Andrey Alekseenko <al42and@gmail.com>
47  */
48 #include "gmxpre.h"
49
50 #include "config.h"
51
52 #include <string>
53
54 #include <gtest/gtest.h>
55
56 #include "gromacs/hardware/device_management.h"
57 #include "gromacs/utility/baseversion.h"
58
59 #include "testutils/test_hardware_environment.h"
60
61 namespace gmx
62 {
63
64 namespace test
65 {
66
67 static unsigned long int getRequiredNumberOfDevices()
68 {
69     const char* required_num_of_devices_str = getenv("GMX_TEST_REQUIRED_NUMBER_OF_DEVICES");
70     if (required_num_of_devices_str == nullptr)
71     {
72         return 0;
73     }
74     else
75     {
76         errno = 0;
77         char*          strtol_end;
78         const long int required_num_of_devices =
79                 std::strtol(required_num_of_devices_str, &strtol_end, 10);
80         GMX_RELEASE_ASSERT(errno == 0, "Invalid value of GMX_TEST_REQUIRED_NUMBER_OF_DEVICES.");
81         GMX_RELEASE_ASSERT(*strtol_end == '\0',
82                            "Trailing characters in GMX_TEST_REQUIRED_NUMBER_OF_DEVICES.");
83         GMX_RELEASE_ASSERT(required_num_of_devices >= 0,
84                            "GMX_TEST_REQUIRED_NUMBER_OF_DEVICES can not be negative.");
85         return required_num_of_devices;
86     }
87 }
88
89 TEST(DevicesAvailable, ShouldBeAbleToRunOnDevice)
90 {
91     const unsigned long int required_num_of_devices = getRequiredNumberOfDevices();
92
93     if (required_num_of_devices != 0)
94     {
95         ASSERT_TRUE(GMX_GPU) << "GROMACS was compiled without GPU support, yet "
96                                 "GMX_TEST_REQUIRED_NUMBER_OF_DEVICES is set.";
97
98         std::string platformString(getGpuImplementationString());
99
100         std::string errorMessage = "Can't perform device detection in " + platformString + ":\n";
101         std::string detectionErrorMessage;
102
103         // Test if the detection is working
104         ASSERT_TRUE(isDeviceDetectionEnabled())
105                 << errorMessage << "GPU device detection is disabled by "
106                 << "GMX_DISABLE_GPU_DETECTION environment variable.";
107         ASSERT_TRUE(isDeviceDetectionFunctional(&detectionErrorMessage))
108                 << errorMessage
109                 << "GPU device checks failed with the following message: " << detectionErrorMessage;
110
111         // This is to test that the GPU detection test environment is working
112         const std::vector<std::unique_ptr<TestDevice>>& testDeviceList =
113                 getTestHardwareEnvironment()->getTestDeviceList();
114         ASSERT_FALSE(testDeviceList.empty())
115                 << errorMessage << "The test environment has an empty list of capable devices.";
116         ASSERT_GE(testDeviceList.size(), required_num_of_devices)
117                 << errorMessage << "Requested " << required_num_of_devices << " device(s), "
118                 << "but only " << testDeviceList.size() << " detected in the test environment.";
119     }
120 }
121
122 } // namespace test
123 } // namespace gmx