我的目标是拥有一个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;
}
帕斯卡主文件:
// 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对象文件并将其链接到free pascal中,方法如下:
$ nvcc progcuda.cu -c -o progcuda.o
然后 $fpc progpas.pas
但是链接失败了。
注:我曾经尝试过将由C代码生成的普通.o文件链接到Pascal代码中,使用gcc和freepascal编译器,它可以工作,但是如果我使用nvcc而不是gcc,并将扩展名改为.cu(代码仍然相同),则链接失败。
注:我是Stack Overflow上的新用户,还不能回答问题。