-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_ptr.h
More file actions
210 lines (159 loc) · 6.49 KB
/
gpu_ptr.h
File metadata and controls
210 lines (159 loc) · 6.49 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef GPU_PTR_H_
#define GPU_PTR_H_
//#include "Exception.hpp"
#include <cassert>
#include <iostream>
#include <vector>
#include <cuda_runtime.h>
/**
* Very simple class that suits the GPU fine for accessing memory
*/
class gpu_raw_ptr {
public:
float* ptr; //!< pointer to allocated memory
size_t pitch; //!< Pitch in bytes of allocated m
};
class gpu_ptr_2D {
public:
// Allocating data on the GPU
gpu_ptr_2D(unsigned int width, unsigned int height, int border = 0, float* cpu_ptr=NULL);
//Deallocates the data
~gpu_ptr_2D();
const gpu_raw_ptr& getRawPtr() const {
return data;
}
const unsigned int& getWidth() const {
return data_width;
}
const unsigned int& getHeight() const {
return data_height;
}
const int& getBorder() const {
return data_border;
}
// Performs GPU-GPU copy of a width x height domain starting at x_offset, y_offset from a different gpu_ptr
void copy(const gpu_ptr_2D& other,
unsigned int x_offset=0, unsigned int y_offset=0,
unsigned int width=0, unsigned int height=0, int border=0);
void download(float* cpu_ptr,
unsigned int x_offset=0, unsigned int y_offset=0,
unsigned int width=0, unsigned int height=0);
// Perform CPU to GPU copy of a witdh x height domain starting at x_offset and y_offset on the cpu_ptr.
void upload(const float* cpu_ptr, unsigned int x_offset=0, unsigned int y_offset=0,
unsigned int width=0, unsigned int height=0);
// Performs GPU "memset" of a width x height domain starting at x-offset, y_offset. Only really useful for setting all bits to 0 or 1.
void set(int value, unsigned int x_offset=0, unsigned int y_offset=0,
unsigned int width=0, unsigned int height=0, int border=0);
// Same as above, only with float.
void set(float value, unsigned int x_offset=0, unsigned int y_offset=0,
unsigned int width=0, unsigned int height=0);
private:
gpu_raw_ptr data;
size_t pitch;
unsigned int data_width;
unsigned int data_height;
int data_border;
};
inline gpu_ptr_2D::gpu_ptr_2D(unsigned int width, unsigned int height, int border, float* cpu_ptr) {
data_width = width + 2*border;
data_height = height + 2*border;
data_border = border;
data.ptr = 0;
data.pitch = 0;
cudaMallocPitch((void**) &data.ptr, &data.pitch, data_width*sizeof(float), data_height);
if (cpu_ptr != NULL) upload(cpu_ptr);
}
inline gpu_ptr_2D::~gpu_ptr_2D() {
cudaFree(data.ptr);
}
inline void gpu_ptr_2D::copy(const gpu_ptr_2D& other, unsigned int x_offset, unsigned int y_offset, unsigned int width, unsigned int height, int border){
// width = (width == 0) ? data_width : width;
// height = (height == 0) ? data_height : height;
data_width = width + 2*border;
data_height = height + 2*border;
data_border = border;
size_t pitch1 = data.pitch;
float* ptr1 = (float*) ((char*) data.ptr+y_offset*pitch1) + x_offset;
size_t pitch2 = other.getRawPtr().pitch;
float* ptr2 = (float*) ((char*) other.getRawPtr().ptr+y_offset*pitch2) + x_offset;
assert(data_width == other.getWidth() && data_height == other.getHeight() && ptr1 != ptr2);
cudaMemcpy2D(ptr1, pitch1, ptr2, pitch2, width*sizeof(float), height, cudaMemcpyDeviceToDevice);
}
inline void gpu_ptr_2D::download(float* cpu_ptr, unsigned int x_offset, unsigned int y_offset, unsigned int width, unsigned int height){
width = (width == 0) ? data_width :width;
height = (height == 0) ? data_height : height;
size_t pitch1 = width*sizeof(float);
float* ptr1 = cpu_ptr;
size_t pitch2 = data.pitch;
float* ptr2 = (float*) ((char*) data.ptr+y_offset*pitch2) + x_offset;
cudaMemcpy2D(ptr1, pitch1, ptr2, pitch2, width*sizeof(float), height, cudaMemcpyDeviceToHost);
}
inline void gpu_ptr_2D::upload(const float* cpu_ptr, unsigned int x_offset, unsigned int y_offset, unsigned int width, unsigned int height){
width = (width == 0) ? data_width :width;
height = (height == 0) ? data_height : height;
size_t pitch1 = data.pitch;
float* ptr1 = (float*) ((char*) data.ptr+y_offset*pitch1) + x_offset;
size_t pitch2 = width*sizeof(float);
const float* ptr2 = cpu_ptr;
cudaMemcpy2D(ptr1, pitch1, ptr2, pitch2, width*sizeof(float), height, cudaMemcpyHostToDevice);
}
inline void gpu_ptr_2D::set(int value, unsigned int x_offset, unsigned int y_offset, unsigned int width, unsigned int height, int border){
width = (width == 0) ? data_width :width;
height = (height == 0) ? data_height : height;
data_width = width + 2*border;
data_height = height + 2*border;
data_border = border;
size_t pitch = data.pitch;
float* ptr = (float*) ((char*) data.ptr+y_offset*pitch) + x_offset;
cudaMemset2D(ptr, pitch, value, width*sizeof(float), height);
}
inline void gpu_ptr_2D::set(float value, unsigned int x_offset, unsigned int y_offset, unsigned int width, unsigned int height){
width = (width == 0) ? data_width :width;
height = (height == 0) ? data_height : height;
std::vector<float> tmp(width*height, value);
size_t pitch1 = data.pitch;
float* ptr1 = (float*) ((char*) data.ptr+y_offset*pitch1) + x_offset;
size_t pitch2 = width*sizeof(float);
const float* ptr2 = &tmp[0];
cudaMemcpy2D(ptr1, pitch1, ptr2, pitch2, width*sizeof(float), height, cudaMemcpyHostToDevice);
}
class gpu_ptr_1D{
public:
gpu_ptr_1D(unsigned int width, float* cpu_ptr=NULL);
~gpu_ptr_1D();
//void download(float* cpu_ptr, unsigned int x_offset=0, unsigned int width=0);
void upload(const float* cpu_ptr, unsigned int x_offset=0, unsigned int width=0);
//void set(int value, unsigned int x_offset=0, unsigned int width=0);
void set(float value, unsigned int x_offset=0, unsigned int width=0);
float* getRawPtr() const {
return data_ptr;
}
const unsigned int& getWidth() const {
return data_width;
}
private:
float* data_ptr;
unsigned int data_width;
};
inline void gpu_ptr_1D::upload(const float* cpu_ptr, unsigned int x_offset, unsigned int width) {
width = (width == 0) ? data_width : width;
float* ptr1 = data_ptr + x_offset;
const float* ptr2 = cpu_ptr;
cudaMemcpy(ptr1, ptr2, width*sizeof(float), cudaMemcpyHostToDevice);
}
inline gpu_ptr_1D::gpu_ptr_1D(unsigned int width, float* cpu_ptr) {
data_width = width;
data_ptr = 0;
cudaMalloc((void**) &data_ptr, data_width*sizeof(float));
if (cpu_ptr != NULL) upload(cpu_ptr);
}
inline gpu_ptr_1D::~gpu_ptr_1D() {
cudaFree(data_ptr);
}
inline void gpu_ptr_1D::set(float value, unsigned int x_offset, unsigned int width) {
width = (width == 0) ? data_width : width;
float* ptr = data_ptr + x_offset;
std::vector<float> tmp(width, value);
cudaMemcpy(ptr, &tmp[0], width*sizeof(float), cudaMemcpyHostToDevice);
}
#endif