简体   繁体   English

如何编译 Linux 中的 ISPC 代码并将其与普通 cpp 文件链接?

[英]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.

相关问题 如何在没有 WSL 或任何虚拟 linux 和 clang 的情况下将 a.cpp 文件编译为 ELF 二进制文件? - How can I compile a .cpp file to ELF binary in windows WITHOUT WSL or any virtual linux, with clang? Rust中如何编译链接.cpp文件? - How to compile and link .cpp file in Rust? wmware编译cpp文件linux - wmware compile cpp file linux 如何在Linux中编译后以这样的方式编译c / cpp代码以提供经过预处理的汇编文件和目标文件? - how to compile c/cpp code in such a way that it gives preprocessed assembled and object file after compilation in linux? 如何在 Linux 中将 .cpp 文件编译为 Windows 可执行 (.exe) 文件 - How to compile a .cpp file into a Windows executable (.exe) file in Linux 如何从 Windows 为 Linux (Dev-Cpp) 编译 C++ 代码 - How to compile C ++ code from Windows for Linux (Dev-Cpp) ISPC-我可以将CPU线程数限制为1吗? - ISPC - can I limit the number of CPU threads to 1? 如何在#cc文件中使#include-file的内容成为编译时常量? - How can I make the contents of an #include-file a compile-time constant in a cpp-file? 如何使用 Visual Studio 代码编译多 cpp 文件? - How to use visual studio code to compile multi-cpp file? 如何在使用模板时将cpp代码编译成库文件 - how to compile cpp code into library file while using template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM