Lesson 3 — Elementwise map
An elementwise map applies a function independently to every element of an array. Because there are no dependencies between elements, this is the ideal GPU workload — every thread does the same work on a different datum.
Available math functions
Sarek exposes the standard GPU math library. Some useful functions in the
Float32 module:
Float32.sin x,Float32.cos xFloat32.sqrt x,Float32.exp x,Float32.log xFloat32.abs x,Float32.ceil x,Float32.floor x
Inline arithmetic: x *. x is faster than calling a power function for squaring.
Your task
Write a kernel that squares every element of a into
b: b[i] = a[i] * a[i].
Click "Run on my GPU" to execute your kernel.
Hint
a.(i) *. a.(i) — that is all. You can also try
Float32.sqrt (a.(i) *. a.(i)) to verify that
sqrt(x²) = |x| for non-negative inputs.