Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Examples/CudaMuonSegmentFitting/src/CudaUtilities.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 #pragma once
0010 
0011 #include <sstream>
0012 
0013 #include <cuda_runtime_api.h>
0014 
0015 namespace ActsExamples {
0016 
0017 inline void cudaAssert(cudaError_t code, const char* file, int line) {
0018   if (code != cudaSuccess) {
0019     std::stringstream ss;
0020     ss << "CUDA error: " << cudaGetErrorString(code) << ", " << file << ":"
0021        << line;
0022     throw std::runtime_error(ss.str());
0023   }
0024 }
0025 
0026 }  // namespace ActsExamples
0027 
0028 #define ACTS_CUDA_CHECK(ans)                             \
0029   do {                                                   \
0030     ActsExamples::cudaAssert((ans), __FILE__, __LINE__); \
0031   } while (0)
0032 
0033 namespace ActsExamples {
0034 
0035 template <typename T>
0036 void allocateDeviceColumn(T*& deviceColumn, std::size_t size) {
0037   if (size == 0) {
0038     return;
0039   }
0040 
0041   ACTS_CUDA_CHECK(
0042       cudaMalloc(reinterpret_cast<void**>(&deviceColumn), size * sizeof(T)));
0043 }
0044 
0045 template <typename T>
0046 void freeDeviceColumn(T*& deviceColumn) noexcept {
0047   if (deviceColumn != nullptr) {
0048     ACTS_CUDA_CHECK(cudaFree(deviceColumn));
0049     deviceColumn = nullptr;
0050   }
0051 }
0052 
0053 template <typename T>
0054 void copyColumnToDevice(T* deviceColumn, const std::vector<T>& hostColumn) {
0055   if (hostColumn.empty()) {
0056     return;
0057   }
0058 
0059   ACTS_CUDA_CHECK(cudaMemcpy(deviceColumn, hostColumn.data(),
0060                              hostColumn.size() * sizeof(T),
0061                              cudaMemcpyHostToDevice));
0062 }
0063 
0064 template <typename T>
0065 void copyColumnToHost(std::vector<T>& hostColumn, const T* deviceColumn) {
0066   if (hostColumn.empty() || deviceColumn == nullptr) {
0067     return;
0068   }
0069 
0070   ACTS_CUDA_CHECK(cudaMemcpy(hostColumn.data(), deviceColumn,
0071                              hostColumn.size() * sizeof(T),
0072                              cudaMemcpyDeviceToHost));
0073 }
0074 
0075 }  // namespace ActsExamples