我的目标是拥有一个Delphi(或freepascal)代码,它将像这样调用C函数 func :
C / Cuda档案:
/* this is the "progcuda.cu" file */
#include <stdio.h>
__global__ void foo(int *a, int *b, int *c, int n){
/*
add all the vector's element
*/
}
void func(int *a, int *b, int *c,int n){
int *da,*db,*dc;
cudaMalloc(&da, n*sizeof(int));
cudaMalloc(&db, n*sizeof(int));
cudaMalloc(&dc, n*sizeof(int));
cudaMemcpy(da,a,sizeof(int)*n,cudaMemcpyHostToDevice);
cudaMemcpy(db,b,sizeof(int)*n,cudaMemcpyHostToDevice);
cudaMemcpy(dc,c,sizeof(int)*n,cudaMemcpyHostToDevice);
foo<<<1,256>>>(da,db,dc);
cudaMemcpy(c,dc,sizeof(int),cudaMemcpyDeviceToHost);
/* do other stuff and call another Host and Device functions*/
return;
}
pascal主文件:
// this is the "progpas.pas" file
program progpas;
{$mode objfpc}{$H+}
uses unitpas;
var
...
begin
...
func(a, b, c, len);
...
end.
pascal单元文件:
// this is the "unitpas.pas" file
unit unitpas;
{$link progcuda.o}
interface
uses ctypes;
procedure func(a, b, c : cpint32 , n:cint32); cdecl; external;
procedure foo(a, b, c : cpint32 , n:cint32);cdecl; external;
implementation
end.
我发现这篇文章Programming CUDA using Delphi or FreePascal ,但它显示了一种在delphi中编写CUDA的方法。
我不想在Delphi中编写CUDA,我想用纯C / C ++代码在CUDA中编程,只在delphi中调用那个C函数。
有什么问题? 如何将.cu代码链接到delphi?
我正在使用linux ubuntu 16.04 LTS,但如果需要,我也会在Windows中使用CUDA和VS.
注意:如果你们可以详细解释如何去做,会有所帮助(对pascal和链接文件不熟悉)
我已经尝试生成.o对象文件,并将其与免费pascal链接
$ nvcc progcuda.cu -c -o progcuda.o
然后$fpc progpas.pas
但它没有链接。
注意:我曾经尝试过将C代码生成的普通.o链接到pascal代码,使用gcc和freepascal编译器,但是如果我使用nvcc代替gcc并将扩展名重命名为.cu(仍然是相同的代码),链接失败。
注意:堆栈溢出的新帐户,我无法回复答案。
答案 0 :(得分:5)
我对Delphi和FreePascal一无所知,但我确实知道CUDA,C和C ++,所以也许我的解决方案也适合你。
我将通过一个简单的问题展示它:
f.cu
的内容:
int f() { return 42; }
main.c
的内容:
extern int f();
int main() {
return f();
}
以下作品:
$ gcc -c -xc f.cu # need -xc to tell gcc it's a C file
$ gcc main.c f.o
(no errors emitted)
现在,我们尝试将gcc
替换为nvcc
:
$ nvcc -c f.cu
$ gcc main.c f.o
/tmp/ccI3tBM1.o: In function `main':
main.c:(.text+0xa): undefined reference to `f'
f.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_0000704e_00000000-5_f.cudafe1.cpp:(.text+0x52): undefined reference to `__cudaUnregisterFatBinary'
f.o: In function `__nv_init_managed_rt_with_module(void**)':
tmpxft_0000704e_00000000-5_f.cudafe1.cpp:(.text+0x6d): undefined reference to `__cudaInitModule'
f.o: In function `__sti____cudaRegisterAll()':
tmpxft_0000704e_00000000-5_f.cudafe1.cpp:(.text+0xa9): undefined reference to `__cudaRegisterFatBinary'
collect2: error: ld returned 1 exit status
这里的问题是nvcc
在编译f.cu
时添加了对CUDA运行时API中某些符号的引用,并且这些符号必须链接到最终的可执行文件。我的CUDA安装在/opt/cuda
,因此我将使用它,但您必须将其替换为系统上安装CUDA的位置。因此,如果我们在编译库时链接libcudart.so
:
$ nvcc -c f.cu
$ gcc main.c f.o -L/opt/cuda/lib64 -lcudart
/tmp/ccUeDZcb.o: In function `main':
main.c:(.text+0xa): undefined reference to `f'
collect2: error: ld returned 1 exit status
这看起来更好,没有奇怪的错误,但它仍然没有找到函数f
。那是因为nvcc
将f.cu
视为C ++文件,所以它在创建目标文件时确实名称错误,我们必须指定我们希望f
拥有C ,而不是C ++链接(请参阅此处:http://en.cppreference.com/w/cpp/language/language_linkage)。
为此,我们必须像这样修改f.cu
:
extern "C" int f() { return 42; }
现在我们这样做:
$ nvcc -c f.cu
$ gcc main.c f.o -L/opt/cuda/lib64 -lcudart
(no errors emitted)
我希望你设法修改它以使用你的语言。
编辑:我尝试了一些更复杂的例子:// f.cu
#include <stdio.h>
__global__ void kernel() {
printf("Running kernel\n");
}
extern "C" void f() {
kernel<<<1, 1>>>();
// make sure the kernel completes before exiting
cudaDeviceSynchronize();
}
// main.c
extern void f();
int main() {
f();
return 0;
}
编译时我得到了:
f.o:(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
要修复它,您还需要将标准C ++库添加到链接器标志:
$ nvcc -c f.cu
$ gcc main.c f.o -L/opt/cuda/lib64 -lcudart -lstdc++
$ ./a.out
Running kernel
答案 1 :(得分:3)
我修复了文件@Goran Flegar解释说:
将extern "C" int func(...);
添加到.cu文件中。然后尝试编译/链接.cu代码,但没有设备调用(但设备代码),并且一切运行良好。
但是当我添加一个设备调用(foo<<<Nb,Nt>>>(...)
)并编译时使用:
$nvcc progcuda.cu -c
$fpc progpas.pas -ofinal.exe -Fl/usr/local/cuda/lib64
我得到:
Free Pascal Compiler version 3.0.4 [2017/12/13] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling prog1.pas
Linking sum.exe
/usr/bin/ld: aviso: link.res contém seções de saída; você se esqueceu -T?
/usr/bin/ld: sum.o: undefined reference to symbol '_Unwind_Resume@@GCC_3.0'
//lib/x86_64-linux-gnu/libgcc_s.so.1: error adding symbols: DSO missing from command line
prog1.pas(16,1) Error: Error while linking
prog1.pas(16,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
所以还有一些缺少的库。
解决方案:
发现将stdc ++和gcc_s lib链接到pascal解决了编译问题。
unit unitpas;
// file "unitpas.pas"
{$LINK progcuda.o}
{$LINKLIB c}
{$LINKLIB cudart}
{$linklib stdc++}
{$linklib gcc_s}
interface
uses ctypes;
function func(x,y: cint32): cint32; cdecl; external;
implementation
end.
运行
$nvcc progcuda.cu -c
$fpc progpas.pas -ofinal.exe -Fl/usr/local/cuda/lib64
一切正常。