unaryFunc

wrapper of CUDA kernel unary functions

void
unaryFunc
(
size_t dim
)
(
Variable!(float, dim, DeviceStorage) x
)

Examples

test neg kernel

import numir;
import grain.kernel;

auto x = [[1f, 2f, 3f], [4f, 5f, 6f]].variable.to!DeviceStorage;
unaryFunc!neg(x);
assert(x.to!HostStorage.sliced == -[[1f, 2f, 3f], [4f, 5f, 6f]].nparray);

test reciprocal kernel

import grain.kernel;

auto x = [[1f, 2f, 3f], [4f, 5f, 6f]].variable.to!DeviceStorage;
unaryFunc!reciprocal(x);
assert(x.to!HostStorage.sliced == [[1f, 1f / 2f, 1f / 3f], [1f / 4f, 1f / 5f, 1f / 6f]]);

test math function kernels. these functions are available in mir.math (as LDC intrinsic) or CUDA fast math

See_also: - http://mir.dlang.io/mir_math_common.html - https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#intrinsic-functions

// FIXME mir.math.log will exit 1
import std.math : log, tan; // , log2, log10, exp, exp2, cos, sin, tan;
import mir.math : log2, log10, exp, exp2, cos, sin;
import grain.kernel : log, log2, log10, exp, exp2, cos, sin, tan;
import std.format : format;
import numir : approxEqual;
import mir.ndslice : iota, as, slice, map;

static foreach (name; ["log", "log2", "log10", "exp", "exp2", "cos", "sin", "tan"]) {
    {
        auto x = iota([2, 3], 1).as!float
            .slice
            .variable
            .to!DeviceStorage;
        mixin(format!q{  unaryFunc!(grain.kernel.%s)(x);  }(name));
        mixin(format!q{  alias func = %s;  }(name));
        assert(approxEqual(x.to!HostStorage.sliced, iota([2, 3], 1).as!float
                .map!func));
    }
}

Meta