-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrmain.cpp
103 lines (91 loc) · 3.52 KB
/
thrmain.cpp
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
/*
* interface, does not validate results
*/
#include "solution.cpp"
pthread_mutex_t g_Mtx;
int g_Cost, g_Crime;
//-------------------------------------------------------------------------------------------------
void costNotify ( const TCostProblem * p,
const TRect * r )
{
printf ( "costNotify\n" );
for ( int i = 0; i < p -> m_Size; i ++ )
delete [] p -> m_Values[i];
delete [] p -> m_Values;
delete p;
}
//-------------------------------------------------------------------------------------------------
const TCostProblem * costGeneratorFunc ( void )
{
printf ( "costGen\n" );
pthread_mutex_lock ( &g_Mtx );
bool gen = g_Cost-- > 0;
pthread_mutex_unlock ( &g_Mtx );
if ( ! gen ) return NULL;
TCostProblem * p = new TCostProblem;
int size = (int)( 10 + 100.0 * rand () / RAND_MAX );
int ** mat = new int * [size];
for ( int y = 0; y < size; y ++ )
{
mat[y] = new int [size];
for ( int x = 0; x < size; x ++ )
mat[y][x] = (int)( -500 + 2000.0 * rand () / RAND_MAX );
}
p -> m_Values = mat;
p -> m_Size = size;
p -> m_MaxCost = (int)(500.0 * size * size * rand () / RAND_MAX );
p -> m_Done = costNotify;
return p;
}
//-------------------------------------------------------------------------------------------------
void crimeNotify ( const TCrimeProblem * p,
const TRect * r )
{
printf ( "crimeNotify\n" );
for ( int i = 0; i < p -> m_Size; i ++ )
delete [] p -> m_Values[i];
delete [] p -> m_Values;
delete p;
}
//-------------------------------------------------------------------------------------------------
const TCrimeProblem * crimeGeneratorFunc ( void )
{
printf ( "crimeGen\n" );
pthread_mutex_lock ( &g_Mtx );
bool gen = g_Crime-- > 0;
pthread_mutex_unlock ( &g_Mtx );
if ( ! gen ) return NULL;
TCrimeProblem * p = new TCrimeProblem;
int size = (int)( 30 + 200.0 * rand () / RAND_MAX );
double ** mat = new double * [size];
for ( int y = 0; y < size; y ++ )
{
mat[y] = new double [size];
for ( int x = 0; x < size; x ++ )
mat[y][x] = 1000.0 * rand () / RAND_MAX;
}
p -> m_Values = mat;
p -> m_Size = size;
p -> m_MaxCrime= 1000.0 * rand () / RAND_MAX;
p -> m_Done = crimeNotify;
return p;
}
//-------------------------------------------------------------------------------------------------
void oneTest ( int threads,
int cost,
int crime )
{
pthread_mutex_init ( &g_Mtx, NULL );
g_Cost = cost;
g_Crime = crime;
MapAnalyzer ( threads, costGeneratorFunc, crimeGeneratorFunc );
pthread_mutex_destroy ( &g_Mtx );
}
//-------------------------------------------------------------------------------------------------
int main ( void )
{
//oneTest ( 500, 1, 1 );
oneTest ( 500, 2, 2 );
//oneTest ( 1, 2, 2 );
return 0;
}