HIP: Heterogenous-computing Interface for Portability
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
staging_buffer.h
1 /*
2 Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
3 Permission is hereby granted, free of charge, to any person obtaining a copy
4 of this software and associated documentation files (the "Software"), to deal
5 in the Software without restriction, including without limitation the rights
6 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in
10 all copies or substantial portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANNTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INNCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANNY CLAIM, DAMAGES OR OTHER
15 LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17 THE SOFTWARE.
18 */
19 
20 //#pragma once
21 #ifndef STAGING_BUFFER_H
22 #define STAGING_BUFFER_H
23 
24 #include "hsa.h"
25 
26 
27 //-------------------------------------------------------------------------------------------------
28 // An optimized "staging buffer" used to implement Host-To-Device and Device-To-Host copies.
29 // Some GPUs may not be able to directly access host memory, and in these cases we need to
30 // stage the copy through a pinned staging buffer. For example, the CopyHostToDevice
31 // uses the CPU to copy to a pinned "staging buffer", and then use the GPU DMA engine to copy
32 // from the staging buffer to the final destination. The copy is broken into buffer-sized chunks
33 // to limit the size of the buffer and also to provide better performance by overlapping the CPU copies
34 // with the DMA copies.
35 //
36 // PinInPlace is another algorithm which pins the host memory "in-place", and copies it with the DMA
37 // engine. This routine is under development.
38 //
39 // Staging buffer provides thread-safe access via a mutex.
40 struct StagingBuffer {
41 
42  static const int _max_buffers = 4;
43 
44  StagingBuffer(hsa_agent_t hsaAgent, hsa_region_t systemRegion, size_t bufferSize, int numBuffers) ;
45  ~StagingBuffer();
46 
47  void CopyHostToDevice(void* dst, const void* src, size_t sizeBytes, hsa_signal_t *waitFor);
48  void CopyHostToDevicePinInPlace(void* dst, const void* src, size_t sizeBytes, hsa_signal_t *waitFor);
49 
50  void CopyDeviceToHost (void* dst, const void* src, size_t sizeBytes, hsa_signal_t *waitFor);
51  void CopyDeviceToHostPinInPlace(void* dst, const void* src, size_t sizeBytes, hsa_signal_t *waitFor);
52 
53 
54 private:
55  hsa_agent_t _hsa_agent;
56  size_t _bufferSize; // Size of the buffers.
57  int _numBuffers;
58 
59  char *_pinnedStagingBuffer[_max_buffers];
60  hsa_signal_t _completion_signal[_max_buffers];
61  std::mutex _copy_lock; // provide thread-safe access
62 };
63 
64 #endif
Definition: staging_buffer.h:40