dongzhuo1733 2016-03-02 21:47
浏览 390
已采纳

Golang调用CUDA库

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

test.h:

int test_add(void);

test.cu:

__global__ void add(int *a, int *b, int *c){
       *c = *a + *b;
}

int test_add(void) {
       int a, b, c; // host copies of a, b, c
       int *d_a, *d_b, *d_c; // device copies of a, b, c
       int size = sizeof(int);
       // Allocate space for device copies of a, b, c
       cudaMalloc((void **)&d_a, size);
       cudaMalloc((void **)&d_b, size);
       cudaMalloc((void **)&d_c, size);
      // Setup input values
      a = 2;
      b = 7;

      // Copy inputs to device
      cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
      cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
      // Launch add() kernel on GPU
     add<<<1,1>>>(d_a, d_b, d_c);
     // Copy result back to host
     cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);
     // Cleanup
     cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
    return 0;
}

test.go:

package main

import "fmt"

//#cgo CFLAGS: -I.
//#cgo LDFLAGS: -L. -ltest
//#cgo LDFLAGS: -lcudart
//#include <test.h>
import "C"


func main() {
     fmt.Printf("Invoking cuda library...
")
     fmt.Println("Done ", C.test_add())
}

I am compiling CUDA code with:

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条回答 默认 最新

  • duanpai6581 2016-03-03 04:21
    关注

    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:

    extern "C" { int test_add(void) { ... code ... }; }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效