3
3
#include < hc.hpp>
4
4
#include < hc_am.hpp>
5
5
6
- template <typename T>
7
- class PrePinned {
8
- public:
9
- PrePinned () [[cpu]] {
6
+ #define DEBUG 1
7
+
8
+ void pin_memory (void * p, size_t s) {
10
9
hc::accelerator acc;
11
10
auto peers = acc.get_peers ();
12
11
peers.push_back (acc);
13
12
am_status_t status;
14
- status = hc::am_memory_host_lock (acc, this , sizeof (PrePinned<T>)
13
+ #ifdef DEBUG
14
+ printf (" trying to pin %p\n " ,p);
15
+ #endif
16
+ status = hc::am_memory_host_lock (acc, p, s
15
17
, peers.data (), peers.size ());
16
18
assert (status == AM_SUCCESS);
19
+ }
20
+
21
+ void unpin_memory (void * p) {
22
+ hc::accelerator acc;
23
+ am_status_t status;
24
+ status = am_memory_host_unlock (acc, p);
25
+ assert (status == AM_SUCCESS);
26
+ }
27
+
28
+ template <typename T>
29
+ class Register {
30
+ public:
31
+ void add (T* p) { _r.push_back (p); }
32
+ ~Register () {
33
+ for (auto p : _r) {
34
+ unpin_memory (p);
35
+ delete p;
36
+ }
17
37
}
38
+ private:
39
+ std::vector<T*> _r;
40
+ };
41
+
42
+ template <typename T>
43
+ class PrePinned {
44
+
45
+ public:
46
+
18
47
~PrePinned () [[cpu]] {
19
48
hc::accelerator acc;
20
49
am_status_t status;
21
50
status = hc::am_memory_host_unlock (acc, this );
22
51
assert (status == AM_SUCCESS);
23
52
}
53
+
54
+ static PrePinned<T>* get_new () {
55
+ auto n = new PrePinned<T>();
56
+ hc::AmPointerInfo info;
57
+ auto status = hc::am_memtracker_getinfo (&info, n);
58
+ assert (status == AM_SUCCESS);
59
+ n->pinned = reinterpret_cast <PrePinned<T>*>(info._devicePointer );
60
+ r.add (n);
61
+ return n->pinned ;
62
+ };
63
+
24
64
T data;
65
+
66
+ private:
67
+ PrePinned () [[cpu]] {
68
+ #if 0
69
+ hc::accelerator acc;
70
+ auto peers = acc.get_peers();
71
+ peers.push_back(acc);
72
+ am_status_t status;
73
+ status = hc::am_memory_host_lock(acc, this, sizeof(PrePinned<T>)
74
+ , peers.data(), peers.size());
75
+ assert(status == AM_SUCCESS);
76
+ #else
77
+ pin_memory (this , sizeof (*this ));
78
+ #endif
79
+
80
+
81
+ r.add (this );
82
+ }
83
+ PrePinned<T>* pinned;
84
+
85
+ static Register<PrePinned<T>> r;
25
86
};
26
87
88
+ template <typename T>
89
+ Register<PrePinned<T>> PrePinned<T>::r;
27
90
28
91
#if 0
29
92
#ifdef __HCC_CPU__
@@ -32,26 +95,36 @@ PrePinned<int> bar;
32
95
[[hc]] PrePinned<int> bar;
33
96
#endif
34
97
#else
35
- PrePinned<int > bar;
98
+ // PrePinned<int> bar;
36
99
#endif
37
100
101
+
102
+ PrePinned<int >* const bar = PrePinned<int >::get_new();
103
+ PrePinned<int >* const foobar = PrePinned<int >::get_new();
104
+
38
105
int main () {
39
106
40
- // PrePinned<int> bar;
41
107
42
- bar. data = 1234 ;
108
+ bar-> data = 1234 ;
43
109
hc::array_view<int ,1 > av (1 );
44
110
45
- printf (" before bar.data: %d\n " , bar.data );
111
+ printf (" before bar.data: %d\n " , bar->data );
112
+
113
+ PrePinned<int >* const barp = bar;
46
114
47
- PrePinned<int >* barp = &bar;
115
+ int foo = 1234 ;
116
+ pin_memory (&foo, sizeof (foo));
117
+ int * foop = &foo;
48
118
49
119
hc::parallel_for_each (hc::extent<1 >(1 ), [=] (hc::index <1 > i) [[hc]] {
50
- av[0 ] = barp->data ;
120
+ // av[0] = barp->data;
121
+ av[0 ] = *foop;
122
+ *foop = *foop+1 ;
51
123
}).wait ();
52
124
53
- printf (" after bar.data: %d\n " , bar. data );
125
+ printf (" after bar.data: %d\n " , bar-> data );
54
126
printf (" av[0]:%d\n " ,av[0 ]);
127
+ printf (" *foop: %d\n " ,*foop);
55
128
56
129
return 0 ;
57
130
}
0 commit comments