-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathvec.cxx
More file actions
43 lines (35 loc) · 949 Bytes
/
vec.cxx
File metadata and controls
43 lines (35 loc) · 949 Bytes
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
#include <vector>
#include <cstdio>
#include <cuda_runtime.h>
// Declare the storage of an object. Don't execute its ctor or dtor!
[[storage_only]] __device__ std::vector<int> global_vec;
__global__ void global_ctors() {
// Launch a single-thread dynamic initializer kernel.
// Use placement new to construct global_vec.
new (&global_vec) std::vector<int>();
}
__global__ void global_dtors() {
// Call the pseudo-destructor to destruct global_vec.
global_vec.~vector();
}
__global__ void load() {
// Push 10 items
for(int i : 10)
global_vec.push_back(i * i);
}
__global__ void print() {
// Each thread prints one vector item.
int tid = threadIdx.x;
if(tid < global_vec.size())
printf("%3d: %3d\n", tid, global_vec[tid]);
}
int main() {
// Inititialize.
global_ctors<<<1,1>>>();
// Do the work.
load<<<1, 1>>>();
print<<<1, 128>>>();
cudaDeviceSynchronize();
// Cleanup.
global_dtors<<<1,1>>>();
}