Skip to content

Commit

Permalink
Compatibility changes with Windows CL and ICL compilers; Windows CUDA…
Browse files Browse the repository at this point in the history
… support and bug fixes in cudamatrix

Parent track:
   c826fe8 P
   16c1ba0 P
   d79a8c9 P
   0a17a84 C
   569c8c2 C
  • Loading branch information
kkm committed May 18, 2015
1 parent c8bf5c6 commit 1cc30dc
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 54 deletions.
4 changes: 1 addition & 3 deletions src/base/kaldi-error.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
#include <sstream>
#include <cstdio>

#ifdef _MSC_VER
#define NOEXCEPT(Predicate)
#elif __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#if _MSC_VER >= 0x1400 || !defined(MSC_VER) && __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#define NOEXCEPT(Predicate) noexcept((Predicate))
#else
#define NOEXCEPT(Predicate)
Expand Down
21 changes: 10 additions & 11 deletions src/base/kaldi-math.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,10 @@
#define M_LN2 0.693147180559945309417232121458
#endif

#ifdef _MSC_VER
# define KALDI_ISNAN _isnan
# define KALDI_ISINF(x) (!_isnan(x) && _isnan(x-x))
# define KALDI_ISFINITE _finite
#else
# define KALDI_ISNAN std::isnan
# define KALDI_ISINF std::isinf
# define KALDI_ISFINITE(x) std::isfinite(x)
#endif
#define KALDI_ISNAN std::isnan
#define KALDI_ISINF std::isinf
#define KALDI_ISFINITE(x) std::isfinite(x)

#if !defined(KALDI_SQR)
# define KALDI_SQR(x) ((x) * (x))
#endif
Expand Down Expand Up @@ -149,7 +144,9 @@ inline double LogAdd(double x, double y) {

if (diff >= kMinLogDiffDouble) {
double res;
#ifdef _MSC_VER
#if _MSC_VER >= 1800 || __cplusplus >= 201103L
res = x + std::log1p(std::exp(diff));
#elif defined _MSC_VER
res = x + log(1.0 + exp(diff));
#else
res = x + log1p(exp(diff));
Expand All @@ -173,7 +170,9 @@ inline float LogAdd(float x, float y) {

if (diff >= kMinLogDiffFloat) {
float res;
#ifdef _MSC_VER
#if _MSC_VER >= 1800 || __cplusplus >= 201103L
res = x + std::log1p(std::exp(diff));
#elif defined _MSC_VER
res = x + logf(1.0 + expf(diff));
#else
res = x + log1pf(expf(diff));
Expand Down
13 changes: 1 addition & 12 deletions src/base/kaldi-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,10 @@ typedef float BaseFloat;
}

#ifdef _MSC_VER
namespace kaldi {
typedef unsigned __int16 uint16;
typedef unsigned __int32 uint32;
typedef __int16 int16;
typedef __int32 int32;
typedef __int64 int64;
typedef unsigned __int64 uint64;
typedef float float32;
typedef double double64;
}
#include <basetsd.h>
#define ssize_t SSIZE_T
#endif

#else
// we can do this a different way if some platform
// we find in the future lacks stdint.h
#include <stdint.h>
Expand All @@ -59,6 +49,5 @@ typedef int64_t int64;
typedef float float32;
typedef double double64;
} // end namespace kaldi
#endif

#endif // KALDI_BASE_KALDI_TYPES_H_
13 changes: 5 additions & 8 deletions src/base/kaldi-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@

#if defined(_MSC_VER)
#pragma warning(disable: 4244 4056 4305 4800 4267 4996 4756 4661)
#if _MSC_VER < 1400
#define __restrict__
#else
#define __restrict__ __restrict
#endif
#endif

#ifdef HAVE_POSIX_MEMALIGN
Expand Down Expand Up @@ -97,17 +101,10 @@ void Sleep(float seconds);


// Makes copy constructor and operator= private. Same as in compat.h of OpenFst
// toolkit. If using VS, for which this results in compilation errors, we
// do it differently.

#if defined(_MSC_VER)
#define KALDI_DISALLOW_COPY_AND_ASSIGN(type) \
void operator = (const type&)
#else
// toolkit.
#define KALDI_DISALLOW_COPY_AND_ASSIGN(type) \
type(const type&); \
void operator = (const type&)
#endif

template<bool B> class KaldiCompileTimeAssert { };
template<> class KaldiCompileTimeAssert<true> {
Expand Down
16 changes: 13 additions & 3 deletions src/cudamatrix/cu-device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
#include <string>
#include <vector>
#include <algorithm>
#ifndef _MSC_VER
#include <dlfcn.h>
#include <unistd.h> // for sleep
#endif

#include "cudamatrix/cu-common.h"
#include "cudamatrix/cu-device.h"
#include "cudamatrix/cu-matrix.h"
#include "base/kaldi-error.h"
#include "base/kaldi-utils.h"
#include "util/common-utils.h"

namespace kaldi {
Expand Down Expand Up @@ -105,7 +107,7 @@ void CuDevice::SelectGpuId(std::string use_gpu) {
int32 sec_sleep = (use_gpu == "yes" ? 20 : 2);
KALDI_WARN << "Will try again to get a GPU after " << sec_sleep
<< " seconds.";
sleep(sec_sleep);
Sleep(sec_sleep);
cudaGetLastError(); // reset the error state
e = cudaThreadSynchronize(); //<< 2nd trial to get CUDA context.
if (e != cudaSuccess) {
Expand All @@ -128,7 +130,7 @@ void CuDevice::SelectGpuId(std::string use_gpu) {
<< " seconds to get a GPU.";
num_times++;
wait_time += sec_sleep;
sleep(sec_sleep);
Sleep(sec_sleep);
cudaGetLastError(); // reset the error state
e = cudaThreadSynchronize();
}
Expand Down Expand Up @@ -377,6 +379,9 @@ std::string CuDevice::GetFreeMemory(int64* free, int64* total) const {
//pre-fill ``safe'' values that will not cause problems
mem_free = 1; mem_total = 1;
//open libcuda.so
#ifdef _MSC_VER
cuMemGetInfo(&mem_free, &mem_total);
#else
void* libcuda = dlopen("libcuda.so",RTLD_LAZY);
if(NULL == libcuda) {
KALDI_WARN << "cannot open libcuda.so";
Expand All @@ -399,6 +404,7 @@ std::string CuDevice::GetFreeMemory(int64* free, int64* total) const {
//close the library
dlclose(libcuda);
}
#endif
}
// copy the output values outside
if(NULL != free) *free = mem_free;
Expand All @@ -416,6 +422,9 @@ std::string CuDevice::GetFreeMemory(int64* free, int64* total) const {
void CuDevice::DeviceGetName(char* name, int32 len, int32 dev) {
//prefill with something reasonable
strncpy(name,"Unknown GPU",len);
#ifdef _MSC_VER
cuDeviceGetName(name, len, dev);
#else
//open libcuda.so
void* libcuda = dlopen("libcuda.so",RTLD_LAZY);
if(NULL == libcuda) {
Expand All @@ -434,6 +443,7 @@ void CuDevice::DeviceGetName(char* name, int32 len, int32 dev) {
//close the library
dlclose(libcuda);
}
#endif
}


Expand Down
2 changes: 1 addition & 1 deletion src/cudamatrix/cu-matrix-speed-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ template<typename Real> void CudaMatrixSpeedTest() {


int main() {
for (int32 loop = 1; loop < 2; loop++) {
for (int32 loop = 0; loop < 2; loop++) {
#if HAVE_CUDA == 1
if (loop == 0)
CuDevice::Instantiate().SelectGpuId("no");
Expand Down
6 changes: 5 additions & 1 deletion src/matrix/sp-matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ class SpMatrix : public PackedMatrix<Real> {
// Duplicating code from PackedMatrix.h
}

using PackedMatrix<Real>::operator =;
SpMatrix<Real>& operator=(const SpMatrix<Real> &other) {
PackedMatrix<Real>::operator=(other);
return *this;
}

using PackedMatrix<Real>::Scale;

/// matrix inverse.
Expand Down
6 changes: 5 additions & 1 deletion src/matrix/tp-matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class TpMatrix : public PackedMatrix<Real> {
this->AddPacked(alpha, M);
}

using PackedMatrix<Real>::operator =;
TpMatrix<Real>& operator=(const TpMatrix<Real> &other) {
PackedMatrix<Real>::operator=(other);
return *this;
}

using PackedMatrix<Real>::Scale;

void Resize(MatrixIndexT nRows, MatrixResizeType resize_type = kSetZero) {
Expand Down
10 changes: 3 additions & 7 deletions src/util/kaldi-io-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/io-funcs.h"
#include "util/kaldi-io.h"
#include "base/kaldi-math.h"
#include "base/kaldi-utils.h"
#ifndef _MSC_VER
#include <unistd.h>
#endif
Expand Down Expand Up @@ -161,14 +162,9 @@ void UnitTestIoNew(bool binary) {
void UnitTestIoPipe(bool binary) {
// This is as UnitTestIoNew except with different filenames.
{
#ifdef _MSC_VER
const char *filename_out = "|more > tmpf.txt",
*filename_in = "type tmpf.txt |";
#else
const char *filename_out = "|gzip -c > tmpf.gz",
*filename_in = "gunzip -c tmpf.gz |";
#endif


Output ko(filename_out, binary);
std::ostream &outfile = ko.Stream();
if (!binary) outfile << "\t";
Expand Down Expand Up @@ -215,7 +211,7 @@ void UnitTestIoPipe(bool binary) {
bool ans = ko.Close();
KALDI_ASSERT(ans);
#ifndef _MSC_VER
sleep(1); // This test does not work without this sleep:
Sleep(1); // This test does not work without this sleep:
// seems to be some kind of file-system latency.
#endif
{
Expand Down
9 changes: 2 additions & 7 deletions src/util/kaldi-table-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
#include "util/kaldi-table.h"
#include "util/kaldi-holder.h"
#include "util/table-types.h"
#ifndef _MSC_VER
#include <unistd.h> // for sleep.
#endif

namespace kaldi {

Expand Down Expand Up @@ -60,13 +57,12 @@ void UnitTestReadScriptFile() {
// suppress the warning since I already checked it's OK.
KALDI_ASSERT(!ReadScriptFile(ss, false, &script));
}
#ifndef _MSC_VER
{
Output ko("| gzip -c > tmpf.gz", false); // text mode.
ko.Stream() << "a b\n";
ko.Close();
std::vector<pr> script;
sleep(1); // This test does not work without this sleep:
Sleep(1); // This test does not work without this sleep:
bool ans = ReadScriptFile("gunzip -c tmpf.gz |", true, &script);
KALDI_ASSERT(ans);
std::vector<pr> script2;
Expand All @@ -80,14 +76,13 @@ void UnitTestReadScriptFile() {
ko.Stream() << "a b\n";
bool ans = ko.Close();
KALDI_ASSERT(ans);
sleep(1); // This test does not work without this sleep:
Sleep(1); // This test does not work without this sleep:
// seems to be some kind of file-system latency.
std::vector<pr> script;
ans = ReadScriptFile("gunzip -c tmpf.gz |", false, &script);
KALDI_ASSERT(!ans);
}
unlink("tmpf.gz");
#endif
}


Expand Down

2 comments on commit 1cc30dc

@jtrmal
Copy link
Contributor

@jtrmal jtrmal commented on 1cc30dc May 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little but concerned about "std::isnan" replacing "::_isnan"
Do you have any knowledge if it's supported in all VS compilers, say, 2010,2012--2015?

Add kaldi-types.h:
Are you sure stdint.h is available in VS? I have the recollection it wasn't in some older versions -- it might be in the newer version, though. I don't exactly know, what "newer" and "older" means :/ So please check and then I'll just trust you :)

That concern actually extends to other modification in this patch. I think we should be clear which versions we support or not. I.e. add #pragma warning (or error) in case the compiler is too old

The second concern I hjave is about "src/util/kaldi-io-test.cc"
Yes, you assume cygwin will be available to run egs, but I don't feel we should make it a requirement for compiling the libraries and running tests. So I'd prefer to go back to the original "windows/dos" commands.

@kkm000
Copy link
Contributor

@kkm000 kkm000 commented on 1cc30dc May 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtrmal: std::isnan is probably not there in VS2010, and certainly not in VS2008. stdint.h is available starting with VS2012. Do you think we need to support anything older than VS2012? AFAIK, the free compiler comes with Visual Studio Express and/or the SDK, so I would not worry about older compilers.

The original more/type trick does not even work. I think, however, we can easily support training under Windows, and should (for the record, @danpovey.thinks otherwise, AFAIU). In any case, piping is either not required on Windows (library decoding only), or should support all pipe tricks (if we support training). as for test only, let me figure something out. Good point not to require cygwin even for testing.

A bigger problem is the matrix backend library on Windows. ATLAS is not available for Windows, AFAIK, and MKL is not free.

Please sign in to comment.