SUBROUTINE flush_stacks(this, left, right, purge)
TYPE(dbcsr_mm_csr_type), INTENT(INOUT) :: this
TYPE(dbcsr_type), INTENT(IN) :: left, right
LOGICAL, INTENT(IN), OPTIONAL :: purge
INTEGER :: i, min_fill, n_stacks
INTEGER, DIMENSION(:, :), POINTER :: stack_data
INTEGER, POINTER :: stack_fillcount
TYPE(stack_descriptor_type) :: stack_descr
n_stacks = SIZE(this%stacks_data, 3)
min_fill = SIZE(this%stacks_data, 2)*3/4 !TODO: play with this
IF (PRESENT(purge)) THEN
IF (purge) min_fill = 0
END IF
CALL dbcsr_mm_sched_begin_burst(this%sched)
DO i = 1, n_stacks
IF (this%stacks_fillcount(i) > min_fill) THEN
stack_data => this%stacks_data(:, :, i)
stack_fillcount => this%stacks_fillcount(i)
stack_descr = this%stacks_descr(i)
CALL dbcsr_mm_sched_process(this%sched, &
left, right, &
stack_data=stack_data, &
stack_fillcount=stack_fillcount, &
stack_descr=stack_descr)
stack_fillcount = 0
END IF
END DO
CALL dbcsr_mm_sched_end_burst()
END SUBROUTINE flush_stacks