Go kodumdan bir CUDA işlevini çağırmaya çalışıyorum. Aşağıdaki üç dosyam var.Golang CUDA kitaplığını çağırıyor
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...\n")
fmt.Println("Done ", C.test_add())
}
Birlikte CUDA kodu derleme am:
nvcc -m64 -arch=sm_20 -o libtest.so --shared -Xcompiler -fPIC test.cu
Tüm üç dosya - test.h, test.cu ve test.go aynı dizinde. Gitmeye çalıştığımda aldığım hata "test_add" için tanımlanmamış başvuru "dır.
C/C++ ile çok az deneyimim var ve CUDA'da yeni bir acemiyim.
Sorunumu iki gün boyunca çözmeye çalışıyorum ve herhangi bir girdi için çok minnettar olacağım.
Teşekkürler.
Ben halindeyken aşina değilim, ama bir C vs C++ bağlama sorun olabilir:
nedenle sorunun test.cu için aşağıdaki modifikasyon yoluyla çözülebilir görünüyor. "Extern" C'deki "test_add()" prototipini sarmalamayı deneyin. {"} ... –
@RobertCrovella: C kodunu nerede görüyorsunuz? CUDA C++ tabanlıdır. – Olaf
@Olaf, "C kodunu görüyorum" dedim. ? CUDA'nın C++ stili bağlantı kullandığını biliyorum. Eğer tesadüfen [gidelim C 'nin ithalatı, C stili bağlantı ile sağlanacak işlevi beklemektedir] (https://golang.org/cmd/cgo/#hdr-Go_references_to_C), bu problemle karşılaşırdınız. Bu sadece bir tahmin. –