Skip to content

Commit caa5463

Browse files
committed
Add CUDA to HIP translation
1 parent 61c9d09 commit caa5463

6 files changed

+94
-9
lines changed

lib/cuda2hip.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2023 Advanced Micro Devices, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef SIMULATOR_CUDA2HIP_H_
16+
#define SIMULATOR_CUDA2HIP_H_
17+
18+
#define cublasCaxpy hipblasCaxpy
19+
#define cublasCdotc hipblasCdotc
20+
#define cublasCreate hipblasCreate
21+
#define cublasCscal hipblasCscal
22+
#define cublasCsscal hipblasCsscal
23+
#define cublasDestroy hipblasDestroy
24+
#define cublasDznrm2 hipblasDznrm2
25+
#define cublasHandle_t hipblasHandle_t
26+
#define cublasScnrm2 hipblasScnrm2
27+
#define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
28+
#define cublasStatus_t hipblasStatus_t
29+
#define cublasZaxpy hipblasZaxpy
30+
#define cublasZdotc hipblasZdotc
31+
#define cublasZdscal hipblasZdscal
32+
#define cublasZscal hipblasZscal
33+
#define cuCimagf hipCimagf
34+
#define cuCimag hipCimag
35+
#define cuComplex hipComplex
36+
#define cuCrealf hipCrealf
37+
#define cuCreal hipCreal
38+
#define CUDA_C_32F HIPBLAS_C_32F
39+
#define CUDA_C_64F HIPBLAS_C_64F
40+
#define cudaDeviceSynchronize hipDeviceSynchronize
41+
#define cudaError_t hipError_t
42+
#define cudaFree hipFree
43+
#define cudaGetErrorString hipGetErrorString
44+
#define cudaMalloc hipMalloc
45+
#define cudaMemcpyAsync hipMemcpyAsync
46+
#define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
47+
#define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
48+
#define cudaMemcpy hipMemcpy
49+
#define cudaMemcpyHostToDevice hipMemcpyHostToDevice
50+
#define cudaMemset hipMemset
51+
#define cudaPeekAtLastError hipPeekAtLastError
52+
#define cudaSuccess hipSuccess
53+
#define cuDoubleComplex hipDoubleComplex
54+
55+
template <typename T>
56+
__device__ __forceinline__ T __shfl_down_sync(
57+
unsigned mask, T var, unsigned int delta, int width = warpSize) {
58+
return __shfl_down(var, delta, width);
59+
}
60+
61+
#endif // SIMULATOR_CUDA2HIP_H_

lib/simulator_cuda_kernels.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
#ifndef SIMULATOR_CUDA_KERNELS_H_
1616
#define SIMULATOR_CUDA_KERNELS_H_
1717

18-
#include <cuda.h>
19-
#include <cuda_runtime.h>
20-
21-
#include "util_cuda.h"
18+
#ifdef __NVCC__
19+
#include <cuda.h>
20+
#include <cuda_runtime.h>
21+
22+
#include "util_cuda.h"
23+
#elif __HIP__
24+
#include <hip/hip_runtime.h>
25+
#include "cuda2hip.h"
26+
#endif
2227

2328
namespace qsim {
2429

lib/statespace_cuda.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
#ifndef STATESPACE_CUDA_H_
1616
#define STATESPACE_CUDA_H_
1717

18-
#include <cuda.h>
18+
#ifdef __NVCC__
19+
#include <cuda.h>
20+
#elif __HIP__
21+
#include <hip/hip_runtime.h>
22+
#include "cuda2hip.h"
23+
#endif
1924

2025
#include <algorithm>
2126
#include <complex>

lib/statespace_cuda_kernels.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
#ifndef STATESPACE_CUDA_KERNELS_H_
1616
#define STATESPACE_CUDA_KERNELS_H_
1717

18-
#include <cuda.h>
18+
#ifdef __NVCC__
19+
#include <cuda.h>
20+
#elif __HIP__
21+
#include <hip/hip_runtime.h>
22+
#include "cuda2hip.h"
23+
#endif
1924

2025
#include "util_cuda.h"
2126

lib/util_cuda.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#ifndef UTIL_CUDA_H_
1616
#define UTIL_CUDA_H_
1717

18-
#include <cuda.h>
18+
#ifdef __NVCC__
19+
#include <cuda.h>
20+
#elif __HIP__
21+
#include <hip/hip_runtime.h>
22+
#endif
1923

2024
#include <cstdlib>
2125

lib/vectorspace_cuda.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
#ifndef VECTORSPACE_CUDA_H_
1616
#define VECTORSPACE_CUDA_H_
1717

18-
#include <cuda.h>
19-
#include <cuda_runtime.h>
18+
#ifdef __NVCC__
19+
#include <cuda.h>
20+
#include <cuda_runtime.h>
21+
#elif __HIP__
22+
#include <hip/hip_runtime.h>
23+
#include "cuda2hip.h"
24+
#endif
2025

2126
#include <memory>
2227
#include <utility>

0 commit comments

Comments
 (0)