3

I wrote a simple program in cuda-c and it works on eclipse nsight. This is source code:

#include <iostream>
#include <stdio.h>


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

int main(void){

int c;
int *dev_c;

cudaMalloc((void**)&dev_c, sizeof(int));

add <<<1,1>>>(2,7,dev_c);

cudaMemcpy(&c, dev_c, sizeof(int),cudaMemcpyDeviceToHost);

printf("\n2+7= %d\n",c);
cudaFree(dev_c);

return 0;
}

Now I'm trying to use this code with Go language with cgo!!! So I wrote this new code:

package main

//#include "/usr/local/cuda-7.0/include/cuda.h"
//#include "/usr/local/cuda-7.0/include/cuda_runtime.h"
//#cgo LDFLAGS: -lcuda
//#cgo LDFLAGS: -lcurand
////default location:
//#cgo LDFLAGS: -L/usr/local/cuda-7.0/lib64 -L/usr/local/cuda-7.0/lib
//#cgo CFLAGS: -I/usr/local/cuda-7.0/include/
//
//
//
//
//
//
//
//
//
//
/*

#include <stdio.h>

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

int esegui_somma(void){

    int c;
    int *dev_c;

    cudaMalloc((void**)&dev_c, sizeof(int));
    add <<<1,1>>> (2,7,dev_c);
    cudaMemcpy(&c, dev_c, sizeof(int),cudaMemcpyDeviceToHost);

    cudaFree(dev_c);
    return c;
}
*/
import "C"
import "fmt"

func main(){
    fmt.Printf("il risultato รจ %d",C.esegui_somma)
}

But it doesn't work!! I read this error message:

cgo_cudabyexample_1/main.go:34:8: error: expected expression before '<' token
add <<<1,1>>> (2,7,dev_c);
      ^

I think that I must to set nvcc cuda compiler for cgo instead of gcc. How can I do it? Can I change CC environment variable? best regards

Dmytro Hutsuliak
  • 1,741
  • 4
  • 21
  • 37
  • Have you tried just using the standard CC anc CXX environment variables? (I have no idea if cgo works with cuda in this case, but it should be easy to try) โ€“ JimB Sep 15 '15 at 15:05
  • Hi, thank you for response. In /etc/profile file i added this: export PATH=$PATH:/usr/local/cuda-7.0/bin export CC=nvcc now this is errore message: nvcc fatal : Unknown option 'dM' โ€“ Marco Jammajalla Mangraviti Sep 15 '15 at 21:07
  • I wasn't sure how cuda compiler worked, but it's obviously not a standard CC replacement. I think you'll need to compile the cuda code outside of the Go program. (as an aside, you don't need to edit the global profile to set an environment variable, you don't even need to export it if you define it with the command). โ€“ JimB Sep 15 '15 at 21:14

1 Answers1

2

I finally figured out how to do this. Thing biggest problem is that nvccdoes not follow gcc standard flags and unlike clang it won't silently ignore them. cgo triggers the problem by adding a bunch of flags not explicitly specified by the user.

To make it all work, you'll need to separate out your device code and the functions that directly call it into separate files and compile/package them directly using nvcc into a shared library (.so). Then you'll use cgo to link this shared library using whatever default linker you have on your system. The only thing you'll have to add is -lcudart to your LDFLAGS (linker flags) to link the CUDA runtime.

alfalfasprout
  • 243
  • 1
  • 12