问题描述
我想编译一个 ispc 程序。我正在尝试为其示例程序之一生成可执行文件。
#include <stdio.h>
#include <stdlib.h>
// Include the header file that the ispc compiler generates
#include "simple_ispc.h"
using namespace ispc;
int main() {
float vin[16],vout[16];
// Initialize input buffer
for (int i = 0; i < 16; ++i)
vin[i] = (float)i;
// Call simple() function from simple.ispc file
simple(vin,vout,16);
// Print results
for (int i = 0; i < 16; ++i)
printf("%d: simple(%f) = %f\n",i,vin[i],vout[i]);
return 0;
}
export void simple(uniform float vin[],uniform float vout[],uniform int count) {
foreach (index = 0 ... count) {
// Load the appropriate input value for this program instance.
float v = vin[index];
// Do an arbitrary little computation,but at least make the
// computation dependent on the value being processed
if (v < 3.)
v = v * v;
else
v = sqrt(v);
// And write the result to the output array.
vout[index] = v;
}
}
我可以使用 cmake https://github.com/ispc/ispc/tree/main/examples/cpu/simple 来获取可执行文件,但我想知道运行 simple.cpp 文件需要执行的原始命令。谁能告诉我如何用ispc编译和运行simple.cpp文件?
解决方法
根据 the ISPC User's guide,您可以在终端中使用 ispc
作为命令:
ispc simple.ispc -o simple.o
这会生成一个目标文件 simple.o
,您可以使用常规的 C++ 编译器(如 g++)将其链接到您的 simple.cpp
文件。
编辑:
编译为simple_ispc.h
:
-h 标志也可以用来指示 ispc 生成 C/C++ 头文件 包含 C 可调用 ispc 函数的 C/C++ 声明的文件 以及传递给它的类型。
所以你可以做类似的事情
ispc simple.ispc -h simple_ispc.h
然后
g++ simple.cpp -o executable
获取可执行文件。
,首先,使用ispc编译器创建ispc头文件和目标文件
ispc --target=avx2-i32x8 simple.ispc -h simple_ispc.h -o simple_ispc.o
然后创建cpp的目标文件并将2个目标文件链接在一起
g++ -c simple.cpp -o simple.o
g++ simple.o simple_ispc.o -o executable
或者在创建 ispc 头文件和 obj 文件后在一个命令中创建可执行文件
g++ simple.cpp simple_ispc.o -o executable
此外,如果您有 clang/llvm,您也可以使用它们进行编译。以下是步骤:https://ispc.github.io/faq.html#is-it-possible-to-inline-ispc-functions-in-c-c-code
// emit llvm IR
ispc --emit-llvm --target=avx2-i32x8 -h simple_ispc.h -o simple_ispc.bc simple.ispc
clang -O2 -c -emit-llvm -o simple.bc simple.cpp
// link the two IR files into a single file and run the LLVM optimizer on the result
llvm-link simple.bc simple_ispc.bc -o - | opt -O3 -o simple_opt.bc
// generate the native object file
llc -filetype=obj simple_opt.bc -o simple.o
// generate the executable
clang -o simple simple.o