cuda日记-20140924

用cufft如下例

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Include CUDA runtime and CUFFT
#include <cuda_runtime.h>
#include <cufft.h>

// Helper functions for CUDA
#include <helper_functions.h>
#include <helper_cuda.h>




#define SIGNAL_SIZE 10


int main()
{
	StopWatchInterface *hTimer = NULL;
	sdkCreateTimer(&hTimer);
	sdkResetTimer(&hTimer);
	sdkStartTimer(&hTimer);
	cufftComplex 
		*d_signalsource,
		*d_signalresult,
		*h_signalsource,
		*h_signalresult;
	//为host信号分配空间和初始化
	h_signalsource = (cufftComplex *)malloc(sizeof(cufftComplex)*SIGNAL_SIZE);
	h_signalresult = (cufftComplex *)malloc(sizeof(cufftComplex)*SIGNAL_SIZE);
	for (int i = 0;i<SIGNAL_SIZE;i++)
	{
		h_signalsource[i].x = i;
		h_signalsource[i].y = i;
	}

	//在gpu上为信号分配空间
	checkCudaErrors(cudaMalloc((void **)&d_signalsource,sizeof(cufftComplex)*SIGNAL_SIZE));
	checkCudaErrors(cudaMalloc((void **)&d_signalresult,sizeof(cufftComplex)*SIGNAL_SIZE));

	//将信号从内存拷贝到显存
	checkCudaErrors(cudaMemcpy(d_signalsource,h_signalsource,sizeof(cufftComplex)*SIGNAL_SIZE,cudaMemcpyHostToDevice));

	//创建cufft句柄
	cufftHandle plan;
	checkCudaErrors(cufftPlan1d(&plan,SIGNAL_SIZE,CUFFT_C2C,1));
    //执行cufft
	checkCudaErrors(cufftExecC2C(plan,d_signalsource,d_signalresult,CUFFT_FORWARD));
	/* 
	* Results may not be immediately available so block device until all 
	*/
	checkCudaErrors(cudaDeviceSynchronize());

	//将结果从显存拷贝到内存
	checkCudaErrors(cudaMemcpy(h_signalresult,d_signalresult,sizeof(cufftComplex)*SIGNAL_SIZE,cudaMemcpyDeviceToHost));

	//输出结果
	for (int i = 0;i<SIGNAL_SIZE;i++)
	{
		printf("%f+%fi\n",h_signalresult[i].x,h_signalresult[i].y);
	}
	//销毁cufft句柄,释放显存
	cufftDestroy(plan);
	cudaFree(d_signalsource);
	cudaFree(d_signalresult);
	sdkStopTimer(&hTimer);
	double gpuTime = sdkGetTimerValue(&hTimer);
	printf("gputime:%lf\n",gpuTime);

	getchar();
	return 0;

}
内存和显存上的开辟空间,内容复制等步骤都是必须的。


关于batch:

比如上例中一维fft,信号长度为10点,把plan改写为

checkCudaErrors(cufftPlan1d(&plan,5,CUFFT_C2C,2));
就会并行地做前五点与后五点各自的5点fft,所以,若要同时做m个n点fft(复数),则输入信号长度为(cufftcomplex *)*m*n,plan则是
cufftPlan1d(&plan,n,CUFFT_C2C,m)
关于batch的限制等等,待查文档

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值