Debian Codegraph @

rocrtst/suites/stress/queue_write_index_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/queue_write_index_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 enum memoryOrdering {
64 SCACQ_SCREL,
65 SCACQUIRE,
66 RELAXED,
67 SCRELEASE,
68 MEM_ORDERING_END};
69
70 static const uint32_t kNumThreadsForAdd = 10;
71
72 static const uint32_t kNumOfAddAtomic = 1*1024*1024;
73
74 typedef struct write_index_add_atomic_data_s {
75 hsa_queue_t* queue;
76 int memory_ordering_type;
77 } write_index_add_atomic_data_t;
78
79
80 static void thread_proc_write_index_add_atomic(void* data) {
81 write_index_add_atomic_data_t* thread_data = reinterpret_cast<write_index_add_atomic_data_t*> (data);
82 uint64_t ii;
83 for (ii = 0; ii < kNumOfAddAtomic; ++ii) {
84 switch (thread_data->memory_ordering_type) {
85 case SCACQ_SCREL:
86 hsa_queue_add_write_index_scacq_screl(thread_data->queue, 1);
87 break;
88 case SCACQUIRE:
89 hsa_queue_add_write_index_scacquire(thread_data->queue, 1);
90 break;
91 case RELAXED:
92 hsa_queue_add_write_index_relaxed(thread_data->queue, 1);
93 break;
94 case SCRELEASE:
95 hsa_queue_add_write_index_screlease(thread_data->queue, 1);
96 break;
97 default:
98 break;
99 }
100 }
101 }
102
103 static const uint32_t kNumThreadsForCas = 4;
104 static const uint32_t kNumOfCasAtomic = 1*1024*1024;
105 typedef struct write_index_cas_thread_data_s {
106 hsa_queue_t* queue;
107 int thread_index;
108 int num_threads;
109 uint64_t termination_value;
110 int memory_ordering_type;
111 } write_index_cas_thread_data_t;
112
113 static void thread_proc_write_index_cas_atomic(void* data) {
114 write_index_cas_thread_data_t* thread_data = reinterpret_cast<write_index_cas_thread_data_t*>(data);
115
116 uint64_t ii;
117 for (ii = thread_data->thread_index; ii < thread_data->termination_value; ii += thread_data->num_threads) {
118 switch (thread_data->memory_ordering_type) {
119 case SCACQ_SCREL:
120 while ((uint64_t)ii !=
121 hsa_queue_cas_write_index_scacq_screl(thread_data->queue, ii, ii + 1)) {}
122 break;
123 case SCACQUIRE:
124 while ((uint64_t)ii !=
125 hsa_queue_cas_write_index_scacquire(thread_data->queue, ii, ii + 1)) {}
126 break;
127 case RELAXED:
128 while ((uint64_t)ii !=
129 hsa_queue_cas_write_index_relaxed(thread_data->queue, ii, ii + 1)) {}
130 break;
131 case SCRELEASE:
132 while ((uint64_t)ii !=
133 hsa_queue_cas_write_index_screlease(thread_data->queue, ii, ii + 1)) {}
134 break;
135 }
136 }
137 }
138
139 static const uint32_t kNumOfLoadStoreAtomic = 1*1024*1024;
140 // Use a 64-bit value to test the atomicity
141 static uint64_t kStoreValue = UINT64_MAX;
142
143 typedef struct write_index_load_atomic_thread_data_s {
144 hsa_queue_t* queue;
145 uint64_t num_iterations;
146 int memory_ordering_type;
147 } write_index_load_atomic_thread_data_t;
148
149 typedef struct write_index_store_atomic_thread_data_s {
150 hsa_queue_t* queue;
151 uint64_t kStoreValue;
152 uint64_t num_iterations;
153 int memory_ordering_type;
154 } write_index_store_atomic_thread_data_t;
155
156 static uint64_t const WRITE_INDEX_FAILURE = 2;
157 void thread_proc_write_index_load_atomic(void* data) {
158 write_index_load_atomic_thread_data_t* thread_data =
159 reinterpret_cast<write_index_load_atomic_thread_data_t*>(data);
160 uint32_t ii;
161 for (ii = 0; ii < thread_data->num_iterations; ++ii) {
162 uint64_t write_index = WRITE_INDEX_FAILURE; // initalized with value other than kStoreValue
163 if (SCRELEASE == thread_data->memory_ordering_type) {
164 write_index = hsa_queue_load_write_index_scacquire(thread_data->queue);
165 } else if (RELAXED == thread_data->memory_ordering_type) {
166 write_index = hsa_queue_load_write_index_relaxed(thread_data->queue);
167 }
168 // The only two possible values
169 EXPECT_TRUE(0 == write_index || kStoreValue == write_index);
170 }
171 }
172
173 void thread_proc_write_index_store_atomic(void* data) {
174 write_index_store_atomic_thread_data_t* thread_data =
175 reinterpret_cast<write_index_store_atomic_thread_data_t*>(data);
176 uint32_t ii;
177 for (ii = 0; ii < thread_data->num_iterations; ++ii) {
178 if (SCRELEASE == thread_data->memory_ordering_type) {
179 hsa_queue_store_write_index_screlease(thread_data->queue, thread_data->kStoreValue);
180 } else if (RELAXED == thread_data->memory_ordering_type) {
181 hsa_queue_store_write_index_relaxed(thread_data->queue, thread_data->kStoreValue);
182 }
183 }
184 }
185
186
187
188 QueueWriteIndexConcurrentTest::QueueWriteIndexConcurrentTest(bool launch_Concurrent_AddWriteIndex,
189 bool launch_Concurrent_CasWriteIndex ,
190 bool launch_Concurrent_LoadStoreWriteIndex) :TestBase() {
191 set_num_iteration(10); // Number of iterations to execute of the main test;
192 // This is a default value which can be overridden
193 // on the command line.
194
195 std::string name;
196 std::string desc;
197
198 name = "RocR Queue write Index Tests";
199 desc = "These series of tests are Stress tests which contains different subtests ";
200
201 if (launch_Concurrent_AddWriteIndex) {
202 name += " AddWriteIndex";
203 desc += " This test Verifies that the hsa_queue_write_index_add operations is atomic"
204 " and 'torn' adds do not occur when this API is executed concurrently.";
205 } else if (launch_Concurrent_CasWriteIndex) {
206 name += " CasWriteIndex";
207 desc += " This test Verifies that the hsa_queue_cas_write_index operations is atomic,"
208 " and 'torn' compare and swaps do not occur when this API is executed"
209 " concurrently.";
210 } else if (launch_Concurrent_LoadStoreWriteIndex) {
211 name += " LoadStoreWriteIndex";
212 desc += " This test Verifies that the hsa_queue_write_index_load and store operations"
213 " are atomic, and 'torn' loads or stores do not occur when these APIs are executed"
214 " concurrently.";
215 }
216 set_title(name);
217 set_description(desc);
218 }
219
220 QueueWriteIndexConcurrentTest::~QueueWriteIndexConcurrentTest(void) {
221 }
222
223 // Any 1-time setup involving member variables used in the rest of the test
224 // should be done here.
225 void QueueWriteIndexConcurrentTest::SetUp(void) {
226 hsa_status_t err;
227
228 TestBase::SetUp();
229
230 err = rocrtst::SetDefaultAgents(this);
231 ASSERT_EQ(HSA_STATUS_SUCCESS, err);
232
233 err = rocrtst::SetPoolsTypical(this);
234 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
235 return;
236 }
237
238 void QueueWriteIndexConcurrentTest::Run(void) {
239 // Compare required profile for this test case with what we're actually
240 // running on
241 if (!rocrtst::CheckProfile(this)) {
242 return;
243 }
244
245 TestBase::Run();
246 }
247
248 void QueueWriteIndexConcurrentTest::DisplayTestInfo(void) {
249 TestBase::DisplayTestInfo();
250 }
251
252 void QueueWriteIndexConcurrentTest::DisplayResults(void) const {
253 // Compare required profile for this test case with what we're actually
254 // running on
255 if (!rocrtst::CheckProfile(this)) {
256 return;
257 }
258
259 return;
260 }
261
262 void QueueWriteIndexConcurrentTest::Close() {
263 // This will close handles opened within rocrtst utility calls and call
264 // hsa_shut_down(), so it should be done after other hsa cleanup
265 TestBase::Close();
266 }
267
268
269
270
271 static const char kSubTestSeparator[] = " **************************";
272
273 static void PrintDebugSubtestHeader(const char *header) {
274 std::cout << " *** QueueWriteIndexConcurrent Subtest: " << header << " ***" << std::endl;
275 }
276
277
278
279 // This test verify check memory can be
280 // concurrently allocated from pool on ROCR agents
281 void QueueWriteIndexConcurrentTest::QueueAddWriteIndexAtomic(hsa_agent_t cpuAgent,
282 hsa_agent_t gpuAgent) {
283 hsa_status_t err;
284
285 // check if the gpuAgent supports kernel dispatch
286 uint32_t features = 0;
287 err = hsa_agent_get_info(gpuAgent, HSA_AGENT_INFO_FEATURE, &features);
288 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
289 if (0 == (features & HSA_AGENT_FEATURE_KERNEL_DISPATCH)) {
290 return;
291 }
292
293
294 // Get max number of queues
295 uint32_t queue_size;
296 err = hsa_agent_get_info(gpuAgent, HSA_AGENT_INFO_QUEUE_MAX_SIZE, &queue_size);
297 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
298
299 // Create a queue
300 hsa_queue_t* queue;
301 err = hsa_queue_create(gpuAgent, queue_size, HSA_QUEUE_TYPE_SINGLE, NULL, NULL, UINT32_MAX, UINT32_MAX, &queue);
302 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
303
304 int memory_ordering_type;
305 for (memory_ordering_type = SCACQ_SCREL; memory_ordering_type < MEM_ORDERING_END; ++memory_ordering_type) {
306 // Thread data
307 write_index_add_atomic_data_t thread_data;
308 thread_data.queue = queue;
309 thread_data.memory_ordering_type = memory_ordering_type;
310
311 // Create a test group
312 rocrtst::test_group* tg_concurrent = rocrtst::TestGroupCreate(kNumThreadsForAdd);
313
314 uint32_t kk;
315 for (kk = 0; kk < kNumThreadsForAdd; kk++) {
316 rocrtst::TestGroupAdd(tg_concurrent, &thread_proc_write_index_add_atomic, &thread_data, 1);
317 }
318
319 // Create threads for each test
320 rocrtst::TestGroupThreadCreate(tg_concurrent);
321
322 // Start to run tests
323 rocrtst::TestGroupStart(tg_concurrent);
324
325 // Wait all tests finish
326 rocrtst::TestGroupWait(tg_concurrent);
327
328 // Exit all tests
329 rocrtst::TestGroupExit(tg_concurrent);
330
331 // Destroy thread group and cleanup resources
332 rocrtst::TestGroupDestroy(tg_concurrent);
333
334 // Verify the write_index
335 uint64_t write_index = hsa_queue_load_write_index_relaxed(queue);
336 uint64_t expected = (uint64_t)(kNumOfAddAtomic * kNumThreadsForAdd);
337 ASSERT_EQ(write_index, expected);
338
339 // Restore the write_index of the queue
340 hsa_queue_store_write_index_screlease(queue, 0);
341 }
342
343 // Destroy queue
344 err = hsa_queue_destroy(queue);
345 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
346 }
347
348
349
350
351 // This test verify check memory can be
352 // concurrently allocated from pool on ROCR agents
353 void QueueWriteIndexConcurrentTest::QueueCasWriteIndexAtomic(hsa_agent_t cpuAgent, hsa_agent_t gpuAgent) {
354 hsa_status_t err;
355
356 // check if the gpuAgent supports kernel dispatch
357 uint32_t features = 0;
358 err = hsa_agent_get_info(gpuAgent, HSA_AGENT_INFO_FEATURE, &features);
359 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
360 if (0 == (features & HSA_AGENT_FEATURE_KERNEL_DISPATCH)) {
361 return;
362 }
363
364
365 // Get max number of queues
366 uint32_t queue_size;
367 err = hsa_agent_get_info(gpuAgent, HSA_AGENT_INFO_QUEUE_MAX_SIZE, &queue_size);
368 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
369
370 // Create a queue
371 hsa_queue_t* queue;
372 err = hsa_queue_create(gpuAgent, queue_size, HSA_QUEUE_TYPE_SINGLE, NULL, NULL, UINT32_MAX, UINT32_MAX, &queue);
373 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
374
375 int memory_ordering_type;
376 for (memory_ordering_type = SCACQ_SCREL; memory_ordering_type < MEM_ORDERING_END; ++memory_ordering_type) {
377 // Thread data
378 write_index_cas_thread_data_t thread_data[kNumThreadsForCas];
379
380 // Create a test group
381 rocrtst::test_group* tg_concurrent = rocrtst::TestGroupCreate(kNumThreadsForCas);
382
383 uint32_t kk;
384 for (kk = 0; kk < kNumThreadsForCas; ++kk) {
385 thread_data[kk].queue = queue;
386 thread_data[kk].thread_index = kk;
387 thread_data[kk].num_threads = kNumThreadsForCas;
388 thread_data[kk].memory_ordering_type = memory_ordering_type;
389 thread_data[kk].termination_value = kNumOfCasAtomic;
390 rocrtst::TestGroupAdd(tg_concurrent, &thread_proc_write_index_cas_atomic, thread_data + kk, 1);
391 }
392
393 // Create threads for each test
394 rocrtst::TestGroupThreadCreate(tg_concurrent);
395
396 // Start to run tests
397 rocrtst::TestGroupStart(tg_concurrent);
398
399 // Wait all tests finish
400 rocrtst::TestGroupWait(tg_concurrent);
401
402 // Exit all tests
403 rocrtst::TestGroupExit(tg_concurrent);
404
405 // Destroy thread group and cleanup resources
406 rocrtst::TestGroupDestroy(tg_concurrent);
407
408 // Verify the write_index
409 uint64_t write_index = hsa_queue_load_write_index_relaxed(queue);
410 uint64_t expected = (uint64_t)(kNumOfCasAtomic);
411 ASSERT_EQ(write_index, expected);
412
413 // Restore the write_index of the queue
414 hsa_queue_store_write_index_screlease(queue, 0);
415 }
416
417 // Destroy queue
418 err = hsa_queue_destroy(queue);
419 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
420 }
421
422
423 // This test verify if each Agent pool's attribute information
424 // is consistent across multiple thread.
425 void QueueWriteIndexConcurrentTest::QueueLoadStoreWriteIndexAtomic(hsa_agent_t cpuAgent, hsa_agent_t gpuAgent) {
426 hsa_status_t err;
427
428 // check if the gpuAgent supports kernel dispatch
429 uint32_t features = 0;
430 err = hsa_agent_get_info(gpuAgent, HSA_AGENT_INFO_FEATURE, &features);
431 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
432 if (0 == (features & HSA_AGENT_FEATURE_KERNEL_DISPATCH)) {
433 return;
434 }
435
436
437 // Get max number of queues
438 uint32_t queue_size;
439 err = hsa_agent_get_info(gpuAgent, HSA_AGENT_INFO_QUEUE_MAX_SIZE, &queue_size);
440 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
441
442 // Create a queue
443 hsa_queue_t* queue;
444 err = hsa_queue_create(gpuAgent, queue_size, HSA_QUEUE_TYPE_SINGLE, NULL, NULL, UINT32_MAX, UINT32_MAX, &queue);
445 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
446
447 // Use a 64-bit value to test the atomicity
448 kStoreValue = UINT64_MAX;
449
450 int memory_ordering_type;
451 for (memory_ordering_type = RELAXED; memory_ordering_type < MEM_ORDERING_END; ++memory_ordering_type) {
452 // Thread data
453 write_index_load_atomic_thread_data_t load_thread_data[2];
454 write_index_store_atomic_thread_data_t store_thread_data[2];
455 load_thread_data[0].queue = queue;
456 load_thread_data[0].num_iterations = kNumOfLoadStoreAtomic;
457 load_thread_data[0].memory_ordering_type = memory_ordering_type;
458 load_thread_data[1].queue = queue;
459 load_thread_data[1].num_iterations = kNumOfLoadStoreAtomic;
460 load_thread_data[1].memory_ordering_type = memory_ordering_type;
461
462 store_thread_data[0].queue = queue;
463 store_thread_data[0].kStoreValue = 0;
464 store_thread_data[0].num_iterations = kNumOfLoadStoreAtomic;
465 store_thread_data[0].memory_ordering_type = memory_ordering_type;
466 store_thread_data[1].queue = queue;
467 store_thread_data[1].kStoreValue = kStoreValue;
468 store_thread_data[1].num_iterations = kNumOfLoadStoreAtomic;
469 store_thread_data[1].memory_ordering_type = memory_ordering_type;
470 // Create a test group
471 rocrtst::test_group* tg_concurrent = rocrtst::TestGroupCreate(4);
472 rocrtst::TestGroupAdd(tg_concurrent, &thread_proc_write_index_load_atomic, load_thread_data, 1);
473 rocrtst::TestGroupAdd(tg_concurrent, &thread_proc_write_index_load_atomic, load_thread_data + 1, 1);
474 rocrtst::TestGroupAdd(tg_concurrent, &thread_proc_write_index_store_atomic, store_thread_data, 1);
475 rocrtst::TestGroupAdd(tg_concurrent, &thread_proc_write_index_store_atomic, store_thread_data + 1, 1);
476
477
478 // Create threads for each test
479 rocrtst::TestGroupThreadCreate(tg_concurrent);
480
481 // Start to run tests
482 rocrtst::TestGroupStart(tg_concurrent);
483
484 // Wait all tests finish
485 rocrtst::TestGroupWait(tg_concurrent);
486
487 // Exit all tests
488 rocrtst::TestGroupExit(tg_concurrent);
489
490 // Destroy thread group and cleanup resources
491 rocrtst::TestGroupDestroy(tg_concurrent);
492 }
493
494 // Destroy queue
495 err = hsa_queue_destroy(queue);
496 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
497 }
498
499
500 void QueueWriteIndexConcurrentTest::QueueAddWriteIndexAtomic(void) {
501 hsa_status_t err;
502
503 if (verbosity() > 0) {
504 PrintDebugSubtestHeader("QueueAddWriteIndexAtomic");
505 }
506
507 // find all cpu agents
508 std::vector<hsa_agent_t> cpus;
509 err = hsa_iterate_agents(rocrtst::IterateCPUAgents, &cpus);
510 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
511
512 // find all gpu agents
513 std::vector<hsa_agent_t> gpus;
514 err = hsa_iterate_agents(rocrtst::IterateGPUAgents, &gpus);
515 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
516
517 for (unsigned int i = 0 ; i< gpus.size(); ++i) {
518 QueueAddWriteIndexAtomic(cpus[0], gpus[i]);
519 }
520
521 if (verbosity() > 0) {
522 std::cout << "subtest Passed" << std::endl;
523 std::cout << kSubTestSeparator << std::endl;
524 }
525 }
526
527 void QueueWriteIndexConcurrentTest::QueueCasWriteIndexAtomic(void) {
528 hsa_status_t err;
529
530 if (verbosity() > 0) {
531 PrintDebugSubtestHeader("QueueCasWriteIndexAtomic");
532 }
533
534 // find all cpu agents
535 std::vector<hsa_agent_t> cpus;
536 err = hsa_iterate_agents(rocrtst::IterateCPUAgents, &cpus);
537 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
538
539 // find all gpu agents
540 std::vector<hsa_agent_t> gpus;
541 err = hsa_iterate_agents(rocrtst::IterateGPUAgents, &gpus);
542 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
543
544 for (unsigned int i = 0 ; i< gpus.size(); ++i) {
545 QueueCasWriteIndexAtomic(cpus[0], gpus[i]);
546 }
547
548 if (verbosity() > 0) {
549 std::cout << "subtest Passed" << std::endl;
550 std::cout << kSubTestSeparator << std::endl;
551 }
552 }
553
554 void QueueWriteIndexConcurrentTest::QueueLoadStoreWriteIndexAtomic(void) {
555 hsa_status_t err;
556
557 if (verbosity() > 0) {
558 PrintDebugSubtestHeader("QueueLoadStoreWriteIndexAtomic");
559 }
560
561 // find all cpu agents
562 std::vector<hsa_agent_t> cpus;
563 err = hsa_iterate_agents(rocrtst::IterateCPUAgents, &cpus);
564 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
565
566 // find all gpu agents
567 std::vector<hsa_agent_t> gpus;
568 err = hsa_iterate_agents(rocrtst::IterateGPUAgents, &gpus);
569 ASSERT_EQ(err, HSA_STATUS_SUCCESS);
570
571 for (unsigned int i = 0 ; i< gpus.size(); ++i) {
572 QueueLoadStoreWriteIndexAtomic(cpus[0], gpus[i]);
573 }
574
575 if (verbosity() > 0) {
576 std::cout << "subtest Passed" << std::endl;
577 std::cout << kSubTestSeparator << std::endl;
578 }
579 }
580