Skip to content
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
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: "CI"

on:
push:
branches:
- main

pull_request:

jobs:
checks:
runs-on: 'ubuntu-latest'
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
- name: Checks
uses: pre-commit/action@v3.0.0

intel-llvm:
runs-on: 'ubuntu-latest'
env:
CXX: icpx
steps:
- uses: actions/checkout@v4
- name: Set up Intel
run: |
wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \
| gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list
sudo apt-get update -y
sudo apt-get install -y intel-oneapi-compiler-dpcpp-cpp intel-oneapi-mkl-devel
- name: CMake
run: |
source /opt/intel/oneapi/setvars.sh
cmake -B build
- name: Build
run: |
source /opt/intel/oneapi/setvars.sh
make -C build -j `nproc`
- name: Test
run: |
source /opt/intel/oneapi/setvars.sh
./build/test/gtest/thrust-tests
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 11.1.4)
FetchContent_MakeAvailable(fmt)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

add_subdirectory(examples)
add_subdirectory(test)
endif()
16 changes: 8 additions & 8 deletions include/thrust/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace thrust {

template <std::contiguous_iterator I, typename T>
requires(std::is_same_v<std::iter_value_t<I>, T> &&
std::is_trivially_move_constructible_v<T>)
requires(std::is_same_v<std::iter_value_t<I>, std::remove_const_t<T>> &&
std::is_trivially_copyable_v<T>)
void copy(I first, I last, device_ptr<T> d_first) {
sycl::queue q = __detail::get_pointer_queue(d_first.get());

Expand All @@ -22,8 +22,8 @@ void copy(I first, I last, device_ptr<T> d_first) {
}

template <typename T, std::contiguous_iterator O>
requires(std::is_same_v<std::iter_value_t<O>, T> &&
std::is_trivially_move_constructible_v<T>)
requires(std::is_same_v<std::iter_value_t<O>, std::remove_const_t<T>> &&
std::is_trivially_copyable_v<T>)
void copy(device_ptr<T> first, device_ptr<T> last, O d_first) {
sycl::queue q = __detail::get_pointer_queue(first.get());

Expand All @@ -33,8 +33,8 @@ void copy(device_ptr<T> first, device_ptr<T> last, O d_first) {
}

template <typename ExecutionPolicy, std::contiguous_iterator I, typename T>
requires(std::is_same_v<std::iter_value_t<I>, T> &&
std::is_trivially_move_constructible_v<T>)
requires(std::is_same_v<std::iter_value_t<I>, std::remove_const_t<T>> &&
std::is_trivially_copyable_v<T>)
void copy(ExecutionPolicy&& policy, I first, I last, device_ptr<T> d_first) {
policy.get_queue()
.memcpy(d_first.get(), std::to_address(first),
Expand All @@ -43,8 +43,8 @@ void copy(ExecutionPolicy&& policy, I first, I last, device_ptr<T> d_first) {
}

template <typename ExecutionPolicy, typename T, std::contiguous_iterator O>
requires(std::is_same_v<std::iter_value_t<O>, T> &&
std::is_trivially_move_constructible_v<T>)
requires(std::is_same_v<std::iter_value_t<O>, std::remove_const_t<T>> &&
std::is_trivially_copyable_v<T>)
void copy(ExecutionPolicy&& policy, device_ptr<T> first, device_ptr<T> last,
O d_first) {
policy.get_queue()
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(gtest)
11 changes: 11 additions & 0 deletions test/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enable_testing()

add_executable(
thrust-tests
device_vector_test.cpp
)

target_link_libraries(thrust-tests sycl_thrust fmt GTest::gtest_main)

include(GoogleTest)
gtest_discover_tests(thrust-tests)
67 changes: 67 additions & 0 deletions test/gtest/device_vector_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <gtest/gtest.h>

#include <random>
#include <ranges>
#include <thrust/copy.h>
#include <thrust/device_vector.h>

#include "util.hpp"

TEST(DeviceVector, Construct) {
using T = int;

for (std::size_t n : {3, 45, 823, 1000}) {
std::vector<T> v(n);

util::fill_random(v.begin(), v.end());

thrust::device_vector<T> d_v1(v);
thrust::device_vector<T> d_v2(v.begin(), v.end());

ASSERT_EQ(v.size(), d_v1.size());
ASSERT_EQ(v.size(), d_v2.size());

for (std::size_t i = 0; i < v.size(); i++) {
T value = v[i];
T d_value1 = d_v1[i];
T d_value2 = d_v2[i];
ASSERT_EQ(value, d_value1);
ASSERT_EQ(value, d_value2);
}
}
}

TEST(DeviceVector, Copy) {
using T = int;

std::mt19937 g(0);

for (std::size_t n : {3, 45, 823, 1000, 9823, 384241, 1824981}) {
std::vector<T> v(n);

util::fill_random(v.begin(), v.end());

std::vector<T> v2(v);
thrust::device_vector<T> d_v(v);

std::size_t n_copies = 10;

std::uniform_int_distribution<std::size_t> first_d(0, n);

for (std::size_t i = 0; i < n_copies; i++) {
std::size_t first = first_d(g);
std::uniform_int_distribution<std::size_t> last_d(first, n);
std::size_t last = last_d(g);

std::uniform_int_distribution<std::size_t> d_first_d(0,
n - (last - first));
std::size_t d_first = d_first_d(g);

std::copy(v.begin() + first, v.begin() + last, v2.begin() + d_first);

thrust::copy(v.begin() + first, v.begin() + last, d_v.begin() + d_first);

ASSERT_TRUE(util::is_equal(v2, d_v));
}
}
}
38 changes: 38 additions & 0 deletions test/gtest/util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <limits>
#include <random>
#include <ranges>

namespace util {

template <std::contiguous_iterator Iter, typename T = int>
void fill_random(Iter first, Iter last, T a = 0,
T b = std::numeric_limits<T>::max()) {
std::mt19937 g(0);
std::uniform_int_distribution<T> d(0, 100);

for (auto iter = first; iter != last; ++iter) {
*iter = d(g);
}
}

template <typename T, typename Allocator>
bool is_equal(const std::vector<T>& a,
const thrust::device_vector<T, Allocator>& d_b) {
if (a.size() != d_b.size()) {
return false;
}

std::vector<T> b(d_b.size());
thrust::copy(d_b.begin(), d_b.end(), b.begin());

for (std::size_t i = 0; i < a.size(); i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

} // namespace util