2

I want to compile an ispc program. I am trying to generate the executable for one of their example programs.

I have simple.cpp with the below content

#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

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. Can someone please tell how to compile and run the simple.cpp file with ispc?

2 Answers2

3

According to the ISPC User's guide you can just use ispc as a command in your terminal:

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++.

Edit:

To compile to 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.

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.

wvdgoot
  • 303
  • 2
  • 10
  • 1
    simple.cpp file contains `#include "simple_ispc.h"`. Can you please post the command to get the executable from simple.cpp linking header file. I am not sure how to do it. – Adhitha Dias Apr 13 '21 at 23:30
  • ```$ 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``` I get the below error when I run the 2nd command. – Adhitha Dias Apr 15 '21 at 01:05
  • 1
    g++ simple.cpp simple_ispc.o -o executable solved the issue after creating the obj and header files using the ispc compiler – Adhitha Dias Apr 20 '21 at 03:59
0

First, create the ispc header file and object file using the ispc compiler

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

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

g++ simple.cpp simple_ispc.o -o executable

Furthermore, if you have clang/llvm you can also use them for compilation. Below are the steps: 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