Golang调用CUDA库

huangapple go评论122阅读模式
英文:

Golang calling CUDA library

问题

我正在尝试从我的Go代码中调用一个CUDA函数。
我有以下三个文件。

test.h:

  1. int test_add(void);

test.cu:

  1. __global__ void add(int *a, int *b, int *c){
  2. *c = *a + *b;
  3. }
  4. int test_add(void) {
  5. int a, b, c; // a、b、c的主机副本
  6. int *d_a, *d_b, *d_c; // a、b、c的设备副本
  7. int size = sizeof(int);
  8. // 为a、b、c的设备副本分配空间
  9. cudaMalloc((void **)&d_a, size);
  10. cudaMalloc((void **)&d_b, size);
  11. cudaMalloc((void **)&d_c, size);
  12. // 设置输入值
  13. a = 2;
  14. b = 7;
  15. // 将输入复制到设备
  16. cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
  17. cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
  18. // 在GPU上启动add()内核
  19. add<<<1,1>>>(d_a, d_b, d_c);
  20. // 将结果复制回主机
  21. cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);
  22. // 清理
  23. cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
  24. return 0;
  25. }

test.go:

  1. package main
  2. import "fmt"
  3. //#cgo CFLAGS: -I.
  4. //#cgo LDFLAGS: -L. -ltest
  5. //#cgo LDFLAGS: -lcudart
  6. //#include <test.h>
  7. import "C"
  8. func main() {
  9. fmt.Printf("调用cuda库...\n")
  10. fmt.Println("完成 ", C.test_add())
  11. }

我使用以下命令编译CUDA代码:

  1. nvcc -m64 -arch=sm_20 -o libtest.so --shared -Xcompiler -fPIC test.cu

test.h、test.cu和test.go这三个文件都在同一个目录中。
当我尝试使用go构建时,我得到的错误是"undefined reference to `test_add'"。

我对C/C++几乎没有经验,对CUDA完全是个新手。

我已经尝试解决这个问题两天了,非常感谢任何帮助。

谢谢。

英文:

I am trying to call a CUDA function from my Go code.
I have the following three files.

test.h:

  1. int test_add(void);

test.cu:

  1. __global__ void add(int *a, int *b, int *c){
  2. *c = *a + *b;
  3. }
  4. int test_add(void) {
  5. int a, b, c; // host copies of a, b, c
  6. int *d_a, *d_b, *d_c; // device copies of a, b, c
  7. int size = sizeof(int);
  8. // Allocate space for device copies of a, b, c
  9. cudaMalloc((void **)&d_a, size);
  10. cudaMalloc((void **)&d_b, size);
  11. cudaMalloc((void **)&d_c, size);
  12. // Setup input values
  13. a = 2;
  14. b = 7;
  15. // Copy inputs to device
  16. cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
  17. cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
  18. // Launch add() kernel on GPU
  19. add<<<1,1>>>(d_a, d_b, d_c);
  20. // Copy result back to host
  21. cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);
  22. // Cleanup
  23. cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
  24. return 0;
  25. }

test.go:

  1. package main
  2. import "fmt"
  3. //#cgo CFLAGS: -I.
  4. //#cgo LDFLAGS: -L. -ltest
  5. //#cgo LDFLAGS: -lcudart
  6. //#include <test.h>
  7. import "C"
  8. func main() {
  9. fmt.Printf("Invoking cuda library...\n")
  10. fmt.Println("Done ", C.test_add())
  11. }

I am compiling CUDA code with:

  1. nvcc -m64 -arch=sm_20 -o libtest.so --shared -Xcompiler -fPIC test.cu

All three files - test.h, test.cu and test.go are in the same directory.
The error I am getting when I try to build with go is "undefined reference to `test_add'".

I have very little experience with C/C++ and am a total novice in CUDA.

I've been trying to solve my problem for two days now and would be
very grateful for any input.

Thanks.

答案1

得分: 5

在这种情况下,似乎go导入C时期望函数提供C风格链接

CUDA(即nvcc)主要遵循C++模式,并默认提供C++风格的链接(包括函数名重载等)。

可以使用extern "C" {...code...}强制将代码段提供给外部,而不是使用C++风格的链接。这是C++语言的特性,与CUDA或nvcc无关。

因此,通过对test.cu进行以下修改,可以解决该问题:

  1. extern "C" { int test_add(void) { ... code ... }; }
英文:

It appears, at least in this case, that the go import of C is expecting the function to be provided with C style linkage.

CUDA (i.e. nvcc) mainly follows C++ patterns and provides by default C++ style linkage (including function name mangling, etc.)

It's possible to force a section of code to be provided externally using C rather than C++ style linkage using extern "C" {...code...}. This is a C++ language feature and not specific to CUDA or nvcc.

Therefore it appears the problem can be solved via the following modification to the test.cu:

  1. extern "C" { int test_add(void) { ... code ... }; }

huangapple
  • 本文由 发表于 2016年3月3日 05:47:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/35758750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定