forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathProgram.cs
131 lines (104 loc) · 3.68 KB
/
Program.cs
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
// Parallel computations sample application
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2006-2011
// contacts@aforgenet.com
//
using System;
namespace ParallelTest
{
class Program
{
static void Main( string[] args )
{
int matrixSize = 250;
int runs = 10;
int tests = 5;
double test1time = 0;
double test2time = 0;
Console.WriteLine( "Starting test with " + AForge.Parallel.ThreadsCount + " threads" );
// allocate matrixes for all tests
double[,] a = new double[matrixSize, matrixSize];
double[,] b = new double[matrixSize, matrixSize];
double[,] c1 = new double[matrixSize, matrixSize];
double[,] c2 = new double[matrixSize, matrixSize];
Random rand = new Random( );
// fill source matrixes with random numbers
for ( int i = 0; i < matrixSize; i++ )
{
for ( int j = 0; j < matrixSize; j++ )
{
a[i, j] = rand.NextDouble( );
b[i, j] = rand.NextDouble( );
}
}
// run specified number of tests
for ( int test = 0; test < tests; test++ )
{
// test 1
DateTime start = DateTime.Now;
for ( int run = 0; run < runs; run++ )
{
Test1( a, b, c1 );
}
DateTime end = DateTime.Now;
TimeSpan span = end - start;
Console.Write( span.TotalMilliseconds.ToString( "F3" ) + "\t | " );
test1time += span.TotalMilliseconds;
// test 2
start = DateTime.Now;
for ( int run = 0; run < runs; run++ )
{
Test2( a, b, c2 );
}
end = DateTime.Now;
span = end - start;
Console.Write( span.TotalMilliseconds.ToString( "F3" ) + "\t | " );
test2time += span.TotalMilliseconds;
Console.WriteLine( " " );
}
// provide average performance
test1time /= tests;
test2time /= tests;
Console.WriteLine( "----------- AVG -----------" );
Console.WriteLine( test1time.ToString( "F3" ) + "\t | " + test2time.ToString( "F3" ) + "\t | " );
Console.WriteLine( "Done" );
}
// Test #1 - multiply 2 square matrixes without using parallel computations
private static void Test1( double[,] a, double[,] b, double[,] c )
{
int s = a.GetLength( 0 );
for ( int i = 0; i < s; i++ )
{
for ( int j = 0; j < s; j++ )
{
double v = 0;
for ( int k = 0; k < s; k++ )
{
v += a[i, k] * b[k, j];
}
c[i, j] = v;
}
}
}
// Test #2 - multiply 2 square matrixes using parallel computations
private static void Test2( double[,] a, double[,] b, double[,] c )
{
int s = a.GetLength( 0 );
AForge.Parallel.For( 0, s, delegate( int i )
{
for ( int j = 0; j < s; j++ )
{
double v = 0;
for ( int k = 0; k < s; k++ )
{
v += a[i, k] * b[k, j];
}
c[i, j] = v;
}
}
);
}
}
}