forked from gravitino/cudalist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudalist.cuh
115 lines (83 loc) · 2.92 KB
/
cudalist.cuh
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
#include <iostream>
#include <assert.h>
#define CHECKARRAYBOUNDS
template<class value_t, class index_t>
class culist {
private:
const bool external_memory;
const index_t capacity;
index_t length, lower, upper;
value_t * data;
public:
__host__
culist(index_t capacity_) : external_memory(false), capacity(capacity_),
length(0), lower(0), upper(0) {
data = new value_t[capacity];
}
__device__
culist(value_t * data_, index_t capacity_) :
external_memory(true),
data(data_), capacity(capacity_),
length(0), lower(0), upper(0) {}
__host__ __device__
~culist() {
if(!external_memory)
delete [] data;
}
__host__
void show() {
std::cout << "[ ";
for (int m = 0, i = lower; m < length;
++m, i = (i+1) == capacity ? 0 : i+1)
std::cout << data[i] << ", ";
std::cout << "]" << std::endl;
}
__forceinline__ __host__ __device__
void push_front(value_t value) {
#ifdef CHECKARRAYBOUNDS
assert (length+1 <= capacity);
#endif
lower = (lower == 0) ? capacity-1 : lower-1;
data[lower] = value;
length += 1;
}
__forceinline__ __host__ __device__
void push_back(value_t value) {
#ifdef CHECKARRAYBOUNDS
assert (length+1 <= capacity);
#endif
data[upper] = value;
upper = (upper == capacity-1) ? 0 : upper+1;
length += 1;
}
__forceinline__ __host__ __device__
value_t pop_front() {
#ifdef CHECKARRAYBOUNDS
assert (length > 0);
#endif
value_t result = data[lower];
lower = (lower == capacity-1) ? 0 : lower+1;
length -= 1;
return result;
}
__forceinline__ __host__ __device__
value_t pop_back() {
#ifdef CHECKARRAYBOUNDS
assert (length > 0);
#endif
upper = (upper == 0) ? capacity-1 : upper-1;
length -= 1;
return data[upper];
}
__forceinline__ __host__ __device__
value_t& get(index_t i) {
#ifdef CHECKARRAYBOUNDS
assert (i < length);
#endif
i += lower;
i = i > capacity-1 ? i-capacity : i;
return data[i];
}
__forceinline__ __host__ __device__
value_t& operator[] (index_t i) {return get(i);}
};