Reduce contiguous segments of a float array. You are given values of length n and a flags array of the same length. flags[i] = 1 marks the start of a new segment (flags[0] is always ); flags[i] = 0 continues the current segment. Write one sum per segment into output, packed from the left, in left-to-right order.
Example of the contract: values = [1, 1, 1], flags = [1, 0, 1] produces [2, 1].
The whole array fits in one block (n <= 1024). Implement a parallel segmented scan / reduce in shared memory, not a sequential walk on one thread.
Complete:
__global__ kernel that computes the packed segment sums.void solve(const float* values, const int* flags, float* output, int n) where every pointer is a host pointer. output has length n (only the first entries are defined, where is the number of ones in flags). Allocate, copy in, launch, copy the packed sums back, and free.Constraints: 1 <= n <= 1024, flags[0] == 1, and every flag is or . Do not write a main() in your solution.