Skip to content

Commit c826fe8

Browse files
kkmkkm000
authored andcommitted
CUDA support; bug fixes in cudamatrix/
1 parent 2a8b35e commit c826fe8

File tree

6 files changed

+58
-19
lines changed

6 files changed

+58
-19
lines changed

msbuild/kaldi.props

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
<ClCompile>
2727
<Optimization>Disabled</Optimization>
2828
<WarningLevel>Level3</WarningLevel>
29-
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
29+
<PreprocessorDefinitions>WIN32;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
3030
<MinimalRebuild>false</MinimalRebuild>
3131
<BufferSecurityCheck>false</BufferSecurityCheck>
3232
<EnableEnhancedInstructionSet>$(KaldiCpuInstructionSet)</EnableEnhancedInstructionSet>
3333
<ErrorReporting>None</ErrorReporting>
34-
<WholeProgramOptimization>true</WholeProgramOptimization>
34+
<WholeProgramOptimization>false</WholeProgramOptimization>
3535
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
3636
<Cpp0xSupport>true</Cpp0xSupport>
3737
<!-- These properties affect the Intel compiler only -->
@@ -125,6 +125,30 @@
125125

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

128+
<!-- CUDA properties may be set up only this late. -->
129+
<PropertyGroup>
130+
<CUDAPropsPath Condition="'$(CUDAPropsPath)'==''">$(VCTargetsPath)/BuildCustomizations</CUDAPropsPath>
131+
<CUDAFound Condition="exists('$(CUDAPropsPath)/CUDA 6.5.props')" >65</CUDAFound>
132+
<!--<CUDAFound Condition="exists('$(CUDAPropsPath)\CUDA 7.0.props')" >70</CUDAFound>-->
133+
</PropertyGroup>
134+
135+
<ImportGroup Condition=" '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' != '' ">
136+
<Import Project="$(CUDAPropsPath)/CUDA 6.5.props" Condition=" '$(CUDAFound)' == '65' " />
137+
</ImportGroup>
138+
139+
<ItemDefinitionGroup Condition=" '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' != '' ">
140+
<CudaCompile>
141+
<CodeGeneration>compute_20,sm_20;compute_30,sm_30;compute_35,sm_35;compute_37,sm_37;compute_50,sm_50;</CodeGeneration>
142+
<!--<Runtime>MT</Runtime>-->
143+
</CudaCompile>
144+
<ClCompile>
145+
<PreprocessorDefinitions>HAVE_CUDA;%(PreprocessorDefinitions)</PreprocessorDefinitions>
146+
</ClCompile>
147+
<Link>
148+
<AdditionalDependencies>cublas.lib;cusparse.lib;cudart_static.lib;cuda.lib;%(AdditionalDependencies)</AdditionalDependencies>
149+
</Link>
150+
</ItemDefinitionGroup>
151+
128152
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
129153
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" />
130154
</Project>

msbuild/kaldi.targets

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
Text="Invalid BuildStage. Are you running the top-level build project?"/>
77
<Error Condition=" '$(BuildStage)' == 'Binary' and '$(KaldiBinaryTarget)' == '' "
88
Text="For the Binary and Test build stages, KaldiBinaryTarget= specification is required. Are you running the top-level build project?"/>
9+
<Warning Condition=" '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' == '' "
10+
Text="CUDA has been requested in kaldi.user.props but not found. Install CUDA 6.5 (not supporting other versions at the moment)." />
911
</Target>
1012

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

5262
<Target Name ="_BeforeBuildLibrary" BeforeTargets="_PrepareForBuild" DependsOnTargets="_BeforeBuildAnyStage" Condition=" '$(BuildStage)' == 'Library' ">
5363
<ItemGroup>
5464
<!-- Fetch all *.cc files from <LibSource> into the build as C/C++ sources. -->
5565
<ClCompile Include="@(LibSource->'$(_KaldiSrcSubdir)%(identity)')" Condition=" '%(extension)' == '.cc' " />
56-
<!-- TODO: Fetch all *.cu files as CUDA sources. -->
57-
66+
<!-- Fetch all *.cu files as CUDA sources. -->
67+
<CudaCompile Include="@(LibSource->'$(_KaldiSrcSubdir)%(identity)')" Condition=" '%(extension)' == '.cu' and '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' != '' " />
5868
</ItemGroup>
5969
</Target>
6070

@@ -97,4 +107,5 @@
97107
</Target>
98108

99109
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" Condition=" '$(BuildStage)' != '' "/>
110+
<Import Project="$(CUDAPropsPath)\CUDA 6.5.targets" Condition=" '$(BuildStage)' != '' and '$(KaldiCUDA)' == 'true' and '$(CUDAFound)' == '65' "/>
100111
</Project>

src/base/kaldi-utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
# include <windows.h>
3131
#endif
3232

33+
#if _MSC_VER < 1400
34+
#define __restrict__
35+
#endif
36+
3337
#if defined(_MSC_VER)
3438
#pragma warning(disable: 4244 4056 4305 4800 4267 4996 4756 4661)
35-
#define __restrict__
3639
#endif
3740

3841
#ifdef HAVE_POSIX_MEMALIGN

src/cudamatrix/cu-device.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
#include <string>
3131
#include <vector>
3232
#include <algorithm>
33+
#ifndef _MSC_VER
3334
#include <dlfcn.h>
34-
#include <unistd.h> // for sleep
35+
#endif
3536

3637
#include "cudamatrix/cu-common.h"
3738
#include "cudamatrix/cu-device.h"
3839
#include "cudamatrix/cu-matrix.h"
3940
#include "base/kaldi-error.h"
41+
#include "base/kaldi-utils.h"
4042
#include "util/common-utils.h"
4143

4244
namespace kaldi {
@@ -105,7 +107,7 @@ void CuDevice::SelectGpuId(std::string use_gpu) {
105107
int32 sec_sleep = (use_gpu == "yes" ? 20 : 2);
106108
KALDI_WARN << "Will try again to get a GPU after " << sec_sleep
107109
<< " seconds.";
108-
sleep(sec_sleep);
110+
Sleep(sec_sleep);
109111
cudaGetLastError(); // reset the error state
110112
e = cudaThreadSynchronize(); //<< 2nd trial to get CUDA context.
111113
if (e != cudaSuccess) {
@@ -128,7 +130,7 @@ void CuDevice::SelectGpuId(std::string use_gpu) {
128130
<< " seconds to get a GPU.";
129131
num_times++;
130132
wait_time += sec_sleep;
131-
sleep(sec_sleep);
133+
Sleep(sec_sleep);
132134
cudaGetLastError(); // reset the error state
133135
e = cudaThreadSynchronize();
134136
}
@@ -377,6 +379,9 @@ std::string CuDevice::GetFreeMemory(int64* free, int64* total) const {
377379
//pre-fill ``safe'' values that will not cause problems
378380
mem_free = 1; mem_total = 1;
379381
//open libcuda.so
382+
#ifdef _MSC_VER
383+
cuMemGetInfo(&mem_free, &mem_total);
384+
#else
380385
void* libcuda = dlopen("libcuda.so",RTLD_LAZY);
381386
if(NULL == libcuda) {
382387
KALDI_WARN << "cannot open libcuda.so";
@@ -399,6 +404,7 @@ std::string CuDevice::GetFreeMemory(int64* free, int64* total) const {
399404
//close the library
400405
dlclose(libcuda);
401406
}
407+
#endif
402408
}
403409
// copy the output values outside
404410
if(NULL != free) *free = mem_free;
@@ -416,6 +422,9 @@ std::string CuDevice::GetFreeMemory(int64* free, int64* total) const {
416422
void CuDevice::DeviceGetName(char* name, int32 len, int32 dev) {
417423
//prefill with something reasonable
418424
strncpy(name,"Unknown GPU",len);
425+
#ifdef _MSC_VER
426+
cuDeviceGetName(name, len, dev);
427+
#else
419428
//open libcuda.so
420429
void* libcuda = dlopen("libcuda.so",RTLD_LAZY);
421430
if(NULL == libcuda) {
@@ -434,6 +443,7 @@ void CuDevice::DeviceGetName(char* name, int32 len, int32 dev) {
434443
//close the library
435444
dlclose(libcuda);
436445
}
446+
#endif
437447
}
438448

439449

src/cudamatrix/cu-matrix-speed-test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ template<typename Real> void CudaMatrixSpeedTest() {
519519

520520

521521
int main() {
522-
for (int32 loop = 1; loop < 2; loop++) {
522+
for (int32 loop = 0; loop < 2; loop++) {
523523
#if HAVE_CUDA == 1
524524
if (loop == 0)
525525
CuDevice::Instantiate().SelectGpuId("no");

src/util/kaldi-io-test.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,9 @@ void UnitTestIoNew(bool binary) {
161161
void UnitTestIoPipe(bool binary) {
162162
// This is as UnitTestIoNew except with different filenames.
163163
{
164-
#ifdef _MSC_VER
165-
const char *filename_out = "|more > tmpf.txt",
166-
*filename_in = "type tmpf.txt |";
167-
#else
168164
const char *filename_out = "|gzip -c > tmpf.gz",
169165
*filename_in = "gunzip -c tmpf.gz |";
170-
#endif
171-
166+
172167
Output ko(filename_out, binary);
173168
std::ostream &outfile = ko.Stream();
174169
if (!binary) outfile << "\t";
@@ -214,10 +209,6 @@ void UnitTestIoPipe(bool binary) {
214209
if (!binary && Rand()%2 == 0) outfile << "\t";
215210
bool ans = ko.Close();
216211
KALDI_ASSERT(ans);
217-
#ifndef _MSC_VER
218-
sleep(1); // This test does not work without this sleep:
219-
// seems to be some kind of file-system latency.
220-
#endif
221212
{
222213
bool binary_in;
223214
Input ki(filename_in, &binary_in);

0 commit comments

Comments
 (0)