2016-03-02 08:53:10 -06:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015-2016 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-03-01 19:33:47 -06:00
|
|
|
#include<iostream>
|
|
|
|
|
#include"test_common.h"
|
|
|
|
|
#include<thread>
|
|
|
|
|
#define N 1000
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
__global__ void Inc(hipLaunchParm lp, T *Array){
|
|
|
|
|
int tx = hipThreadIdx_x + hipBlockIdx_x * hipBlockDim_x;
|
|
|
|
|
Array[tx] = Array[tx] + T(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void run1(size_t size, hipStream_t stream){
|
|
|
|
|
float *Ah, *Bh, *Cd, *Dd, *Eh;
|
|
|
|
|
|
2016-03-22 02:30:10 -05:00
|
|
|
HIPCHECK(hipHostMalloc((void**)&Ah, size, hipHostMallocDefault));
|
|
|
|
|
HIPCHECK(hipHostMalloc((void**)&Bh, size, hipHostMallocDefault));
|
2016-03-15 06:30:16 -05:00
|
|
|
HIPCHECK(hipMalloc(&Cd, size));
|
|
|
|
|
HIPCHECK(hipMalloc(&Dd, size));
|
2016-03-22 02:30:10 -05:00
|
|
|
HIPCHECK(hipHostMalloc((void**)&Eh, size, hipHostMallocDefault));
|
2016-03-01 19:33:47 -06:00
|
|
|
|
|
|
|
|
for(int i=0;i<N;i++){
|
|
|
|
|
Ah[i] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 06:30:16 -05:00
|
|
|
HIPCHECK(hipMemcpyAsync(Bh, Ah, size, hipMemcpyHostToHost, stream));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Cd, Bh, size, hipMemcpyHostToDevice, stream));
|
2016-03-01 19:33:47 -06:00
|
|
|
hipLaunchKernel(HIP_KERNEL_NAME(Inc), dim3(N/500), dim3(500), 0, stream, Cd);
|
2016-03-15 06:30:16 -05:00
|
|
|
HIPCHECK(hipMemcpyAsync(Dd, Cd, size, hipMemcpyDeviceToDevice, stream));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Eh, Dd, size, hipMemcpyDeviceToHost, stream));
|
2016-03-01 19:33:47 -06:00
|
|
|
HIPCHECK(hipDeviceSynchronize());
|
|
|
|
|
HIPASSERT(Eh[10] == Ah[10] + 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void run(size_t size, hipStream_t stream1, hipStream_t stream2){
|
|
|
|
|
float *Ah, *Bh, *Cd, *Dd, *Eh;
|
|
|
|
|
float *Ahh, *Bhh, *Cdd, *Ddd, *Ehh;
|
|
|
|
|
|
2016-03-22 02:30:10 -05:00
|
|
|
HIPCHECK(hipHostMalloc((void**)&Ah, size, hipHostMallocDefault));
|
|
|
|
|
HIPCHECK(hipHostMalloc((void**)&Bh, size, hipHostMallocDefault));
|
2016-03-15 06:30:16 -05:00
|
|
|
HIPCHECK(hipMalloc(&Cd, size));
|
|
|
|
|
HIPCHECK(hipMalloc(&Dd, size));
|
2016-03-22 02:30:10 -05:00
|
|
|
HIPCHECK(hipHostMalloc((void**)&Eh, size, hipHostMallocDefault));
|
|
|
|
|
HIPCHECK(hipHostMalloc((void**)&Ahh, size, hipHostMallocDefault));
|
|
|
|
|
HIPCHECK(hipHostMalloc((void**)&Bhh, size, hipHostMallocDefault));
|
2016-03-15 06:30:16 -05:00
|
|
|
HIPCHECK(hipMalloc(&Cdd, size));
|
|
|
|
|
HIPCHECK(hipMalloc(&Ddd, size));
|
2016-03-22 02:30:10 -05:00
|
|
|
HIPCHECK(hipHostMalloc((void**)&Ehh, size, hipHostMallocDefault));
|
2016-03-15 06:30:16 -05:00
|
|
|
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Bh, Ah, size, hipMemcpyHostToHost, stream1));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Bhh, Ahh, size, hipMemcpyHostToHost, stream2));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Cd, Bh, size, hipMemcpyHostToDevice, stream1));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Cdd, Bhh, size, hipMemcpyHostToDevice, stream2));
|
2016-03-01 19:33:47 -06:00
|
|
|
hipLaunchKernel(HIP_KERNEL_NAME(Inc), dim3(N/500), dim3(500), 0, stream1, Cd);
|
|
|
|
|
hipLaunchKernel(HIP_KERNEL_NAME(Inc), dim3(N/500), dim3(500), 0, stream2, Cdd);
|
2016-03-15 06:30:16 -05:00
|
|
|
HIPCHECK(hipMemcpyAsync(Dd, Cd, size, hipMemcpyDeviceToDevice, stream1));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Ddd, Cdd, size, hipMemcpyDeviceToDevice, stream2));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Eh, Dd, size, hipMemcpyDeviceToHost, stream1));
|
|
|
|
|
HIPCHECK(hipMemcpyAsync(Ehh, Ddd, size, hipMemcpyDeviceToHost, stream2));
|
2016-03-01 19:33:47 -06:00
|
|
|
HIPCHECK(hipDeviceSynchronize());
|
|
|
|
|
HIPASSERT(Eh[10] = Ah[10] + 1.0f);
|
|
|
|
|
HIPASSERT(Ehh[10] = Ahh[10] + 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv){
|
|
|
|
|
HipTest::parseStandardArguments(argc, argv, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hipStream_t stream[3];
|
|
|
|
|
for(int i=0;i<3;i++){
|
|
|
|
|
HIPCHECK(hipStreamCreate(&stream[i]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t size = N * sizeof(float);
|
|
|
|
|
|
|
|
|
|
std::thread t1(run1, size, stream[0]);
|
|
|
|
|
std::thread t2(run1, size, stream[0]);
|
|
|
|
|
std::thread t3(run, size, stream[1], stream[2]);
|
|
|
|
|
t1.join();
|
|
|
|
|
// std::cout<<"T1"<<std::endl;
|
|
|
|
|
t2.join();
|
|
|
|
|
// std::cout<<"T2"<<std::endl;
|
|
|
|
|
t3.join();
|
|
|
|
|
passed();
|
|
|
|
|
}
|
|
|
|
|
|