Implement L1 normalization of a 1D vector on the GPU. For each element of the input array, compute:
output[i]=∑j=0N−1∣input[j]∣input[i]The kernel must first perform a reduction over ∣input∣ to compute the denominator, then divide each element by that sum. The output array has the same length as the input.
Input: input = [1, 2, 3, 4]
Output: [0.1, 0.2, 0.3, 0.4]
The sum of absolute values is 10, so each element is divided by 10.
Input: input = [-1, 2, -3]
Output: [-0.1667, 0.3333, -0.5]
The sum of absolute values is 6, so each element is divided by 6 while keeping its sign.
Compute the denominator in a separate reduction kernel before the divide pass. A shared-memory tree reduction followed by atomicAdd into a single device float is a clean pattern.
fabsf gives the per-element absolute value. Zero the scratch sum with cudaMemset before launching the reduction so the atomics accumulate from a clean baseline.
Once the sum is on the device, a second elementwise kernel can read the scratch scalar and write input[i] / sum into output[i].
Accepts: array
Submit a correct solution to see your kernel performance.
Implement L1 normalization of a 1D vector on the GPU. For each element of the input array, compute:
output[i]=∑j=0N−1∣input[j]∣input[i]The kernel must first perform a reduction over ∣input∣ to compute the denominator, then divide each element by that sum. The output array has the same length as the input.
Input: input = [1, 2, 3, 4]
Output: [0.1, 0.2, 0.3, 0.4]
The sum of absolute values is 10, so each element is divided by 10.
Input: input = [-1, 2, -3]
Output: [-0.1667, 0.3333, -0.5]
The sum of absolute values is 6, so each element is divided by 6 while keeping its sign.
Compute the denominator in a separate reduction kernel before the divide pass. A shared-memory tree reduction followed by atomicAdd into a single device float is a clean pattern.
fabsf gives the per-element absolute value. Zero the scratch sum with cudaMemset before launching the reduction so the atomics accumulate from a clean baseline.
Once the sum is on the device, a second elementwise kernel can read the scratch scalar and write input[i] / sum into output[i].
Accepts: array
Submit a correct solution to see your kernel performance.