Skip to content

Commit 7f7eb85

Browse files
committed
Merge branch 'master' of github.com:NLESC-JCER/Fortran_Davidson
2 parents 72139e9 + 853e460 commit 7f7eb85

14 files changed

+717
-829
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# create binary
3-
set(SOURCES main.f90 numeric_kinds.f90 davidson.f90 benchmark.f90)
3+
set(UTILS array_utils.f90 numeric_kinds.f90 lapack_wrapper.f90)
4+
set(SOURCES main.f90 davidson.f90 ${UTILS})
45
message (STATUS "SOURCES: " ${SOURCES})
56
add_executable(main ${SOURCES})
67

@@ -29,7 +30,7 @@ target_compile_options(main
2930
target_link_libraries(main PRIVATE ${LINEAR_ALGEBRA} ${OpenMP_Fortran_FLAGS})
3031

3132
# Benchmark free matrix
32-
add_executable(benchmark_free benchmark_free.f90 davidson.f90 numeric_kinds.f90)
33+
add_executable(benchmark_free benchmark_free.f90 davidson.f90 ${UTILS})
3334

3435
target_compile_options(main
3536
PRIVATE

src/array_utils.f90

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
module array_utils
2+
3+
use numeric_kinds, only: dp
4+
use lapack_wrapper, only: lapack_generalized_eigensolver, lapack_matmul, lapack_matrix_vector, &
5+
lapack_qr, lapack_solver
6+
implicit none
7+
8+
!> \private
9+
private
10+
!> \public
11+
public :: concatenate, diagonal,eye, generate_diagonal_dominant, norm
12+
13+
contains
14+
15+
pure function eye(m, n, alpha)
16+
!> Create a matrix with ones in the diagonal and zero everywhere else
17+
!> \param m: number of rows
18+
!> \param n: number of colums
19+
!> \param alpha: optional diagonal value
20+
!> \return matrix of size n x m
21+
integer, intent(in) :: n, m
22+
real(dp), dimension(m, n) :: eye
23+
real(dp), intent(in), optional :: alpha
24+
25+
!local variable
26+
integer :: i, j
27+
real(dp) :: x
28+
29+
! check optional values
30+
x = 1.d0
31+
if (present(alpha)) x = alpha
32+
33+
do i=1, m
34+
do j=1, n
35+
if (i /= j) then
36+
eye(i, j) = 0
37+
else
38+
eye(i, i) = x
39+
end if
40+
end do
41+
end do
42+
43+
end function eye
44+
45+
pure function norm(vector)
46+
!> compute the norm-2 of a vector
47+
real(dp), dimension(:), intent(in) :: vector
48+
real(dp) :: norm
49+
50+
norm = sqrt(sum(vector ** 2.d0))
51+
52+
end function norm
53+
54+
subroutine concatenate(arr, brr)
55+
56+
!> Concatenate two matrices
57+
!> \param arr: first array
58+
!> \param brr: second array
59+
!> \return arr concatenate brr (overwrites arr)
60+
61+
real(dp), dimension(:, :), intent(inout), allocatable :: arr
62+
real(dp), dimension(:, :), intent(in) :: brr
63+
real(dp), dimension(:, :), allocatable :: tmp_array
64+
integer :: new_dim, dim_cols, dim_rows
65+
66+
! dimension
67+
dim_rows = size(arr, 1)
68+
dim_cols = size(arr, 2)
69+
70+
! Number of columns of the new matrix
71+
new_dim = dim_cols + size(brr, 2)
72+
73+
! move to temporal array
74+
allocate(tmp_array(dim_rows, new_dim))
75+
tmp_array(:, :dim_cols) = arr
76+
77+
! Move to new expanded matrix
78+
deallocate(arr)
79+
call move_alloc(tmp_array, arr)
80+
81+
arr(:, dim_cols + 1:) = brr
82+
83+
end subroutine concatenate
84+
85+
function generate_diagonal_dominant(m, sparsity, diag_val) result(arr)
86+
!> Generate a diagonal dominant square matrix of dimension m
87+
!> \param m dimension of the matrix
88+
!> \param sparsity magnitude order of the off-diagonal values
89+
90+
integer, intent(in) :: m ! size of the square matrix
91+
real(dp), optional :: diag_val
92+
integer :: i, j
93+
real(dp) :: sparsity
94+
real(dp), dimension(m, m) :: arr
95+
call random_number(arr)
96+
97+
arr = arr * sparsity
98+
do j=1, m
99+
do i=1, m
100+
if (i > j) then
101+
arr(i, j) = arr(j, i)
102+
else if(i == j) then
103+
if (present(diag_val))then
104+
arr(i,i) = diag_val
105+
else
106+
arr(i, i) = i
107+
end if
108+
end if
109+
end do
110+
end do
111+
112+
end function generate_diagonal_dominant
113+
114+
function diagonal(matrix)
115+
!> return the diagonal of a matrix
116+
real(dp), dimension(:, :), intent(in) :: matrix
117+
real(dp), dimension(size(matrix, 1)) :: diagonal
118+
119+
! local variables
120+
integer :: i, j, m
121+
122+
! dimension of the matrix
123+
m = size(matrix, 1)
124+
125+
do i=1,m
126+
do j=1,m
127+
if (i == j) then
128+
diagonal(i) = matrix(i, j)
129+
end if
130+
end do
131+
end do
132+
133+
end function diagonal
134+
135+
end module array_utils

src/benchmark.f90

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/benchmark_free.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ end module matrix_free
5151

5252
program main
5353
use numeric_kinds, only: dp
54-
use davidson, only: generalized_eigensolver, generate_diagonal_dominant, norm
54+
use davidson, only: generalized_eigensolver
55+
use array_utils, only: generate_diagonal_dominant, norm
5556
use matrix_free, only: compute_matrix_on_the_fly, compute_stx_on_the_fly
5657

5758
implicit none

0 commit comments

Comments
 (0)