- 操作系統:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 編程語言:C++11
算法描述
該函數用于計算四維浮點向量(float4類型)的雙曲余弦值,作用于CUDA設備端。雙曲余弦函數定義為cosh(x) = (e? + e??)/2,具有偶函數性質(cosh(-x) = cosh(x))
函數原型
__device__ __forceinline__ float4 cv::cudev::cosh ( const float4 & a )
參數
參數 | 類型 | 描述 |
---|---|---|
a | const float4& | 輸入向量,每個分量獨立計算雙曲余弦 |
返回值
返回float4類型向量,其每個分量為輸入向量對應分量的雙曲余弦值,值域為[1, +∞)
應用場景?
適用于GPU加速的數學計算、圖像處理(如非線性濾波)和物理模擬等領域。
代碼示例
#include <opencv2/opencv.hpp>
#include <opencv2/cudev/common.hpp>
#include <opencv2/cudev/util/vec_math.hpp>__global__ void kernel_cosh(const float4* input, float4* output, int size) {int idx = blockIdx.x * blockDim.x + threadIdx.x;if (idx < size) {output[idx] = cv::cudev::cosh(input[idx]);}
}int main() {const int N = 4;float4 h_input[N] = {{0.0f, 1.0f, 2.0f, 3.0f},{-1.0f, -2.0f, -3.0f, -4.0f},{0.5f, 1.5f, 2.5f, 3.5f},{-0.5f, -1.5f, -2.5f, -3.5f}};float4 h_output[N];// 分配設備內存float4* d_input;float4* d_output;cudaMalloc(&d_input, N * sizeof(float4));cudaMalloc(&d_output, N * sizeof(float4));// 拷貝數據到設備cudaMemcpy(d_input, h_input, N * sizeof(float4), cudaMemcpyHostToDevice);// 調用核函數dim3 block(4);dim3 grid(1);kernel_cosh<<<grid, block>>>(d_input, d_output, N);// 拷貝結果回主機cudaMemcpy(h_output, d_output, N * sizeof(float4), cudaMemcpyDeviceToHost);// 打印結果for (int i = 0; i < N; ++i) {printf("cosh(%.1f, %.1f, %.1f, %.1f) = (%.6f, %.6f, %.6f, %.6f)\n",h_input[i].x, h_input[i].y, h_input[i].z, h_input[i].w,h_output[i].x, h_output[i].y, h_output[i].z, h_output[i].w);}// 釋放內存cudaFree(d_input);cudaFree(d_output);return 0;
}
運行結果
cosh(0.0, 1.0, 2.0, 3.0) = (1.000000, 1.543081, 3.762196, 10.067662)
cosh(-1.0, -2.0, -3.0, -4.0) = (1.543081, 3.762196, 10.067662, 27.308231)
cosh(0.5, 1.5, 2.5, 3.5) = (1.127626, 2.352410, 6.132289, 16.572824)
cosh(-0.5, -1.5, -2.5, -3.5) = (1.127626, 2.352410, 6.132289, 16.572824)