c++ispc

How can I compile ISPC code in Linux and link it with normal cpp file?


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?


Solution

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