Files
rocm-systems/tests/featuretests/profiler/apps/vector_add_mpi.cpp
T
gobhardw 13c12bc7e7 Making v2 tests CI ready
Change-Id: Ia32c9b2a1b5f833d1c7b078b678895b736d1e2a1
2023-04-11 03:56:46 +00:00

133 строки
5.0 KiB
C++
Исполняемый файл

/*
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.
*/
#include <mpi.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include "hip/hip_runtime.h"
#define HIP_RC(call) \
do { \
hipError_t err = call; \
if (hipSuccess != err) { \
printf("HIP ERROR (code = %d, %s) at %s:%d\n", err, \
hipGetErrorString(err), __FILE__, __LINE__); \
assert(0); \
exit(1); \
} \
} while (0)
#define HIP_KL(call) \
do { \
call; \
hipError_t err = hipGetLastError(); \
if (hipSuccess != err) { \
printf("HIP ERROR (code = %d, %s) at %s:%d\n", err, \
hipGetErrorString(err), __FILE__, __LINE__); \
assert(0); \
exit(1); \
} \
} while (0)
// CUDA kernel to add elements of two arrays
__global__ void add(int n, float *x, float *y) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < n; i += stride)
y[i] = x[i] + y[i];
}
int main(int argc, char *argv[]) {
int N = 1 << 20;
float *x = new float[N];
float *y = new float[N];
float *d_x;
float *d_y;
int myId;
int devId;
int numRank;
int deviceCount;
// init MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myId);
MPI_Comm_size(MPI_COMM_WORLD, &numRank);
hipGetDeviceCount(&deviceCount);
std::cout << "device count and rank is" << deviceCount << ": " << numRank
<< std::endl;
// set the device ID to the rank ID mod deviceCount (in this case 4 since
// there are 4 devices on a node)
devId = myId % deviceCount;
// set the device ID
hipSetDevice(devId);
printf("Rank Id: %d | Device Id : %d | Num Devices: %d\n", myId, devId,
deviceCount);
fflush(stdout);
// Allocate Unified Memory -- accessible from CPU or GPU
hipMallocManaged(&d_x, N * sizeof(float));
hipMallocManaged(&d_y, N * sizeof(float));
// initialize x and y arrays on the host
for (int i = 0; i < N; i++) {
x[i] = 1.0f;
y[i] = 2.0f;
}
HIP_RC(hipMemcpy(d_x, x, N * sizeof(float), hipMemcpyHostToDevice));
HIP_RC(hipMemcpy(d_y, y, N * sizeof(float), hipMemcpyHostToDevice));
// Launch kernel on 1M elements on the GPU
int blockSize = 256;
int numBlocks = (N + blockSize - 1) / blockSize;
HIP_KL(hipLaunchKernelGGL(add, numBlocks, blockSize, 0, 0, N, d_x, d_y));
// Wait for GPU to finish before accessing on host
HIP_RC(hipDeviceSynchronize());
HIP_RC(hipMemcpy(x, d_x, N * sizeof(float), hipMemcpyDeviceToHost));
HIP_RC(hipMemcpy(y, d_y, N * sizeof(float), hipMemcpyDeviceToHost));
// Check for errors (all values should be 3.0f)
float maxError = 0.0f;
for (int i = 0; i < N; i++)
maxError = fmax(maxError, fabs(y[i] - 3.0f));
printf("Max error: %f\n", maxError);
// Free memory
HIP_RC(hipFree(d_x));
HIP_RC(hipFree(d_y));
delete[] x;
delete[] y;
MPI_Finalize();
return 0;
}