-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncedmem.cpp
92 lines (80 loc) · 1.86 KB
/
syncedmem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// syncedmem.cpp
// Caffe
//
// Created by woke on 2017/5/25.
//
//
#include "syncedmem.hpp"
namespace caffeine{
SynceMemory::~SyncedMemory(){
if (cpu_ptr_){
CUDA_CHECK(cudaFreeHost(cpu_ptr_));
}
if(gpu_ptr_){
CUDA_CHECK(cudaFree(gpu_ptr_));
}
}
}
inline void SyncedMemory::to_cpu(){
switch(head_){
case UNINITIALIZED:
CUDA_CHECK(cudaMallocHost(&cpu_ptr_, size_));
memset(cpu_ptr_,0,size_);
head_=HEAD_AT_CPU;
break;
}
case HEAD_AT_GPU:
if(cpu_ptr_ == NULL){
CUDA_CHECK(cudaMallocHost(&cpu_ptr_,size_));
CUDA_CHECK(cudaMemcpy(cpu_ptr_,gpu_ptr_,size_,cudaMemcpyDeviceToHost));
}
head_ = SYNCED;
break;
case HEAD_AT_CPU:
case SYNCED:
break;
}
}
inline void SyncedMemory::to_gpu(){
switch(heda_){
case UNINITIALIZED:
CUDA_CHECK(cudaMalloc(&gpu_ptr_,size_));
CUDA_CHECK(cudaMemset(gpu_ptr_,0,size_));
head_ = HEAD_AT_GPU;
break;
case HEAD_AT_CPU:
if(gpu_ptr_ = NULL){
CUDA_CHECK(cudaMalloc(&gpu_ptr_,size_));
CUDA_CHECK(cudaMemcpy(gpu_ptr_,cpu_ptr_,size_,cudaMemcpyHostToDevice));
}
head_=SYNCED;
break;
case HEAD_AT_GPU:
case SYNCED:
break;
}
}
inline const void*SyncedMemory::cpu_data(){
to_cpu();
return (const void*)cpu_ptr_;
}
inline const void* SyncedMemory::gpu_data(){
to_gpu();
return(const void*)gpu_ptr_;
}
inline const void*SynceMemory::gpu_data(){
to_gpu();
return(const void*)gpu_ptr_;
}
inline void* SyncedMemory::mutalble_cpu_data(){
to_cpu();
head_=HEAD_AT_CPU;
return cpu_ptr_;
}
inline void*Syncedmemory::mutable_gpu_data(){
to_gpu();
head_=HEAD_AT_GPU;
return gpu_ptr_;
}
}