Remove the CPU hardware context from the general list of test hardware contexts
[gromacs.git] / src / gromacs / gpu_utils / tests / device_availability.cpp
blob18073678725efd55701131ed3b743b5eb6c2be7d
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
35 /*! \internal \file
36 * \brief
37 * Check if GPUs are available when they should be.
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.
45 * \author Artem Zhmurov <zhmurov@gmail.com>
46 * \author Andrey Alekseenko <al42and@gmail.com>
48 #include "gmxpre.h"
50 #include "config.h"
52 #include <string>
54 #include <gtest/gtest.h>
56 #include "gromacs/hardware/device_management.h"
57 #include "gromacs/utility/baseversion.h"
59 #include "testutils/test_hardware_environment.h"
61 namespace gmx
64 namespace test
67 static unsigned long int getRequiredNumberOfDevices()
69 const char* required_num_of_devices_str = getenv("GMX_TEST_REQUIRED_NUMBER_OF_DEVICES");
70 if (required_num_of_devices_str == nullptr)
72 return 0;
74 else
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;
89 TEST(DevicesAvailable, ShouldBeAbleToRunOnDevice)
91 const unsigned long int required_num_of_devices = getRequiredNumberOfDevices();
93 if (required_num_of_devices != 0)
95 ASSERT_TRUE(GMX_GPU) << "GROMACS was compiled without GPU support, yet "
96 "GMX_TEST_REQUIRED_NUMBER_OF_DEVICES is set.";
98 std::string platformString(getGpuImplementationString());
100 std::string errorMessage = "Can't perform device detection in " + platformString + ":\n";
101 std::string detectionErrorMessage;
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;
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.";
122 } // namespace test
123 } // namespace gmx