[英]How can I compile ISPC code in Linux and link it with normal cpp file?
I want to compile an ispc program.我想编译一个ispc程序。 I am trying to generate the executable for one of their example programs.
我正在尝试为他们的示例程序之一生成可执行文件。
I have simple.cpp with the below content我有以下内容的 simple.cpp
#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;
}
I have simple.ispc with the below content我有以下内容的 simple.ispc
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;
}
}
I can use cmake https://github.com/ispc/ispc/tree/main/examples/cpu/simple to get the executable but I want to know the raw commands that I need to execute to run simple.cpp file.我可以使用 cmake https://github.com/ispc/ispc/tree/main/examples/cpu/simple来获取可执行文件,但我想知道运行 simple.cpp 文件需要执行的原始命令。 Can someone please tell how to compile and run the simple.cpp file with ispc?
有人可以告诉我如何用 ispc 编译和运行 simple.cpp 文件吗?
According to the ISPC User's guide you can just use ispc
as a command in your terminal:根据ISPC 用户指南,您可以在终端中使用
ispc
作为命令:
ispc simple.ispc -o simple.o
This generates an object file simple.o
that you can link to your simple.cpp
file with a regular C++ compiler like g++.这会生成一个 object 文件
simple.o
,您可以使用常规 C++ 编译器(如 g++)链接到您的simple.cpp
文件。
Edit :编辑:
To compile to simple_ispc.h
:编译为
simple_ispc.h
:
The -h flag can also be used to direct ispc to generate a C/C++ header file that includes C/C++ declarations of the C-callable ispc functions and the types passed to it.
-h 标志还可用于指示 ispc 生成 C/C++ header 文件,其中包括 C 可调用 ispc 函数的 C/C++ 声明以及传递给它的类型。
So you can probably do something like所以你可以做类似的事情
ispc simple.ispc -h simple_ispc.h
and then接着
g++ simple.cpp -o executable
to get the executable.获取可执行文件。
First, create the ispc header file and object file using the ispc compiler首先,使用ispc编译器创建ispc header文件和object文件
ispc --target=avx2-i32x8 simple.ispc -h simple_ispc.h -o simple_ispc.o
Then create the object file of the cpp and link the 2 object files together然后创建cpp的object文件,将2个object文件链接在一起
g++ -c simple.cpp -o simple.o
g++ simple.o simple_ispc.o -o executable
Or do the executable creation in one command after ispc header and obj files are created或者在创建 ispc header 和 obj 文件后在一个命令中创建可执行文件
g++ simple.cpp simple_ispc.o -o executable
Furthermore, if you have clang/llvm you can also use them for compilation.此外,如果你有 clang/llvm,你也可以使用它们进行编译。 Below are the steps: https://ispc.github.io/faq.html#is-it-possible-to-inline-ispc-functions-in-cc-code
以下是步骤: https://ispc.github.io/faq.html#is-it-possible-to-inline-ispc-functions-in-cc-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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.