CUDA和C ++简单项目 [英] CUDA and C++ simple project

查看:963
本文介绍了CUDA和C ++简单项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个CUDA + C ++项目。基本上是一个.cpp项目,它需要一些CUDA内核。所以我只是跟着这里的例子,这基本上添加两个向量。内核确实求和工作:
http://blog.norture.com/2012/10/gpu-parallel-programming-in-vs2012-with-nvidia-cuda/

I am trying to create a CUDA + C++ project. Basically a .cpp project that calls for some CUDA kernel. So I simply followed the example here, which basically adds two vectors. The kernel does the summation job: http://blog.norture.com/2012/10/gpu-parallel-programming-in-vs2012-with-nvidia-cuda/

这里是代码,

#include <iostream>
#include "cuda_runtime.h"
#include "cuda.h"
#include "device_launch_parameters.h"

using namespace std;

__global__ void saxpy(int n, float a, float *x, float *y)
{
  int i = blockIdx.x*blockDim.x + threadIdx.x;
  if (i < n) y[i] = a*x[i] + y[i];
}

int main(void)
{
  int N = 1<<20;
  float *x, *y, *d_x, *d_y;
  x = (float*)malloc(N*sizeof(float));
  y = (float*)malloc(N*sizeof(float));

  cudaMalloc(&d_x, N*sizeof(float));
  cudaMalloc(&d_y, N*sizeof(float));

  for (int i = 0; i < N; i++) {
    x[i] = 1.0f;
    y[i] = 2.0f;
  }

  cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
  cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice);

  // Perform SAXPY on 1M elements
  saxpy<<<(N+255)/256, 256>>>(N, 2.0, d_x, d_y);

  cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost);

  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = max(maxError, abs(y[i]-4.0f));
  cout << "Max error: " << maxError;
}

当我构建时,我得到这个错误:

When I built I got this error:

1>------ Rebuild All started: Project: CUDATest001, Configuration: Debug x64 ------
1>  CUDATestZeroZeroOne.cpp
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol threadIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockDim
1>D:\Projects\CUDATest001\x64\Debug\CUDATest001.exe : fatal error LNK1120: 3 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

如果注释掉行 saxpy <<<(N + 255)/ 256,256>(N,2.0,d_x,d_y); ,那么这个错误出现了:

If the line saxpy<<<(N+255)/256, 256>>>(N, 2.0, d_x, d_y); is commented out, then this error appeared:

1>------ Rebuild All started: Project: CUDATest001, Configuration: Debug x64 ------
1>  CUDATestZeroZeroOne.cpp
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol threadIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockDim
1>D:\Projects\CUDATest001\x64\Debug\CUDATest001.exe : fatal error LNK1120: 3 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

我使用vs2012 + CUDA 5.5。我开始与一个空的C ++ Win32控制台项目,添加一个.cpp文件,其中包括上述的所有代码。我什至不知道在这一点上它应该是一个.CU或.cpp文件?

I am using vs2012 + CUDA 5.5. I started with a empty C++ win32 console project, added a .cpp file which includes all the code above. I am not even sure at this point should it be a .cu or a .cpp file?

任何人都有任何想法如何使这项工作?谢谢你。

Anyone has any idea how to make this work? Thanks.

推荐答案

在你的项目的上下文菜单中,单击构建自定义。打开 CUDA 5.5 目标。

In the context menu for your project, click Build Customizations. Turn on the CUDA 5.5 target.

/ code>文件,单击重命名并将其重命名为 .cu

.cu 文件(刚重命名)的上下文菜单中,选择属性。然后转到 General 并确保项类型设置为 CUDA C / C ++

In the context menu for your .cu file (that you just renamed), select Properties. Then go to General and make sure Item Type is set to CUDA C/C++.

重建。

当您启动一个新的CUDA项目时, code>模板> NVIDIA> CUDA 5.5> CUDA 5.5 Runtime 以获取应该编译而无需任何修改的项目。

When you start a new CUDA project, you can select Templates > NVIDIA > CUDA 5.5 > CUDA 5.5 Runtime to get a project that should compile without any modifications.

这篇关于CUDA和C ++简单项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆