Skip to content

Commit

Permalink
submatrix_types: use sort from util
Browse files Browse the repository at this point in the history
Instead of implementing a custom quicksort, we can use sort from util.
qsort_two is still required within submatrix_dissection. However, this
is a very specific use case so we can make it a private part of
submatrix_dissection and get rid of submatrix_methods entirely.
  • Loading branch information
michaellass authored and oschuett committed Mar 12, 2020
1 parent 8629c76 commit b2deae6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 122 deletions.
55 changes: 54 additions & 1 deletion src/submatrix_dissection.F
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ MODULE submatrix_dissection
mp_send,&
mp_wait
USE kinds, ONLY: dp
USE submatrix_methods, ONLY: qsort_two
USE submatrix_types, ONLY: buffer_type,&
bufptr_type,&
intBuffer_type,&
Expand Down Expand Up @@ -819,4 +818,58 @@ SUBROUTINE submatrix_communicate_results(this, resultmat)
END SUBROUTINE submatrix_communicate_results
! **************************************************************************************************
!> \brief sort two integer arrays using quicksort, using the second list as second-level sorting criterion
!> \param arr_a - first input array
!> \param arr_b - second input array
!> \param left - left boundary of region to be sorted
!> \param right - right boundary of region to be sorted
! **************************************************************************************************
RECURSIVE PURE SUBROUTINE qsort_two(arr_a, arr_b, left, right)
INTEGER, DIMENSION(:), INTENT(inout) :: arr_a, arr_b
INTEGER, INTENT(in) :: left, right
INTEGER :: i, j, pivot_a, pivot_b, tmp
IF (right - left .LT. 1) RETURN
i = left
j = right - 1
pivot_a = arr_a(right)
pivot_b = arr_b(right)
DO
DO WHILE ((arr_a(i) .LT. pivot_a) .OR. ((arr_a(i) .EQ. pivot_a) .AND. (arr_b(i) .LT. pivot_b)))
i = i + 1
ENDDO
DO WHILE ((j .GT. i) .AND. ((arr_a(j) .GT. pivot_a) .OR. ((arr_a(j) .EQ. pivot_a) .AND. (arr_b(j) .GE. pivot_b))))
j = j - 1
ENDDO
IF (i .LT. j) THEN
tmp = arr_a(i)
arr_a(i) = arr_a(j)
arr_a(j) = tmp
tmp = arr_b(i)
arr_b(i) = arr_b(j)
arr_b(j) = tmp
ELSE
EXIT
ENDIF
ENDDO
IF ((arr_a(i) .GT. pivot_a) .OR. (arr_a(i) .EQ. pivot_a .AND. arr_b(i) .GT. pivot_b)) THEN
tmp = arr_a(i)
arr_a(i) = arr_a(right)
arr_a(right) = tmp
tmp = arr_b(i)
arr_b(i) = arr_b(right)
arr_b(right) = tmp
ENDIF
IF (i - 1 .GT. left) CALL qsort_two(arr_a, arr_b, left, i - 1)
IF (i + 1 .LT. right) CALL qsort_two(arr_a, arr_b, i + 1, right)
END SUBROUTINE qsort_two
END MODULE submatrix_dissection
116 changes: 0 additions & 116 deletions src/submatrix_methods.F

This file was deleted.

13 changes: 8 additions & 5 deletions src/submatrix_types.F
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MODULE submatrix_types

USE kinds, ONLY: dp
USE submatrix_methods, ONLY: qsort
USE util, ONLY: sort

IMPLICIT NONE
PRIVATE
Expand Down Expand Up @@ -199,9 +199,10 @@ END FUNCTION set_getall
!> \brief update internal list of set elements
!> \param this - instance of extendable vector
! **************************************************************************************************
PURE SUBROUTINE set_update_sorted(this)
CLASS(set_type), INTENT(INOUT) :: this
INTEGER :: i, idx
SUBROUTINE set_update_sorted(this)
CLASS(set_type), INTENT(INOUT) :: this
INTEGER :: i, idx
INTEGER, DIMENSION(:), ALLOCATABLE :: tmp

IF (ALLOCATED(this%sorted)) DEALLOCATE (this%sorted)
ALLOCATE (this%sorted(this%elements))
Expand All @@ -214,7 +215,9 @@ PURE SUBROUTINE set_update_sorted(this)
ENDIF
ENDDO

CALL qsort(this%sorted, 1, this%elements)
ALLOCATE (tmp(this%elements))
CALL sort(this%sorted, this%elements, tmp)
DEALLOCATE (tmp)

this%sorted_up_to_date = .TRUE.
END SUBROUTINE set_update_sorted
Expand Down

0 comments on commit b2deae6

Please sign in to comment.