I have the following code for a sparse matrix times a vector. In the sparse matrix, the number of nonzero elements per row is stored, alongside the index in the array of elements at which the first element of that row occurs. These are used to ENSURE that no element of the output vector is written two by multiple threads at once. This code works fine when building without optimisations or when building in debug mode. However when I said the optimisation level to -Ofast, I get a double free error.
Any ideas on what I might have missed?
func parallelVectorMultiply(vector: Vector<T>) -> Vector<T> {
guard space == vector.space else {
fatalError("Number of columns in the matrix must match the size of the vector.")
}
var outputElements = [T](repeating: T(0), count: space.dimension)
DispatchQueue.concurrentPerform(iterations: space.dimension) { row in
let start_idx = row_first_element_offsets[row]
if start_idx == -1 { return }
let end_idx = start_idx + nonzero_elements_per_row[row]
var rowSum = T(0)
for i in start_idx..<end_idx {
rowSum = rowSum + values[i].value * vector[values[i].col]
}
outputElements[row] = rowSum
}
return Vector(elements: outputElements, in: space)
}
I expect no error. In the equation for matrix $$A$$ times vector $$X$$,
$$AX = Y$$,br>
$$Y_i = sum_k A_ik X_k$$
In my code, I compute this sum then store the result to the corresponding element in the output array.
There should be no two threads that free up a single element of the array concurrently.