forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
319 lines (230 loc) · 8.44 KB
/
memory.cpp
File metadata and controls
319 lines (230 loc) · 8.44 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
const size_t step_bytes = 1024;
void cleanSlate()
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
af::deviceGC();
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 0u);
ASSERT_EQ(lock_buffers, 0u);
ASSERT_EQ(alloc_bytes, 0u);
ASSERT_EQ(lock_bytes, 0u);
af::setMemStepSize(step_bytes);
ASSERT_EQ(af::getMemStepSize(), step_bytes);
}
TEST(Memory, GetDevicePtr)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
af::array a = af::randu(5, 5);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
a.device<float>();
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 0u); // 0 because device should unlock the buffer
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 0u);
}
TEST(Memory, Scope)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
{
af::array a = af::randu(5, 5);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 0u); // 0 because a is out of scope
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 0u);
}
TEST(Memory, SingleSizeLoop)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
{
af::array a = af::randu(5, 5);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
for (int i = 0; i < 100; i++) {
a = af::randu(5,5);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 2u); //2 because a new one is created before a is destroyed
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 2 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}
}
}
TEST(Memory, LargeLoop)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
const int num = step_bytes / sizeof(float);
size_t allocated = step_bytes;
af::array a = af::randu(num);
std::vector<float> hA(num);
a.host(&hA[0]);
// Run a large loop that allocates more and more memory at each step
for (int i = 0; i < 250; i++) {
af::array b = af::randu(num * (i + 1));
size_t current = (i + 1) * step_bytes;
allocated += current;
// Verify that new buffers are being allocated
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
// Limit to 10 to check before garbage collection
if (i < 10) {
ASSERT_EQ(alloc_buffers, (size_t)(i + 2)); //i is zero based
ASSERT_EQ(lock_buffers, 2u);
ASSERT_EQ(alloc_bytes, allocated);
ASSERT_EQ(lock_bytes, current + step_bytes);
}
}
size_t old_alloc_bytes = alloc_bytes;
size_t old_alloc_buffers = alloc_buffers;
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(old_alloc_bytes, alloc_bytes);
ASSERT_EQ(old_alloc_buffers, alloc_buffers);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}
TEST(Memory, IndexingOffset)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
const int num = step_bytes / sizeof(float);
af::array a = af::randu(num);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
{
af::array b = a(af::seq(1, num/2)); // Should just be an offset
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}
// b should not have deleted a
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}
TEST(Memory, IndexingCopy)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
const int num = step_bytes / sizeof(float);
af::array a = af::randu(num);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
{
// Should just a copy
af::array b = a(af::seq(0, num/2-1, 2));
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 2u);
ASSERT_EQ(lock_buffers, 2u);
ASSERT_EQ(alloc_bytes, 2 * step_bytes);
ASSERT_EQ(lock_bytes, 2 * step_bytes);
}
// b should not have deleted a
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 2u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 2 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}
TEST(Memory, Assign)
{
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
cleanSlate(); // Clean up everything done so far
const int num = step_bytes / sizeof(float);
af::array a = af::randu(num);
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 1u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 1 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
{
// Should just a copy
af::array b = af::randu(num / 2);
a(af::seq(num / 2)) = b;
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
// FIXME: An extra buffer is used because of copy on write
// Fix to not perform a copy when the buffer does not have children
ASSERT_EQ(alloc_buffers, 3u);
ASSERT_EQ(lock_buffers, 2u);
ASSERT_EQ(alloc_bytes, 3 * step_bytes);
ASSERT_EQ(lock_bytes, 2 * step_bytes);
}
// b should not have deleted a
af::deviceMemInfo(&alloc_bytes, &alloc_buffers,
&lock_bytes, &lock_buffers);
ASSERT_EQ(alloc_buffers, 3u);
ASSERT_EQ(lock_buffers, 1u);
ASSERT_EQ(alloc_bytes, 3 * step_bytes);
ASSERT_EQ(lock_bytes, 1 * step_bytes);
}