由于毕业需要,需要cuda来写毕业论文。楼主小白一枚,跟着开勇的博客开始学习。博客详细地址请参考:http://blog.youkuaiyun.com/openhero/article/details/42131771
《GPU的革命》
首先新建一个win32空项目,进行如下设置:
1、右键源文件文件夹->新建项->选择cuda c/c++->新建一个以.cu结尾的文件
2、右键工程-》生成自定义-》选择cuda生成
3、右键test.cu-》属性-》选择cuda c/c++编译器
4、右键工程-》属性-》链接器-》常规-》附加库目录-》添加目录 $(CUDA_PATH_V5_5)\lib\$(Platform);
5、在链接器-》输入中添加 cudart.lib
6、在工具-》选项-》文本编辑器-》文件扩展名-》添加cu \cuh两个文件扩展名
设置完毕后开始输入代码
#include<stdio.h>
#include<iostream>
#include<cuda_runtime.h>
using namespace std;
/*=============================================================*/
/*************************thread demo***************************/
/*=============================================================*/
#define BLOCK_DIM 512
const int size_x = 512;
const int size_y = 1;
__global__ static void ThreadDemo1(unsigned int *ret)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int yIndex = blockDim.y * blockIdx.y + threadIdx.y;
if(xIndex < size_x && yIndex < size_y)
{
unsigned int index = xIndex + size_x * yIndex;
ret[index] = xIndex;
ret[index + size_x * size_y] = yIndex;
}
}
int main()
{
unsigned int *ret = 0;
unsigned int host_ret[size_x * size_y * 2] = {0};
int i = 0;
cudaMalloc((void**)&ret, sizeof(unsigned