Commit 084eed5d authored by Ilya Edrenkin's avatar Ilya Edrenkin
Browse files

Adding configuration flag and check for slow expf()


git-svn-id: https://svn.code.sf.net/p/kaldi/code/trunk@4325 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
parent 8a2c913b
......@@ -313,7 +313,11 @@ inline float Log1p(float x) { return log1pf(x); }
inline double Exp(double x) { return exp(x); }
#ifndef KALDI_NO_EXPF
inline float Exp(float x) { return expf(x); }
#else
inline float Exp(float x) { return exp(x); }
#endif
inline double Log(double x) { return log(x); }
......
all:
include ../kaldi.mk
BINFILES = exp-test
include ../makefiles/default_rules.mk
On some machines, expf() turns out to be very slow: much slower than its double precision counterpart exp().
Probably this is concerned with the version of glibc.
Here are a couple of examples:
Normal behaviour:
configuration$ ldd --version | head -1 | awk '{print $NF}'
2.15
configuration$ ./exp-test
exp() time: 0.00249362
expf() time: 0.0017401
Slow behaviour:
configuration$ ldd --version | head -1 | awk '{print $NF}'
2.11.1
configuration$ ./exp-test
exp() time: 0.0028439
expf() time: 0.00713329
If slow behaviour is detected, then KALDI_NO_EXPF macro will be used, and the Exp() wrapper in base/kaldi-math.h will use exp() even for single precision floats.
The behaviour of expf() is considered to be slow if it is slower than exp() by at least 10%.
\ No newline at end of file
// configuration/exp-test.cc
// Copyright 2014 Yandex LLC (Author: Ilya Edrenkin)
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <cmath>
#include "util/timer.h"
#define SAMPLE 100000
int main() {
float dummy;
kaldi::Timer exp_timer;
for(int i = 0; i < SAMPLE; ++i) {
dummy = exp(float(SAMPLE / 2) - i);
}
double exp_time = exp_timer.Elapsed();
kaldi::Timer expf_timer;
for(int i = 0; i < SAMPLE; ++i) {
dummy = expf(float(SAMPLE / 2) - i);
}
double expf_time = expf_timer.Elapsed();
// Often exp() and expf() perform very similarly,
// so we will replace expf() by exp() only if there is at least 10% difference
if (expf_time < exp_time * 1.1) {
return 0;
} else {
std::cerr << "exp() time: " << exp_time << std::endl;
std::cerr << "expf() time: " << expf_time << std::endl;
return 1;
}
std::cerr << dummy << std::endl; // No complaint about the unused variable
}
......@@ -164,8 +164,21 @@ function check_for_bad_gcc {
fi
}
function check_for_slow_expf {
cd configuration
make -f Makefile.slow_expf
./exp-test
if [ $? -eq 1 ]; then
echo "*** WARNING: expf() seems to be slower than exp() on your machine. This is a known bug in old versions of glibc. Please consider updating glibc. ***"
echo "*** Kaldi will be configured to use exp() instead of expf() in base/kaldi-math.h Exp() routine for single-precision floats. ***"
echo "CXXFLAGS += -DKALDI_NO_EXPF" >> ../kaldi.mk
fi
cd ..
}
function exit_success {
check_for_bad_gcc;
check_for_slow_expf;
exit 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment