OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CpuBuffer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <cassert>
20 #include <cstring>
21 
22 #include "CudaMgr/CudaMgr.h"
23 #include "Logger/Logger.h"
24 
25 namespace Buffer_Namespace {
26 
28  BufferList::iterator segment_iter,
29  const int device_id,
31  const size_t page_size,
32  const size_t num_bytes)
33  : Buffer(bm, segment_iter, device_id, page_size, num_bytes), cuda_mgr_(cuda_mgr) {}
34 
35 void CpuBuffer::readData(int8_t* const dst,
36  const size_t num_bytes,
37  const size_t offset,
38  const MemoryLevel dst_memory_level,
39  const int dst_device_id) {
40  if (dst_memory_level == CPU_LEVEL) {
41  memcpy(dst, mem_ + offset, num_bytes);
42  } else if (dst_memory_level == GPU_LEVEL) {
43  CHECK_GE(dst_device_id, 0);
44  cuda_mgr_->copyHostToDevice(dst, mem_ + offset, num_bytes, dst_device_id);
45  } else {
46  LOG(FATAL) << "Unsupported buffer type";
47  }
48 }
49 
50 void CpuBuffer::writeData(int8_t* const src,
51  const size_t num_bytes,
52  const size_t offset,
53  const MemoryLevel src_memory_level,
54  const int src_device_id) {
55  if (src_memory_level == CPU_LEVEL) {
56  // std::cout << "Writing to CPU from source CPU" << std::endl;
57  memcpy(mem_ + offset, src, num_bytes);
58  } else if (src_memory_level == GPU_LEVEL) {
59  // std::cout << "Writing to CPU from source GPU" << std::endl;
60  CHECK_GE(src_device_id, 0);
61  cuda_mgr_->copyDeviceToHost(mem_ + offset, src, num_bytes);
62  } else {
63  LOG(FATAL) << "Unsupported buffer type";
64  }
65 }
66 
67 } // namespace Buffer_Namespace
void copyHostToDevice(int8_t *device_ptr, const int8_t *host_ptr, const size_t num_bytes, const int device_num, CUstream cuda_stream=0)
Definition: CudaMgr.cpp:127
void readData(int8_t *const dst, const size_t num_bytes, const size_t offset=0, const MemoryLevel dst_memory_level=CPU_LEVEL, const int dst_device_id=-1) override
Definition: CpuBuffer.cpp:35
#define LOG(tag)
Definition: Logger.h:285
#define CHECK_GE(x, y)
Definition: Logger.h:306
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:96
CpuBuffer(BufferMgr *bm, BufferList::iterator segment_iter, const int device_id, CudaMgr_Namespace::CudaMgr *cuda_mgr, const size_t page_size=512, const size_t num_bytes=0)
Definition: CpuBuffer.cpp:27
void copyDeviceToHost(int8_t *host_ptr, const int8_t *device_ptr, const size_t num_bytes, CUstream cuda_stream=0)
Definition: CudaMgr.cpp:143
CudaMgr_Namespace::CudaMgr * cuda_mgr_
Definition: CpuBuffer.h:49
void writeData(int8_t *const src, const size_t num_bytes, const size_t offset=0, const MemoryLevel src_memory_level=CPU_LEVEL, const int src_device_id=-1) override
Definition: CpuBuffer.cpp:50
Note(s): Forbid Copying Idiom 4.1.
Definition: Buffer.h:42