使用CUDA的逐元素向量乘法
我在CUDA中构建了一个基本内核,用于对两个复数向量进行元素向量乘法。内核代码插入到下面(使用CUDA的逐元素向量乘法,cuda,complex-numbers,cublas,Cuda,Complex Numbers,Cublas,我在CUDA中构建了一个基本内核,用于对两个复数向量进行元素向量乘法。内核代码插入到下面(multipleyelementwise)。它工作得很好,但由于我注意到其他看似简单的操作(如缩放向量)在库(如CUBLAS或CULA)中得到了优化,我想知道是否有可能用库调用替换我的代码?让我惊讶的是,CUBLAS和CULA都没有这个选项,我试图通过使其中一个向量成为对角矩阵向量积的对角来伪造它,但结果非常缓慢 作为最后的手段,我试图自己优化这段代码(参见下面的multiplyElementwiseFas
multipleyelementwise
)。它工作得很好,但由于我注意到其他看似简单的操作(如缩放向量)在库(如CUBLAS或CULA)中得到了优化,我想知道是否有可能用库调用替换我的代码?让我惊讶的是,CUBLAS和CULA都没有这个选项,我试图通过使其中一个向量成为对角矩阵向量积的对角来伪造它,但结果非常缓慢
作为最后的手段,我试图自己优化这段代码(参见下面的multiplyElementwiseFast
),将这两个向量加载到共享内存中,然后从那里开始工作,但这比我原来的代码要慢
所以我的问题是:
multiplyElementwise
)__global__ void multiplyElementwise(cufftComplex* f0, cufftComplex* f1, int size)
{
const int i = blockIdx.x*blockDim.x + threadIdx.x;
if (i < size)
{
float a, b, c, d;
a = f0[i].x;
b = f0[i].y;
c = f1[i].x;
d = f1[i].y;
float k;
k = a * (c + d);
d = d * (a + b);
c = c * (b - a);
f0[i].x = k - d;
f0[i].y = k + c;
}
}
__global__ void multiplyElementwiseFast(cufftComplex* f0, cufftComplex* f1, int size)
{
const int i = blockIdx.x*blockDim.x + threadIdx.x;
if (i < 4*size)
{
const int N = 256;
const int thId = threadIdx.x / 4;
const int rem4 = threadIdx.x % 4;
const int i4 = i / 4;
__shared__ float a[N];
__shared__ float b[N];
__shared__ float c[N];
__shared__ float d[N];
__shared__ float Re[N];
__shared__ float Im[N];
if (rem4 == 0)
{
a[thId] = f0[i4].x;
Re[thId] = 0.f;
}
if (rem4 == 1)
{
b[thId] = f0[i4].y;
Im[thId] = 0.f;
}
if (rem4 == 2)
c[thId] = f1[i4].x;
if (rem4 == 0)
d[thId] = f1[i4].y;
__syncthreads();
if (rem4 == 0)
atomicAdd(&(Re[thId]), a[thId]*c[thId]);
if (rem4 == 1)
atomicAdd(&(Re[thId]), -b[thId]*d[thId]);
if (rem4 == 2)
atomicAdd(&(Im[thId]), b[thId]*c[thId]);
if (rem4 == 3)
atomicAdd(&(Im[thId]), a[thId]*d[thId]);
__syncthreads();
if (rem4 == 0)
f0[i4].x = Re[thId];
if (rem4 == 1)
f0[i4].y = Im[thId];
}
}
\uuuuu全局\uuuuu无效乘法器(CuftComplex*f0,CuftComplex*f1,整数大小)
{
const int i=blockIdx.x*blockDim.x+threadIdx.x;
如果(i<尺寸)
{
浮子a、b、c、d;
a=f0[i].x;
b=f0[i].y;
c=f1[i].x;
d=f1[i].y;
浮动k;
k=a*(c+d);
d=d*(a+b);
c=c*(b-a);
f0[i].x=k-d;
f0[i].y=k+c;
}
}
__全局\uuuo无效乘法lementWiseFast(cufftComplex*f0,cufftComplex*f1,整数大小)
{
const int i=blockIdx.x*blockDim.x+threadIdx.x;
如果(i<4*尺寸)
{
常数int N=256;
const int thId=threadIdx.x/4;
const int rem4=threadIdx.x%4;
常数int i4=i/4;
__共享浮点数a[N];
__共享浮点数b[N];
__共享浮点数c[N];
__共享浮点数d[N];
__共享浮点数Re[N];
__共享浮点数Im[N];
如果(rem4==0)
{
a[thId]=f0[i4].x;
Re[thId]=0.f;
}
如果(rem4==1)
{
b[thId]=f0[i4].y;
Im[thId]=0.f;
}
if(rem4==2)
c[thId]=f1[i4].x;
如果(rem4==0)
d[thId]=f1[i4].y;
__同步线程();
如果(rem4==0)
原子添加(&(Re[thId]),a[thId]*c[thId]);
如果(rem4==1)
原子添加(&(Re[thId]),-b[thId]*d[thId]);
if(rem4==2)
原子添加(&(Im[thId]),b[thId]*c[thId]);
如果(rem4==3)
原子添加(&(Im[thId]),a[thId]*d[thId]);
__同步线程();
如果(rem4==0)
f0[i4].x=Re[thId];
如果(rem4==1)
f0[i4].y=Im[thId];
}
}
如果您试图实现的是一个简单的、具有复数的元素式产品,那么您确实似乎在乘法器内核中执行了一些额外的步骤,以提高寄存器的使用率。您尝试计算的是:
f0[i].x = a*c - b*d;
f0[i].y = a*d + b*c;
因为(a+ib)*(c+id)=(a*c-b*d)+i(a*d+b*c)
。通过使用改进的复数乘法,您可以用1次乘法换取3次加法和一些额外的寄存器。这是否合理可能取决于您使用的硬件。例如,如果您的硬件支持(融合乘加),那么这种优化可能不会有效率。你应该考虑阅读这个文档:“”它也解决了浮点精度问题。
但是,你应该考虑使用。该库提供了许多高级工具,可以对主机和设备向量进行操作。您可以在此处看到一长串示例:。这会让你的生活轻松很多
更新代码
在您的情况下,您可以使用它并使其适应以下情况:
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <time.h>
struct ElementWiseProductBasic : public thrust::binary_function<float2,float2,float2>
{
__host__ __device__
float2 operator()(const float2& v1, const float2& v2) const
{
float2 res;
res.x = v1.x * v2.x - v1.y * v2.y;
res.y = v1.x * v2.y + v1.y * v2.x;
return res;
}
};
/**
* See: http://www.embedded.com/design/embedded/4007256/Digital-Signal-Processing-Tricks--Fast-multiplication-of-complex-numbers%5D
*/
struct ElementWiseProductModified : public thrust::binary_function<float2,float2,float2>
{
__host__ __device__
float2 operator()(const float2& v1, const float2& v2) const
{
float2 res;
float a, b, c, d, k;
a = v1.x;
b = v1.y;
c = v2.x;
d = v2.y;
k = a * (c + d);
d = d * (a + b);
c = c * (b - a);
res.x = k -d;
res.y = k + c;
return res;
}
};
int get_random_int(int min, int max)
{
return min + (rand() % (int)(max - min + 1));
}
thrust::host_vector<float2> init_vector(const size_t N)
{
thrust::host_vector<float2> temp(N);
for(size_t i = 0; i < N; i++)
{
temp[i].x = get_random_int(0, 10);
temp[i].y = get_random_int(0, 10);
}
return temp;
}
int main(void)
{
const size_t N = 100000;
const bool compute_basic_product = true;
const bool compute_modified_product = true;
srand(time(NULL));
thrust::host_vector<float2> h_A = init_vector(N);
thrust::host_vector<float2> h_B = init_vector(N);
thrust::device_vector<float2> d_A = h_A;
thrust::device_vector<float2> d_B = h_B;
thrust::host_vector<float2> h_result(N);
thrust::host_vector<float2> h_result_modified(N);
if (compute_basic_product)
{
thrust::device_vector<float2> d_result(N);
thrust::transform(d_A.begin(), d_A.end(),
d_B.begin(), d_result.begin(),
ElementWiseProductBasic());
h_result = d_result;
}
if (compute_modified_product)
{
thrust::device_vector<float2> d_result_modified(N);
thrust::transform(d_A.begin(), d_A.end(),
d_B.begin(), d_result_modified.begin(),
ElementWiseProductModified());
h_result_modified = d_result_modified;
}
std::cout << std::fixed;
for (size_t i = 0; i < 4; i++)
{
float2 a = h_A[i];
float2 b = h_B[i];
std::cout << "(" << a.x << "," << a.y << ")";
std::cout << " * ";
std::cout << "(" << b.x << "," << b.y << ")";
if (compute_basic_product)
{
float2 prod = h_result[i];
std::cout << " = ";
std::cout << "(" << prod.x << "," << prod.y << ")";
}
if (compute_modified_product)
{
float2 prod_modified = h_result_modified[i];
std::cout << " = ";
std::cout << "(" << prod_modified.x << "," << prod_modified.y << ")";
}
std::cout << std::endl;
}
return 0;
}
然后,您可以比较两种不同乘法策略的计时,并选择最适合您的硬件。您可以使用cublasZdgmm
cublasStatus_t cublasZdgmm(cublasHandle_t handle, cublasSideMode_t mode,
int m, int n,
const cuDoubleComplex *A, int lda,
const cuDoubleComplex *x, int incx,
cuDoubleComplex *C, int ldc)
“元素向量乘法”是指点积吗?@Benc。。。不。对于实向量,点积是元素乘积的和。@sgar91:如果他是复数的“乘法”,他可能实际上想要计算一个倍线性形式,在这种情况下可以称为内积/点积(请参阅)。我只是想确定他的意图。非常感谢,我会调查一下推力,看看我是否能让它为我工作。复杂乘积以这种“向后”方式计算的原因是我在这里读到的[处理器在加法方面比乘法方面麻烦少,所以可能会快一点。尽管向量的缩放或两个向量的和在许多库中都很容易找到,但元素级乘法不是……总之,谢谢你的答案!@WVDB:我明白了,我想这是值得比较的,b但是这种优化实际上依赖于硬件,您需要记住,如果使用编译器优化,编译器可能会重新排序操作。您可以将计时与nvp
/nvp
甚至生成的PTX代码进行比较,以便更好地了解实际情况。@WVDB:我更新了对的答案可以与NVIDIA的评测工具进行比较。@BenC目前我正在尝试使用Nsight评测我的代码。再次感谢您提供的可靠建议!
cublasStatus_t cublasZdgmm(cublasHandle_t handle, cublasSideMode_t mode,
int m, int n,
const cuDoubleComplex *A, int lda,
const cuDoubleComplex *x, int incx,
cuDoubleComplex *C, int ldc)