Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tmva] MLP codegen threadsafety #572

Merged
merged 3 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions tmva/tmva/src/MethodANNBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,6 @@ void TMVA::MethodANNBase::MakeClassSpecific( std::ostream& fout, const TString&
numNodesFrom = numNodesTo;
}
fout << std::endl;
fout << " double * fWeights["<<numLayers<<"];" << std::endl;
fout << "};" << std::endl;

fout << std::endl;
Expand All @@ -1071,7 +1070,7 @@ void TMVA::MethodANNBase::MakeClassSpecific( std::ostream& fout, const TString&
for (Int_t lIdx = 0; lIdx < numLayers; lIdx++) {
TObjArray* layer = (TObjArray*)fNetwork->At(lIdx);
int numNodes = layer->GetEntries();
fout << " fLayerSize[" << lIdx << "] = " << numNodes << "; fWeights["<<lIdx<<"] = new double["<<numNodes<<"]; " << std::endl;
fout << " fLayerSize[" << lIdx << "] = " << numNodes << ";" << std::endl;
}

for (Int_t i = 0; i < numLayers-1; i++) {
Expand Down Expand Up @@ -1099,14 +1098,17 @@ void TMVA::MethodANNBase::MakeClassSpecific( std::ostream& fout, const TString&
fout << " return 0;" << std::endl;
fout << " }" << std::endl;
fout << std::endl;
fout << " for (int l=0; l<fLayers; l++)" << std::endl;
fout << " for (int i=0; i<fLayerSize[l]; i++) fWeights[l][i]=0;" << std::endl;
fout << std::endl;
fout << " for (int l=0; l<fLayers-1; l++)" << std::endl;
fout << " fWeights[l][fLayerSize[l]-1]=1;" << std::endl;
for (Int_t lIdx = 0; lIdx < numLayers; lIdx++) {
TObjArray *layer = (TObjArray *)fNetwork->At(lIdx);
int numNodes = layer->GetEntries();
fout << " std::array<double, " << numNodes << "> fWeights" << lIdx << " {{}};" << std::endl;
}
for (Int_t lIdx = 0; lIdx < numLayers - 1; lIdx++) {
fout << " fWeights" << lIdx << ".back() = 1.;" << std::endl;
}
fout << std::endl;
fout << " for (int i=0; i<fLayerSize[0]-1; i++)" << std::endl;
fout << " fWeights[0][i]=inputValues[i];" << std::endl;
fout << " fWeights0[i]=inputValues[i];" << std::endl;
fout << std::endl;
for (Int_t i = 0; i < numLayers-1; i++) {
fout << " // layer " << i << " to " << i+1 << std::endl;
Expand All @@ -1117,25 +1119,28 @@ void TMVA::MethodANNBase::MakeClassSpecific( std::ostream& fout, const TString&
fout << " for (int o=0; o<fLayerSize[" << i+1 << "]-1; o++) {" << std::endl;
}
fout << " for (int i=0; i<fLayerSize[" << i << "]; i++) {" << std::endl;
fout << " double inputVal = fWeightMatrix" << i << "to" << i+1 << "[o][i] * fWeights[" << i << "][i];" << std::endl;
fout << " double inputVal = fWeightMatrix" << i << "to" << i + 1 << "[o][i] * fWeights" << i << "[i];"
<< std::endl;

if ( fNeuronInputType == "sum") {
fout << " fWeights[" << i+1 << "][o] += inputVal;" << std::endl;
fout << " fWeights" << i + 1 << "[o] += inputVal;" << std::endl;
}
else if ( fNeuronInputType == "sqsum") {
fout << " fWeights[" << i+1 << "][o] += inputVal*inputVal;" << std::endl;
fout << " fWeights" << i + 1 << "[o] += inputVal*inputVal;" << std::endl;
}
else { // fNeuronInputType == TNeuronInputChooser::kAbsSum
fout << " fWeights[" << i+1 << "][o] += fabs(inputVal);" << std::endl;
fout << " fWeights" << i + 1 << "[o] += fabs(inputVal);" << std::endl;
}
fout << " }" << std::endl;
fout << " } // loop over i" << std::endl;
if (i+1 != numLayers-1) // in the last layer no activation function is applied
fout << " fWeights[" << i+1 << "][o] = ActivationFnc(fWeights[" << i+1 << "][o]);" << std::endl;
else fout << " fWeights[" << i+1 << "][o] = OutputActivationFnc(fWeights[" << i+1 << "][o]);" << std::endl; //zjh
fout << " }" << std::endl;
fout << " fWeights" << i + 1 << "[o] = ActivationFnc(fWeights" << i + 1 << "[o]);" << std::endl;
else
fout << " fWeights" << i + 1 << "[o] = OutputActivationFnc(fWeights" << i + 1 << "[o]);"
<< std::endl; // zjh
fout << " } // loop over o" << std::endl;
}
fout << std::endl;
fout << " return fWeights[" << numLayers-1 << "][0];" << std::endl;
fout << " return fWeights" << numLayers - 1 << "[0];" << std::endl;
fout << "}" << std::endl;

fout << std::endl;
Expand All @@ -1148,10 +1153,6 @@ void TMVA::MethodANNBase::MakeClassSpecific( std::ostream& fout, const TString&
fout << "// Clean up" << std::endl;
fout << "inline void " << className << "::Clear() " << std::endl;
fout << "{" << std::endl;
fout << " // clean up the arrays" << std::endl;
fout << " for (int lIdx = 0; lIdx < "<<numLayers<<"; lIdx++) {" << std::endl;
fout << " delete[] fWeights[lIdx];" << std::endl;
fout << " }" << std::endl;
fout << "}" << std::endl;
}

Expand Down
1 change: 1 addition & 0 deletions tmva/tmva/src/MethodBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,7 @@ void TMVA::MethodBase::MakeClass( const TString& theClassFileName ) const

// generate the class
fout << "" << std::endl;
fout << "#include <array>" << std::endl;
fout << "#include <vector>" << std::endl;
fout << "#include <cmath>" << std::endl;
fout << "#include <string>" << std::endl;
Expand Down