【发布时间】:2014-04-12 15:12:06
【问题描述】:
最近,我用 Cuda 写了一个算法,叫做“正交匹配追踪”。在我丑陋的 Cuda 代码中,整个迭代需要 60 秒,而 Eigen lib 只需 3 秒......
在我的代码中,矩阵 A 是 [640,1024] 并且 y 是 [640,1] ,在每一步中我从 A 中选择一些向量来组成一个名为 A_temp [640,itera] 的新矩阵,iter=1:500 .我在 cpu 中新建了一个数组 MaxDex_Host[] 来告诉选择哪一列。
我想使用最小二乘法从 A_temp*x_temp=y 获取 x_temp[itera,1],我使用 cula API 'culaDeviceSgels' 和 cublas 矩阵向量乘法 API。
所以 culaDeviceSgels 会调用 500 次,我认为这会比 Eigen lib 的 QR.Sovler 更快。
我检查了 Nisight 性能分析,我发现 custreamdestory 需要很长时间。我在迭代之前初始化 cublas 并在得到结果后破坏它。所以我想知道 custreamdestory 是什么,与 cublasdestory 不同?
主要问题是 memcpy 和函数 'gemm_kernel1x1val' 。我认为这个函数来自'culaDeviceSgels'
while(itera
MaxDex_Host[itera]=pos;
itera++;
float* A_temp_cpu=new float[M*itera]; // matrix all in col-major
for (int j=0;j<itera;j++) // to get A_temp [M,itera] , the MaxDex_Host[] shows the positon of which column of A to chose ,
{
for (int i=0;i<M;i++) //M=640 , and A is 640*1024 ,itera is add 1 each step
{
A_temp_cpu[j*M+i]=A[MaxDex_Host[j]*M+i];
}
}
// I must allocate one more array because culaDeviceSgels will decompose the one input Array , and I want to use A_temp after least-square solving.
float* A_temp_gpu;
float* A_temp2_gpu;
cudaMalloc((void**)&A_temp_gpu,Size_float*M*itera);
cudaMalloc((void**)&A_temp2_gpu,Size_float*M*itera);
cudaMemcpy(A_temp_gpu,A_temp_cpu,Size_float*M*itera,cudaMemcpyHostToDevice);
cudaMemcpy(A_temp2_gpu,A_temp_gpu,Size_float*M*itera,cudaMemcpyDeviceToDevice);
culaDeviceSgels('N',M,itera,1,A_temp_gpu,M,y_Gpu_temp,M);// the x_temp I want is in y_Gpu_temp's return value , stored in the y_Gpu_temp[0]——y_Gpu_temp[itera-1]
float* x_temp;
cudaMalloc((void**)&x_temp,Size_float*itera);
cudaMemcpy(x_temp,y_Gpu_temp,Size_float*itera,cudaMemcpyDeviceToDevice);
cuda的内存管理好像太复杂了,有没有其他方便的方法来解决最小二乘法?
【问题讨论】: