是否可以将具有nvcc的GPU的代码(C ++)编译为共享对象(.so文件),然后从C ++程序(在本例中为Cern的ROOT,它实际上是C ++解释器(“ CINT”))动态加载代码)。
我要运行的一个简单示例是:
extern "C"
void TestCompiled() {
printf("test\n");
exit(0);
}
此代码使用编译nvcc --compiler-options '-fPIC' -o TestCompiled_C.so --shared TestCompiled.cu
。使用以下命令将共享对象加载到ROOT中:
{ // Test.C program
int error, check;
check = gROOT->LoadMacro("TestCompiled_C.so", &error);
cout << "check " << check << " " << " error: " << error << endl;
TestCompiled(); // run macro
exit(0);
}
可以加载库,但是找不到TestCompiled()
:
$ root -b -l Test.C
root [0]
Processing Test.C...
check 0 error: 0
Error: Function Hello() is not defined in current scope Test.C:11:
*** Interpreter error recovered ***
通过使用ROOT编译第一个测试脚本来完成相同的工作(无代码extern
行,使用编译root TestCompiled.C++
)……当nvcc进行编译时,我可以尝试使C ++程序找到测试功能吗?
我正在复制RootTalk论坛中解决问题的答案的要点,以供参考:
关键是,ROOT(CINT)的C解释器需要外部编译函数的“ CINT词典”。(通过ROOT编译时没有问题,因为ACLiC在预编译宏[ root TestCompiled.C++
]时会创建此字典)。
因此,TestCompiled.h++
必须创建一个接口:
#ifdef __cplusplus
extern "C" {
#endif
void TestCompiled(void);
#ifdef __cplusplus
} /* end of extern "C" */
#endif
然后必须将接口与共享库一起装入ROOT内部:
{ // Test.C ROOT/CINT unnamed macro (interpreted)
Int_t check, error;
check = gROOT->LoadMacro("TestCompiled_C.so", &error);
std::cout << "_C.so check " << check << " error " << error << std::endl;
check = gROOT->LoadMacro("TestCompiled.h++", &error);
std::cout << "_h.so check " << check << " error " << error << std::endl;
TestCompiled(); // execute the compiled function
}
现在,ROOT可以使用外部编译的程序:root -b -l -n -q Test.C
有效。
可以使用例如以下的g ++进行测试TestCompiled.C
:
#include <cstdio>
extern "C" void TestCompiled(void) { printf("test\n"); }
用...编译
g++ -fPIC -shared -o TestCompiled_C.so TestCompiled.C
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句