EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CpuMatrix.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CpuMatrix.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include "Acts/Plugins/Cuda/Utilities/CudaMatrix.cu"
12 
13 // column-major style Matrix Definition
14 
15 namespace Acts {
16 
17 template <typename var_t>
18 class CudaMatrix;
19 
20 template <typename var_t>
21 class CpuMatrix {
22  public:
23  CpuMatrix() = delete;
24  CpuMatrix(size_t nRows, size_t nCols, bool pinned = 0) {
25  m_setSize(nRows, nCols);
26  m_pinned = pinned;
27  if (pinned == 0) {
28  m_hostPtr = new var_t[m_size];
29  } else if (pinned == 1) {
30  cudaMallocHost(&m_hostPtr, m_size * sizeof(var_t));
31  }
32  }
33 
34  CpuMatrix(size_t nRows, size_t nCols, CudaMatrix<var_t>* cuMat,
35  bool pinned = 0) {
36  m_setSize(nRows, nCols);
37  m_pinned = pinned;
38  if (pinned == 0) {
39  m_hostPtr = new var_t[m_size];
40  } else if (pinned == 1) {
41  cudaMallocHost(&m_hostPtr, m_nRows * m_nCols * sizeof(var_t));
42  }
43  cudaMemcpy(m_hostPtr, cuMat->get(0, 0), m_size * sizeof(var_t),
44  cudaMemcpyDeviceToHost);
45  }
46 
48  if (!m_pinned) {
49  delete m_hostPtr;
50  } else if (m_pinned && m_hostPtr) {
51  cudaFreeHost(m_hostPtr);
52  }
53  }
54 
55  var_t* get(size_t row = 0, size_t col = 0) {
56  size_t offset = row + col * m_nRows;
57  return m_hostPtr + offset;
58  }
59 
60  void set(size_t row, size_t col, var_t val) {
61  size_t offset = row + col * m_nRows;
62  m_hostPtr[offset] = val;
63  }
64 
65  void copyD2H(var_t* devPtr, size_t len, size_t offset) {
66  cudaMemcpy(m_hostPtr + offset, devPtr, len * sizeof(var_t),
67  cudaMemcpyDeviceToHost);
68  }
69 
70  void copyD2H(var_t* devPtr, size_t len, size_t offset, cudaStream_t* stream) {
71  cudaMemcpyAsync(m_hostPtr + offset, devPtr, len * sizeof(var_t),
72  cudaMemcpyDeviceToHost, *stream);
73  }
74 
75  void zeros() { memset(m_hostPtr, 0, m_size * sizeof(var_t)); }
76 
77  private:
78  var_t* m_hostPtr = nullptr;
79  size_t m_nCols;
80  size_t m_nRows;
81  size_t m_size;
82  bool m_pinned;
83 
84  void m_setSize(size_t row, size_t col) {
85  m_nRows = row;
86  m_nCols = col;
88  }
89 };
90 
91 } // namespace Acts