File: extern_sort.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 (48 lines) | stat: -rw-r--r-- 1,296 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
#include "Halide.h"
#include <algorithm>
#include <stdio.h>

using namespace Halide;

// Use an extern stage to do a sort
extern "C" HALIDE_EXPORT_SYMBOL int sort_buffer(halide_buffer_t *in, halide_buffer_t *out) {
    if (in->is_bounds_query()) {
        in->dim[0].min = out->dim[0].min;
        in->dim[0].extent = out->dim[0].extent;
    } else {
        memcpy(out->host, in->host, out->dim[0].extent * out->type.bytes());
        float *out_start = (float *)out->host;
        float *out_end = out_start + out->dim[0].extent;
        std::sort(out_start, out_end);
        out->set_host_dirty();
    }
    return 0;
}

int main(int argc, char **argv) {
    Func data;
    Var x;
    data(x) = sin(x);
    data.compute_root();

    Func sorted;
    std::vector<ExternFuncArgument> args;
    args.push_back(data);
    sorted.define_extern("sort_buffer", args, Float(32), 1);
    Buffer<float> output = sorted.realize({100});

    // Check the output
    Buffer<float> reference = lambda(x, sin(x)).realize({100});
    std::sort(&reference(0), &reference(100));

    RDom r(reference);
    float error = evaluate_may_gpu<float>(sum(abs(reference(r) - output(r))));

    if (error != 0) {
        printf("Output incorrect\n");
        return 1;
    }

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