File: convolve.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (59 lines) | stat: -rw-r--r-- 1,919 bytes parent folder | download
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
/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <stdio.h>
#include <cstdlib>
#include <arrayfire.h>
using namespace af;

// use static variables at file scope so timeit() wrapper functions
// can reference image/kernels

// image to convolve
static array img;

// 5x5 derivative with separable kernels
static float h_dx[] = {1.f / 12, -8.f / 12, 0, 8.f / 12, -1.f / 12}; // five point stencil
static float h_spread[] = {1.f / 5, 1.f / 5, 1.f / 5, 1.f / 5, 1.f / 5};
static array dx, spread, kernel; // device kernels

static array full_out, dsep_out, hsep_out; // save output for value checks
// wrapper functions for timeit() below
static void full() { full_out = convolve2(img, kernel);}
static void dsep() { dsep_out = convolve(dx, spread, img); }

static bool fail(array &left, array &right)
{
    return (max<float>(abs(left - right)) > 1e-6);
}

int main(int argc, char **argv)
{
    try {
        int device = argc > 1 ? atoi(argv[1]) : 0;
        af::setDevice(device);
        af::info();

        // setup image and device copies of kernels
        img = randu(640, 480);
        dx = array(5, 1, h_dx); // 5x1 kernel
        spread = array(1, 5, h_spread); // 1x5 kernel
        kernel = matmul(dx, spread); // 5x5 kernel

        printf("full 2D convolution:         %.5f seconds\n", timeit(full));
        printf("separable, device pointers:  %.5f seconds\n", timeit(dsep));

        // ensure values are all the same across versions
        if (fail(full_out, dsep_out)) { throw af::exception("full != dsep"); }
    } catch (af::exception& e) {
        fprintf(stderr, "%s\n", e.what());
    }

    return 0;
}