1 #Region "Microsoft.VisualBasic::21183c37f8ca04b784aced47ebe7c7ea, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\Threads\Groups\GroupAPI.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module GroupAPI
35     
36     '         Function: ParallelGroup
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Runtime.CompilerServices
44
45 Namespace Parallel
46
47     Public Module GroupAPI
48
49         ''' <summary>
50         ''' 貌似使用LINQ进行Group操作的时候是没有并行化的,灰非常慢,则可以使用这个拓展函数来获取较好的性能
51         ''' </summary>
52         ''' <typeparam name="T"></typeparam>
53         ''' <typeparam name="T_TAG"></typeparam>
54         ''' <param name="source"></param>
55         ''' <returns></returns>
56         <Extension> Public Function ParallelGroup(Of T, T_TAG)(source As IEnumerable(Of T), __getGuid As Func(Of T, T_TAG)) As GroupResult(Of T, T_TAG)()
57             Call $"Generating guid index...".__DEBUG_ECHO
58             Dim TAGS = (From x As T In source.AsParallel Select guid = __getGuid(x), x).ToArray
59             Call Console.WriteLine($"[DEBUG {Now.ToString}] Start to create lquery partitions...")
60             Dim Partitions = TAGS.Split(TAGS.Length / Environment.ProcessorCount)
61             Call Console.WriteLine($"[DEBUG {Now.ToString}] Invoke parallel group operations....")
62             Call Console.WriteLine($"[DEBUG {Now.ToString}] First groups...")
63             Dim FirstGroups = (From Partition In Partitions.AsParallel
64                                Select (From obj In (From Token In Partition
65                                                     Select Token.guid
66                                                     Group guid By guid Into Group).ToArray Select obj.guid).ToArray).ToArray.Unlist
67             Call Console.WriteLine($"[DEBUG {Now.ToString}] Unique group...")
68             Dim UniqueGroup = (From TAG As T_TAG
69                                In FirstGroups.AsParallel
70                                Select TAG
71                                Group TAG By TAG Into Group).ToArray
72             Call Console.WriteLine($"[DEBUG {Now.ToString}] Generating group result....")
73             Call Console.WriteLine(" * Cache data....")
74             Dim Cache = (From TAG In UniqueGroup.AsParallel Select TAG, List = New List(Of T)).ToArray
75             Call Console.WriteLine(" * Address data.....")
76             Dim Addressing = (From obj In TAGS.AsParallel Select obj, List = (From item In Cache Where item.TAG.TAG.Equals(obj.guid) Select item.List).FirstOrDefault).ToArray
77             For Each Address In Addressing
78                 Call Address.List.Add(Address.obj.x)
79             Next
80             Call Console.WriteLine(" * Generate result.....")
81             Dim LQuery = (From TAG In Cache.AsParallel Select New GroupResult(Of T, T_TAG)() With {.Tag = TAG.TAG.TAG, .Group = TAG.List.ToArray}).ToArray
82             Call Console.WriteLine($"[DEBUG {Now.ToString}] Parallel group operation job done!")
83             Return LQuery
84         End Function
85     End Module
86 End Namespace