2016-03-08 12:57:22 -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-09-23 16:15:31 +05:30
|
|
|
/* HIT_START
|
2017-02-03 10:53:36 -06:00
|
|
|
* BUILD: %t %s ../../test_common.cpp
|
2016-09-23 16:15:31 +05:30
|
|
|
* RUN: %t
|
|
|
|
|
* HIT_END
|
|
|
|
|
*/
|
|
|
|
|
|
2016-03-07 03:42:50 -06:00
|
|
|
#include"test_common.h"
|
2016-03-08 12:57:22 -06:00
|
|
|
#include<malloc.h>
|
2016-03-07 03:42:50 -06:00
|
|
|
|
|
|
|
|
__global__ void Inc(hipLaunchParm lp, float *Ad){
|
|
|
|
|
int tx = hipThreadIdx_x + hipBlockIdx_x * hipBlockDim_x;
|
|
|
|
|
Ad[tx] = Ad[tx] + float(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(){
|
2016-04-15 10:08:10 -05:00
|
|
|
float *A, **Ad;
|
|
|
|
|
int num_devices;
|
|
|
|
|
HIPCHECK(hipGetDeviceCount(&num_devices));
|
|
|
|
|
Ad = new float*[num_devices];
|
2016-03-07 03:42:50 -06:00
|
|
|
const size_t size = N * sizeof(float);
|
2016-04-15 10:08:10 -05:00
|
|
|
A = (float*)malloc(size);
|
2016-03-07 03:42:50 -06:00
|
|
|
HIPCHECK(hipHostRegister(A, size, 0));
|
2017-02-17 11:53:38 -06:00
|
|
|
|
|
|
|
|
|
2016-03-07 03:42:50 -06:00
|
|
|
for(int i=0;i<N;i++){
|
|
|
|
|
A[i] = float(1);
|
|
|
|
|
}
|
2017-02-17 11:53:38 -06:00
|
|
|
|
|
|
|
|
|
2016-04-15 10:08:10 -05:00
|
|
|
for(int i=0;i<num_devices;i++){
|
2017-02-17 11:53:38 -06:00
|
|
|
HIPCHECK(hipSetDevice(i));
|
|
|
|
|
HIPCHECK(hipHostGetDevicePointer((void**)&Ad[i], A, 0));
|
2016-04-15 10:08:10 -05:00
|
|
|
}
|
2016-03-07 03:42:50 -06:00
|
|
|
|
2017-02-17 15:43:09 -06:00
|
|
|
// Use device pointer inside a kernel:
|
2016-04-15 10:08:10 -05:00
|
|
|
for(int i=0;i<num_devices;i++){
|
2017-02-17 11:53:38 -06:00
|
|
|
HIPCHECK(hipSetDevice(i));
|
|
|
|
|
hipLaunchKernel(HIP_KERNEL_NAME(Inc), dim3(N/512), dim3(512), 0, 0, Ad[i]);
|
2016-03-07 03:42:50 -06:00
|
|
|
|
2017-02-17 11:53:38 -06:00
|
|
|
HIPCHECK(hipDeviceSynchronize());
|
2016-04-15 10:08:10 -05:00
|
|
|
}
|
2017-02-17 11:53:38 -06:00
|
|
|
|
2017-02-17 15:43:09 -06:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Senstizes HIP bug if device does not match where the memory was registered.
|
|
|
|
|
HIPCHECK(hipSetDevice(0));
|
|
|
|
|
|
|
|
|
|
// Copy to B, this should be optimal pinned malloc copy:
|
|
|
|
|
// Note we are using the host pointer here:
|
|
|
|
|
float *Bh, *Bd;
|
|
|
|
|
Bh = (float*)malloc(size);
|
|
|
|
|
HIPCHECK(hipMalloc(&Bd, size));
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<N;i++){
|
|
|
|
|
A[i] = float(i);
|
|
|
|
|
Bh[i] = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HIPCHECK(hipMemcpy(Bd, A, size, hipMemcpyHostToDevice));
|
|
|
|
|
|
|
|
|
|
HIPCHECK(hipMemcpy(Bh, Bd, size, hipMemcpyDeviceToHost));
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
//TODO - disable check until we update HCC to deal with registered memory pointers.
|
|
|
|
|
for(int i=0;i<N;i++){
|
|
|
|
|
if (Bh[i] != A[i]) {
|
|
|
|
|
printf ("mismatch at Bh[%d]=%f, A[%d]=%f\n", i, Bh[i], i, A[i]);
|
|
|
|
|
failed("mismatch");
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure the copy worked
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-15 10:08:10 -05:00
|
|
|
HIPASSERT(A[10] == 1.0f + float(num_devices));
|
2016-03-07 03:42:50 -06:00
|
|
|
HIPCHECK(hipHostUnregister(A));
|
|
|
|
|
passed();
|
|
|
|
|
}
|