Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:16

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 // CUDA plugin include(s).
0010 #include "Acts/Plugins/Cuda/Seeding2/Details/Types.hpp"
0011 #include "Acts/Plugins/Cuda/Utilities/Arrays.hpp"
0012 #include "Acts/Plugins/Cuda/Utilities/MemoryManager.hpp"
0013 
0014 #include "ErrorCheck.cuh"
0015 #include "StreamHandlers.cuh"
0016 
0017 // CUDA include(s).
0018 #include <cuda_runtime.h>
0019 
0020 // System include(s).
0021 #include <cstdlib>
0022 
0023 namespace Acts {
0024 namespace Cuda {
0025 namespace Details {
0026 
0027 void DeviceArrayDeleter::operator()(void*) {
0028   // The memory is managed by @c Acts::Cuda::MemoryManager, don't do anything
0029   // here.
0030   return;
0031 }
0032 
0033 void HostArrayDeleter::operator()(void* ptr) {
0034   // Ignore null-pointers.
0035   if (ptr == nullptr) {
0036     return;
0037   }
0038 
0039   // Free the host memory.
0040   free(ptr);
0041   return;
0042 }
0043 
0044 }  // namespace Details
0045 
0046 template <typename T>
0047 device_array<T> make_device_array(std::size_t size) {
0048   // Allocate the memory.
0049   T* ptr = nullptr;
0050   if (size != 0) {
0051     ptr = static_cast<T*>(MemoryManager::instance().allocate(size * sizeof(T)));
0052   }
0053   // Create the smart pointer.
0054   return device_array<T>(ptr);
0055 }
0056 
0057 template <typename T>
0058 host_array<T> make_host_array(std::size_t size) {
0059   // Allocate the memory.
0060   T* ptr = nullptr;
0061   if (size != 0) {
0062     ptr = static_cast<T*>(malloc(size * sizeof(T)));
0063   }
0064   // Create the smart pointer.
0065   return host_array<T>(ptr);
0066 }
0067 
0068 template <typename T>
0069 void copyToDevice(device_array<T>& dev, const host_array<T>& host,
0070                   std::size_t arraySize) {
0071   ACTS_CUDA_ERROR_CHECK(cudaMemcpy(dev.get(), host.get(), arraySize * sizeof(T),
0072                                    cudaMemcpyHostToDevice));
0073   return;
0074 }
0075 
0076 template <typename T>
0077 void copyToDevice(device_array<T>& dev, const host_array<T>& host,
0078                   std::size_t arraySize, const StreamWrapper& stream) {
0079   ACTS_CUDA_ERROR_CHECK(
0080       cudaMemcpyAsync(dev.get(), host.get(), arraySize * sizeof(T),
0081                       cudaMemcpyHostToDevice, getStreamFrom(stream)));
0082   return;
0083 }
0084 
0085 template <typename T>
0086 void copyToHost(host_array<T>& host, const device_array<T>& dev,
0087                 std::size_t arraySize) {
0088   ACTS_CUDA_ERROR_CHECK(cudaMemcpy(host.get(), dev.get(), arraySize * sizeof(T),
0089                                    cudaMemcpyDeviceToHost));
0090   return;
0091 }
0092 
0093 template <typename T>
0094 void copyToHost(host_array<T>& host, const device_array<T>& dev,
0095                 std::size_t arraySize, const StreamWrapper& stream) {
0096   ACTS_CUDA_ERROR_CHECK(
0097       cudaMemcpyAsync(host.get(), dev.get(), arraySize * sizeof(T),
0098                       cudaMemcpyDeviceToHost, getStreamFrom(stream)));
0099   return;
0100 }
0101 
0102 }  // namespace Cuda
0103 }  // namespace Acts
0104 
0105 /// Helper macro for instantiating the template code for a given type
0106 ///
0107 /// Note that nvcc (at least as of CUDA version 11.0.2) does not allow us to
0108 /// instantiate our custom unique pointer types through their typedef'd names.
0109 /// That's why the following expressions are as long as they are.
0110 ///
0111 #define INST_ARRAY_FOR_TYPE(TYPE)                                              \
0112   template class std::unique_ptr<TYPE,                                         \
0113                                  Acts::Cuda::Details::DeviceArrayDeleter>;     \
0114   template std::unique_ptr<TYPE, Acts::Cuda::Details::DeviceArrayDeleter>      \
0115       Acts::Cuda::make_device_array<TYPE>(std::size_t);                        \
0116   template class std::unique_ptr<TYPE, Acts::Cuda::Details::HostArrayDeleter>; \
0117   template std::unique_ptr<TYPE, Acts::Cuda::Details::HostArrayDeleter>        \
0118       Acts::Cuda::make_host_array<TYPE>(std::size_t);                          \
0119   template void Acts::Cuda::copyToDevice<TYPE>(                                \
0120       std::unique_ptr<TYPE, Acts::Cuda::Details::DeviceArrayDeleter>&,         \
0121       const std::unique_ptr<TYPE, Acts::Cuda::Details::HostArrayDeleter>&,     \
0122       std::size_t);                                                            \
0123   template void Acts::Cuda::copyToDevice<TYPE>(                                \
0124       std::unique_ptr<TYPE, Acts::Cuda::Details::DeviceArrayDeleter>&,         \
0125       const std::unique_ptr<TYPE, Acts::Cuda::Details::HostArrayDeleter>&,     \
0126       std::size_t, const Acts::Cuda::StreamWrapper&);                          \
0127   template void Acts::Cuda::copyToHost<TYPE>(                                  \
0128       std::unique_ptr<TYPE, Acts::Cuda::Details::HostArrayDeleter>&,           \
0129       const std::unique_ptr<TYPE, Acts::Cuda::Details::DeviceArrayDeleter>&,   \
0130       std::size_t);                                                            \
0131   template void Acts::Cuda::copyToHost<TYPE>(                                  \
0132       std::unique_ptr<TYPE, Acts::Cuda::Details::HostArrayDeleter>&,           \
0133       const std::unique_ptr<TYPE, Acts::Cuda::Details::DeviceArrayDeleter>&,   \
0134       std::size_t, const Acts::Cuda::StreamWrapper&)
0135 
0136 // Instantiate the templated functions for all primitive types.
0137 INST_ARRAY_FOR_TYPE(char);
0138 INST_ARRAY_FOR_TYPE(unsigned char);
0139 INST_ARRAY_FOR_TYPE(short);
0140 INST_ARRAY_FOR_TYPE(unsigned short);
0141 INST_ARRAY_FOR_TYPE(int);
0142 INST_ARRAY_FOR_TYPE(unsigned int);
0143 INST_ARRAY_FOR_TYPE(long);
0144 INST_ARRAY_FOR_TYPE(unsigned long);
0145 INST_ARRAY_FOR_TYPE(long long);
0146 INST_ARRAY_FOR_TYPE(unsigned long long);
0147 INST_ARRAY_FOR_TYPE(float);
0148 INST_ARRAY_FOR_TYPE(double);
0149 
0150 // Instantiate them for any necessary custom type(s) as well.
0151 INST_ARRAY_FOR_TYPE(Acts::Cuda::Details::SpacePoint);
0152 INST_ARRAY_FOR_TYPE(Acts::Cuda::Details::DubletCounts);
0153 INST_ARRAY_FOR_TYPE(Acts::Cuda::Details::LinCircle);
0154 INST_ARRAY_FOR_TYPE(Acts::Cuda::Details::Triplet);
0155 
0156 // Clean up.
0157 #undef INST_ARRAY_FOR_TYPE