-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphdata.F90
406 lines (373 loc) · 14 KB
/
graphdata.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
! MIT License
!
! Copyright (c) 2021 Florian Goth
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
! OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
! THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
! FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
! DEALINGS IN THE SOFTWARE.
module graphdata_mod
use colorvertex_mod, only: ColorVertex, colorvertex_init
use vertex_mod
use Exponentials_mod, only: EulerExp, FullExp
implicit none
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> This type is used to store the information contained in a symmetric
!> matrix in terms of the associated graph. The information about the
!> connectivity is stored in the vert array. The actual elements
!> are stored in the elems array.
!--------------------------------------------------------------------
type :: GraphData
type(ColorVertex), allocatable, dimension(:) :: verts !< An array with all vertices. Each vertex knows the index of its neighbours in this array.
complex(kind=kind(0.D0)), allocatable, dimension(:) :: elems !< The actual weight of the connection that was stored in the matrix.
integer :: ndim !< corresponds to the number of columns/rows of the associated matrix. Hence this is the number of vertices.
integer :: nredges !< The number of edges in the graph.
integer :: deltag !< the graph degree. It is determined by the vertex that has the most connections.
integer :: usedcolors !< the number of colors that would be used in this graph decomposition.
end type GraphData
contains
subroutine dealloc_graphdata(gd)
implicit none
type(GraphData) :: gd
deallocate(gd%elems, gd%verts)
end subroutine
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> An implementation of quicksort to sort an array of integers.
!
!> @param[in] a the array in which we search
!> @param[in] first where to start sorting
!> @param[in] last where to stop sorting
!--------------------------------------------------------------------
recursive subroutine quicksort(a, first, last)
implicit none
integer, dimension(:), intent(inout) :: a
integer, intent(in) :: first, last
integer :: x, t
integer :: i, j
x = a( (first+last) / 2 )
i = first
j = last
do
do while (a(i) < x)
i=i+1
end do
do while (x < a(j))
j=j-1
end do
if (i >= j) exit
t = a(i); a(i) = a(j); a(j) = t
i=i+1
j=j-1
end do
if (first < i-1) call quicksort(a, first, i-1)
if (j+1 < last) call quicksort(a, j+1, last)
end subroutine quicksort
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> A function that transforms a matrix into our internal data structure.
!
!> @param[in] A the matrix that we bring into our internal datastructure
!> @result our internal data structure of graph vertices
!--------------------------------------------------------------------
function mat2verts(A) result(gd)
implicit none
complex (kind=kind(0.d0)), intent(in) :: A(:,:)
type(GraphData) :: gd
integer :: i, j, maxcolors, k, i2
integer, allocatable, dimension(:) :: cntarr
gd%ndim = size(A, 1)
! check input
! first check diagonal
do i = 1, gd%ndim
if(A(i, i) /= 0.D0) then
write (*, *) "the main-diagonal must be zero!"
stop
endif
enddo
! check symmetry of input matrix
do i = 1, gd%ndim
do j = 1, gd%ndim
if(A(i, j) /= conjg(A(j, i))) then
write (*, *) "Non-hermitian matrix encountered!"
stop
endif
enddo
enddo
allocate(gd%verts(gd%ndim), cntarr(gd%ndim))
associate(ndim => gd%ndim, verts => gd%verts)
! calculate Vertex degree of each vertex
cntarr = 0
do j = 1, ndim-1
do i = j+1, ndim
if(A(i, j) /= 0.D0) then
cntarr(i) = cntarr(i) + 1
cntarr(j) = cntarr(j) + 1
endif
enddo
enddo
gd%deltag = maxval(cntarr)
write (*,*) "Delta(G) = ", gd%deltag
maxcolors = gd%deltag + 1
allocate(gd%elems(sum(cntarr)))
i2 = 1
do j = 1, ndim
call colorvertex_init(gd%verts(j), cntarr(j), maxcolors)
! call gd%verts(j)%init(cntarr(j), maxcolors)
k = 1
do i = 1, ndim
if(A(i, j) /= 0.D0) then
verts(j)%nbrs(k) = i
k = k + 1
endif
enddo
call quicksort(verts(j)%nbrs, 1, verts(j)%degree) !< it is probably sorted already...
do i = 1, verts(j)%degree ! set up array of elements
gd%elems(i2) = A(verts(j)%nbrs(i), j)
i2 = i2 + 1
enddo
enddo
deallocate(cntarr)
end associate
end function
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> A function to fill the color information of a graph.
!
!> @param[inout] gd A graphdata object.
!--------------------------------------------------------------------
subroutine determine_used_colors_of_graph(gd)
implicit none
type(GraphData), intent(inout) :: gd
integer :: i, k
gd%usedcolors = 0
gd%nredges = 0
do i = 1, gd%ndim
gd%deltag = max(gd%deltag, gd%verts(i)%degree)
do k = 1, gd%verts(i)%degree
if (gd%verts(i)%nbrs(k) > i) gd%nredges = gd%nredges + 1
if (gd%verts(i)%nbrs(k) > gd%ndim) then
write(*,*) "invalid nbr!!!"
STOP
endif
gd%usedcolors = max(gd%usedcolors, gd%verts(i)%cols(k))
enddo
enddo
end subroutine
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> A function that transforms the graphdata object to an array of nodes.
!
!> @param[in] gd A fully prepared graphdata object.
!> @result An allocated array of nodes.
!--------------------------------------------------------------------
function gd_to_nodes(gd) result(nodes)
use node_mod
implicit none
type(GraphData), intent(in) :: gd
type(node), allocatable, dimension(:) :: nodes
integer :: k, elempos, i, l, mynbr, nbr1
logical, allocatable, dimension(:) :: usedcols
allocate(nodes(gd%nredges), usedcols(gd%usedcolors))
! set up data in an edges based layout
k = 0
elempos = 0
do i = 1, gd%ndim-1
! check validity of the coloring locally
usedcols = .false.
do l = 1, gd%verts(i)%degree
if(gd%verts(i)%cols(l) == 0) then
write (*,*) "forgotten edge found!"
STOP
endif
if (usedcols(gd%verts(i)%cols(l)) .eqv. .true. ) then
write (*,*) "invalid coloring!!"
STOP
else
usedcols(gd%verts(i)%cols(l)) = .true.
endif
enddo
do l = 1, gd%usedcolors
mynbr = gd%verts(i)%nbrbycol(l)
if (mynbr > 0) then ! only do sth. if the color is associated with an edge
nbr1 = gd%verts(i)%nbrs(mynbr)
if (nbr1 > i) then ! nbr1 could be zero if there is no such edge
k = k+1
nodes(k)%x = i
nodes(k)%y = nbr1
nodes(k)%axy = gd%elems(elempos + mynbr)
nodes(k)%col = l
endif
endif
enddo
elempos = elempos + gd%verts(i)%degree
enddo
deallocate(usedcols)
end function
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> A function that findsfrom an array of colors
!> The color that has the most edges associated to it,
!> according to the data in edges_per_color.
!
!> @param[in] edges_per_color An array containing for each color the number of edges
!> @param[in] cols An array of colors from which we rty to find the biggest
!> @result The color in col that has the most edges associated with it.
!--------------------------------------------------------------------
function find_biggest_color(edges_per_color, cols) result(ret)
integer, allocatable, dimension(:), intent(in) :: edges_per_color, cols
integer :: ret
integer :: i, start
start = 1
do while (cols(start) == 0)
start = start + 1
enddo
ret = cols(start)
do i = start+1, size(cols)
if (cols(i) > 0) then
if (edges_per_color(cols(i)) > edges_per_color(ret)) ret = cols(i)
endif
enddo
end function
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> This function encodes the strategy that is ued to distribute the
!> main-diagonal ov the various colors.
!
!> Currently we try to put the diagonal into the color which hosts the most
!> edges. If the edges of touch the entire diagonal then only a single color
!> with a GeneralExp, or HomogeneousExp is generated. The rest will be the fast
!> ZeroDiag type exponentials.
!
!> @param gd The matrix in the graphdata representation.
!> @param nodes The matrix in an array of nodes represntation.
!> @param diag The diagonal of the matrix
!> @result A nrofcolors x ndim array where for each color a diagonal is present according to the strategy.
!--------------------------------------------------------------------
function distribute_diagonal_over_colors(gd, nodes, diag) result(dcol)
use node_mod
implicit none
type(GraphData), intent(in) :: gd
type(node), allocatable, dimension(:), intent(in) :: nodes
real(kind=kind(0.D0)), intent(in), allocatable, dimension(:) :: diag
real(kind=kind(0.D0)), allocatable, dimension(:, :) :: dcol ! color separated diagonal
integer :: i, tmpcol
integer, allocatable, dimension(:) :: edgespercol
allocate(edgespercol(gd%usedcolors), dcol(gd%usedcolors, gd%ndim) )
edgespercol = 0
do i = 1, size(nodes)
edgespercol(nodes(i)%col) = edgespercol(nodes(i)%col) + 1
enddo
dcol = 0
do i = 1, gd%ndim ! for every chemical potential on the diagonal, do
! Data is actually present.
! Scale-wise decisions are taken when setting up the *expbase classes
if(abs(diag(i)) > 0.D0 ) then
tmpcol = find_biggest_color(edgespercol, gd%verts(i)%cols)
dcol(tmpcol, i) = diag(i) ! move diagonal to color
endif
enddo
deallocate(edgespercol)
end function
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> This function takes a graphdata object as e.g. determined with the
!> help of the MvG_decomp function and creates a EulerExp(=product
!> of checkerboard exponentials) object from it.
!
!> @param gd
!> @result A EulerExp object that can be utilized for matrix multiplications
!--------------------------------------------------------------------
function createEulerExponentialfromGraphData(gd, diags) result(ee)
use node_mod
implicit none
type(GraphData) :: gd
real(kind=kind(0.D0)), intent(in), allocatable, dimension(:) :: diags
type(EulerExp) :: ee
real(kind=kind(0.D0)) :: weight
real(kind=kind(0.D0)), allocatable, dimension(:, :) :: dcol ! color separated diagonal
type(node), allocatable, dimension(:) :: nodes
if ((gd%usedcolors == 0) .or. (gd%nredges == 0)) then ! check that those are available
call determine_used_colors_of_graph(gd)
endif
! set up data in an edges based layout
nodes = gd_to_nodes(gd)
! distribute the diagonal over the colors
dcol = distribute_diagonal_over_colors(gd, nodes, diags)
weight = 1.0
call ee%init(nodes, gd%usedcolors, dcol, weight)
deallocate(nodes, dcol)
end function
!--------------------------------------------------------------------
!> @author
!> Florian Goth
!
!> @brief
!> This function takes a graphdata object as e.g. determined with the
!> help of the MvG_decomp function and creates a FullExp(=product
!> of checkerboard exponentials) object from it.
!
!> @param gd
!> @param method the method that determines the approximation type.
!> @result A FullExp object that can be utilized for matrix multiplications.
!--------------------------------------------------------------------
function createFullExponentialfromGraphData(gd, diags, method) result(fe)
use node_mod
implicit none
type(GraphData) :: gd
real(kind=kind(0.D0)), intent(in), allocatable, dimension(:) :: diags
real(kind=kind(0.D0)), allocatable, dimension(:,:) :: dcol
integer, intent(in) :: method
type(FullExp) :: fe
real(kind=kind(0.D0)) :: weight
type(node), allocatable, dimension(:) :: nodes
if ((gd%usedcolors == 0) .or. (gd%nredges == 0)) then ! check that those are available
call determine_used_colors_of_graph(gd)
endif
! set up data in an edges based layout
nodes = gd_to_nodes(gd)
! distribute the diagonal over the colors
dcol = distribute_diagonal_over_colors(gd, nodes, diags)
weight = 1.0
call fe%init(nodes, gd%usedcolors, dcol, method, weight)
deallocate(nodes, dcol)
end function
end module graphdata_mod