将静态cuda库包含到c ++项目中 [英] Include a static cuda library into a c++ project
问题描述
我有一个模板化的静态CUDA库,我想包含在一个通用的c ++项目中。当我包括库的头文件时,编译器崩溃,并说它无法解析CUDA特定的符号。当然,g ++编译器不能解释这些符号。我知道问题,但我不知道如何使用nsight IDE来解决这个问题。
我使用nsight的cuda / nvcc库和
make all
构建文件:../src/MedPrak.cpp
调用:GCC C ++编译器
g ++ -I / home / voodoocode / Praktikum / MedPrak / PrivateRepo / MedPrakCuda / src -O0 -g3 -Wall -c -fmessage-length = 0-MMD -MP -MFsrc / MedPrak.d-MTsrc / MedPrak.d-osrc / MedPrak.o../src/MedPrak.cpp
在../src/cudaWrapper.cu:8:0中包含的文件中,
来自../src/MedPrak.cpp:3:
/ home /voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src/kernel.h:15:23:错误:'__global__'不命名类型
模板< typename T> __global__ void squareVector(T * input,T * output,int size){
我有一个cuda项目与库中相同的文件。 cuda项目编译正常,运行正常,所以我认为在我的代码中没有一个巨大的错误。
Edit2:避免模板库的想法。我有一个包装器实际的模板类。因此,没有空库。
这里有一组说明,可以帮助:
A。创建库项目:
- 选择文件...新建... CUDA C / C ++项目
-
- 选择静态库...空项目并为项目命名(test8)
- 下一步...下一步...完成创建项目
- 在项目浏览器窗口中右键单击项目名称,选择新建...头文件,给它一个名称(test8lib.h)
- 编辑test8lib.h(包含来自下面的内容),保存
- 为cuda模板创建另一个新头文件(test8.cuh)
- 保存
- 创建新的源文件(test8.cu)
- 编辑test8.cu
-
- 选择项目...构建项目(libtest8.a现已生成)
test8lib.h:
#ifndef TEST8LIB_H_
#define TEST8LIB_H_
b $ b void calc_square_vec_float(float * in_data,float * out_data,int size);
#endif / * TEST8LIB_H_ * /
test8。 cuh:
#ifndef TEST8_CUH_
#define TEST8_CUH_
template< typename T> __global__ void squareVector(T * input,T * output,int size){
int idx = threadIdx.x + blockDim.x * blockIdx.x;
if(idx< size)output [idx] = input [idx] * input [idx];
}
#endif / * TEST8_CUH_ * /
test8.cu:
#includetest8lib.h
#includetest8.cuh
#define nTPB 256
void calc_square_vec_float(float * in_data,float * out_data,int size){
float * d_in_data,* d_out_data;
cudaMalloc(& d_in_data,size * sizeof(float));
cudaMalloc(& d_out_data,size * sizeof(float));
cudaMemcpy(d_in_data,in_data,size * sizeof(float),cudaMemcpyHostToDevice);
squareVector<<<(size + nTPB-1)/ nTPB,nTPB>>(d_in_data,d_out_data,size);
cudaMemcpy(out_data,d_out_data,size * sizeof(float),cudaMemcpyDeviceToHost);
}
创建主项目:
- 文件... new ... C ++项目...空项目。 ..Linux GCC工具链,给它一个名称(test9)
- 下一步...完成创建项目
<源文件...默认C ++源模板,给它一个名称(test9.cpp) - 使用下面的内容编辑文件,保存。
- 添加包含路径:
Project ...属性...构建...设置...工具设置... GCC C ++
编译器...包括...包含路径.. 。添加和添加目录
其中test8lib.h所在。 - 添加lib:工具设置... GCC C ++链接器...库...库。添加并添加之前构建的库(test8)的名称
- 也添加CUDA运行时库(cudart)
- 添加lib路径:设置... GCC C ++链接器...库...库路径...添加和添加到以前构建的库的路径(例如
/ path / to / cuda-workspace / test8 / Debug
) - 也添加cudart的路径(例如
/ usr / local / cuda / lib64
) - 构建项目
- li>
test9.cpp:
include< stdio.h>
#include< stdlib.h>
#includetest8lib.h
#define DSIZE 4
#define TEST_VAL 2.0f
int main(){
float * in,*出口;
in =(float *)malloc(DSIZE * sizeof(float));
out =(float *)malloc(DSIZE * sizeof(float));
for(int i = 0; i in [i] = TEST_VAL;
out [i] = 0.0f;
}
calc_square_vec_float(in,out,DSIZE);
for(int i = 0; i< DSIZE; i ++)
if(out [i]!=(float)(TEST_VAL * TEST_VAL)){
printf ,is:%f,应该是:%f \\\
,i,out [i],(float)(TEST_VAL * TEST_VAL)
return 1;
}
printf(Success!\\\
);
return 0;
}
I have a templated static CUDA library which I want to include into a common c++ project. When I include the headers of the library the compiler crashes and says It cannot resolve the CUDA-specific symbols. Of course the g++ compiler cannot interpret these symbols. I know the problem, but I do not know how to fix this problem using the nsight IDE.
I'm using nsight for both, the cuda/nvcc library and the c++/g++ project.
Console output:
make all
Building file: ../src/MedPrak.cpp
Invoking: GCC C++ Compiler
g++ -I/home/voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/MedPrak.d" -MT"src/MedPrak.d" -o "src/MedPrak.o" "../src/MedPrak.cpp"
In file included from ../src/cudaWrapper.cu:8:0,
from ../src/MedPrak.cpp:3:
/home/voodoocode/Praktikum/MedPrak/PrivateRepo/MedPrakCuda/src/kernel.h:15:23: error: ‘__global__’ does not name a type
template <typename T> __global__ void squareVector(T *input, T *output, int size) {
Edit: Forgot to mention that I have a cuda project with the same files as in the library. The cuda project compiles fine and runs properly, so I think there is not a huge error within my code.
Edit2: To avoid the "template library" idea. I have a wrapper around the actual template classes. So there is no "empty" library.
Here's a set of instructions that should help:
A. Create library project:
- select File...New...CUDA C/C++ Project
- Select Static Library...Empty Project and give the project a name (test8)
- Next...Next...Finish to finish creating the project
- right click on project name in Project Explorer window, select New...Header File, give it a name (test8lib.h)
- edit test8lib.h (with contents from below), save it
- create another new header file for cuda template, (test8.cuh)
- edit test8.cuh (with contents from below), save it
- create a new source file, (test8.cu)
- edit test8.cu (with contents from below), save it
- select Project...Build Project (libtest8.a is now built)
test8lib.h:
#ifndef TEST8LIB_H_
#define TEST8LIB_H_
void calc_square_vec_float(float *in_data, float *out_data, int size);
#endif /* TEST8LIB_H_ */
test8.cuh:
#ifndef TEST8_CUH_
#define TEST8_CUH_
template <typename T> __global__ void squareVector(T *input, T *output, int size) {
int idx = threadIdx.x+blockDim.x*blockIdx.x;
if (idx < size) output[idx]=input[idx]*input[idx];
}
#endif /* TEST8_CUH_ */
test8.cu:
#include "test8lib.h"
#include "test8.cuh"
#define nTPB 256
void calc_square_vec_float(float *in_data, float *out_data, int size){
float *d_in_data, *d_out_data;
cudaMalloc(&d_in_data, size*sizeof(float));
cudaMalloc(&d_out_data, size*sizeof(float));
cudaMemcpy(d_in_data, in_data, size*sizeof(float),cudaMemcpyHostToDevice);
squareVector<<<(size+nTPB-1)/nTPB, nTPB>>>(d_in_data, d_out_data, size);
cudaMemcpy(out_data, d_out_data, size*sizeof(float),cudaMemcpyDeviceToHost);
}
B. Create main project:
- File...new...C++ project...empty project...Linux GCC toolchain, give it a name (test9)
- Next...Finish to finish creating the project
- File...New Source File...Default C++ source template, give it a name (test9.cpp)
- edit the file with contents from below, save it.
- add the include path: Project...Properties...Build...Settings...Tool Settings...GCC C++ Compiler...Includes...Include Paths...Add and add the directory where test8lib.h is located.
- add the lib: Tool Settings...GCC C++ Linker...Libraries...Libraries...Add and add the name of the previously built library (test8)
- also add CUDA runtime library (cudart)
- add the lib path: Tool Settings...GCC C++ Linker...Libraries...Library Paths...Add and add the path to the previously built library (e.g.
/path/to/cuda-workspace/test8/Debug
) - also add the path to cudart (e.g.
/usr/local/cuda/lib64
) - Build Project
- Run Project
test9.cpp:
#include <stdio.h>
#include <stdlib.h>
#include "test8lib.h"
#define DSIZE 4
#define TEST_VAL 2.0f
int main(){
float *in, *out;
in = (float *)malloc(DSIZE*sizeof(float));
out = (float *)malloc(DSIZE*sizeof(float));
for (int i=0; i<DSIZE; i++){
in[i] = TEST_VAL;
out[i] = 0.0f;
}
calc_square_vec_float(in, out, DSIZE);
for (int i=0; i<DSIZE; i++)
if (out[i] != (float)(TEST_VAL*TEST_VAL)){
printf("mismatch at %d, was: %f, should be: %f\n", i, out[i], (float)(TEST_VAL*TEST_VAL));
return 1;
}
printf("Success!\n");
return 0;
}
这篇关于将静态cuda库包含到c ++项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!