Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Reduce coupling to CUDA backend #9

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/stdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
add_executable(teststdgpu main.cpp)

target_sources(teststdgpu PRIVATE cuda/atomic.cu
cuda/bit.cu
algorithm.cpp
atomic.cu
bit.cu
bit.cpp
bitset.cu
cmath.cpp
cstdlib.cpp
deque.cu
functional.cpp
iterator.cu
iterator.cpp
limits.cpp
memory.cpp
memory.cu
Expand Down
100 changes: 3 additions & 97 deletions test/stdgpu/bit.cu → test/stdgpu/bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@

#include <gtest/gtest.h>

#include <algorithm>
#include <limits>
#include <random>
#include <thread>
#include <type_traits>
#include <unordered_set>
#include <thrust/sequence.h>
#include <thrust/transform.h>

#include <test_utils.h>
#include <stdgpu/bit.h>
#include <stdgpu/iterator.h>
#include <stdgpu/memory.h>



Expand Down Expand Up @@ -137,123 +131,35 @@ TEST_F(stdgpu_bit, mod2_one_zero)
}


TEST_F(stdgpu_bit, log2pow2_host)
TEST_F(stdgpu_bit, log2pow2)
{
for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(stdgpu::log2pow2(static_cast<size_t>(1) << i), static_cast<size_t>(i));
}
}

struct log2pow2_functor
{
STDGPU_HOST_DEVICE size_t
operator()(const size_t i) const
{
return stdgpu::log2pow2(static_cast<size_t>(1) << i);
}
};

TEST_F(stdgpu_bit, log2pow2_device)
{
size_t* powers = createDeviceArray<size_t>(std::numeric_limits<size_t>::digits);
thrust::sequence(stdgpu::device_begin(powers), stdgpu::device_end(powers));

thrust::transform(stdgpu::device_begin(powers), stdgpu::device_end(powers),
stdgpu::device_begin(powers),
log2pow2_functor());

size_t* host_powers = copyCreateDevice2HostArray<size_t>(powers, std::numeric_limits<size_t>::digits);

for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(host_powers[i], static_cast<size_t>(i));
}

destroyDeviceArray<size_t>(powers);
destroyHostArray<size_t>(host_powers);
}


TEST_F(stdgpu_bit, popcount_zero)
{
EXPECT_EQ(stdgpu::popcount(static_cast<size_t>(0)), 0);
}


TEST_F(stdgpu_bit, popcount_pow2_host)
TEST_F(stdgpu_bit, popcount_pow2)
{
for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(stdgpu::popcount(static_cast<size_t>(1) << i), 1);
}
}

struct popcount_pow2
{
STDGPU_HOST_DEVICE size_t
operator()(const size_t i) const
{
return stdgpu::popcount(static_cast<size_t>(1) << i);
}
};

TEST_F(stdgpu_bit, popcount_pow2_device)
{
size_t* powers = createDeviceArray<size_t>(std::numeric_limits<size_t>::digits);
thrust::sequence(stdgpu::device_begin(powers), stdgpu::device_end(powers));

thrust::transform(stdgpu::device_begin(powers), stdgpu::device_end(powers),
stdgpu::device_begin(powers),
popcount_pow2());

size_t* host_powers = copyCreateDevice2HostArray<size_t>(powers, std::numeric_limits<size_t>::digits);

for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(host_powers[i], 1);
}

destroyDeviceArray<size_t>(powers);
destroyHostArray<size_t>(host_powers);
}


TEST_F(stdgpu_bit, popcount_pow2m1_host)
TEST_F(stdgpu_bit, popcount_pow2m1)
{
for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(stdgpu::popcount((static_cast<size_t>(1) << i) - 1), i);
}
}


struct popcount_pow2m1
{
STDGPU_HOST_DEVICE size_t
operator()(const size_t i) const
{
return stdgpu::popcount((static_cast<size_t>(1) << i) - 1);
}
};

TEST_F(stdgpu_bit, popcount_pow2m1_device)
{
size_t* powers = createDeviceArray<size_t>(std::numeric_limits<size_t>::digits);
thrust::sequence(stdgpu::device_begin(powers), stdgpu::device_end(powers));

thrust::transform(stdgpu::device_begin(powers), stdgpu::device_end(powers),
stdgpu::device_begin(powers),
popcount_pow2m1());

size_t* host_powers = copyCreateDevice2HostArray<size_t>(powers, std::numeric_limits<size_t>::digits);

for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(host_powers[i], i);
}

destroyDeviceArray<size_t>(powers);
destroyHostArray<size_t>(host_powers);
}

133 changes: 133 additions & 0 deletions test/stdgpu/cuda/bit.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2019 Patrick Stotko
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <gtest/gtest.h>

#include <thrust/sequence.h>
#include <thrust/transform.h>

#include <stdgpu/bit.h>
#include <stdgpu/iterator.h>
#include <stdgpu/memory.h>



class stdgpu_cuda_bit : public ::testing::Test
{
protected:
// Called before each test
virtual void SetUp()
{

}

// Called after each test
virtual void TearDown()
{

}

};


struct log2pow2_functor
{
STDGPU_DEVICE_ONLY size_t
operator()(const size_t i) const
{
return stdgpu::cuda::log2pow2(static_cast<unsigned long long>(1) << i);
}
};

TEST_F(stdgpu_cuda_bit, log2pow2)
{
size_t* powers = createDeviceArray<size_t>(std::numeric_limits<size_t>::digits);
thrust::sequence(stdgpu::device_begin(powers), stdgpu::device_end(powers));

thrust::transform(stdgpu::device_begin(powers), stdgpu::device_end(powers),
stdgpu::device_begin(powers),
log2pow2_functor());

size_t* host_powers = copyCreateDevice2HostArray<size_t>(powers, std::numeric_limits<size_t>::digits);

for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(host_powers[i], static_cast<size_t>(i));
}

destroyDeviceArray<size_t>(powers);
destroyHostArray<size_t>(host_powers);
}


struct popcount_pow2
{
STDGPU_DEVICE_ONLY size_t
operator()(const size_t i) const
{
return stdgpu::cuda::popcount(static_cast<unsigned long long>(1) << i);
}
};

TEST_F(stdgpu_cuda_bit, popcount_pow2)
{
size_t* powers = createDeviceArray<size_t>(std::numeric_limits<size_t>::digits);
thrust::sequence(stdgpu::device_begin(powers), stdgpu::device_end(powers));

thrust::transform(stdgpu::device_begin(powers), stdgpu::device_end(powers),
stdgpu::device_begin(powers),
popcount_pow2());

size_t* host_powers = copyCreateDevice2HostArray<size_t>(powers, std::numeric_limits<size_t>::digits);

for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(host_powers[i], 1);
}

destroyDeviceArray<size_t>(powers);
destroyHostArray<size_t>(host_powers);
}


struct popcount_pow2m1
{
STDGPU_DEVICE_ONLY size_t
operator()(const size_t i) const
{
return stdgpu::popcount((static_cast<size_t>(1) << i) - 1);
}
};

TEST_F(stdgpu_cuda_bit, popcount_pow2m1)
{
size_t* powers = createDeviceArray<size_t>(std::numeric_limits<size_t>::digits);
thrust::sequence(stdgpu::device_begin(powers), stdgpu::device_end(powers));

thrust::transform(stdgpu::device_begin(powers), stdgpu::device_end(powers),
stdgpu::device_begin(powers),
popcount_pow2m1());

size_t* host_powers = copyCreateDevice2HostArray<size_t>(powers, std::numeric_limits<size_t>::digits);

for (size_t i = 0; i < std::numeric_limits<size_t>::digits; ++i)
{
EXPECT_EQ(host_powers[i], i);
}

destroyDeviceArray<size_t>(powers);
destroyHostArray<size_t>(host_powers);
}

Loading