File: c_function.cpp

package info (click to toggle)
halide 21.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,752 kB
  • sloc: cpp: 289,334; ansic: 22,751; python: 7,486; makefile: 4,299; sh: 2,508; java: 1,549; javascript: 282; pascal: 207; xml: 127; asm: 9
file content (101 lines) | stat: -rw-r--r-- 2,931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

// NB: You must compile with -rdynamic for llvm to be able to find the appropriate symbols
// This is not supported by the C backend.

// On windows, you need to use declspec to do the same.
int call_counter = 0;
extern "C" HALIDE_EXPORT_SYMBOL float my_func(int x, float y) {
    call_counter++;
    return x * y;
}
HalideExtern_2(float, my_func, int, float);

int call_counter2 = 0;
extern "C" HALIDE_EXPORT_SYMBOL float my_func2(int x, float y) {
    call_counter2++;
    return x * y;
}

int call_counter3 = 0;
extern "C" HALIDE_EXPORT_SYMBOL float my_func3(int x, float y) {
    call_counter3++;
    return x * y;
}

int main(int argc, char **argv) {
    Var x, y;
    Func f;

    f(x, y) = my_func(x, cast<float>(y));

    Buffer<float> imf = f.realize({32, 32});

    // Check the result was what we expected
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < 32; j++) {
            float correct = (float)(i * j);
            float delta = imf(i, j) - correct;
            if (delta < -0.001 || delta > 0.001) {
                printf("imf[%d, %d] = %f instead of %f\n", i, j, imf(i, j), correct);
                return 1;
            }
        }
    }

    if (call_counter != 32 * 32) {
        printf("C function my_func was called %d times instead of %d\n", call_counter, 32 * 32);
        return 1;
    }

    Func g;
    g(x, y) = my_func(x, cast<float>(y));

    Pipeline p(g);
    p.set_jit_externs({{"my_func", JITExtern{my_func2}}});
    Buffer<float> imf2 = p.realize({32, 32});

    // Check the result was what we expected
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < 32; j++) {
            float correct = (float)(i * j);
            float delta = imf2(i, j) - correct;
            if (delta < -0.001 || delta > 0.001) {
                printf("imf2[%d, %d] = %f instead of %f\n", i, j, imf2(i, j), correct);
                return 1;
            }
        }
    }

    if (call_counter2 != 32 * 32) {
        printf("C function my_func2 was called %d times instead of %d\n", call_counter2, 32 * 32);
        return 1;
    }

    // Switch from my_func2 to my_func and verify a recompile happens.
    p.set_jit_externs({{"my_func", JITExtern{my_func3}}});
    Buffer<float> imf3 = p.realize({32, 32});

    // Check the result was what we expected
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < 32; j++) {
            float correct = (float)(i * j);
            float delta = imf3(i, j) - correct;
            if (delta < -0.001 || delta > 0.001) {
                printf("imf3[%d, %d] = %f instead of %f\n", i, j, imf3(i, j), correct);
                return 1;
            }
        }
    }

    if (call_counter3 != 32 * 32) {
        printf("C function my_func3 was called %d times instead of %d\n", call_counter3, 32 * 32);
        return 1;
    }

    printf("Success!\n");
    return 0;
}