-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgpu-ops.cu
More file actions
152 lines (127 loc) · 5.19 KB
/
Copy pathgpu-ops.cu
File metadata and controls
152 lines (127 loc) · 5.19 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
#include "dynet/cuda.h"
#include "dynet/gpu-ops.h"
#include "dynet/gpu-kernels.h"
#include "dynet/functors.h"
namespace dynet {
namespace gpu {
// CUDA kernel. Each thread takes care of one element of c
__global__ void ker_dense_to_sparse_assign(int n, const unsigned int *idx, float *src, float *trg) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
// Make sure we do not go out of bounds
if (id < n)
trg[idx[id]] = src[id];
}
void dense_to_sparse_assign(int n, const unsigned int *idx, float *src, float *trg) {
if(n > 0) {
auto tb = SizeToBlockThreadPair(n);
int total_size = tb.first*tb.second;
for(int curr_pos = 0; curr_pos < n; curr_pos += total_size)
ker_dense_to_sparse_assign<<<tb.first, tb.second>>>(
std::min(total_size, n-curr_pos), idx+curr_pos, src+curr_pos, trg);
}
}
// CUDA kernel. Each thread takes care of one element of c
__global__ void ker_sparse_to_dense_assign(int n, const unsigned int *idx, float *src, float *trg) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
// Make sure we do not go out of bounds
if (id < n)
trg[id] = src[idx[id]];
}
void sparse_to_dense_assign(int n, const unsigned int *idx, float *src, float *trg) {
if(n > 0) {
auto tb = SizeToBlockThreadPair(n);
int total_size = tb.first*tb.second;
for(int curr_pos = 0; curr_pos < n; curr_pos += total_size)
ker_sparse_to_dense_assign<<<tb.first, tb.second>>>(
std::min(total_size, n-curr_pos), idx+curr_pos, src, trg+curr_pos);
}
}
// CUDA kernel. Each thread takes care of one element of c
__global__ void ker_dense_to_sparse_subtract(int n, const unsigned int *idx, float *src, float *trg) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
// Make sure we do not go out of bounds
if (id < n)
atomicAdd(trg + idx[id], -src[id]);
}
void dense_to_sparse_subtract(int n, const unsigned int *idx, float *src, float *trg) {
if(n > 0) {
auto tb = SizeToBlockThreadPair(n);
int total_size = tb.first*tb.second;
for(int curr_pos = 0; curr_pos < n; curr_pos += total_size)
ker_dense_to_sparse_subtract<<<tb.first, tb.second>>>(
std::min(total_size, n-curr_pos), idx+curr_pos, src+curr_pos, trg);
}
}
// CUDA kernel. Each thread takes care of one element of c
__global__ void ker_sparse_to_dense_block_assign_and_multiply(int n, const unsigned *idx, int bsize, float mult, float* src, float *trg) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
// Make sure we do not go out of bounds
if (id < n*bsize)
trg[id] = src[idx[id/bsize]*bsize+id%bsize] * mult;
}
void sparse_to_dense_block_assign_and_multiply(int n, const unsigned *idx, int bsize, float mult, float *src, float *trg) {
if(n > 0) {
auto tb = SizeToBlockThreadPair(n*bsize);
int total_size = tb.first*tb.second;
for(int curr_pos = 0; curr_pos < n; curr_pos += total_size/bsize)
ker_sparse_to_dense_block_assign_and_multiply<<<tb.first, tb.second>>>(
std::min(total_size/bsize, n-curr_pos),
idx+curr_pos, bsize, mult, src, trg+curr_pos*bsize);
}
}
// CUDA kernel. Each thread takes care of one row copy.
__global__ void ker_parallel_memcpy(int num_seqs, float **src, float **trg, float **len) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
int seq_id = id % num_seqs;
int i = id / num_seqs;
if (i < (unsigned long)len[seq_id])
trg[seq_id][i] = src[seq_id][i];
__syncthreads();
}
void parallel_memcpy(int num_seqs, int max_len, float **src, float **trg, float **len) {
if(num_seqs > 0) {
auto tb = SizeToBlockThreadPair(num_seqs*max_len);
ker_parallel_memcpy<<<tb.first, tb.second>>>(num_seqs, src, trg, len);
}
}
// CUDA kernel. Each thread takes care of one row copy.
__global__ void ker_parallel_accumulate(int num_seqs, float **src, float **trg, float **len) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
int seq_id = id % num_seqs;
int i = id / num_seqs;
if (i < (unsigned long)len[seq_id])
atomicAdd(&trg[seq_id][i], src[seq_id][i]);
__syncthreads();
}
void parallel_accumulate(int num_seqs, int max_len, float **src, float **trg, float **len) {
if(num_seqs > 0) {
auto tb = SizeToBlockThreadPair(num_seqs*max_len);
ker_parallel_accumulate<<<tb.first, tb.second>>>(num_seqs, src, trg, len);
}
}
// CUDA kernel. Each thread takes care of one element of c
__global__ void ker_dense_to_sparse_block_add(int n, const unsigned *idx, int bsize, float* src, float *trg) {
// Get our global thread ID
int id = blockIdx.x*blockDim.x+threadIdx.x;
// Make sure we do not go out of bounds
if (id < n*bsize)
atomicAdd(trg + idx[id/bsize]*bsize+id%bsize, src[id]);
}
void dense_to_sparse_block_add(int n, const unsigned *idx, int bsize, float *src, float *trg) {
if(n > 0) {
auto tb = SizeToBlockThreadPair(n*bsize);
int total_size = tb.first*tb.second;
for(int curr_pos = 0; curr_pos < n; curr_pos += total_size/bsize)
ker_dense_to_sparse_block_add<<<tb.first, tb.second>>>(
std::min(total_size/bsize, n-curr_pos),
idx+curr_pos, bsize, src+curr_pos*bsize, trg);
}
}
} // namespace gpu
} // namespace dynet