Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:26

0001 /**
0002  * TRACCC library, part of the ACTS project (R&D line)
0003  *
0004  * (c) 2023 CERN for the benefit of the ACTS project
0005  *
0006  * Mozilla Public License Version 2.0
0007  */
0008 
0009 // Projection include(s).
0010 #include "traccc/device/container_d2h_copy_alg.hpp"
0011 #include "traccc/device/container_h2d_copy_alg.hpp"
0012 #include "traccc/edm/container.hpp"
0013 
0014 // VecMem include(s).
0015 #include <vecmem/containers/device_vector.hpp>
0016 #include <vecmem/memory/cuda/device_memory_resource.hpp>
0017 #include <vecmem/memory/host_memory_resource.hpp>
0018 #include <vecmem/utils/cuda/copy.hpp>
0019 
0020 // Thrust include(s).
0021 #include <thrust/execution_policy.h>
0022 #include <thrust/fill.h>
0023 
0024 // Google test include(s).
0025 #include <gtest/gtest.h>
0026 
0027 namespace {
0028 
0029 using test_container_types = traccc::container_types<int, int>;
0030 
0031 // Memory resources used by the application.
0032 vecmem::host_memory_resource host_mr;
0033 vecmem::cuda::device_memory_resource device_mr;
0034 traccc::memory_resource mr{device_mr, &host_mr};
0035 
0036 // Copy objects
0037 vecmem::cuda::copy copy;
0038 traccc::device::container_h2d_copy_alg<test_container_types> h2d{mr, copy};
0039 traccc::device::container_d2h_copy_alg<test_container_types> d2h{mr, copy};
0040 
0041 }  // namespace
0042 
0043 TEST(CUDAContainerCopy, DeviceToHost) {
0044   // Create cuda container
0045   test_container_types::buffer device_buffer{{3, mr.main},
0046                                              {{1, 3, 2}, mr.main, mr.host}};
0047 
0048   // Get device vectors and fill them in the device
0049   vecmem::device_vector<int> device_headers(device_buffer.headers);
0050   vecmem::device_vector<int> device_items_0(device_buffer.items.host_ptr()[0]);
0051   vecmem::device_vector<int> device_items_1(device_buffer.items.host_ptr()[1]);
0052   vecmem::device_vector<int> device_items_2(device_buffer.items.host_ptr()[2]);
0053 
0054   thrust::fill(thrust::device, device_headers.begin(), device_headers.end(), 7);
0055   thrust::fill(thrust::device, device_items_0.begin(), device_items_0.end(), 4);
0056   thrust::fill(thrust::device, device_items_1.begin(), device_items_1.end(), 1);
0057   thrust::fill(thrust::device, device_items_2.begin(), device_items_2.end(), 2);
0058 
0059   // Device-to-Host Copy
0060   test_container_types::host host_copy = d2h(device_buffer);
0061 
0062   // Check the copied container
0063   const auto& host_headers = host_copy.get_headers();
0064   ASSERT_EQ(host_headers.size(), 3u);
0065   ASSERT_EQ(host_headers[0], 7);
0066   ASSERT_EQ(host_headers[1], 7);
0067   ASSERT_EQ(host_headers[2], 7);
0068 
0069   const auto& host_items = host_copy.get_items();
0070   ASSERT_EQ(host_items.size(), 3u);
0071   ASSERT_EQ(host_items[0].size(), 1u);
0072   ASSERT_EQ(host_items[0][0], 4);
0073   ASSERT_EQ(host_items[1].size(), 3u);
0074   ASSERT_EQ(host_items[1][0], 1);
0075   ASSERT_EQ(host_items[1][1], 1);
0076   ASSERT_EQ(host_items[1][2], 1);
0077   ASSERT_EQ(host_items[2].size(), 2u);
0078   ASSERT_EQ(host_items[2][0], 2);
0079   ASSERT_EQ(host_items[2][1], 2);
0080 }
0081 
0082 TEST(CUDAContainerCopy, HostToDeviceToHost) {
0083   // Create a host container
0084   test_container_types::host host_orig{&host_mr};
0085 
0086   // Fill the host container
0087   for (int i = 0; i < 3; ++i) {
0088     host_orig.push_back(i,
0089                         test_container_types::host::vector_type<int>{i, i + 1});
0090   }
0091 
0092   // Copy the container to the device, and back.
0093   test_container_types::host host_copy = d2h(h2d(traccc::get_data(host_orig)));
0094 
0095   // Check the copied container
0096   ASSERT_EQ(host_copy.size(), 3u);
0097   const auto& host_headers = host_copy.get_headers();
0098   ASSERT_EQ(host_headers.size(), 3u);
0099   ASSERT_EQ(host_headers[0], 0);
0100   ASSERT_EQ(host_headers[1], 1);
0101   ASSERT_EQ(host_headers[2], 2);
0102 
0103   const auto& host_items = host_copy.get_items();
0104   ASSERT_EQ(host_items.size(), 3u);
0105   ASSERT_EQ(host_items[0][0], 0);
0106   ASSERT_EQ(host_items[0][1], 1);
0107   ASSERT_EQ(host_items[1][0], 1);
0108   ASSERT_EQ(host_items[1][1], 2);
0109   ASSERT_EQ(host_items[2][0], 2);
0110   ASSERT_EQ(host_items[2][1], 3);
0111 }