Skip to content

Commit

Permalink
CUDA support; bug fixes in cudamatrix/
Browse files Browse the repository at this point in the history
  • Loading branch information
kkm authored and kkm000 committed May 12, 2015
1 parent 2a8b35e commit c826fe8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
28 changes: 26 additions & 2 deletions msbuild/kaldi.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
<ClCompile>
<Optimization>Disabled</Optimization>
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>false</MinimalRebuild>
<BufferSecurityCheck>false</BufferSecurityCheck>
<EnableEnhancedInstructionSet>$(KaldiCpuInstructionSet)</EnableEnhancedInstructionSet>
<ErrorReporting>None</ErrorReporting>
<WholeProgramOptimization>true</WholeProgramOptimization>
<WholeProgramOptimization>false</WholeProgramOptimization>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<Cpp0xSupport>true</Cpp0xSupport>
<!-- These properties affect the Intel compiler only -->
Expand Down Expand Up @@ -125,6 +125,30 @@

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

<!-- CUDA properties may be set up only this late. -->
<PropertyGroup>
<CUDAPropsPath Condition="'$(CUDAPropsPath)'==''">$(VCTargetsPath)/BuildCustomizations</CUDAPropsPath>
<CUDAFound Condition="exists('$(CUDAPropsPath)/CUDA 6.5.props')" >65</CUDAFound>
<!--<CUDAFound Condition="exists('$(CUDAPropsPath)\CUDA 7.0.props')" >70</CUDAFound>-->
</PropertyGroup>

<ImportGroup Condition=" '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' != '' ">
<Import Project="$(CUDAPropsPath)/CUDA 6.5.props" Condition=" '$(CUDAFound)' == '65' " />
</ImportGroup>

<ItemDefinitionGroup Condition=" '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' != '' ">
<CudaCompile>
<CodeGeneration>compute_20,sm_20;compute_30,sm_30;compute_35,sm_35;compute_37,sm_37;compute_50,sm_50;</CodeGeneration>
<!--<Runtime>MT</Runtime>-->
</CudaCompile>
<ClCompile>
<PreprocessorDefinitions>HAVE_CUDA;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies>cublas.lib;cusparse.lib;cudart_static.lib;cuda.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" />
</Project>
15 changes: 13 additions & 2 deletions msbuild/kaldi.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Text="Invalid BuildStage. Are you running the top-level build project?"/>
<Error Condition=" '$(BuildStage)' == 'Binary' and '$(KaldiBinaryTarget)' == '' "
Text="For the Binary and Test build stages, KaldiBinaryTarget= specification is required. Are you running the top-level build project?"/>
<Warning Condition=" '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' == '' "
Text="CUDA has been requested in kaldi.user.props but not found. Install CUDA 6.5 (not supporting other versions at the moment)." />
</Target>

<Target Name="Build" DependsOnTargets="_PrepareBuildStages" Condition=" '$(BuildStage)' == '' ">
Expand Down Expand Up @@ -47,14 +49,22 @@
--><!-- Convert all library references into project references. --><!--
<ProjectReference Include="@(DependsOnLibs->'$(MSBuildProjectDirectory)\%(relativedir)%(filename).kwproj')" />
</ItemGroup>-->
<!-- Hack CUDA 6.5 into working with Intel XE 15.0 compiler. Other combinations untested. -->
<ItemGroup Condition=" '$(PlatformToolset.StartsWith(`Intel C++ Compiler`))' == 'true' ">
<CudaCompile>
<VCBinDir>$(ICInstallDir)bin\ia32\icl.exe</VCBinDir>
<BaseCommandLineTemplate>-ccbin "%(CudaCompile.VCBinDir)" [GenerateRelocatableDeviceCode] [Include] [RequiredIncludes] [CInterleavedPTX] [GPUDebugInfo] [GenerateLineInfo] [Keep] [KeepDir] [MaxRegCount] [PtxAsOptionV] [TargetMachinePlatform] [NvccCompilation] [CudaRuntime] [AdditionalOptions]</BaseCommandLineTemplate>
<RuntimeApiCommandLineTemplate>%(CudaCompile.BaseCommandLineTemplate) [HostDebugInfo] [Emulation] [FastMath] [Defines] %(CudaCompile.HostCommandLineTemplate) [CompileOut] "%(CudaCompile.FullPath)"</RuntimeApiCommandLineTemplate>
</CudaCompile>
</ItemGroup>
</Target>

<Target Name ="_BeforeBuildLibrary" BeforeTargets="_PrepareForBuild" DependsOnTargets="_BeforeBuildAnyStage" Condition=" '$(BuildStage)' == 'Library' ">
<ItemGroup>
<!-- Fetch all *.cc files from <LibSource> into the build as C/C++ sources. -->
<ClCompile Include="@(LibSource->'$(_KaldiSrcSubdir)%(identity)')" Condition=" '%(extension)' == '.cc' " />
<!-- TODO: Fetch all *.cu files as CUDA sources. -->

<!-- Fetch all *.cu files as CUDA sources. -->
<CudaCompile Include="@(LibSource->'$(_KaldiSrcSubdir)%(identity)')" Condition=" '%(extension)' == '.cu' and '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' != '' " />
</ItemGroup>
</Target>

Expand Down Expand Up @@ -97,4 +107,5 @@
</Target>

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" Condition=" '$(BuildStage)' != '' "/>
<Import Project="$(CUDAPropsPath)\CUDA 6.5.targets" Condition=" '$(BuildStage)' != '' and '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' == '65' "/>
</Project>
5 changes: 4 additions & 1 deletion src/base/kaldi-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
# include <windows.h>
#endif

#if _MSC_VER < 1400
#define __restrict__
#endif

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

#ifdef HAVE_POSIX_MEMALIGN
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
11 changes: 1 addition & 10 deletions src/util/kaldi-io-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,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 @@ -214,10 +209,6 @@ void UnitTestIoPipe(bool binary) {
if (!binary && Rand()%2 == 0) outfile << "\t";
bool ans = ko.Close();
KALDI_ASSERT(ans);
#ifndef _MSC_VER
sleep(1); // This test does not work without this sleep:
// seems to be some kind of file-system latency.
#endif
{
bool binary_in;
Input ki(filename_in, &binary_in);
Expand Down

0 comments on commit c826fe8

Please sign in to comment.