现在的位置: 首页 > 综合 > 正文

CUDA范例编程中的shaed memory bitmap

2014年02月22日 ⁄ 综合 ⁄ 共 1072字 ⁄ 字号 评论关闭

 glut32.lib放到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.1\lib\Win32(根据安装的目录)目录下,并把glut32.dll放到C:\Windows\System32下面,这个可以通过去网上下载一个压缩包cuda by example【完整版】(cuda_by_example.zip).cpu_bitmap的头文件也在这个压缩包中

#include "C://Users//XX//Desktop//CUDA//common//cpu_bitmap.h"
#define DIM 1024
#define PI 3.1415926535897932

__global__ void kernel(unsigned char *ptr)
{

	int x=threadIdx.x+blockIdx.x*blockDim.x;
	int y=threadIdx.y+blockIdx.y*blockDim.y;
	int offset=x+y*blockDim.x*gridDim.x;
	__shared__ float shared[16][16];
	const float period=128.0f;
	shared[threadIdx.x][threadIdx.y]=
		255*(sinf(x*2.0f*PI/period)+1.0f)*
		(sinf(y*2.0f*PI/period)+1.0f)/4.0f;
	__syncthreads();
	ptr[offset*4+0]=0;
	ptr[offset*4+1]=shared[15-threadIdx.x][15-threadIdx.y];
	ptr[offset*4+2]=0;
	ptr[offset*4+3]=255;

}
int main()
{
	CPUBitmap bitmap(DIM,DIM);
	unsigned char *dev_bitmap;

	cudaMalloc((void**)&dev_bitmap,bitmap.image_size());

	dim3 grids(DIM/16,DIM/16);
	dim3 threads(16,16);
	kernel<<<grids,threads>>>(dev_bitmap);
	cudaMemcpy(bitmap.get_ptr(),dev_bitmap,bitmap.image_size(),cudaMemcpyDeviceToHost);
	bitmap.display_and_exit();
	cudaFree(dev_bitmap);

	
	


}

 

运行结果如下

 

 

 

抱歉!评论已关闭.