RasterProcessTool/GPUBaseLib/GPUTool/GPUTool.cu

685 lines
19 KiB
Plaintext
Raw Normal View History

#include <iostream>
#include <memory>
#include <cmath>
#include <complex>
#include <device_launch_parameters.h>
#include <cuda_runtime.h>
2025-02-25 05:18:19 +00:00
#include <cufft.h>
#include <cufftw.h>
#include <cufftXt.h>
#include <cublas_v2.h>
#include <cuComplex.h>
#include <chrono>
#include "BaseConstVariable.h"
#include "GPUTool.cuh"
2025-02-19 04:29:15 +00:00
#include "PrintMsgToQDebug.h"
#ifdef __CUDANVCC___
#define BLOCK_DIM 1024
#define REDUCE_SCALE 4
2025-02-25 05:18:19 +00:00
// CUDA<44>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
__global__ void scaleKernel(cuComplex* data, int size, float scale) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < size) {
data[idx].x *= scale;
data[idx].y *= scale;
}
}
2025-01-20 07:49:54 +00:00
// <20><>ӡGPU<50><55><EFBFBD><EFBFBD>
void printDeviceInfo(int deviceId) {
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, deviceId);
2025-02-25 05:18:19 +00:00
2025-01-20 07:49:54 +00:00
std::cout << "Device " << deviceId << ": " << deviceProp.name << std::endl;
std::cout << " Compute Capability: " << deviceProp.major << "." << deviceProp.minor << std::endl;
std::cout << " Total Global Memory: " << deviceProp.totalGlobalMem / (1024 * 1024) << " MB" << std::endl;
std::cout << " Shared Memory per Block: " << deviceProp.sharedMemPerBlock << " Bytes" << std::endl;
std::cout << " Registers per Block: " << deviceProp.regsPerBlock << std::endl;
std::cout << " Warp Size: " << deviceProp.warpSize << std::endl;
std::cout << " Max Threads per Block: " << deviceProp.maxThreadsPerBlock << std::endl;
std::cout << " Max Threads Dim: (" << deviceProp.maxThreadsDim[0] << ", "
<< deviceProp.maxThreadsDim[1] << ", " << deviceProp.maxThreadsDim[2] << ")" << std::endl;
std::cout << " Max Grid Size: (" << deviceProp.maxGridSize[0] << ", "
<< deviceProp.maxGridSize[1] << ", " << deviceProp.maxGridSize[2] << ")" << std::endl;
2025-02-25 05:18:19 +00:00
std::cout << " Multiprocessor Count: " << deviceProp.multiProcessorCount << std::endl;
std::cout << " Clock Rate: " << deviceProp.clockRate / 1000 << " MHz" << std::endl;
std::cout << " Memory Clock Rate: " << deviceProp.memoryClockRate / 1000 << " MHz" << std::endl;
std::cout << " Memory Bus Width: " << deviceProp.memoryBusWidth << " bits" << std::endl;
std::cout << " L2 Cache Size: " << deviceProp.l2CacheSize / 1024 << " KB" << std::endl;
std::cout << " Max Texture Dimensions: (" << deviceProp.maxTexture1D << ", "
2025-01-20 07:49:54 +00:00
<< deviceProp.maxTexture2D[0] << "x" << deviceProp.maxTexture2D[1] << ", "
<< deviceProp.maxTexture3D[0] << "x" << deviceProp.maxTexture3D[1] << "x" << deviceProp.maxTexture3D[2] << ")" << std::endl;
std::cout << " Unified Addressing: " << (deviceProp.unifiedAddressing ? "Yes" : "No") << std::endl;
std::cout << " Concurrent Kernels: " << (deviceProp.concurrentKernels ? "Yes" : "No") << std::endl;
std::cout << " ECC Enabled: " << (deviceProp.ECCEnabled ? "Yes" : "No") << std::endl;
std::cout << " PCI Bus ID: " << deviceProp.pciBusID << std::endl;
std::cout << " PCI Device ID: " << deviceProp.pciDeviceID << std::endl;
std::cout << " PCI Domain ID: " << deviceProp.pciDomainID << std::endl;
std::cout << std::endl;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2025-01-02 17:05:04 +00:00
__device__ cuComplex cuCexpf(cuComplex d)
{
2025-01-02 17:05:04 +00:00
float factor = exp(d.x);
return make_cuComplex(factor * cos(d.y), factor * sin(d.y));
}
__device__ CUDAVector GPU_VectorAB(CUDAVector A, CUDAVector B) {
CUDAVector C;
C.x = B.x - A.x;
C.y = B.y - A.y;
C.z = B.z - A.z;
return C;
}
__device__ float GPU_VectorNorm2(CUDAVector A) {
return sqrtf(A.x * A.x + A.y * A.y + A.z * A.z);
}
__device__ float GPU_dotVector(CUDAVector A, CUDAVector B) {
return A.x * B.x + A.y * B.y + A.z * B.z;
}
__device__ float GPU_CosAngle_VectorA_VectorB(CUDAVector A, CUDAVector B) {
return GPU_dotVector(A, B) / (GPU_VectorNorm2(A) * GPU_VectorNorm2(B));
}
2025-01-20 07:49:54 +00:00
extern __global__ void CUDAKernel_MemsetBlock(cuComplex* data, cuComplex init0, long len) {
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
data[idx] = init0;
}
}
extern __global__ void CUDAKernel_MemsetBlock(float* data, float init0, long len) {
2025-01-02 13:25:16 +00:00
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
data[idx] = init0;
}
}
2025-01-15 03:35:48 +00:00
__global__ void CUDACkernel_SUM_reduce_dynamicshared(float* d_x, float* d_y, long N)
{
const int tid = threadIdx.x; // ij<><C4B3>block<63>ڵ<EFBFBD><DAB5>̱߳<DFB3><CCB1><EFBFBD> index
const int bid = blockIdx.x; // ij<><C4B3>block<63><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD>grid<69>ڵı<DAB5><C4B1><EFBFBD> index
const int n = bid * blockDim.x + tid; // n <20><>ij<EFBFBD><C4B3><EFBFBD>̵߳ı<CCB5><C4B1><EFBFBD> index
__shared__ float s_y[128]; // <20><><EFBFBD><EFBFBD><E4B9B2><EFBFBD>ڴ<EFBFBD><DAB4>ռ<D5BC><E4A3AC>ͬ<EFBFBD><CDAC>block<63><6B><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>
s_y[tid] = (n < N) ? d_x[n] : 0.0; // ÿ<><C3BF>block<63>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>d_x<5F><78><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
__syncthreads(); // <20>߳̿<DFB3><CCBF>ڲ<EFBFBD>ֱ<EFBFBD><D6B1>ͬ<EFBFBD><CDAC>
for (int offset = blockDim.x >> 1; offset > 0; offset >>= 1) // <20>۰<EFBFBD>
{
if (tid < offset) // <20>̱߳<DFB3><CCB1>ŵ<EFBFBD>index <20><>Խ<EFBFBD><D4BD> <20>۰<EFBFBD>
{
s_y[tid] += s_y[tid + offset]; // ij<><C4B3>block<63>ڵ<EFBFBD><DAB5>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>۰<EFBFBD><DBB0><EFBFBD>Լ
}
__syncthreads(); // ͬ<><CDAC>block<63>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD>߳<EFBFBD>
}
if (tid == 0) // ij<><C4B3>blockֻ<6B><D6BB>һ<EFBFBD>β<EFBFBD><CEB2><EFBFBD>
{
d_y[bid] = s_y[0]; // <20><><EFBFBD>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۼӵĽ<D3B5><C4BD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>ڴ<EFBFBD>
}
}
__global__ void CUDA_DistanceAB(float* Ax, float* Ay, float* Az, float* Bx, float* By, float* Bz, float* R, long len) {
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
R[idx] = sqrtf(powf(Ax[idx] - Bx[idx], 2) + powf(Ay[idx] - By[idx], 2) + powf(Az[idx] - Bz[idx], 2));
}
}
__global__ void CUDA_B_DistanceA(float* Ax, float* Ay, float* Az, float Bx, float By, float Bz, float* R, long len) {
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
R[idx] = sqrtf(powf(Ax[idx] - Bx, 2) + powf(Ay[idx] - By, 2) + powf(Az[idx] - Bz, 2));
}
}
__global__ void CUDA_make_VectorA_B(float sX, float sY, float sZ, float* tX, float* tY, float* tZ, float* RstX, float* RstY, float* RstZ, long len) {
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
RstX[idx] = sX - tX[idx]; // <20><><EFBFBD><EFBFBD>-><3E><>
RstY[idx] = sY - tY[idx];
RstZ[idx] = sZ - tZ[idx];
}
}
__global__ void CUDA_Norm_Vector(float* Vx, float* Vy, float* Vz, float* R, long len) {
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
R[idx] = sqrtf(powf(Vx[idx], 2) + powf(Vy[idx], 2) + powf(Vz[idx], 2));
}
}
__global__ void CUDA_cosAngle_VA_AB(float* Ax, float* Ay, float* Az, float* Bx, float* By, float* Bz, float* anglecos, long len) {
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < len) {
float tAx = Ax[idx];
float tAy = Ay[idx];
float tAz = Az[idx];
float tBx = Bx[idx];
float tBy = By[idx];
float tBz = Bz[idx];
float AR = sqrtf(powf(tAx, 2) + powf(tAy, 2) + powf(tAz, 2));
float BR = sqrtf(powf(tBx, 2) + powf(tBy, 2) + powf(tBz, 2));
float dotAB = tAx * tBx + tAy * tBy + tAz * tBz;
float result = acosf(dotAB / (AR * BR));
anglecos[idx] = result;
}
}
__global__ void CUDA_GridPoint_Linear_Interp1(float* v, float* q, float* qv, long xlen, long qlen)
{
long idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < qlen) {
float qx = q[idx];
// <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
if (qx < 0 || qx > xlen - 1) {}
else {
long x1 = floor(qx);
long x2 = ceil(qx);
if (x1 >= 0 && x2 < xlen) {
float y1 = v[x1];
float y2 = v[x2];
float y = y1 + (y2 - y1) * (qx - x1) / (x2 - x1);
qv[idx] = y;
}
else {
}
}
}
}
2025-03-01 12:36:59 +00:00
// һάFFTShift<66>˺<EFBFBD><CBBA><EFBFBD>
__global__ void fftshift_1d_kernel(cuComplex* data, int batch_size, int signal_length) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= batch_size * signal_length) return;
int batch_id = idx / signal_length;
int signal_id = idx % signal_length;
int half = (signal_length + 1) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ż<EFBFBD><C5BC><EFBFBD><EFBFBD>
if (signal_id >= half) return;
int new_pos = (signal_id + half) % signal_length;
int src_idx = batch_id * signal_length + new_pos;
// <20><><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
cuComplex temp = data[idx];
data[idx] = data[src_idx];
data[src_idx] = temp;
}
// <20><><EFBFBD><EFBFBD>һάFFTShift<66><74><EFBFBD><EFBFBD>
extern "C" void FFTShift1D(cuComplex* d_data, int batch_size, int signal_length) {
if (signal_length <= 1) return; // <20><><EFBFBD><EFBFBD><E8B4A6>
// <20><><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><CBBA><EFBFBD>
int total_elements = batch_size * signal_length;
int threads_per_block = 256;
int blocks_per_grid = (total_elements + threads_per_block - 1) / threads_per_block;
fftshift_1d_kernel << <blocks_per_grid, threads_per_block >> > (d_data, batch_size, signal_length);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
PrintLasterError("FFTShift1D");
cudaDeviceSynchronize();
}
2025-03-03 16:52:41 +00:00
extern "C" void shared_complexPtrToHostCuComplex(std::complex<double>* src, cuComplex* dst, long len)
2025-03-03 09:50:28 +00:00
{
for (long i = 0; i < len; i++) {
dst[i] = make_cuComplex(src[i].real(), src[i].imag());
}
return ;
}
2025-03-03 16:52:41 +00:00
extern "C" void HostCuComplexToshared_complexPtr( cuComplex* src, std::complex<double>* dst, long len)
{
double maxvalue = src[0].x;
for (long i = 0; i < len; i++) {
dst[i] = std::complex<double>(src[i].x, src[i].y);
if (maxvalue < src[i].x) {
maxvalue = src[i].x;
}
if (maxvalue < src[i].y) {
maxvalue = src[i].y;
}
}
printf("max value %e\n", maxvalue);
return;
}
2025-01-15 03:35:48 +00:00
extern __global__ void CUDA_D_sin(double* y, double* X, int n) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
y[idx] = sin(X[idx]);
}
}
extern __global__ void CUDA_D_cos(double* y, double* X, int n) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
y[idx] = cos(X[idx]);
}
}
2025-01-02 13:25:16 +00:00
extern "C" void CUDA_MemsetBlock(cuComplex* data, cuComplex init0, long len) {
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (len + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
CUDAKernel_MemsetBlock << <numBlocks, blockSize >> > (data, init0, len);
2025-01-02 13:25:16 +00:00
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDAmake_VectorA_B CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
2025-01-02 13:25:16 +00:00
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
extern "C" void checkCudaError(cudaError_t err, const char* msg) {
if (err != cudaSuccess) {
std::cerr << "CUDA error: " << msg << " (" << cudaGetErrorString(err) << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>
extern "C" void* mallocCUDAHost(long memsize) {
void* ptr;
cudaMallocHost(&ptr, memsize);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("mallocCUDAHost CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
return ptr;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>ͷ<EFBFBD>
extern "C" void FreeCUDAHost(void* ptr) {
2025-03-03 16:52:41 +00:00
if (nullptr == ptr||NULL==ptr) {
return;
}
cudaFreeHost(ptr);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("FreeCUDAHost CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
2025-03-03 16:52:41 +00:00
ptr = nullptr;
}
// GPU<50><55><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>
extern "C" void* mallocCUDADevice(long memsize) {
void* ptr;
cudaMalloc(&ptr, memsize);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("mallocCUDADevice CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
return ptr;
}
// GPU<50><55><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>ͷ<EFBFBD>
extern "C" void FreeCUDADevice(void* ptr) {
2025-03-03 16:52:41 +00:00
if (nullptr == ptr || NULL == ptr) {
return;
}
cudaFree(ptr);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("FreeCUDADevice CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
2025-03-03 16:52:41 +00:00
ptr = nullptr;
}
// GPU <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
extern "C" void HostToDevice(void* hostptr, void* deviceptr, long memsize) {
cudaMemcpy(deviceptr, hostptr, memsize, cudaMemcpyHostToDevice);
2025-01-15 03:35:48 +00:00
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("HostToDevice CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void DeviceToHost(void* hostptr, void* deviceptr, long memsize) {
cudaMemcpy(hostptr, deviceptr, memsize, cudaMemcpyDeviceToHost);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("DeviceToHost CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
2025-01-15 03:35:48 +00:00
void DeviceToDevice(void* s_deviceptr, void* t_deviceptr, long memsize)
{
cudaMemcpy(t_deviceptr, s_deviceptr, memsize, cudaMemcpyDeviceToDevice);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("DeviceToDevice CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
2025-01-15 03:35:48 +00:00
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BAAF>
extern "C" void CUDAdistanceAB(float* Ax, float* Ay, float* Az, float* Bx, float* By, float* Bz, float* R, long len) {
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><CDBF>ijߴ<C4B3>
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (len + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
CUDA_DistanceAB << <numBlocks, blockSize >> > (Ax, Ay, Az, Bx, By, Bz, R, len);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDAdistanceAB CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void CUDABdistanceAs(float* Ax, float* Ay, float* Az, float Bx, float By, float Bz, float* R, long len) {
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><CDBF>ijߴ<C4B3>
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (len + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
CUDA_B_DistanceA << <numBlocks, blockSize >> > (Ax, Ay, Az, Bx, By, Bz, R, len);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDABdistanceAs CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void CUDAmake_VectorA_B(float sX, float sY, float sZ, float* tX, float* tY, float* tZ, float* RstX, float* RstY, float* RstZ, long len) {
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (len + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
CUDA_make_VectorA_B << <numBlocks, blockSize >> > (sX, sY, sZ, tX, tY, tZ, RstX, RstY, RstZ, len);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDAmake_VectorA_B CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void CUDANorm_Vector(float* Vx, float* Vy, float* Vz, float* R, long len) {
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><CDBF>ijߴ<C4B3>
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (len + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
CUDA_Norm_Vector << <numBlocks, blockSize >> > (Vx, Vy, Vz, R, len);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDANorm_Vector CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void CUDAcosAngle_VA_AB(float* Ax, float* Ay, float* Az, float* Bx, float* By, float* Bz, float* anglecos, long len) {
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (len + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
CUDA_cosAngle_VA_AB << <numBlocks, blockSize >> > (Ax, Ay, Az, Bx, By, Bz, anglecos, len);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDAcosAngle_VA_AB CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void CUDAGridPointLinearInterp1(float* v, float* q, float* qv, long xlen, long qlen)
{
int blockSize = 256; // ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
int numBlocks = (qlen + blockSize - 1) / blockSize; // <20><><EFBFBD><EFBFBD> pixelcount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
// <20><><EFBFBD><EFBFBD> CUDA <20>˺<EFBFBD><CBBA><EFBFBD>
2025-01-15 03:35:48 +00:00
CUDA_GridPoint_Linear_Interp1 << <numBlocks, blockSize >> > (v, q, qv, xlen, qlen);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDALinearInterp1 CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
2025-01-15 03:35:48 +00:00
extern "C" void CUDADSin(double* y, double* X, int n)
{
// <20><><EFBFBD><EFBFBD> sin(temp) <20><><EFBFBD><EFBFBD><E6B4A2> d_temp <20><>
int blockSize = 256;
int numBlocks = (n + blockSize - 1) / blockSize;
CUDA_D_sin << <numBlocks, blockSize >> > (y, X, n);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("sin CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
2025-01-15 03:35:48 +00:00
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
extern "C" void CUDADCos(double* y, double* X, int n)
{
// <20><><EFBFBD><EFBFBD> sin(temp) <20><><EFBFBD><EFBFBD><E6B4A2> d_temp <20><>
int blockSize = 256;
int numBlocks = (n + blockSize - 1) / blockSize;
CUDA_D_cos << <numBlocks, blockSize >> > (y, X, n);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("sin CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
2025-01-15 03:35:48 +00:00
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
}
2025-01-20 07:49:54 +00:00
long NextBlockPad(long num, long blocksize)
{
return ((num + blocksize - 1) / blocksize) * blocksize;
}
void PrintLasterError(const char* s)
{
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
2025-02-19 04:29:15 +00:00
//printf("%s: %s\n", s, cudaGetErrorString(err));
PrintTipMsgToQDebug(s, cudaGetErrorString(err));
exit(2);
}
}
2025-01-15 03:35:48 +00:00
2025-02-25 05:18:19 +00:00
extern "C" void CUDAIFFT(cuComplex* inArr, cuComplex* outArr, long InRowCount, long InColCount, long outColCount) {
cufftHandle plan;
cufftResult result;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IFFT<46>ƻ<EFBFBD>
int rank = 1;
int n[] = { InColCount }; // ÿ<><C3BF>IFFT<46><54><EFBFBD><EFBFBD>freqcount<6E><74>
int inembed[] = { InColCount };
2025-02-26 11:39:46 +00:00
int onembed[] = { outColCount };
2025-02-25 05:18:19 +00:00
int istride = 1;
int ostride = 1;
int idist = InColCount; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD><CEBC><EFBFBD>
2025-02-26 11:39:46 +00:00
int odist = outColCount; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD><CEBC><EFBFBD>
2025-02-25 05:18:19 +00:00
int batch = InRowCount; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
result = cufftPlanMany(&plan, rank, n,
inembed, istride, idist,
onembed, ostride, odist,
CUFFT_C2C, batch);
if (result != CUFFT_SUCCESS) {
PrintLasterError("CUDAIFFT");
return;
}
// ִ<><D6B4>IFFT
cuComplex* in_ptr = inArr;
cuComplex* out_ptr = outArr;
result = cufftExecC2C(plan, (cufftComplex*)in_ptr, (cufftComplex*)out_ptr, CUFFT_INVERSE);
2025-02-26 11:39:46 +00:00
2025-02-25 05:18:19 +00:00
if (result != CUFFT_SUCCESS) {
cufftDestroy(plan);
return;
}
// <20>ȴ<EFBFBD>IFFT<46><54><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cudaDeviceSynchronize();
cufftDestroy(plan);
}
extern "C" void CUDAIFFTScale(cuComplex* inArr, cuComplex* outArr, long InRowCount, long InColCount, long outColCount)
{
CUDAIFFT(inArr, outArr, InRowCount, InColCount, outColCount);
float scale = 1.0f / InColCount;
int totalElements = InRowCount * InColCount;
dim3 block(256);
dim3 grid((totalElements + block.x - 1) / block.x);
scaleKernel << <grid, block >> > (outArr, totalElements, scale);
cudaDeviceSynchronize();
return ;
}
#endif
extern "C" float CUDA_SUM(float* d_x, long N)
{
long NUM_REPEATS = 100;
2025-01-15 03:35:48 +00:00
int grid_size = (N + BLOCK_SIZE - 1) / BLOCK_SIZE;
const int ymem = sizeof(float) * grid_size;
const int smem = sizeof(float) * BLOCK_SIZE;
2025-01-15 03:35:48 +00:00
float* d_y = (float*)mallocCUDADevice(ymem);
float* h_y = (float*)mallocCUDAHost(ymem);
2025-01-15 03:35:48 +00:00
CUDACkernel_SUM_reduce_dynamicshared << <grid_size, BLOCK_SIZE, smem >> > (d_x, d_y, N);
#ifdef __CUDADEBUG__
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
printf("CUDALinearInterp1 CUDA Error: %s\n", cudaGetErrorString(err));
exit(2);
}
#endif // __CUDADEBUG__
cudaDeviceSynchronize();
DeviceToHost(h_y, d_y, ymem);
2025-01-15 03:35:48 +00:00
float result = 0.0;
for (int n = 0; n < grid_size; ++n)
{
result += h_y[n];
}
2025-01-15 03:35:48 +00:00
FreeCUDAHost(h_y);
FreeCUDADevice(d_y);
2025-01-15 03:35:48 +00:00
return result;
}