-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemset_offset_current.cpp
More file actions
577 lines (512 loc) · 21.9 KB
/
Copy pathmemset_offset_current.cpp
File metadata and controls
577 lines (512 loc) · 21.9 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <hip/hip_runtime.h>
#include <assert.h>
#include <cstring>
#include <iostream>
#include <set>
#include <map>
#include <cstdlib>
#include <string>
#define HIP_CHECK(error) \
{ \
hipError_t localError = error; \
if ((localError != hipSuccess)) { \
printf("HIP error %d at %s:%d\n", localError, __FILE__, __LINE__); \
assert(false); \
} \
}
// Poison value: chosen to be distinct from any memsetval we use
#define POISON_VALUE 0xDE
__global__ void memset_(char* dst, char value, size_t N, unsigned* wid, unsigned* xcc) {
size_t i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < N) {
unsigned smid = __smid();
wid[i] = smid;
xcc[i] = (smid >> 9) & 0xF;
dst[i] = value;
}
}
__global__ void verify_value(char* dst, char value, size_t N, unsigned* wid, unsigned* xcc, bool* mismatch) {
size_t i = blockIdx.x * blockDim.x + threadIdx.x;
size_t idx = N - i - 1;
if (idx < N && idx >= 0) {
unsigned smid = __smid();
wid[i] = smid;
xcc[i] = (smid >> 9) & 0xF;
dst[i] = value;
if (dst[idx] != value) {
//printf("Mismatch at i=%zu: expected 0x%02X got 0x%02X\n",
// i, value, dst[i]);
mismatch[idx] = true;
//abort();
}
}
}
__global__ void memset_xcd(char* dst, char value, size_t N, size_t xcd, unsigned* xcd_count) {
#if 0
unsigned smid = __smid();
auto xcc = (smid >> 9) & 0xF;
if (xcc == xcd) {
__shared__ size_t c;
if (threadIdx.x == 0) {
c = atomicAdd(xcd_count, 1);
}
__syncthreads();
if (c == 0 && threadIdx.x == 0) {
#if 1
for (size_t i = 0; i < N; i ++) {
dst[i] = value;
}
#endif
}
}
#endif
}
static bool testhipMemset(char memsetval, size_t numElements, size_t offset = 0) {
if (numElements <= offset) {
printf("Error: numElements (%zu) must be greater than offset (%zu)\n", numElements, offset);
return false;
}
size_t Nbytes = numElements * sizeof(char);
bool testResult = true;
printf("\n=== Test: memsetval=0x%02X numElements=%zu offset=%zu ===\n",
(unsigned char)memsetval, numElements, offset);
char* A_d = nullptr;
HIP_CHECK(hipMalloc(&A_d, Nbytes));
char* A_h = reinterpret_cast<char*>(malloc(Nbytes));
assert(A_h != nullptr);
// ---------------------------------------------------------------
// 1) Poison device memory with a known value from CPU
// Fill host buffer with POISON_VALUE, then copy to device.
// This way, if the kernel fails to write a byte, we'll see
// POISON_VALUE instead of uninitialized garbage.
// ---------------------------------------------------------------
::memset(A_h, POISON_VALUE, Nbytes);
HIP_CHECK(hipMemcpy(A_d, A_h, Nbytes, hipMemcpyHostToDevice));
printf("Poisoned device memory with 0x%02X (%zu bytes)\n", POISON_VALUE, Nbytes);
// ---------------------------------------------------------------
// 2) Verify poison landed correctly by reading back immediately
// ---------------------------------------------------------------
char* verify_h = reinterpret_cast<char*>(malloc(Nbytes));
assert(verify_h != nullptr);
HIP_CHECK(hipMemcpy(verify_h, A_d, Nbytes, hipMemcpyDeviceToHost));
bool poisonOk = true;
for (size_t i = 0; i < numElements; i++) {
if (verify_h[i] != (char)POISON_VALUE) {
printf("Poison verify FAILED at i=%zu: expected 0x%02X got 0x%02X\n",
i, POISON_VALUE,
(unsigned)(unsigned char)verify_h[i]);
poisonOk = false;
break;
}
}
if (poisonOk) {
printf("Poison verify OK: device buffer confirmed 0x%02X before kernel\n", POISON_VALUE);
}
free(verify_h);
// ---------------------------------------------------------------
// 3) Launch custom memset kernel with XCC tracking
// ---------------------------------------------------------------
size_t Nmemset = numElements - offset;
const unsigned blockSize = 256;
int maxGridX = 0;
HIP_CHECK(hipDeviceGetAttribute(&maxGridX, hipDeviceAttributeMaxGridDimX, 0));
unsigned gridSize = (unsigned)((Nmemset + blockSize - 1) / blockSize);
if (gridSize == 0) gridSize = 1;
if (gridSize > (unsigned)maxGridX) gridSize = (unsigned)maxGridX;
unsigned* wid_d = nullptr;
unsigned* xcc_d = nullptr;
HIP_CHECK(hipMalloc(&wid_d, Nmemset * sizeof(unsigned)));
HIP_CHECK(hipMalloc(&xcc_d, Nmemset * sizeof(unsigned)));
printf("Launching kernel: gridSize=%u blockSize=%u Nmemset=%zu\n", gridSize, blockSize, Nmemset);
memset_<<<gridSize, blockSize>>>(A_d + offset, memsetval, Nmemset, wid_d, xcc_d);
bool* mismatch_d = nullptr;
HIP_CHECK(hipMalloc(&mismatch_d, Nmemset * sizeof(bool)));
HIP_CHECK(hipMemset(mismatch_d, 0, Nmemset * sizeof(bool)));
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipDeviceSynchronize());
#if 0 /**/
printf("Checking values inside A_d directly on CPU\n\n\n\n\n");
for (size_t i = 0; i < Nmemset; i++) {
if (A_d[i + offset] != memsetval) {
printf("Mismatch at i=%zu: expected 0x%02X got 0x%02X\n",
i, memsetval, A_d[i + offset]);\
}
}
#endif
#if 0
printf("Checking values inside A_d directly on GPU\n\n\n\n\n");
verify_value<<<gridSize, blockSize>>>(A_d + offset, memsetval, Nmemset, wid_d, xcc_d, mismatch_d);
#endif
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipDeviceSynchronize());
for (size_t i = 0; i < Nmemset; i++) {
if (mismatch_d[i]) {
printf("Mismatch at i=%zu: expected 0x%02X got 0x%02X\n",
i, memsetval, A_d[i + offset]);
}
}
HIP_CHECK(hipFree(mismatch_d));
// ---------------------------------------------------------------
// 4) Copy back XCC tracking data
// ---------------------------------------------------------------
unsigned* wid_h = reinterpret_cast<unsigned*>(malloc(Nmemset * sizeof(unsigned)));
unsigned* xcc_h = reinterpret_cast<unsigned*>(malloc(Nmemset * sizeof(unsigned)));
assert(wid_h != nullptr && xcc_h != nullptr);
HIP_CHECK(hipMemcpy(wid_h, wid_d, Nmemset * sizeof(unsigned), hipMemcpyDeviceToHost));
HIP_CHECK(hipMemcpy(xcc_h, xcc_d, Nmemset * sizeof(unsigned), hipMemcpyDeviceToHost));
// ---------------------------------------------------------------
// 5) Copy back device buffer and check from CPU
// ---------------------------------------------------------------
::memset(A_h, 0, Nbytes); // clear host buffer before readback
HIP_CHECK(hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
// Check region before offset still has poison (kernel should not have touched it)
for (size_t i = 0; i < offset; i++) {
if (A_h[i] != (char)POISON_VALUE) {
printf("WARNING: pre-offset region corrupted at i=%zu: expected poison 0x%02X got 0x%02X\n",
i, POISON_VALUE, (unsigned)(unsigned char)A_h[i]);
}
}
// ---------------------------------------------------------------
// 6) Check kernel-written region and collect XCC stats
// ---------------------------------------------------------------
std::set<unsigned> xcc_set;
std::map<unsigned, size_t> xcc_counts; // xcc -> number of elements written
std::map<unsigned, size_t> xcc_fail_counts; // xcc -> number of mismatches
size_t mismatchCount = 0;
size_t mismatchPoisonCount = 0; // mismatches that are still the poison value
printf("Checking values inside after Memcpy\n\n\n\n\n");
for (size_t i = offset; i < numElements; i++) {
size_t ki = i - offset; // index into kernel arrays
unsigned this_xcc = xcc_h[ki];
xcc_set.insert(this_xcc);
xcc_counts[this_xcc]++;
if (A_h[i] != memsetval) {
testResult = false;
mismatchCount++;
xcc_fail_counts[this_xcc]++;
if (A_h[i] == (char)POISON_VALUE) {
mismatchPoisonCount++;
}
if (mismatchCount <= 20) { // print first 20 mismatches
std::cout << "Mismatch data : "
<< "numElements: " << numElements << " "
<< "offset: " << offset << " "
<< "i: " << i << " "
<< "A_h[i]: 0x" << std::hex << std::uppercase
<< static_cast<unsigned>(static_cast<unsigned char>(A_h[i]))
<< std::dec << std::nouppercase << " "
<< "memsetval: 0x" << std::hex << std::uppercase
<< static_cast<unsigned>(static_cast<unsigned char>(memsetval))
<< std::dec << std::nouppercase << " "
<< "smid: " << wid_h[ki] << " "
<< "xcc: " << this_xcc
<< (A_h[i] == (char)POISON_VALUE ? " [STILL POISON]" : " [CORRUPTED]")
<< std::endl;
}
}
}
// ---------------------------------------------------------------
// 7) Print XCC summary (always, even in passing case)
// ---------------------------------------------------------------
printf("\n--- XCC Summary ---\n");
printf("XCCs seen (count=%zu): {", xcc_set.size());
for (auto it = xcc_set.begin(); it != xcc_set.end(); ++it) {
if (it != xcc_set.begin()) printf(", ");
printf("%u", *it);
}
printf("}\n");
// Check XCC mask: XCD 0-3 means XCC values 0-3 (mask 0xF)
bool allOnOneAid = true;
for (unsigned xcc_val : xcc_set) {
if (xcc_val > 3) {
allOnOneAid = false;
break;
}
}
printf("All XCCs in XCD 0-3 (mask 0xF, one AID): %s\n", allOnOneAid ? "YES" : "NO");
printf("\nPer-XCC element counts:\n");
for (auto& [xcc_val, count] : xcc_counts) {
printf(" XCC %2u: %zu elements", xcc_val, count);
if (xcc_fail_counts.count(xcc_val)) {
printf(" *** %zu MISMATCHES ***", xcc_fail_counts[xcc_val]);
}
printf("\n");
}
if (mismatchCount > 0) {
printf("\nTotal mismatches: %zu / %zu (%.4f%%)\n",
mismatchCount, Nmemset, 100.0 * mismatchCount / Nmemset);
printf(" Of which still poison (0x%02X): %zu\n", POISON_VALUE, mismatchPoisonCount);
printf(" Of which corrupted (other value): %zu\n", mismatchCount - mismatchPoisonCount);
} else {
printf("\nResult: PASS (all %zu elements match 0x%02X)\n",
Nmemset, (unsigned char)memsetval);
}
printf("-------------------\n");
HIP_CHECK(hipFree(A_d));
HIP_CHECK(hipFree(wid_d));
HIP_CHECK(hipFree(xcc_d));
free(A_h);
free(wid_h);
free(xcc_h);
return testResult;
}
static bool testhipMemsetXcd(char memsetval, size_t numElements, size_t xcd, size_t offset = 0) {
if (numElements <= offset) {
printf("Error: numElements (%zu) must be greater than offset (%zu)\n", numElements, offset);
return false;
}
size_t Nbytes = numElements * sizeof(char);
bool testResult = true;
printf("\n=== Test: memsetval=0x%02X numElements=%zu offset=%zu xcd=%zu ===\n",
(unsigned char)memsetval, numElements, offset, xcd);
char* A_d = nullptr;
HIP_CHECK(hipMalloc(&A_d, Nbytes));
char* A_h = reinterpret_cast<char*>(malloc(Nbytes));
assert(A_h != nullptr);
// ---------------------------------------------------------------
// 1) Poison device memory with a known value from CPU
// Fill host buffer with POISON_VALUE, then copy to device.
// This way, if the kernel fails to write a byte, we'll see
// POISON_VALUE instead of uninitialized garbage.
// ---------------------------------------------------------------
::memset(A_h, POISON_VALUE, Nbytes);
HIP_CHECK(hipMemcpy(A_d, A_h, Nbytes, hipMemcpyHostToDevice));
printf("Poisoned device memory with 0x%02X (%zu bytes)\n", POISON_VALUE, Nbytes);
// ---------------------------------------------------------------
// 2) Verify poison landed correctly by reading back immediately
// ---------------------------------------------------------------
char* verify_h = reinterpret_cast<char*>(malloc(Nbytes));
assert(verify_h != nullptr);
HIP_CHECK(hipMemcpy(verify_h, A_d, Nbytes, hipMemcpyDeviceToHost));
bool poisonOk = true;
for (size_t i = 0; i < numElements; i++) {
if (verify_h[i] != (char)POISON_VALUE) {
printf("Poison verify FAILED at i=%zu: expected 0x%02X got 0x%02X\n",
i, POISON_VALUE,
(unsigned)(unsigned char)verify_h[i]);
poisonOk = false;
break;
}
}
if (poisonOk) {
printf("Poison verify OK: device buffer confirmed 0x%02X before kernel\n", POISON_VALUE);
}
free(verify_h);
// ---------------------------------------------------------------
// 3) Launch custom memset kernel with XCC tracking
// ---------------------------------------------------------------
size_t Nmemset = numElements - offset;
const unsigned blockSize = 256;
int maxGridX = 0;
HIP_CHECK(hipDeviceGetAttribute(&maxGridX, hipDeviceAttributeMaxGridDimX, 0));
unsigned gridSize = (unsigned)((Nmemset + blockSize - 1) / blockSize);
if (gridSize == 0) gridSize = 1;
if (gridSize > (unsigned)maxGridX) gridSize = (unsigned)maxGridX;
unsigned* wid_d = nullptr;
unsigned* xcc_d = nullptr;
HIP_CHECK(hipMalloc(&wid_d, Nmemset * sizeof(unsigned)));
HIP_CHECK(hipMalloc(&xcc_d, Nmemset * sizeof(unsigned)));
unsigned* xcd_count_d = nullptr;
HIP_CHECK(hipMalloc(&xcd_count_d, sizeof(unsigned)));
HIP_CHECK(hipMemset(xcd_count_d, 0, sizeof(unsigned)));
printf("Launching kernel: gridSize=%u blockSize=%u Nmemset=%zu xcd=%zu\n", gridSize, blockSize, Nmemset, xcd);
memset_xcd<<<gridSize, blockSize>>>(A_d + offset, memsetval, Nmemset, xcd, xcd_count_d);
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipDeviceSynchronize());
bool* mismatch_d = nullptr;
HIP_CHECK(hipMalloc(&mismatch_d, Nmemset * sizeof(bool)));
HIP_CHECK(hipMemset(mismatch_d, 0, Nmemset * sizeof(bool)));
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipDeviceSynchronize());
#if 0
printf("Checking values inside A_d directly on CPU\n\n\n\n\n");
for (size_t i = 0; i < Nmemset; i++) {
if (A_d[i + offset] != memsetval) {
printf("Mismatch at i=%zu: expected 0x%02X got 0x%02X\n",
i, memsetval, A_d[i + offset]);\
}
}
#endif
printf("Checking values inside A_d directly on GPU\n\n\n\n\n");
verify_value<<<gridSize, blockSize>>>(A_d + offset, memsetval, Nmemset, wid_d, xcc_d, mismatch_d);
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipDeviceSynchronize());
for (size_t i = 0; i < Nmemset; i++) {
if (mismatch_d[i]) {
printf("Mismatch at i=%zu: expected 0x%02X got 0x%02X\n",
i, memsetval, A_d[i + offset]);
}
}
HIP_CHECK(hipFree(mismatch_d));
// ---------------------------------------------------------------
// 4) Copy back XCC tracking data
// ---------------------------------------------------------------
unsigned* wid_h = reinterpret_cast<unsigned*>(malloc(Nmemset * sizeof(unsigned)));
unsigned* xcc_h = reinterpret_cast<unsigned*>(malloc(Nmemset * sizeof(unsigned)));
assert(wid_h != nullptr && xcc_h != nullptr);
HIP_CHECK(hipMemcpy(wid_h, wid_d, Nmemset * sizeof(unsigned), hipMemcpyDeviceToHost));
HIP_CHECK(hipMemcpy(xcc_h, xcc_d, Nmemset * sizeof(unsigned), hipMemcpyDeviceToHost));
// ---------------------------------------------------------------
// 5) Copy back device buffer and check from CPU
// ---------------------------------------------------------------
::memset(A_h, 0, Nbytes); // clear host buffer before readback
HIP_CHECK(hipMemcpy(A_h, A_d, Nbytes, hipMemcpyDeviceToHost));
// Check region before offset still has poison (kernel should not have touched it)
for (size_t i = 0; i < offset; i++) {
if (A_h[i] != (char)POISON_VALUE) {
printf("WARNING: pre-offset region corrupted at i=%zu: expected poison 0x%02X got 0x%02X\n",
i, POISON_VALUE, (unsigned)(unsigned char)A_h[i]);
}
}
// ---------------------------------------------------------------
// 6) Check kernel-written region and collect XCC stats
// ---------------------------------------------------------------
std::set<unsigned> xcc_set;
std::map<unsigned, size_t> xcc_counts; // xcc -> number of elements written
std::map<unsigned, size_t> xcc_fail_counts; // xcc -> number of mismatches
size_t mismatchCount = 0;
size_t mismatchPoisonCount = 0; // mismatches that are still the poison value
printf("Checking values inside after Memcpy\n\n\n\n\n");
for (size_t i = offset; i < numElements; i++) {
size_t ki = i - offset; // index into kernel arrays
unsigned this_xcc = xcc_h[ki];
xcc_set.insert(this_xcc);
xcc_counts[this_xcc]++;
if (A_h[i] != memsetval) {
testResult = false;
mismatchCount++;
xcc_fail_counts[this_xcc]++;
if (A_h[i] == (char)POISON_VALUE) {
mismatchPoisonCount++;
}
if (mismatchCount <= 20) { // print first 20 mismatches
std::cout << "Mismatch data : "
<< "numElements: " << numElements << " "
<< "offset: " << offset << " "
<< "i: " << i << " "
<< "A_h[i]: 0x" << std::hex << std::uppercase
<< static_cast<unsigned>(static_cast<unsigned char>(A_h[i]))
<< std::dec << std::nouppercase << " "
<< "memsetval: 0x" << std::hex << std::uppercase
<< static_cast<unsigned>(static_cast<unsigned char>(memsetval))
<< std::dec << std::nouppercase << " "
<< "smid: " << wid_h[ki] << " "
<< "xcc: " << this_xcc
<< (A_h[i] == (char)POISON_VALUE ? " [STILL POISON]" : " [CORRUPTED]")
<< std::endl;
}
}
}
// ---------------------------------------------------------------
// 7) Print XCC summary (always, even in passing case)
// ---------------------------------------------------------------
printf("\n--- XCC Summary ---\n");
printf("XCCs seen (count=%zu): {", xcc_set.size());
for (auto it = xcc_set.begin(); it != xcc_set.end(); ++it) {
if (it != xcc_set.begin()) printf(", ");
printf("%u", *it);
}
printf("}\n");
// Check XCC mask: XCD 0-3 means XCC values 0-3 (mask 0xF)
bool allOnOneAid = true;
for (unsigned xcc_val : xcc_set) {
if (xcc_val > 3) {
allOnOneAid = false;
break;
}
}
printf("All XCCs in XCD 0-3 (mask 0xF, one AID): %s\n", allOnOneAid ? "YES" : "NO");
printf("\nPer-XCC element counts:\n");
for (auto& [xcc_val, count] : xcc_counts) {
printf(" XCC %2u: %zu elements", xcc_val, count);
if (xcc_fail_counts.count(xcc_val)) {
printf(" *** %zu MISMATCHES ***", xcc_fail_counts[xcc_val]);
}
printf("\n");
}
if (mismatchCount > 0) {
printf("\nTotal mismatches: %zu / %zu (%.4f%%)\n",
mismatchCount, Nmemset, 100.0 * mismatchCount / Nmemset);
printf(" Of which still poison (0x%02X): %zu\n", POISON_VALUE, mismatchPoisonCount);
printf(" Of which corrupted (other value): %zu\n", mismatchCount - mismatchPoisonCount);
} else {
printf("\nResult: PASS (all %zu elements match 0x%02X)\n",
Nmemset, (unsigned char)memsetval);
}
printf("-------------------\n");
HIP_CHECK(hipFree(A_d));
HIP_CHECK(hipFree(wid_d));
HIP_CHECK(hipFree(xcc_d));
HIP_CHECK(hipFree(xcd_count_d));
free(A_h);
free(wid_h);
free(xcc_h);
return testResult;
}
static void printUsage(const char* prog) {
printf("Usage: %s [options]\n", prog);
printf("Options:\n");
printf(" --test-xcd <N> Run testhipMemsetXcd targeting XCD N\n");
printf(" -n <N> Number of elements (default: 4*1024*1024 = 4194304)\n");
printf("\nIf no options are given, compile-time #ifdef tests are run.\n");
}
int main(int argc, char* argv[]) {
bool ret;
printf("Running..\n");
size_t numElements = 4 * 1024 * 1024; // default
bool numElementsSet = false;
bool ran_cli_test = false;
int test_xcd_val = -1; // -1 means not set
// First pass: parse all options
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-n") {
if (i + 1 >= argc) {
printf("Error: -n requires a value (e.g. -n 4194304)\n");
printUsage(argv[0]);
return 1;
}
numElements = (size_t)strtol(argv[i + 1], nullptr, 10);
numElementsSet = true;
i++;
} else if (arg == "--test-xcd") {
if (i + 1 >= argc) {
printf("Error: --test-xcd requires a value (e.g. --test-xcd 0)\n");
printUsage(argv[0]);
return 1;
}
test_xcd_val = (int)strtol(argv[i + 1], nullptr, 10);
i++;
} else if (arg == "--help" || arg == "-h") {
printUsage(argv[0]);
return 0;
} else {
printf("Unknown option: %s\n", arg.c_str());
printUsage(argv[0]);
return 1;
}
}
// Run --test-xcd if requested
if (test_xcd_val >= 0) {
size_t xcd = (size_t)test_xcd_val;
printf("Running testhipMemsetXcd with xcd=%zu numElements=%zu\n", xcd, numElements);
ret = testhipMemsetXcd(0x42, numElements, xcd, 3); assert(ret);
ran_cli_test = true;
}
if (!ran_cli_test) {
#ifdef POISON_INIT_OFFSET_0
// Fails with offset = 0 consistently
size_t n0 = numElementsSet ? numElements : 256 * 1024 * 1024;
ret = testhipMemset(0xa6, n0); assert(ret);
#endif
#ifdef POISON_INIT_OFFSET_3
// Fails more reliably with offset = 3 with below combinations.
ret = testhipMemset(0x42, numElements, 3); assert(ret);
#endif
#ifdef POISON_INIT_OFFSET_3_XCD_0
ret = testhipMemsetXcd(0x42, numElements, 0, 3); assert(ret);
#endif
}
printf("\nAll tests PASSED.\n");
return 0;
}