unaryFunc

wrapper of CUDA kernel unary functions

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

Examples

test neg kernel

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

test reciprocal kernel

1 import grain.kernel;
2 
3 auto x = [[1f, 2f, 3f], [4f, 5f, 6f]].variable.to!DeviceStorage;
4 unaryFunc!reciprocal(x);
5 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

1 // FIXME mir.math.log will exit 1
2 import std.math : log, tan; // , log2, log10, exp, exp2, cos, sin, tan;
3 import mir.math : log2, log10, exp, exp2, cos, sin;
4 import grain.kernel : log, log2, log10, exp, exp2, cos, sin, tan;
5 import std.format : format;
6 import numir : approxEqual;
7 import mir.ndslice : iota, as, slice, map;
8 
9 static foreach (name; ["log", "log2", "log10", "exp", "exp2", "cos", "sin", "tan"]) {
10     {
11         auto x = iota([2, 3], 1).as!float
12             .slice
13             .variable
14             .to!DeviceStorage;
15         mixin(format!q{  unaryFunc!(grain.kernel.%s)(x);  }(name));
16         mixin(format!q{  alias func = %s;  }(name));
17         assert(approxEqual(x.to!HostStorage.sliced, iota([2, 3], 1).as!float
18                 .map!func));
19     }
20 }

Meta