2021-03-29 08:12:07 -07:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015-2021 Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* HIT_START
|
|
|
|
|
* BUILD: %t %s ../../test_common.cpp EXCLUDE_HIP_PLATFORM nvidia
|
2021-05-17 00:38:50 -07:00
|
|
|
* TEST: %t EXCLUDE_HIP_PLATFORM amd
|
2021-03-29 08:12:07 -07:00
|
|
|
* HIT_END
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "test_common.h"
|
|
|
|
|
#include <thread>
|
2021-05-17 00:38:50 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#define THREADS 8
|
|
|
|
|
#define MAX_NUM_THREADS 512
|
|
|
|
|
#define ITER 100
|
2021-03-29 08:12:07 -07:00
|
|
|
|
|
|
|
|
extern "C" __global__ void WaitKernel(int *Ad, int clockrate) {
|
|
|
|
|
uint64_t wait_t = 500,
|
|
|
|
|
start = clock64()/clockrate, cycles;
|
|
|
|
|
do { cycles = clock64()/clockrate-start;} while (cycles < wait_t);
|
|
|
|
|
*Ad = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:38:50 -07:00
|
|
|
void t1(hipEvent_t start, hipStream_t stream1, int clkRate, int *A, int *Ad) {
|
2021-03-29 08:12:07 -07:00
|
|
|
*A = 0;
|
|
|
|
|
|
|
|
|
|
hipLaunchKernelGGL(HIP_KERNEL_NAME(WaitKernel), dim3(1), dim3(1), 0, stream1, Ad, clkRate);
|
|
|
|
|
|
|
|
|
|
HIPCHECK(hipEventRecord(start, stream1));
|
2021-05-17 00:38:50 -07:00
|
|
|
|
2021-03-29 08:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2021-05-17 00:38:50 -07:00
|
|
|
|
|
|
|
|
int NUM_THREADS = min(THREADS * std::thread::hardware_concurrency(), MAX_NUM_THREADS);
|
2021-03-29 08:12:07 -07:00
|
|
|
int clkRate = 0;
|
2021-05-17 00:38:50 -07:00
|
|
|
std::vector<int *> A, Ad;
|
|
|
|
|
bool TestPassed = true;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_THREADS; i++) {
|
|
|
|
|
int *aPtr, *adPtr;
|
|
|
|
|
aPtr = (int *)malloc(sizeof(int));
|
|
|
|
|
A.push_back(aPtr);
|
|
|
|
|
Ad.push_back(adPtr);
|
|
|
|
|
HIPCHECK(hipHostRegister(A[i], sizeof(int), 0));
|
|
|
|
|
HIPCHECK(hipHostGetDevicePointer((void**)&Ad[i], A[i], 0));
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 08:12:07 -07:00
|
|
|
HIPCHECK(hipDeviceGetAttribute(&clkRate, hipDeviceAttributeClockRate, 0));
|
|
|
|
|
hipStream_t stream1;
|
|
|
|
|
hipStreamCreate(&stream1);
|
|
|
|
|
hipEvent_t start;
|
|
|
|
|
hipEventCreate(&start);
|
2021-05-17 00:38:50 -07:00
|
|
|
std::thread t[NUM_THREADS];
|
2021-03-29 08:12:07 -07:00
|
|
|
|
2021-05-17 00:38:50 -07:00
|
|
|
for (int i = 0; i < ITER; i++) {
|
|
|
|
|
for (int j = 0; j < NUM_THREADS; j++) {
|
|
|
|
|
t[j] = std::thread(t1, start, stream1, clkRate, A[j], Ad[j]);
|
|
|
|
|
}
|
2021-03-29 08:12:07 -07:00
|
|
|
|
2021-05-17 00:38:50 -07:00
|
|
|
for (int j = 0 ; j < NUM_THREADS; j++) {
|
|
|
|
|
t[j].join();
|
|
|
|
|
}
|
2021-03-29 08:12:07 -07:00
|
|
|
|
|
|
|
|
HIPCHECK(hipStreamWaitEvent(stream1, start, 0));
|
|
|
|
|
hipError_t err = hipEventQuery(start);
|
|
|
|
|
while(err != hipSuccess) {
|
|
|
|
|
err = hipEventQuery(start);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:38:50 -07:00
|
|
|
for (int j = 0; j < NUM_THREADS; j++) {
|
|
|
|
|
if (*A[j] != 1) {
|
|
|
|
|
TestPassed = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-03-29 08:12:07 -07:00
|
|
|
}
|
2021-05-17 00:38:50 -07:00
|
|
|
|
|
|
|
|
if (!TestPassed) {
|
|
|
|
|
failed("Test Failed due to possible race condition!");
|
2021-03-29 08:12:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-17 00:38:50 -07:00
|
|
|
|
|
|
|
|
HIPCHECK(hipStreamDestroy(stream1));
|
|
|
|
|
HIPCHECK(hipEventDestroy(start));
|
|
|
|
|
|
|
|
|
|
for (auto ptr: A) {
|
|
|
|
|
free(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
A.clear();
|
|
|
|
|
Ad.clear();
|
|
|
|
|
|
2021-03-29 08:12:07 -07:00
|
|
|
passed();
|
|
|
|
|
}
|