Skip to content

Commit

Permalink
Optimize LSTM code for builds without OpenMP
Browse files Browse the repository at this point in the history
The constant value kNumThreads is not only used to configure the number
of threads but also to allocate vectors used in those threads.

There is only a single thread without OpenMP, so it is sufficient to
allocate vectors with only one element in that case.

Replace also the upper limit in the for loops by the known vector size.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed May 22, 2017
1 parent 5a06417 commit 15b3596
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lstm/fullyconnected.cpp
Expand Up @@ -28,7 +28,11 @@
#include "networkscratch.h"

// Number of threads to use for parallel calculation of Forward and Backward.
#ifdef _OPENMP
const int kNumThreads = 4;
#else
const int kNumThreads = 1;
#endif

namespace tesseract {

Expand Down Expand Up @@ -117,7 +121,7 @@ void FullyConnected::Forward(bool debug, const NetworkIO& input,
temp_lines.init_to_size(kNumThreads, NetworkScratch::FloatVec());
GenericVector<NetworkScratch::FloatVec> curr_input;
curr_input.init_to_size(kNumThreads, NetworkScratch::FloatVec());
for (int i = 0; i < temp_lines.size(); ++i) {
for (int i = 0; i < kNumThreads; ++i) {
temp_lines[i].Init(no_, scratch);
curr_input[i].Init(ni_, scratch);
}
Expand Down Expand Up @@ -208,7 +212,7 @@ bool FullyConnected::Backward(bool debug, const NetworkIO& fwd_deltas,
back_deltas->Resize(fwd_deltas, ni_);
GenericVector<NetworkScratch::FloatVec> errors;
errors.init_to_size(kNumThreads, NetworkScratch::FloatVec());
for (int i = 0; i < errors.size(); ++i) errors[i].Init(no_, scratch);
for (int i = 0; i < kNumThreads; ++i) errors[i].Init(no_, scratch);
GenericVector<NetworkScratch::FloatVec> temp_backprops;
if (needs_to_backprop_) {
temp_backprops.init_to_size(kNumThreads, NetworkScratch::FloatVec());
Expand Down

0 comments on commit 15b3596

Please sign in to comment.