【问题标题】:How can I compile ISPC code in Linux and link it with normal cpp file?如何在 Linux 中编译 ISPC 代码并将其与普通 cpp 文件链接?
【发布时间】:2021-04-13 18:45:13
【问题描述】:

我想编译一个 ispc 程序。我正在尝试为他们的示例程序之一生成可执行文件。

我有以下内容的 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;
}

我有以下内容的 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;
    }
}

我可以使用 cmake https://github.com/ispc/ispc/tree/main/examples/cpu/simple 来获取可执行文件,但我想知道运行 simple.cpp 文件需要执行的原始命令。有人能告诉我如何用 ispc 编译和运行 simple.cpp 文件吗?

【问题讨论】:

    标签: c++ ispc


    【解决方案1】:

    根据the ISPC User's guide,您可以在终端中使用ispc 作为命令:

    ispc simple.ispc -o simple.o
    

    这会生成一个目标文件 simple.o,您可以使用 g++ 等常规 C++ 编译器链接到您的 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
    

    获取可执行文件。

    【讨论】:

    • simple.cpp 文件包含#include "simple_ispc.h"。您能否发布命令以从 simple.cpp 链接头文件中获取可执行文件。我不知道该怎么做。
    • $ g++ simple.cpp -o executable /usr/bin/ld: /tmp/ccg6KMuX.o: in function `main': simple.cpp:(.text+0x6a): undefined reference to `simple' collect2: error: ld returned 1 exit status 运行第二个命令时出现以下错误。
    • g++ simple.cpp simple_ispc.o -o 可执行文件解决了使用ispc编译器创建obj和头文件后的问题
    【解决方案2】:

    首先,使用ispc编译器创建ispc头文件和目标文件

    ispc --target=avx2-i32x8 simple.ispc -h simple_ispc.h -o simple_ispc.o
    

    然后创建cpp的object文件并将2个object文件链接在一起

    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
    

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多