-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassifier.cpp
173 lines (140 loc) · 5.01 KB
/
Classifier.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
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
#include "Classifier.h"
Classifier::Classifier()
{
// Init MPI
MPI_Initialized( &mpiInitialized );
if (!mpiInitialized) MPI_Init( nullptr, nullptr );
MPI_Comm_size( MPI_COMM_WORLD, &numMpiNodes );
MPI_Comm_rank( MPI_COMM_WORLD, &mpiNodeId );
if (mpiNodeId == MPI_ROOT_ID)
printf( "There are %d nodes doing computation.\n", numMpiNodes );
printf( "Id of this node is: %d.\n", mpiNodeId );
}
Classifier::~Classifier()
{
// Destory trees
if (rootArr != nullptr)
{
for (unsigned int i = 0; i < numTrees; i++)
treeBuilder.DestroyNode( rootArr[i] );
free( rootArr );
rootArr = nullptr;
}
MPI_Initialized( &mpiInitialized );
if (mpiInitialized) MPI_Finalize();
}
void Classifier::Train(
const Instance* instanceTable,
const vector<NumericAttr>& fv,
const vector<char*>& cv,
const unsigned int numInstances )
{
classVec = cv;
featureVec = fv;
/******************** Init tree constructer ********************/
if (NUM_TREES % numMpiNodes > 0)
{
numTrees = NUM_TREES / numMpiNodes + 1;
if (mpiNodeId == numMpiNodes - 1)
numTrees = NUM_TREES - numTrees * (numMpiNodes - 1);
}
else
numTrees = NUM_TREES / numMpiNodes;
rootArr = (TreeNode**) malloc( numTrees * sizeof( TreeNode* ) );
treeBuilder.Init( fv, cv, instanceTable, numInstances );
printf( "Node %d constructed %u trees.\n", mpiNodeId, numTrees );
// Seed randomizer based on mpi node id
srand( mpiNodeId + 1 );
time_t start, end;
double dif;
time( &start );
#pragma omp parallel
{
#pragma omp single
printf(
"There're %d threads running on node %d.\n",
omp_get_num_threads(),
mpiNodeId );
#pragma omp for schedule(dynamic)
for (unsigned int treeId = 0; treeId < numTrees; treeId++)
{
rootArr[treeId] = treeBuilder.BuildTree( RANDOM_FEATURE_SET_SIZE );
//treeBuilder.PrintTree( rootArr[treeId], 0 );
}
}
time( &end );
dif = difftime( end, start );
printf( "Node %d builds forests: time taken is %.2lf seconds.\n", mpiNodeId, dif );
}
void Classifier::Classify(
const Instance* instanceTable,
const unsigned int numInstances )
{
if (classVec.empty())
{
printf( "Please train the model first.\n" );
MPI_Initialized( &mpiInitialized );
if (mpiInitialized)
{
int err = 0;
MPI_Abort( MPI_COMM_WORLD, err );
}
return;
}
/******************* Prepare buffer *******************/
unsigned short numClasses = classVec.size();
unsigned int correctCounter = 0;
unsigned int* votes = (unsigned int*)
calloc( numClasses * numInstances, sizeof( unsigned int ) );
#pragma omp parallel for schedule(dynamic)
for (unsigned int i = 0; i < numInstances; i++)
Classify( instanceTable[i], votes, i );
/************************** Do reduction *************************/
if (mpiNodeId == MPI_ROOT_ID)
CheckMPIErr( MPI_Reduce( MPI_IN_PLACE, votes, numClasses * numInstances,
MPI_UNSIGNED, MPI_SUM, 0, MPI_COMM_WORLD ), mpiNodeId );
else
CheckMPIErr( MPI_Reduce( votes, nullptr, numClasses * numInstances,
MPI_UNSIGNED, MPI_SUM, 0, MPI_COMM_WORLD ), mpiNodeId );
/************************ Compute accuracy ************************/
if (mpiNodeId == MPI_ROOT_ID)
{
#pragma omp parallel for reduction(+: correctCounter) schedule(static)
for (unsigned int i = 0; i < numInstances; i++)
{
unsigned short predictedClassIndex =
getIndexOfMax( votes + i * numClasses, numClasses );
if (predictedClassIndex == instanceTable[i].classIndex)
correctCounter++;
}
double correctRate = (double) correctCounter / (double) numInstances;
double incorrectRate = 1.0 - correctRate;
printf( "Correct rate: %f\n", correctRate );
printf( "Incorrect rate: %f\n", incorrectRate );
}
free( votes );
votes = nullptr;
}
inline void Classifier::Classify(
const Instance& instance,
unsigned int* votes,
const unsigned int instId )
{
unsigned short numClasses = classVec.size();
for (unsigned int treeId = 0; treeId < numTrees; treeId++)
{
TreeNode* node = rootArr[treeId];
if (node == nullptr) continue;
while (node->childrenArr != nullptr)
{
// 2 children by default:
// one group having feature value smaller than threshold,
// another group having feature value greater than threshold.
unsigned int childId = (unsigned int)
(instance.featureAttrArray[node->featureIndex] >= node->threshold);
if (node->childrenArr[childId] == nullptr) break;
else node = node->childrenArr[childId];
}
votes[instId * numClasses + node->classIndex]++;
}
}