Skip to content

Commit a73f0d4

Browse files
committed
update self_pin
1 parent cc9508c commit a73f0d4

File tree

1 file changed

+85
-12
lines changed

1 file changed

+85
-12
lines changed

self_pin/self_pin.cpp

+85-12
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,90 @@
33
#include <hc.hpp>
44
#include <hc_am.hpp>
55

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) {
109
hc::accelerator acc;
1110
auto peers = acc.get_peers();
1211
peers.push_back(acc);
1312
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
1517
, peers.data(), peers.size());
1618
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+
}
1737
}
38+
private:
39+
std::vector<T*> _r;
40+
};
41+
42+
template <typename T>
43+
class PrePinned {
44+
45+
public:
46+
1847
~PrePinned() [[cpu]] {
1948
hc::accelerator acc;
2049
am_status_t status;
2150
status = hc::am_memory_host_unlock(acc, this);
2251
assert(status == AM_SUCCESS);
2352
}
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+
2464
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;
2586
};
2687

88+
template <typename T>
89+
Register<PrePinned<T>> PrePinned<T>::r;
2790

2891
#if 0
2992
#ifdef __HCC_CPU__
@@ -32,26 +95,36 @@ PrePinned<int> bar;
3295
[[hc]] PrePinned<int> bar;
3396
#endif
3497
#else
35-
PrePinned<int> bar;
98+
//PrePinned<int> bar;
3699
#endif
37100

101+
102+
PrePinned<int>* const bar = PrePinned<int>::get_new();
103+
PrePinned<int>* const foobar = PrePinned<int>::get_new();
104+
38105
int main() {
39106

40-
// PrePinned<int> bar;
41107

42-
bar.data = 1234;
108+
bar->data = 1234;
43109
hc::array_view<int,1> av(1);
44110

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;
46114

47-
PrePinned<int>* barp = &bar;
115+
int foo = 1234;
116+
pin_memory(&foo, sizeof(foo));
117+
int* foop = &foo;
48118

49119
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;
51123
}).wait();
52124

53-
printf("after bar.data: %d\n", bar.data);
125+
printf("after bar.data: %d\n", bar->data);
54126
printf("av[0]:%d\n",av[0]);
127+
printf("*foop: %d\n",*foop);
55128

56129
return 0;
57130
}

0 commit comments

Comments
 (0)