840aa1edb25876902ba9e523df4a6d9f04c4018f
[alexxy/gromacs.git] / src / gromacs / gpu_utils / tests / pinnedmemorychecker.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018,2019, 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  * Tests for GPU memory status checker isHostMemoryPinned() being correct.
38  *
39  * \author Aleksei Iupinov <a.yupinov@gmail.com>
40  */
41 #include "gmxpre.h"
42
43 #include <vector>
44
45 #include <gtest/gtest.h>
46
47 #include "gromacs/gpu_utils/gpu_utils.h"
48 #include "gromacs/gpu_utils/hostallocator.h"
49 #include "gromacs/gpu_utils/pmalloc_cuda.h"
50 #include "gromacs/utility/real.h"
51 #include "gromacs/utility/smalloc.h"
52
53 #include "gputest.h"
54
55 namespace gmx
56 {
57
58 namespace test
59 {
60
61 namespace
62 {
63
64 //! Test fixture
65 using PinnedMemoryCheckerTest = GpuTest;
66
67 TEST_F(PinnedMemoryCheckerTest, DefaultContainerIsRecognized)
68 {
69     if (!haveValidGpus())
70     {
71         return;
72     }
73
74     std::vector<real> dummy(3, 1.5);
75     EXPECT_FALSE(isHostMemoryPinned(dummy.data()));
76 }
77
78 TEST_F(PinnedMemoryCheckerTest, NonpinnedContainerIsRecognized)
79 {
80     if (!haveValidGpus())
81     {
82         return;
83     }
84
85     HostVector<real> dummy(3, 1.5);
86     changePinningPolicy(&dummy, PinningPolicy::CannotBePinned);
87     EXPECT_FALSE(isHostMemoryPinned(dummy.data()));
88 }
89
90 TEST_F(PinnedMemoryCheckerTest, PinnedContainerIsRecognized)
91 {
92     if (!haveValidGpus())
93     {
94         return;
95     }
96
97     HostVector<real> dummy(3, 1.5);
98     changePinningPolicy(&dummy, PinningPolicy::PinnedIfSupported);
99     EXPECT_TRUE(isHostMemoryPinned(dummy.data()));
100 }
101
102 TEST_F(PinnedMemoryCheckerTest, DefaultCBufferIsRecognized)
103 {
104     if (!haveValidGpus())
105     {
106         return;
107     }
108
109     real* dummy;
110     snew(dummy, 3);
111     EXPECT_FALSE(isHostMemoryPinned(dummy));
112     sfree(dummy);
113 }
114
115 TEST_F(PinnedMemoryCheckerTest, PinnedCBufferIsRecognized)
116 {
117     if (!haveValidGpus())
118     {
119         return;
120     }
121
122     real* dummy = nullptr;
123     pmalloc(reinterpret_cast<void**>(&dummy), 3 * sizeof(real));
124     EXPECT_TRUE(isHostMemoryPinned(dummy));
125     pfree(dummy);
126 }
127
128 } // namespace
129 } // namespace test
130 } // namespace gmx