在vs中CUDA下c++混编实现c++运行gpu程序

来源:互联网 发布:台式电脑网络连接红叉 编辑:程序博客网 时间:2024/05/19 18:17

首先建立自己的项目,普通的c++项目

写入自己需要运行的c++程序



勾选一个cuda的选项,因为我安装了两个,随便勾选一个即可

选择属性

当然如果新建的时候基于cudac++那么就不需要选这一步了。

然后打开项目的属性,在配置属性-》链接器-》输入 中的附件依赖项中加入cudart.lib


然后就可以写入自己的代码了

其中GpuCputest.cu中的代码如下:

#include <cstdio>
#include <iostream>
#include <cuda_runtime.h>
#include <cutil.h>
#include<cuda.h>
using namespace std;
bool InitCUDA(void)
{
 int count = 0;
 int i = 0;
 cudaGetDeviceCount(&count);
 if(count == 0) {
  fprintf(stderr, "There is no device.\n");
  return false;
 }
 for(i = 0; i < count; i++) {
  cudaDeviceProp prop;
  if(cudaGetDeviceProperties(&prop, i) == cudaSuccess) {
   if(prop.major >= 1) {
    break;
   }
  }
 }
 if(i == count) {
  fprintf(stderr, "There is no device supporting CUDA.\n");
  return false;
 }
 cudaSetDevice(i);
 printf("CUDA initialized.\n");
 cudaDeviceProp prop;
 cudaGetDeviceProperties(&prop,i);
 printf("Device : \" %s \" \n\n", prop.name);
 return true;
}


//#define aW 855
//#define aH 511
//#define bW 1013
//#define blocknum 32//32
//#define threadnum 256//256


#define aW 2000
#define aH 2000
#define bW 2000
#define blocknum 445//32
#define threadnum 445//256


typedef struct
{
 int width;
 int height;
 int *element;
}Matrix;
Matrix InitMatrix(int w, int h)
{
 Matrix t;
 t.element=(int *)malloc(w * h * sizeof(int) );
 for ( int i=0 ; i < w*h ; i ++)
  t.element[i]= rand() % 10;
 t.width=w;
 t.height=h;
 return t;
}
Matrix MM( Matrix a, Matrix b)
{
 Matrix t;
 t.element=(int *) malloc (a.height * b.width * sizeof(int));
 t.width=b.width;
 t.height=a.height;
 int x;
 int y;
 for ( int i =0 ; i < t.width * t.height ; i ++ )
 {
  x=i / t.width * a.width;
  y=i - i / t.width * t.width;
  t.element[i]=0;
  for ( int k = 0 ; k < a. width ; k ++ )
  {
   t.element[i] += a.element[x + k] * b.element [y +b.width * k];
  } 
 }
 return t;
}
 






__global__ static void MatrixMul(int *ma , int *mb , int *mc , int *mp)
{
 int aw = mp[0];
 int bw = mp[2];
 int cw = mp[4];
 int ch = mp[5];
 const int bid = blockIdx.x;
 const int tid = threadIdx.x;
 int i , x , y  ;
 
 for ( i = bid * threadnum + tid ; i < cw * ch ; i += threadnum * blocknum )
 {
  x = i / cw * aw;
  y = i - i / cw * cw;
  mc[i] = 0;
  for ( int k = 0 ; k < aw ; k ++ )
  {
   mc[i] += ma[ x + k ] * mb[ y + k * bw ];
  }
 }
}
extern "C" void gpucputest()
{
if(!InitCUDA()) {
  return;
 }
 //定义矩阵
 //int matrixa[N][N] , matrixb[N][N] , matrixc[N][N] , gpuresult[N][N] , matrixd[N][N] ;
 Matrix matrixa=InitMatrix(aW,aH);
 Matrix matrixb=InitMatrix(bW,aW);
 Matrix matrixc;
 Matrix gpuresult=InitMatrix(bW,aH);
 
 int matrixprop[6];
 
 //为CPU运算计时
 unsigned int timer1 = 0;
 //CPU矩阵相乘
 int start = clock();
 matrixc=MM(matrixa,matrixb);
 int finish = clock();
 printf("cpu time = %d\n", finish-start);
 start = clock();
 matrixprop[0] = matrixa.width;
 matrixprop[1] = matrixa.height;
 matrixprop[2] = matrixb.width;
 matrixprop[3] = matrixb.height;
 matrixprop[4] = matrixc.width;
 matrixprop[5] = matrixc.height;
 
 //申请显存
 int *ma, *mb, *mc, *mp;
 cudaMalloc( (void**)&ma , sizeof(int) * matrixa.width * matrixa.height );
 cudaMalloc( (void**)&mb , sizeof(int) * matrixb.width * matrixb.height );
 cudaMalloc( (void**)&mc , sizeof(int) * matrixc.width * matrixc.height );
 cudaMalloc( (void**)&mp , sizeof(int) * 6 );
 //将数据复制到显存内
 cudaMemcpy(ma ,  matrixa.element , sizeof(int) * matrixa.width * matrixa.height ,cudaMemcpyHostToDevice);
 cudaMemcpy(mb ,  matrixb.element , sizeof(int) * matrixb.width * matrixb.height ,cudaMemcpyHostToDevice);
 cudaMemcpy(mp , matrixprop , sizeof(int) * 6 , cudaMemcpyHostToDevice);
 unsigned int timer2 = 0;
 //调用CUDA函数
 MatrixMul <<< blocknum , threadnum , 0 >>>( ma , mb , mc , mp);
 cudaThreadSynchronize();
 //cutilCheckError( cutStopTimer( timer2));
 //将数据从显存中复制出来
 cudaMemcpy( gpuresult.element , mc , sizeof(int) * gpuresult.width * gpuresult.height , cudaMemcpyDeviceToHost );
 finish = clock();
 printf("gpu time = %d\n", finish-start);


 for ( int i =0 ; i < gpuresult.width * gpuresult.height ; i ++ )
 {
   //printf("%d -- %d\n",matrixc.element[ i ],gpuresult.element[ i ]);
   if ( matrixc.element[i] != gpuresult.element[i] )
   {
    printf("ERROR");
   }
 }


 cudaFree(ma);
 cudaFree(mb);
 cudaFree(mc);
 cudaFree(mp);
 system("pause");
}


在cpp程序中调用的时候注意加入extern "C" 其中c大写

然后运行即可(在cpp或h文件中调用cu或cuh中定义的函数,直接采用include的方式调用不通过,可以采用extern的方式将cu或cuh文件中定义的cuda函数作为公共函数,在cpp中或h中声明extern函数,然后就可以直接使用了。

上面程序运行如下:



写的比较糙,主要是为了留个记号!


0 0
原创粉丝点击