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 |
using
System; using
System.Collections.Generic; using
System.Linq; using
System.Text; using
System.Management; using
System.Threading.Tasks; namespace
TaskTool { using
System.Collections; using
System.Collections.Concurrent; using
System.Diagnostics; using
System.Threading; class
Program { private
int TotalNum; private
const int MAXDICNUM = 40000000; private
static void Main( string [] args) { var
stopWath = new
Stopwatch(); var
allCells= new
List<Point>(); var
cash = new
ConcurrentDictionary<DisCashPair, short >(); InitialCells(allCells); stopWath.Start(); Cal(allCells, cash, 0, allCells.Count); stopWath.Stop(); Console.WriteLine( string .Format( "Single CPU {0},Cash num{1}" , stopWath.ElapsedMilliseconds, cash.Count)); cash.Clear(); stopWath.Reset(); stopWath.Start(); var
cts = new
CancellationTokenSource(); var
tf = new
TaskFactory(cts.Token, TaskCreationOptions.AttachedToParent, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); var
blockNum = ( int )Math.Ceiling(allCells.Count*1.0/CPUTool.GetCpuNum()); var
calTasks = new
Task[CPUTool.GetCpuNum()]; for
( int
i = 0; i < CPUTool.GetCpuNum(); i++) { var
startNum = i * blockNum; var
endNum = (i + 1) * blockNum; if
(endNum >= allCells.Count) { endNum = allCells.Count - 1; } calTasks[i] = Task.Factory.StartNew(() => Cal(allCells,cash, startNum, endNum)); } var
endTask = tf.ContinueWhenAll(calTasks, (tasks) =>{}); endTask.Wait(cts.Token); //Task.WaitAll(calTasks.ToArray()); stopWath.Stop(); Console.WriteLine( string .Format( "Single CPU {0},Cash num{1}" , stopWath.ElapsedMilliseconds, cash.Count)); //Console.WriteLine(string.Format("Single CPU {0},Cash num{1}", stopWath.ElapsedMilliseconds, cash.Count)); } //private static Dictionary<DisCashPair, short> Finish(Task<Dictionary<DisCashPair, short>>[] tasks) //{ // IEnumerable<KeyValuePair<DisCashPair, short>> result = new Dictionary<DisCashPair, short>(); // result = tasks.Select(t => t.Result).Aggregate(result, (current, dic) => current.Union(dic)); // return result.ToDictionary(r => r.Key, r => r.Value); //} private
static ConcurrentDictionary<DisCashPair, short > Cal(List<Point> allCells, int
start, int
end) { var
cash = new
ConcurrentDictionary<DisCashPair, short >(); for
( int
i = start; i < end; i++) { var
cell = allCells[i]; CalDis(cell, allCells, cash); } return
cash; } private
static void Cal(List<Point> allCells, ConcurrentDictionary<DisCashPair, short > cash, int
start, int
end) { for
( int
i = start; i < end; i++) { var
cell = allCells[i]; CalDis(cell, allCells, cash); } } private
static void CalDis(Point cell, List<Point> allCells, ConcurrentDictionary<DisCashPair, short > cash) { foreach
( var
desCell in
allCells) { short
dis; if
(cash.TryGetValue( new
DisCashPair(cell, desCell), out
dis)) { continue ; } else { cash[ new
DisCashPair(cell, desCell)] = GetDis(cell,desCell); } } } private
static short GetDis(Point cell, Point desCell) { var
difX = (cell.X - desCell.X); var
difY = (cell.Y - desCell.Y); var
dis = Math.Sqrt(difX*difX + difY*difY); return
( short ) (Math.Round(dis, 2)*100); } private
static void InitialCells(List<Point> allCells) { var
rd = new
Random(); for
( int
i = 0; i < 5000; i++) { allCells.Add( new
Point() { ID=i, X = rd.NextDouble(), Y = rd.NextDouble() }); } } } public
class DisCashPair { public
Point Src; public
Point Des; public
DisCashPair(Point src,Point des) { Src = src; Des = des; } public
override int GetHashCode() { return
(Src.ID*397) ^ (Des.ID*397); } public
override bool Equals( object
obj) { if
(ReferenceEquals(obj, null )) return
false ; if
(obj.GetType() != typeof
(DisCashPair)) return
false ; return
Equals((DisCashPair) obj); } private
bool Equals(DisCashPair pair) { return
(pair.Src.ID == this .Src.ID && pair.Des.ID == this .Des.ID) || (pair.Src.ID == this .Des.ID && pair.Des.ID == this .Src.ID); } } public
struct Point { public
int ID; public
double X; public
double Y; } public
static class CPUTool { private
static int CPUNUM=Int32.MaxValue; private
const string SEARCHSR = "Select * from Win32_Processor" ; private
const string CORESNUM = "NumberOfCores" ; public
static int GetCpuNum() { if
(CPUNUM == Int32.MaxValue) { CPUNUM = new
System.Management.ManagementObjectSearcher(SEARCHSR).Get() .Cast<ManagementBaseObject>() .Sum(item => int .Parse(item[CORESNUM].ToString())); } return
CPUNUM; } } } |
using System;using System.Collections.Generic;using System.Linq;using
System.Text;using System.Management;using System.Threading.Tasks;
namespace
TaskTool{ using System.Collections; using
System.Collections.Concurrent; using System.Diagnostics;
using System.Threading;
class Program
{ private int TotalNum;
private const int MAXDICNUM = 40000000;
private
static void Main(string[] args) {
var stopWath = new Stopwatch();
var allCells=new List<Point>();
var cash = new ConcurrentDictionary<DisCashPair,
short>();
InitialCells(allCells);
stopWath.Start(); Cal(allCells,
cash, 0, allCells.Count);
stopWath.Stop();
Console.WriteLine(string.Format("Single CPU {0},Cash num{1}",
stopWath.ElapsedMilliseconds, cash.Count));
cash.Clear();
stopWath.Reset();
stopWath.Start();
var cts = new
CancellationTokenSource(); var tf = new
TaskFactory(cts.Token, TaskCreationOptions.AttachedToParent,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
var
blockNum = (int)Math.Ceiling(allCells.Count*1.0/CPUTool.GetCpuNum());
var calTasks = new
Task[CPUTool.GetCpuNum()];
for (int
i = 0; i < CPUTool.GetCpuNum(); i++)
{ var startNum = i *
blockNum; var endNum = (i
+ 1) * blockNum; if
(endNum >= allCells.Count)
{
endNum = allCells.Count - 1;
}
calTasks[i]
=
Task.Factory.StartNew(() => Cal(allCells,cash, startNum, endNum));
}
var endTask = tf.ContinueWhenAll(calTasks, (tasks) =>{});
endTask.Wait(cts.Token);
//Task.WaitAll(calTasks.ToArray());
stopWath.Stop();
Console.WriteLine(string.Format("Single CPU {0},Cash num{1}",
stopWath.ElapsedMilliseconds, cash.Count));
//Console.WriteLine(string.Format("Single CPU {0},Cash num{1}",
stopWath.ElapsedMilliseconds, cash.Count));
}
//private static
Dictionary<DisCashPair, short> Finish(Task<Dictionary<DisCashPair,
short>>[] tasks) //{
// IEnumerable<KeyValuePair<DisCashPair, short>> result
= new Dictionary<DisCashPair, short>();
//
result = tasks.Select(t => t.Result).Aggregate(result, (current,
dic) => current.Union(dic));
//
return result.ToDictionary(r => r.Key, r => r.Value);
//}
private static
ConcurrentDictionary<DisCashPair, short> Cal(List<Point> allCells,
int start, int end) {
var cash = new ConcurrentDictionary<DisCashPair,
short>(); for (int i = start; i <
end; i++) {
var cell = allCells[i];
CalDis(cell, allCells, cash);
} return
cash; }
private static
void Cal(List<Point> allCells, ConcurrentDictionary<DisCashPair,
short> cash, int start, int end) {
for (int i = start; i < end; i++)
{
var cell = allCells[i];
CalDis(cell, allCells, cash);
} }
private static
void CalDis(Point cell, List<Point> allCells,
ConcurrentDictionary<DisCashPair, short> cash)
{ foreach (var desCell in
allCells) {
short dis;
if (cash.TryGetValue(new DisCashPair(cell, desCell), out
dis)) {
continue;
}
else
{ cash[new
DisCashPair(cell, desCell)] = GetDis(cell,desCell);
} }
}
private static short
GetDis(Point cell, Point desCell) {
var difX = (cell.X - desCell.X);
var difY = (cell.Y - desCell.Y);
var dis = Math.Sqrt(difX*difX + difY*difY);
return (short) (Math.Round(dis, 2)*100);
}
private static void
InitialCells(List<Point> allCells) {
var rd = new Random();
for (int i = 0; i < 5000; i++)
{
allCells.Add(new Point()
{
ID=i, X =
rd.NextDouble(),
Y = rd.NextDouble()
}); }
} }
public class DisCashPair {
public Point Src; public Point
Des;
public DisCashPair(Point src,Point
des) { Src =
src; Des = des;
}
public override int GetHashCode()
{ return
(Src.ID*397) ^ (Des.ID*397); }
public override bool Equals(object obj)
{ if (ReferenceEquals(obj, null))
return false; if (obj.GetType() !=
typeof (DisCashPair)) return false;
return Equals((DisCashPair) obj); }
private bool Equals(DisCashPair pair)
{ return (pair.Src.ID == this.Src.ID
&& pair.Des.ID == this.Des.ID) ||
(pair.Src.ID == this.Des.ID &&
pair.Des.ID == this.Src.ID); }
}
public struct Point {
public int ID; public double X;
public double Y; }
public static class
CPUTool { private static int
CPUNUM=Int32.MaxValue;
private const string
SEARCHSR = "Select * from Win32_Processor"; private
const string CORESNUM = "NumberOfCores";
public
static int GetCpuNum() {
if (CPUNUM == Int32.MaxValue)
{ CPUNUM =
new
System.Management.ManagementObjectSearcher(SEARCHSR).Get()
.Cast<ManagementBaseObject>()
.Sum(item =>
int.Parse(item[CORESNUM].ToString()));
}
return CPUNUM;
}
}}
原文地址:http://www.cnblogs.com/suriyel/p/3738074.html