Debian Codegraph @

rocrtst/suites/stress/memory_concurrent_tests.cc

raw
1 /*
2 * =============================================================================
3 * ROC Runtime Conformance Release License
4 * =============================================================================
5 * The University of Illinois/NCSA
6 * Open Source License (NCSA)
7 *
8 * Copyright (c) 2018, Advanced Micro Devices, Inc.
9 * All rights reserved.
10 *
11 * Developed by:
12 *
13 * AMD Research and AMD ROC Software Development
14 *
15 * Advanced Micro Devices, Inc.
16 *
17 * www.amd.com
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to
21 * deal with the Software without restriction, including without limitation
22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 * and/or sell copies of the Software, and to permit persons to whom the
24 * Software is furnished to do so, subject to the following conditions:
25 *
26 * - Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimers.
28 * - Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimers in
30 * the documentation and/or other materials provided with the distribution.
31 * - Neither the names of <Name of Development Group, Name of Institution>,
32 * nor the names of its contributors may be used to endorse or promote
33 * products derived from this Software without specific prior written
34 * permission.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39 * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
42 * DEALINGS WITH THE SOFTWARE.
43 *
44 */
45
46
47 #include <fcntl.h>
48 #include <algorithm>
49 #include <iostream>
50 #include <vector>
51 #include <memory>
52 #include <string>
53
54 #include "suites/stress/memory_concurrent_tests.h"
55 #include "common/base_rocr_utils.h"
56 #include "common/common.h"
57 #include "common/helper_funcs.h"
58 #include "common/hsatimer.h"
59 #include "common/concurrent_utils.h"
60 #include "gtest/gtest.h"
61 #include "hsa/hsa.h"
62
63
64 static const uint32_t kNumThreads = 1024;
65 static const uint32_t kMaxAllocSize = 1024 * 1024;
66
67
68
69
70 typedef struct control_block {
71 hsa_amd_memory_pool_t* pool;
72 size_t alloc_size;
73 void* alloc_pointer;
74 } cb_t;
75
76
77 // Callback function which will call upon when need
78 // to allocate memory from the pool in the thread.
79 static void CallbackHSAMemoryAllocateFunc(void *data) {
80 hsa_status_t err;
81 cb_t *cb = static_cast<cb_t*>(data);
82
83 err = hsa_amd_memory_pool_allocate(*(cb->pool),
84 cb->alloc_size, 0,
85 reinterpret_cast<void**>(&(cb->alloc_pointer)));
86 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
87
88 return;
89 }
90
91 // Callback function which will call upon when need
92 // to Free memory from the pool in the thread.
93 static void CallbackHSAMemoryFreeFunc(void *data) {
94 hsa_status_t err;
95 cb_t *cb = static_cast<cb_t*>(data);
96
97 err = hsa_memory_free(cb->alloc_pointer);
98 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
99
100 return;
101 }
102
103 typedef struct thread_data_get_pool_info_s {
104 // The current pool
105 hsa_amd_memory_pool_t pool;
106 // The pool info retrieved from main thread
107 rocrtst::pool_info_t* info;
108 // Consistency check result
109 int consistency;
110 } thread_data_get_pool_info_t;
111
112 // Callback function which will call upon when need
113 // to Fetch different info for the pool in the thread.
114 static void CallbackGetPoolInfo(void* data) {
115 hsa_status_t err;
116
117 thread_data_get_pool_info_t* thread_data =
118 static_cast<thread_data_get_pool_info_t*>(data);
119
120 rocrtst::pool_info_t info;
121 memset(&info, 0, sizeof(rocrtst::pool_info_t));
122 err = rocrtst::AcquirePoolInfo(thread_data->pool, &info);
123 ASSERT_EQ(HSA_STATUS_SUCCESS, err);
124
125 if (*(thread_data->info) == info) {
126 // The pool info is consistent with the one got from the main thread
127 thread_data->consistency = 1;
128 } else {
129 thread_data->consistency = 0;
130 }
131 }
132
133 MemoryConcurrentTest::MemoryConcurrentTest(bool launch_Concurrent_Allocate_,
134 bool launch_Concurrent_Free_ ,
135 bool launch_Concurrent_PoolGetInfo_) :TestBase() {
136 set_num_iteration(10); // Number of iterations to execute of the main test;
137 // This is a default value which can be overridden
138 // on the command line.
139
140 std::string name;
141 std::string desc;
142
143 name = "RocR Memory Concurrent";
144 desc = "These series of tests are Stress tests which contains different subtests ";
145
146 if (launch_Concurrent_Allocate_) {
147 name += " Allocate";
148 desc += " This test Verify that memory can be concurrently allocated from pool"
149 " and thread safety while allocating memory from different threads"
150 " on ROCR agents";
151 } else if (launch_Concurrent_Free_) {
152 name += " Free";
153 desc += " This test thet memory Verify can be concurrently freed from pool"
154 " and thread safety while memory free from different threads"
155 " on ROCR agents";
156 } else if (launch_Concurrent_PoolGetInfo_) {
157 name += " PoolGetInfo";
158 desc += " This test Verify that memory pool info can be concurrently "
159 " get from different threads on ROCR agents";
160 }
161 set_title(name);
162 set_description(desc);
163 }
164
165 MemoryConcurrentTest::~MemoryConcurrentTest(void) {
166 }
167
168 // Any 1-time setup involving member variables used in the rest of the test
169 // should be done here.
170 void MemoryConcurrentTest::SetUp(void) {
171 hsa_status_t err;
172
173 TestBase::SetUp();
174
175 err = rocrtst::SetDefaultAgents(this);
176 ASSERT_EQ(HSA_STATUS_SUCCESS, err);
177
178 err = rocrtst::SetPoolsTypical(this);
179 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
180 return;
181 }
182
183 void MemoryConcurrentTest::Run(void) {
184 // Compare required profile for this test case with what we're actually
185 // running on
186 if (!rocrtst::CheckProfile(this)) {
187 return;
188 }
189
190 TestBase::Run();
191 }
192
193 void MemoryConcurrentTest::DisplayTestInfo(void) {
194 TestBase::DisplayTestInfo();
195 }
196
197 void MemoryConcurrentTest::DisplayResults(void) const {
198 // Compare required profile for this test case with what we're actually
199 // running on
200 if (!rocrtst::CheckProfile(this)) {
201 return;
202 }
203
204 return;
205 }
206
207 void MemoryConcurrentTest::Close() {
208 // This will close handles opened within rocrtst utility calls and call
209 // hsa_shut_down(), so it should be done after other hsa cleanup
210 TestBase::Close();
211 }
212
213
214
215
216 static const char kSubTestSeparator[] = " **************************";
217
218 static void PrintMemorySubtestHeader(const char *header) {
219 std::cout << " *** Memory Stress Subtest: " << header << " ***" << std::endl;
220 }
221
222 static void PrintAgentNameAndType(hsa_agent_t agent) {
223 hsa_status_t err;
224
225 char ag_name[64];
226 hsa_device_type_t ag_type;
227
228 err = hsa_agent_get_info(agent, HSA_AGENT_INFO_NAME, ag_name);
229 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
230
231 err = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &ag_type);
232 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
233
234 std::cout << " Agent: " << ag_name << " (";
235 switch (ag_type) {
236 case HSA_DEVICE_TYPE_CPU:
237 std::cout << "CPU)";
238 break;
239 case HSA_DEVICE_TYPE_GPU:
240 std::cout << "GPU)";
241 break;
242 case HSA_DEVICE_TYPE_DSP:
243 std::cout << "DSP)";
244 break;
245 case HSA_DEVICE_TYPE_AIE:
246 std::cout << "AIE)";
247 break;
248 }
249 std::cout << std::endl;
250 return;
251 }
252
253 // This test verify check memory can be
254 // concurrently allocated from pool on ROCR agents
255 void MemoryConcurrentTest::MemoryConcurrentAllocate(hsa_agent_t agent,
256 hsa_amd_memory_pool_t pool) {
257 hsa_status_t err;
258
259 rocrtst::pool_info_t pool_i;
260 err = rocrtst::AcquirePoolInfo(pool, &pool_i);
261 ASSERT_EQ(HSA_STATUS_SUCCESS, err);
262
263 if (verbosity() > 0) {
264 PrintAgentNameAndType(agent);
265 }
266
267 // Determine if allocation is allowed in this memory pool
268 bool alloc = false;
269 err = hsa_amd_memory_pool_get_info(pool,
270 HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_ALLOWED, &alloc);
271
272 if (alloc) {
273 size_t alloc_size;
274 size_t total_vram_size;
275 hsa_device_type_t ag_type;
276
277 err = hsa_amd_memory_pool_get_info(pool, HSA_AMD_MEMORY_POOL_INFO_ALLOC_MAX_SIZE,
278 &total_vram_size);
279 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
280
281 err = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &ag_type);
282 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
283
284 // If VRAM size <= 512MB, it should be APU whose VRAM is carved from system memory
285 // and much smaller than dGPU. Change the threshold accordingly.
286 if (total_vram_size <= 536870912 && ag_type == HSA_DEVICE_TYPE_GPU) {
287 // Make sure do not allocate more than 1/4 of the available vram size
288 err = hsa_agent_get_info(agent, (hsa_agent_info_t)HSA_AMD_AGENT_INFO_MEMORY_AVAIL,
289 &total_vram_size);
290 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
291 alloc_size = (total_vram_size*1/4 <= kMaxAllocSize*kNumThreads) ? total_vram_size*1/(4*kNumThreads): kMaxAllocSize;
292 } else {
293 // Make sure do not allocate more than 3/4 of the vram size
294 alloc_size = (total_vram_size*3/4 <= kMaxAllocSize*kNumThreads) ? total_vram_size*3/(4*kNumThreads): kMaxAllocSize;
295 }
296
297 // Page align the alloc_size
298 alloc_size = alloc_size - (alloc_size & ((1 << 12) - 1));
299
300 // Create a test group
301 rocrtst::test_group* tg_concurrent = rocrtst::TestGroupCreate(kNumThreads);
302
303 // The control blocks are used to pass data to the threads
304 uint32_t kk;
305 cb_t cb[kNumThreads];
306 for (kk = 0; kk < kNumThreads; kk++) {
307 cb[kk].pool = &pool;
308 cb[kk].alloc_size = alloc_size;
309 rocrtst::TestGroupAdd(tg_concurrent, &CallbackHSAMemoryAllocateFunc, &cb[kk], 1);
310 }
311
312 // Create threads for each test
313 rocrtst::TestGroupThreadCreate(tg_concurrent);
314
315 // Start to run tests
316 rocrtst::TestGroupStart(tg_concurrent);
317
318 // Wait all tests finish
319 rocrtst::TestGroupWait(tg_concurrent);
320
321 // Exit all tests
322 rocrtst::TestGroupExit(tg_concurrent);
323
324 // Destroy thread group and cleanup resources
325 rocrtst::TestGroupDestroy(tg_concurrent);
326
327 // Check for overlapping addresses
328 char *addr1, *addr2;
329 for (kk = 0; kk < kNumThreads; ++kk) {
330 addr1 = reinterpret_cast<char *>(cb[kk].alloc_pointer);
331 addr2 = addr1+alloc_size;
332 ASSERT_NE(reinterpret_cast<void *>(addr1), nullptr);
333 uint32_t ll;
334 for (ll = kk+1; ll < kNumThreads; ++ll) {
335 if (addr1 < reinterpret_cast<char *>(cb[ll].alloc_pointer)) {
336 ASSERT_LE(addr2, reinterpret_cast<char *>(cb[ll].alloc_pointer));
337 }
338 if (addr2 > reinterpret_cast<char *>(cb[ll].alloc_pointer)+alloc_size) {
339 ASSERT_GE(addr1, reinterpret_cast<char *>(cb[ll].alloc_pointer)+alloc_size);
340 }
341 }
342 }
343
344 for (uint32_t ii = 0; ii < kNumThreads; ii++) {
345 err = hsa_memory_free(cb[ii].alloc_pointer);
346 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
347 }
348 }
349 return;
350 }
351
352
353
354
355 // This test verify check memory can be
356 // concurrently allocated from pool on ROCR agents
357 void MemoryConcurrentTest::MemoryConcurrentFree(hsa_agent_t agent,
358 hsa_amd_memory_pool_t pool) {
359 hsa_status_t err;
360
361 rocrtst::pool_info_t pool_i;
362 err = rocrtst::AcquirePoolInfo(pool, &pool_i);
363 ASSERT_EQ(HSA_STATUS_SUCCESS, err);
364
365 if (verbosity() > 0) {
366 PrintAgentNameAndType(agent);
367 }
368
369 // Determine if allocation is allowed in this pool
370 bool alloc = false;
371 err = hsa_amd_memory_pool_get_info(pool,
372 HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_ALLOWED, &alloc);
373 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
374
375 if (alloc) {
376 // Get the maximum allocation size
377 size_t alloc_size;
378 size_t total_vram_size;
379 hsa_device_type_t ag_type;
380
381 err = hsa_amd_memory_pool_get_info(pool, HSA_AMD_MEMORY_POOL_INFO_ALLOC_MAX_SIZE,
382 &total_vram_size);
383 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
384
385 err = hsa_agent_get_info(agent, HSA_AGENT_INFO_DEVICE, &ag_type);
386 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
387
388 // If VRAM size <= 512MB, it should be APU whose VRAM is carved from system memory
389 // and much smaller than dGPU. Change the threshold accordingly.
390 if (total_vram_size <= 536870912 && ag_type == HSA_DEVICE_TYPE_GPU) {
391 // Make sure do not allocate more than 1/4 of the available vram size
392 err = hsa_agent_get_info(agent, (hsa_agent_info_t)HSA_AMD_AGENT_INFO_MEMORY_AVAIL,
393 &total_vram_size);
394 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
395 alloc_size = (total_vram_size*1/4 <= kMaxAllocSize*kNumThreads) ? total_vram_size*1/(4*kNumThreads): kMaxAllocSize;
396 } else {
397 // Make sure do not allocate more than 3/4 of the vram size
398 alloc_size = (total_vram_size*3/4 <= kMaxAllocSize*kNumThreads) ? total_vram_size*3/(4*kNumThreads): kMaxAllocSize;
399 }
400
401 // Page align the alloc_size
402 alloc_size = alloc_size - (alloc_size & ((1 << 12) - 1));
403
404 // Create a test group
405 rocrtst::test_group* tg_concurrent = rocrtst::TestGroupCreate(kNumThreads);
406
407 // The control blocks are used to pass data to the threads
408 uint32_t kk;
409 cb_t cb[kNumThreads];
410 for (kk = 0; kk < kNumThreads; kk++) {
411 cb[kk].pool = &pool;
412 cb[kk].alloc_size = alloc_size;
413 err = hsa_amd_memory_pool_allocate(*(cb[kk].pool), cb[kk].alloc_size, 0, &(cb[kk].alloc_pointer));
414 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
415
416 rocrtst::TestGroupAdd(tg_concurrent, &CallbackHSAMemoryFreeFunc, &cb[kk], 1);
417 }
418
419 // Create threads for each test
420 rocrtst::TestGroupThreadCreate(tg_concurrent);
421
422 // Start to run tests
423 rocrtst::TestGroupStart(tg_concurrent);
424
425 // Wait all tests finish
426 rocrtst::TestGroupWait(tg_concurrent);
427
428 // Exit all tests
429 rocrtst::TestGroupExit(tg_concurrent);
430
431 // Destroy thread group and cleanup resources
432 rocrtst::TestGroupDestroy(tg_concurrent);
433 }
434 return;
435 }
436
437
438 // This test verify if each Agent pool's attribute information
439 // is consistent across multiple thread.
440 void MemoryConcurrentTest::MemoryConcurrentPoolGetInfo(hsa_agent_t agent,
441 hsa_amd_memory_pool_t pool) {
442 hsa_status_t err;
443
444 rocrtst::pool_info_t pool_i;
445 err = rocrtst::AcquirePoolInfo(pool, &pool_i);
446 ASSERT_EQ(HSA_STATUS_SUCCESS, err);
447
448 if (verbosity() > 0) {
449 PrintAgentNameAndType(agent);
450 }
451
452
453 uint32_t kk;
454 thread_data_get_pool_info_t thread_data[kNumThreads];
455
456 // Create a test group
457 rocrtst::test_group* tg_concurrent = rocrtst::TestGroupCreate(kNumThreads);
458
459 for (kk = 0; kk < kNumThreads; kk++) {
460 thread_data[kk].pool = pool;
461 thread_data[kk].info = &pool_i;
462 thread_data[kk].consistency = 0;
463 rocrtst::TestGroupAdd(tg_concurrent, &CallbackGetPoolInfo, thread_data + kk, 1);
464 }
465
466 // Create threads for each test
467 rocrtst::TestGroupThreadCreate(tg_concurrent);
468
469 // Start to run tests
470 rocrtst::TestGroupStart(tg_concurrent);
471
472 // Wait all tests finish
473 rocrtst::TestGroupWait(tg_concurrent);
474
475 // Exit all tests
476 rocrtst::TestGroupExit(tg_concurrent);
477
478 // Destroy thread group and cleanup resources
479 rocrtst::TestGroupDestroy(tg_concurrent);
480
481 // Verify pool info is consistent among all threads
482 for (kk = 0; kk < kNumThreads; kk++) {
483 ASSERT_EQ(thread_data[kk].consistency, 1);
484 }
485 return;
486 }
487
488
489
490 void MemoryConcurrentTest::MemoryConcurrentAllocate(void) {
491 hsa_status_t err;
492 std::vector<std::shared_ptr<rocrtst::agent_pools_t>> agent_pools;
493
494 if (verbosity() > 0) {
495 PrintMemorySubtestHeader("MemoryConcurrentAllocate in Stress Test");
496 }
497 err = rocrtst::GetAgentPools(&agent_pools);
498 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
499
500 auto pool_idx = 0;
501 for (auto a : agent_pools) {
502 for (auto p : a->pools) {
503 if (verbosity() > 0) {
504 std::cout << " Pool " << pool_idx++ << ":" << std::endl;
505 }
506 MemoryConcurrentAllocate(a->agent, p);
507 }
508 }
509
510 if (verbosity() > 0) {
511 std::cout << "subtest Passed" << std::endl;
512 std::cout << kSubTestSeparator << std::endl;
513 }
514 }
515
516 void MemoryConcurrentTest::MemoryConcurrentFree(void) {
517 hsa_status_t err;
518 std::vector<std::shared_ptr<rocrtst::agent_pools_t>> agent_pools;
519
520 if (verbosity() > 0) {
521 PrintMemorySubtestHeader("MemoryConcurrentFree in Stress Test");
522 }
523
524 err = rocrtst::GetAgentPools(&agent_pools);
525 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
526
527 auto pool_idx = 0;
528 for (auto a : agent_pools) {
529 for (auto p : a->pools) {
530 if (verbosity() > 0) {
531 std::cout << " Pool " << pool_idx++ << ":" << std::endl;
532 }
533 MemoryConcurrentFree(a->agent, p);
534 }
535 }
536
537 if (verbosity() > 0) {
538 std::cout << "subtest Passed" << std::endl;
539 std::cout << kSubTestSeparator << std::endl;
540 }
541 }
542
543 void MemoryConcurrentTest::MemoryConcurrentPoolGetInfo(void) {
544 hsa_status_t err;
545 std::vector<std::shared_ptr<rocrtst::agent_pools_t>> agent_pools;
546
547 if (verbosity() > 0) {
548 PrintMemorySubtestHeader("MemoryConcurrentPoolGetInfo in Stress Test");
549 }
550 err = rocrtst::GetAgentPools(&agent_pools);
551 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
552
553 auto pool_idx = 0;
554 for (auto a : agent_pools) {
555 for (auto p : a->pools) {
556 if (verbosity() > 0) {
557 std::cout << " Pool " << pool_idx++ << ":" << std::endl;
558 }
559 MemoryConcurrentPoolGetInfo(a->agent, p);
560 }
561 }
562
563 if (verbosity() > 0) {
564 std::cout << "subtest Passed" << std::endl;
565 std::cout << kSubTestSeparator << std::endl;
566 }
567 }