Skip to content

Commit

Permalink
Progress towards beta release #1 0.9.5-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
ToddThomson committed Jan 28, 2022
1 parent 0af3314 commit 4a89be5
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 247 deletions.
2 changes: 1 addition & 1 deletion License.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2021 Todd J. Thomson
Copyright (c) 2021..2022 Todd J. Thomson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Mila/Mila.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Achilles.Mila</id>
<version>0.9.4-alpha</version>
<version>0.9.5-alpha</version>
<title>Mila Deep Neural Net Library</title>
<authors>Todd J. Thomson</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand Down
33 changes: 24 additions & 9 deletions Mila/Source/Dnn/CuDNN/CuDnnDescriptor.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ namespace Mila::Dnn::CuDnn
}

/// <summary>
/// Gets the current status of the descriptor
/// Gets the current status of the descriptor.
/// </summary>
/// <returns></returns>
cudnnStatus_t get_status() const
Expand All @@ -140,7 +140,10 @@ namespace Mila::Dnn::CuDnn
err_msg_ = message;
}

//! Diagonistic error message if any
/// <summary>
/// Gets the diagnostic error message.
/// </summary>
/// <returns>A diagnostic string</returns>
const char* get_error() const
{
return err_msg_.c_str();
Expand Down Expand Up @@ -174,13 +177,6 @@ namespace Mila::Dnn::CuDnn
return isFinalized_;
}

cudnnStatus_t SetFinalized()
{
isFinalized_ = true;

return CUDNN_STATUS_SUCCESS;
}

protected:

Descriptor() = default;
Expand Down Expand Up @@ -227,6 +223,18 @@ namespace Mila::Dnn::CuDnn

virtual ~Descriptor() = default;

void SetErrorAndThrow(
cudnnStatus_t status,
const char* message )
{
status_ = status;
err_msg_ = message;

throw CudnnException(
std::string( std::string( message ) + std::string( " cudnn_status: " ) + to_string( status ) ).c_str(),
status );
}

void CheckFinalizedThrow()
{
if ( isFinalized_ )
Expand All @@ -235,6 +243,13 @@ namespace Mila::Dnn::CuDnn
}
}

cudnnStatus_t SetFinalized()
{
isFinalized_ = true;

return CUDNN_STATUS_SUCCESS;
}

ManagedDescriptor managedDescriptor_ = nullptr;
ManagedCudnnHandle cudnn_handle_ = nullptr;

Expand Down
53 changes: 7 additions & 46 deletions Mila/Source/Dnn/CuDNN/CuDnnError.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -33,72 +33,33 @@ import CuDnn.Utils;

namespace Mila::Dnn::CuDnn
{
#ifndef NV_CUDNN_DISABLE_EXCEPTION
export class cudnnException : public std::runtime_error
export class CudnnException : public std::runtime_error
{
public:
cudnnException( const char* message, cudnnStatus_t status_ )

CudnnException( const char* message, cudnnStatus_t status_ )
throw() : std::runtime_error( message )
{
error_status = status_;
}

virtual const char* what() const throw()
{
return std::runtime_error::what();
}

cudnnStatus_t GetStatus()
{
return error_status;
}

cudnnStatus_t error_status;
};
#endif
private:

static inline void throw_if( std::function<bool()> expr, const char* message, cudnnStatus_t status )
{
if ( expr() )
{
#ifndef NV_CUDNN_DISABLE_EXCEPTION
throw cudnnException( message, status );
#endif
}
};

static inline void throw_if( bool expr, const char* message, cudnnStatus_t status )
{
if ( expr )
{
#ifndef NV_CUDNN_DISABLE_EXCEPTION
throw cudnnException( message, status );
#endif
}
cudnnStatus_t error_status;
};

export inline void SetErrorAndThrow(
//Descriptor *desc,
cudnnStatus_t status,
const char* message )
{
/*if ( desc != nullptr )
{
desc->set_status( status );
desc->set_error( message );
}*/

#ifndef NV_CUDNN_DISABLE_EXCEPTION
throw cudnnException(
std::string( std::string( message ) + std::string( " cudnn_status: " ) + to_string( status ) ).c_str(), status );
#endif
};


static inline void cudnnStatusCheck( cudnnStatus_t status )
{
if ( status != CUDNN_STATUS_SUCCESS )
{
throw cudnnException(
throw CudnnException(
std::string( "CuDNN Error: " + std::string(" cudnn_status: ") + to_string(status)).c_str(),
status );
}
Expand Down
15 changes: 12 additions & 3 deletions Mila/Source/Dnn/CuDNN/CudnnContext.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ module;
export module CuDnn.Context;

import Cuda.Device;
import Cuda.Helpers;
import CuDnn.OpaqueHandle;
import CuDnn.Error;
import CuDnn.Utils;

using namespace Mila::Dnn::Cuda;

namespace Mila::Dnn::CuDnn
{
class CudnnContext;
Expand All @@ -47,16 +50,20 @@ namespace Mila::Dnn::CuDnn
/// <summary>
/// CuDNN context class constructor
/// </summary>
CudnnContext() /* CudaDevice device ) */
CudnnContext( int deviceId = 0 )
: device_id_( deviceId )
{
std::cout << "In CudnnContext constructor\n";

CUDA_CALL( cudaSetDevice( device_id_ ) );

auto status = InitializeManagedCudnnHandle();

if ( status != CUDNN_STATUS_SUCCESS )
{
throw cudnnException(
std::string( std::string( "Failed to create CuDNN handle. Status: " ) + to_string( status ) ).c_str(), status );
throw CudnnException(
std::string( std::string( "Failed to create CuDNN handle. Status: " ) + to_string( status ) ).c_str(),
status );
}
}

Expand All @@ -67,6 +74,8 @@ namespace Mila::Dnn::CuDnn

private:

int device_id_ = 0;

CudnnContext( CudnnContext const& ) = delete;
CudnnContext& operator=( CudnnContext const& ) = delete;

Expand Down
3 changes: 2 additions & 1 deletion Mila/Source/Dnn/Cuda/Cuda.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

export module Cuda;

export import Cuda.Helpers;
export import Cuda.Device;
export import Cuda.DeviceProps;
export import Cuda.DeviceProperties;
export import Cuda.Profiler;
37 changes: 19 additions & 18 deletions Mila/Source/Dnn/Cuda/CudaDevice.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,63 @@ module;

export module Cuda.Device;

import Cuda.DeviceProperties;
import Cuda.Helpers;

namespace Mila::Dnn::Cuda
{
/// <summary>
///
/// Represents a Cuda device.
/// </summary>
export class CudaDevice // : public unique_handle<int, CudaDevice>
{
public:

/// <summary>
///
/// Creates a CudaDevice object with the specified device id.
/// </summary>
/// <param name="device_id"></param>
CudaDevice( int device = 0 )
/// <param name="deviceId">Cuda GPU device id</param>
CudaDevice( int deviceId = 0 )
: device_id_( CheckDevice( deviceId ) ), props_( CudaDeviceProperties( device_id_ ) )
{
device_ = Init( device );
}

private:

int Init( int device )
int CheckDevice( int deviceId )
{
if ( device < 0 )
if ( deviceId < 0 )
{
throw std::invalid_argument( "Invalid device." );
throw std::invalid_argument( "Invalid device id." );
}

int devCount = getDeviceCount();
int devCount = GetDeviceCount();

if ( devCount == 0 )
{
throw std::runtime_error( "No devices found." );
throw std::runtime_error( "No Cuda devices found." );
}

if ( device > devCount - 1 )
if ( deviceId > devCount - 1 )
{
throw std::out_of_range( "device id out of range." );
throw std::out_of_range( "Device id out of range." );
}

int computeMode = -1,
CUDA_CALL( cudaDeviceGetAttribute( &computeMode, cudaDevAttrComputeMode, device ) );
CUDA_CALL( cudaDeviceGetAttribute( &computeMode, cudaDevAttrComputeMode, device_id_ ) );

if ( computeMode == cudaComputeModeProhibited )
{
throw std::runtime_error( "Device is running in Compute ModeProhibited." );
}

//CUDA_CALL( cudaSetDevice( device ) );

return device;
};
return deviceId;
}

private:

int device_;
int device_id_;

CudaDeviceProperties props_;
};
}
30 changes: 7 additions & 23 deletions Mila/Source/Dnn/Cuda/CudaDeviceProps.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,28 @@ module;
#include <stdexcept>
#include <cuda_runtime.h>

export module Cuda.DeviceProps;
export module Cuda.DeviceProperties;

import Cuda.Helpers;

namespace Mila::Dnn::Cuda
{
export class CudaDeviceProps
export class CudaDeviceProperties
{
public:

CudaDeviceProps()
CudaDeviceProperties( int deviceId )
{
int devCount = 0;
CUDA_CALL( cudaGetDeviceCount( &devCount ) );

if ( devCount > 0 )
{
props_.resize( devCount );

for ( int devId = 0; devId < devCount; ++devId )
{
CUDA_CALL( cudaGetDeviceProperties( &props_[ devId ], devId ) );
}
}
CUDA_CALL( cudaGetDeviceProperties( &props_, deviceId ) );
}

const cudaDeviceProp* get( int devId ) const
const cudaDeviceProp* GetProperties() const
{
if ( static_cast<size_t>(devId) < props_.size() )
{
throw std::out_of_range( "device id out of range." );
}

return &props_[ devId ];
return &props_;
};

private:

std::vector<cudaDeviceProp> props_;
cudaDeviceProp props_;
};
}
7 changes: 4 additions & 3 deletions Mila/Source/Dnn/Cuda/CudaEnv.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import Cuda.Helpers;

namespace Mila::Dnn::Cuda
{
export class CudaEnvironment
// TJT: To be removed.

/*export class CudaEnvironment
{
public:
Expand All @@ -40,6 +42,5 @@ namespace Mila::Dnn::Cuda
private:
int device_count_ = 0;

};
};*/
}
Loading

0 comments on commit 4a89be5

Please sign in to comment.