| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include "stk_util/util/StridedArray.hpp" |
| 36 |
#include "Kokkos_Core.hpp" |
| 37 |
#include "gtest/gtest.h" |
| 38 |
#include <vector> |
| 39 |
|
| 40 |
TEST( StridedArray, ptr_and_size) |
| 41 |
{ |
| 42 |
std::vector<int> vec = {1, 3, 5, 2}; |
| 43 |
stk::util::StridedArray<int> stridedArray(vec.data(), vec.size()); |
| 44 |
|
| 45 |
EXPECT_EQ(vec.size(), stridedArray.size()); |
| 46 |
|
| 47 |
for(unsigned i=0; i<stridedArray.size(); ++i) { |
| 48 |
EXPECT_EQ(vec[i], stridedArray[i]); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
TEST( StridedArray, pair_iter) |
| 53 |
{ |
| 54 |
std::vector<int> vec = {1, 3, 5, 2}; |
| 55 |
stk::util::StridedArray<int> stridedArray(stk::PairIter<int*>(vec.data(), vec.data()+vec.size())); |
| 56 |
|
| 57 |
EXPECT_EQ(vec.size(), stridedArray.size()); |
| 58 |
|
| 59 |
for(unsigned i=0; i<stridedArray.size(); ++i) { |
| 60 |
EXPECT_EQ(vec[i], stridedArray[i]); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
TEST( StridedArray, comparison) |
| 65 |
{ |
| 66 |
std::vector<int> vec = {1, 2, 3, 4}; |
| 67 |
stk::util::StridedArray<int> stridedArray1(vec.data(), vec.size()); |
| 68 |
stk::util::StridedArray<int> stridedArray2(vec.data(), vec.size()); |
| 69 |
|
| 70 |
EXPECT_EQ(stridedArray1, stridedArray2); |
| 71 |
|
| 72 |
std::vector<int> otherVec = {1, 4, 3, 2}; |
| 73 |
stk::util::StridedArray<int> otherStridedArray(otherVec.data(), otherVec.size()); |
| 74 |
|
| 75 |
EXPECT_NE(stridedArray1, otherStridedArray); |
| 76 |
|
| 77 |
std::vector<int> smallerVec = {1, 2}; |
| 78 |
stk::util::StridedArray<int> smallerStridedArray(smallerVec.data(), smallerVec.size()); |
| 79 |
|
| 80 |
EXPECT_NE(stridedArray1, smallerStridedArray); |
| 81 |
} |
| 82 |
|
| 83 |
#ifdef STK_ENABLE_GPU |
| 84 |
void run_comparison_test_on_device() |
| 85 |
{ |
| 86 |
constexpr int N1 = 3; |
| 87 |
constexpr int N2 = 5; |
| 88 |
Kokkos::View<int**> v1("v1", N1, N2); |
| 89 |
|
| 90 |
Kokkos::parallel_for(N1, KOKKOS_LAMBDA(const int& i) { |
| 91 |
for(int j=0; j<N2; ++j) { |
| 92 |
v1(i,j) = 1 + i + j; |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
int result = 0; |
| 97 |
Kokkos::parallel_reduce(1, KOKKOS_LAMBDA(const int& i, int& localResult) { |
| 98 |
int stride = N1; |
| 99 |
int wrongStride = 2; |
| 100 |
stk::util::StridedArray<int> sa(v1.data(), N2, stride); |
| 101 |
localResult = static_cast<int>(sa.size())==N2 ? 0 : 1; |
| 102 |
localResult += sa[1]==v1(0,1) ? 0 : 3; |
| 103 |
stk::util::StridedArray<int> sa2(v1.data(), N2, stride); |
| 104 |
localResult += (sa == sa2) ? 0 : 7; |
| 105 |
|
| 106 |
stk::util::StridedArray<int> saWrong(v1.data(), N2, wrongStride); |
| 107 |
localResult += (sa != saWrong) ? 0 : 15; |
| 108 |
}, result); |
| 109 |
|
| 110 |
EXPECT_EQ(0, result); |
| 111 |
} |
| 112 |
|
| 113 |
TEST(StridedArray, comparison_on_device) |
| 114 |
{ |
| 115 |
run_comparison_test_on_device(); |
| 116 |
} |
| 117 |
|
| 118 |
#endif |
| 119 |
|