2016-10-15 23:05:04 +05:30
|
|
|
/*
|
2019-05-20 18:46:23 +03:00
|
|
|
Copyright (c) 2015-present Advanced Micro Devices, Inc. All rights reserved.
|
2016-10-15 23:05:04 +05:30
|
|
|
|
|
|
|
|
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-10-04 22:21:10 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
2016-04-13 17:32:38 -05:00
|
|
|
|
2018-10-17 12:01:44 +05:30
|
|
|
__global__ void vadd_hip(const float* a, const float* b, float* c, int N) {
|
2016-04-13 17:32:38 -05:00
|
|
|
int idx = (hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x);
|
|
|
|
|
|
|
|
|
|
if (idx < N) {
|
|
|
|
|
c[idx] = a[idx] + b[idx];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
int main(int argc, char* argv[]) {
|
2016-04-13 17:32:38 -05:00
|
|
|
int sizeElements = 1000000;
|
|
|
|
|
size_t sizeBytes = sizeElements * sizeof(float);
|
2016-05-03 14:32:59 +05:30
|
|
|
bool pass = true;
|
2016-04-13 17:32:38 -05:00
|
|
|
|
|
|
|
|
// Allocate host memory
|
2018-03-12 11:29:03 +05:30
|
|
|
float* A_h = (float*)malloc(sizeBytes);
|
|
|
|
|
float* B_h = (float*)malloc(sizeBytes);
|
|
|
|
|
float* C_h = (float*)malloc(sizeBytes);
|
2016-04-13 17:32:38 -05:00
|
|
|
|
|
|
|
|
// Allocate device memory:
|
|
|
|
|
float *A_d, *B_d, *C_d;
|
|
|
|
|
hipMalloc(&A_d, sizeBytes);
|
|
|
|
|
hipMalloc(&B_d, sizeBytes);
|
|
|
|
|
hipMalloc(&C_d, sizeBytes);
|
|
|
|
|
|
2016-09-01 14:00:46 -05:00
|
|
|
// Initialize host memory
|
2018-03-12 11:29:03 +05:30
|
|
|
for (int i = 0; i < sizeElements; i++) {
|
|
|
|
|
A_h[i] = 1.618f * i;
|
2016-04-13 17:32:38 -05:00
|
|
|
B_h[i] = 3.142f * i;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 14:00:46 -05:00
|
|
|
// H2D Copy
|
2016-04-13 17:32:38 -05:00
|
|
|
hipMemcpy(A_d, A_h, sizeBytes, hipMemcpyHostToDevice);
|
|
|
|
|
hipMemcpy(B_d, B_h, sizeBytes, hipMemcpyHostToDevice);
|
|
|
|
|
|
2016-09-01 14:00:46 -05:00
|
|
|
// Launch kernel onto default accelerator
|
2018-03-12 11:29:03 +05:30
|
|
|
int blockSize = 256; // pick arbitrary block size
|
|
|
|
|
int blocks = (sizeElements + blockSize - 1) / blockSize; // round up to launch enough blocks
|
2018-10-17 12:01:44 +05:30
|
|
|
hipLaunchKernelGGL(vadd_hip, dim3(blocks), dim3(blockSize), 0, 0, A_d, B_d, C_d, sizeElements);
|
2016-04-13 17:32:38 -05:00
|
|
|
|
2016-09-01 14:00:46 -05:00
|
|
|
// D2H Copy
|
2016-04-13 17:32:38 -05:00
|
|
|
hipMemcpy(C_h, C_d, sizeBytes, hipMemcpyDeviceToHost);
|
|
|
|
|
|
2016-09-01 14:00:46 -05:00
|
|
|
// Verify
|
2018-03-12 11:29:03 +05:30
|
|
|
for (int i = 0; i < sizeElements; i++) {
|
|
|
|
|
float ref = 1.618f * i + 3.142f * i;
|
2016-04-13 17:32:38 -05:00
|
|
|
if (C_h[i] != ref) {
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("error:%d computed=%6.2f, reference=%6.2f\n", i, C_h[i], ref);
|
2016-05-03 14:32:59 +05:30
|
|
|
pass = false;
|
2016-04-13 17:32:38 -05:00
|
|
|
}
|
|
|
|
|
};
|
2018-03-12 11:29:03 +05:30
|
|
|
if (pass) printf("PASSED!\n");
|
2016-04-13 17:32:38 -05:00
|
|
|
}
|