First attempt at getting stuff to compile under Linux. Not everything
in Math/Math works, but hopefully this is forward progress.
This commit is contained in:
Родитель
c848afb42b
Коммит
33e48b635f
|
@ -123,13 +123,18 @@ public:
|
|||
template <typename T>
|
||||
File& operator<<(T val)
|
||||
{
|
||||
#ifndef LINUX
|
||||
attempt([=]()
|
||||
#endif
|
||||
{
|
||||
if (IsTextBased())
|
||||
fputText(m_file, val);
|
||||
else
|
||||
fput(m_file, val);
|
||||
});
|
||||
}
|
||||
#ifndef LINUX
|
||||
);
|
||||
#endif
|
||||
return *this;
|
||||
}
|
||||
File& operator<<(const std::wstring& val);
|
||||
|
@ -156,13 +161,18 @@ public:
|
|||
template <typename T>
|
||||
File& operator>>(T& val)
|
||||
{
|
||||
#ifndef LINUX
|
||||
attempt([&]()
|
||||
#endif
|
||||
{
|
||||
if (IsTextBased())
|
||||
fgetText(m_file, val);
|
||||
else
|
||||
fget(m_file, val);
|
||||
});
|
||||
}
|
||||
#ifndef LINUX
|
||||
);
|
||||
#endif
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,12 @@
|
|||
#ifndef _BASETYPES_
|
||||
#define _BASETYPES_
|
||||
|
||||
#ifdef LINUX
|
||||
typedef char16_t TCHAR;
|
||||
#include <stdarg.h>
|
||||
#define vsprintf_s vsprintf /* Not sure this is right... Malcolm */
|
||||
#endif /* LINUX */
|
||||
|
||||
#ifndef UNDER_CE // fixed-buffer overloads not available for wince
|
||||
#ifdef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES // fixed-buffer overloads for strcpy() etc.
|
||||
#undef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
|
||||
|
@ -72,7 +78,7 @@ OACR_WARNING_DISABLE(POTENTIAL_ARGUMENT_TYPE_MISMATCH, "Not level1 or level2_sec
|
|||
#include <errno.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cmath> // for HUGE_VAL
|
||||
#include <cmath> // for HUGE_VAL // potential double isnan definition
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <map>
|
||||
|
@ -283,6 +289,16 @@ public:
|
|||
noncopyable(){}
|
||||
};
|
||||
|
||||
|
||||
#ifdef LINUX
|
||||
#define CRITICAL_SECTION int
|
||||
void InitializeCriticalSection(int *) {}
|
||||
void DeleteCriticalSection(int *) {}
|
||||
void EnterCriticalSection(int *) {}
|
||||
void LeaveCriticalSection(int *) {}
|
||||
|
||||
#endif
|
||||
|
||||
// class CCritSec and CAutoLock -- simple critical section handling
|
||||
// TODO: Currently only working under Windows; BROKEN otherwise, to be fixed
|
||||
class CCritSec
|
||||
|
@ -305,6 +321,8 @@ public:
|
|||
#endif
|
||||
};
|
||||
|
||||
#ifndef LINUX
|
||||
|
||||
// locks a critical section, and unlocks it automatically
|
||||
// when the lock goes out of scope
|
||||
class CAutoLock
|
||||
|
@ -399,6 +417,8 @@ public:
|
|||
};
|
||||
#endif
|
||||
|
||||
#endif /* LINUX */
|
||||
|
||||
};}; // namespace
|
||||
|
||||
#if 0 //ndef BASETYPES_NO_UNSAFECRTOVERLOAD // if on, no unsafe CRT overload functions
|
||||
|
@ -552,6 +572,7 @@ typedef strfun::_strprintf<wchar_t> wstrprintf; // wchar_t version
|
|||
#ifdef _WIN32
|
||||
struct utf8 : std::string { utf8 (const std::wstring & p) // utf-16 to -8
|
||||
{
|
||||
#ifdef MALCOLM
|
||||
size_t len = p.length();
|
||||
if (len == 0) { return;} // empty string
|
||||
msra::basetypes::fixed_vector<char> buf (3 * len + 1); // max: 1 wchar => up to 3 mb chars
|
||||
|
@ -561,9 +582,11 @@ struct utf8 : std::string { utf8 (const std::wstring & p) // utf-16 to -8
|
|||
&buf[0], (int) buf.size(), NULL, NULL);
|
||||
if (rc == 0) throw std::runtime_error ("WideCharToMultiByte");
|
||||
(*(std::string*)this) = &buf[0];
|
||||
#endif /* Malcolm */
|
||||
}};
|
||||
struct utf16 : std::wstring { utf16 (const std::string & p) // utf-8 to -16
|
||||
{
|
||||
#ifdef MALCOLM
|
||||
size_t len = p.length();
|
||||
if (len == 0) { return;} // empty string
|
||||
msra::basetypes::fixed_vector<wchar_t> buf (len + 1);
|
||||
|
@ -574,6 +597,7 @@ struct utf16 : std::wstring { utf16 (const std::string & p) // utf-8 to -16
|
|||
if (rc == 0) throw std::runtime_error ("MultiByteToWideChar");
|
||||
ASSERT (rc < buf.size ());
|
||||
(*(std::wstring*)this) = &buf[0];
|
||||
#endif /* Malcolm */
|
||||
}};
|
||||
#else // TODO: complete this once we are building on actual Linux, currently using default locale instead of UTF-8 locale
|
||||
static inline std::string utf8(const std::wstring & p) // output: UTF-8
|
||||
|
@ -603,8 +627,10 @@ static inline std::string wcstombs (const std::wstring & p) // output: MBCS
|
|||
{
|
||||
size_t len = p.length();
|
||||
msra::basetypes::fixed_vector<char> buf (2 * len + 1); // max: 1 wchar => 2 mb chars
|
||||
#ifdef MALCOLM
|
||||
std::fill (buf.begin (), buf.end (), 0);
|
||||
::wcstombs (&buf[0], p.c_str(), 2 * len + 1);
|
||||
#endif /* Malcolm */
|
||||
return std::string (&buf[0]);
|
||||
}
|
||||
static inline std::wstring mbstowcs (const std::string & p) // input: MBCS
|
||||
|
@ -647,7 +673,7 @@ template<class _T> static inline std::basic_string<_T> join (const std::vector<s
|
|||
static inline int toint (const wchar_t * s)
|
||||
{
|
||||
return (int)wcstol(s, 0, 10);
|
||||
//return _wtoi (s); // ... TODO: check it
|
||||
//return _wtoi (s); // ... TODO: test this
|
||||
}
|
||||
static inline int toint (const char * s)
|
||||
{
|
||||
|
@ -737,6 +763,8 @@ static inline FILE* _wfopen(const wchar_t * path, const wchar_t * mode) { return
|
|||
|
||||
namespace msra { namespace basetypes {
|
||||
|
||||
#ifdef MALCOLM
|
||||
|
||||
// FILE* with auto-close; use auto_file_ptr instead of FILE*.
|
||||
// Warning: do not pass an auto_file_ptr to a function that calls fclose(),
|
||||
// except for fclose() itself.
|
||||
|
@ -763,6 +791,7 @@ public:
|
|||
void swap (auto_file_ptr & other) throw() { std::swap (f, other.f); }
|
||||
};
|
||||
inline int fclose (auto_file_ptr & af) { return af.fclose(); }
|
||||
#endif /* MALCOLM */
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// auto-closing container for Win32 handles.
|
||||
|
@ -783,6 +812,7 @@ public:
|
|||
typedef auto_handle_t<HANDLE> auto_handle;
|
||||
#endif
|
||||
|
||||
#ifdef MALCOLM
|
||||
// like auto_ptr but calls freeFunc_p (type free_func_t) instead of delete to clean up
|
||||
// minor difference - wrapped object is T, not T *, so to wrap a
|
||||
// T *, use auto_clean<T *>
|
||||
|
@ -802,6 +832,7 @@ public:
|
|||
operator const T () const { return it; }
|
||||
T detach () { T tmp = it; it = 0; return tmp; } // release ownership of object
|
||||
};
|
||||
#endif /* MALCOLM */
|
||||
|
||||
#if 0
|
||||
// simple timer
|
||||
|
@ -844,12 +875,23 @@ namespace msra { namespace files {
|
|||
|
||||
class textreader
|
||||
{
|
||||
#ifndef LINUX
|
||||
msra::basetypes::auto_file_ptr f;
|
||||
#else
|
||||
FILE *f;
|
||||
#endif /* LINUX */
|
||||
std::vector<char> buf; // read buffer (will only grow, never shrink)
|
||||
int ch; // next character (we need to read ahead by one...)
|
||||
char getch() { char prevch = (char) ch; ch = fgetc (f); return prevch; }
|
||||
public:
|
||||
#ifndef LINUX
|
||||
textreader (const std::wstring & path) : f (path.c_str(), "rb") { buf.reserve (10000); ch = fgetc (f); }
|
||||
#else
|
||||
textreader (const std::wstring & path) {
|
||||
f = fopen((char *)path.c_str(), "rb");
|
||||
ch = fgetc(f); /* I Think this is right ... Malcolm */
|
||||
}
|
||||
#endif /* LINUX */
|
||||
operator bool() const { return ch != EOF; } // true if still a line to read
|
||||
std::string getline() // get and consume the next line
|
||||
{
|
||||
|
@ -935,7 +977,11 @@ template<typename FUNCTION> static void attempt (int retries, const FUNCTION & b
|
|||
if (attempt >= retries)
|
||||
throw; // failed N times --give up and rethrow the error
|
||||
fprintf (stderr, "attempt: %s, retrying %d-th time out of %d...\n", e.what(), attempt+1, retries);
|
||||
#ifndef LINUX
|
||||
::Sleep (1000); // wait a little, then try again
|
||||
#else
|
||||
sleep(1);
|
||||
#endif /* LINUX */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -7,16 +7,22 @@
|
|||
#include <vector>
|
||||
#include <stdio.h>
|
||||
#include <ctime>
|
||||
#include <limits.h> /* LINUX */
|
||||
#include "File.h"
|
||||
#include "Helpers.h"
|
||||
#include "CommonMatrix.h"
|
||||
|
||||
#ifndef LINUX
|
||||
#ifdef MATH_EXPORTS
|
||||
#define MATH_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATH_API __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#else /* LINUX */
|
||||
#define MATH_API
|
||||
#endif /* LINUX */
|
||||
|
||||
#ifndef USE_TIME_BASED_SEED
|
||||
#define USE_TIME_BASED_SEED ULONG_MAX
|
||||
#endif
|
||||
|
@ -51,8 +57,8 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
~CPUMatrix();
|
||||
|
||||
public:
|
||||
size_t BufferSize() const {return m_numRows*m_numCols*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const {return m_pArray;}
|
||||
size_t BufferSize() const {return this->m_numRows*this->m_numCols*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const {return this->m_pArray;}
|
||||
|
||||
CPUMatrix<ElemType> ColumnSlice(size_t startColumn, size_t numCols) const;
|
||||
CPUMatrix<ElemType>& AssignColumnSlice(const CPUMatrix<ElemType>& fromMatrix, size_t startColumn, size_t numCols);
|
||||
|
@ -73,15 +79,15 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
inline ElemType& operator() (const size_t row, const size_t col)
|
||||
{
|
||||
return m_pArray[LocateElement(row, col)];
|
||||
return this->m_pArray[LocateElement(row, col)];
|
||||
}
|
||||
inline const ElemType& operator() (const size_t row, const size_t col) const
|
||||
{
|
||||
return m_pArray[LocateElement(row, col)];
|
||||
return this->m_pArray[LocateElement(row, col)];
|
||||
}
|
||||
inline ElemType Get00Element() const
|
||||
{
|
||||
return m_pArray[0];
|
||||
return this->m_pArray[0];
|
||||
}
|
||||
|
||||
void SetValue(const ElemType v);
|
||||
|
@ -347,8 +353,9 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
|
||||
protected:
|
||||
inline size_t LocateElement (const size_t i, const size_t j) const;
|
||||
inline size_t LocateColumn (const size_t j) const;
|
||||
// Was inline.. but without definition, it doesn't make sense.
|
||||
size_t LocateElement (const size_t i, const size_t j) const;
|
||||
size_t LocateColumn (const size_t j) const;
|
||||
|
||||
private:
|
||||
void ZeroInit(); //should only be used by constructors.
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
#include "CPUSparseMatrix.h"
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#ifndef LINUX
|
||||
#include <Windows.h>
|
||||
#endif /* LINUX */
|
||||
#ifdef LEAKDETECT
|
||||
#include <vld.h>
|
||||
#endif
|
||||
|
@ -90,70 +92,77 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
template<class ElemType>
|
||||
void CPUSparseMatrix<ElemType>::ZeroInit()
|
||||
{
|
||||
m_numRows = 0;
|
||||
m_numCols = 0;
|
||||
m_elemSizeAllocated = 0;
|
||||
m_externalBuffer = false;
|
||||
m_pArray = NULL;
|
||||
m_computeDevice = CPUDEVICE;
|
||||
m_nz = 0;
|
||||
m_matrixName = NULL;
|
||||
this->m_numRows = 0;
|
||||
this->m_numCols = 0;
|
||||
this->m_elemSizeAllocated = 0;
|
||||
this->m_externalBuffer = false;
|
||||
this->m_pArray = NULL;
|
||||
this->m_computeDevice = CPUDEVICE;
|
||||
this->m_nz = 0;
|
||||
this->m_matrixName = NULL;
|
||||
|
||||
if(m_format == MatrixFormat::matrixFormatSparseCSC || m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseCSC || this->m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
{
|
||||
m_colIdx = -1;
|
||||
m_val = NULL;
|
||||
m_row = NULL;
|
||||
m_pb = NULL;
|
||||
this->m_colIdx = -1;
|
||||
this->m_val = NULL;
|
||||
this->m_row = NULL;
|
||||
this->m_pb = NULL;
|
||||
}
|
||||
else if (m_format == MatrixFormat::matrixFormatSparseBlockCol || m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
else if (this->m_format == MatrixFormat::matrixFormatSparseBlockCol || this->m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
m_blockSize = 0;
|
||||
m_blockVal = NULL;
|
||||
m_blockIds = NULL;
|
||||
this->m_blockSize = 0;
|
||||
this->m_blockVal = NULL;
|
||||
this->m_blockIds = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
template<class ElemType>
|
||||
CPUSparseMatrix<ElemType>::CPUSparseMatrix(const MatrixFormat format)
|
||||
{
|
||||
this->CheckInit(format);
|
||||
}
|
||||
|
||||
//should only be used by constructors.
|
||||
template<class ElemType>
|
||||
void CPUSparseMatrix<ElemType>::CheckInit(const MatrixFormat format)
|
||||
{
|
||||
if(format != MatrixFormat::matrixFormatSparseCSC && format != MatrixFormat::matrixFormatSparseCSR && format != MatrixFormat::matrixFormatSparseBlockCol && format != MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
throw std::logic_error("CPUSparseMatrix: unsupported sparse matrix format");
|
||||
}
|
||||
m_format = format;
|
||||
this->m_format = format;
|
||||
ZeroInit();
|
||||
}
|
||||
|
||||
template<class ElemType>
|
||||
CPUSparseMatrix<ElemType>::CPUSparseMatrix(const MatrixFormat format, const size_t numRows, const size_t numCols, const size_t size)
|
||||
{ CPUSparseMatrix<ElemType>::CPUSparseMatrix(format);
|
||||
{ this->CheckInit(format);
|
||||
Resize(numRows, numCols, size);
|
||||
}
|
||||
|
||||
template<class ElemType>
|
||||
CPUSparseMatrix<ElemType>::~CPUSparseMatrix()
|
||||
{
|
||||
if (m_matrixName!=NULL)
|
||||
if (this->m_matrixName!=NULL)
|
||||
{
|
||||
delete[] m_matrixName;
|
||||
m_matrixName = nullptr;
|
||||
delete[] this->m_matrixName;
|
||||
this->m_matrixName = nullptr;
|
||||
}
|
||||
if(m_format == MatrixFormat::matrixFormatSparseCSC || m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseCSC || this->m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
{
|
||||
if(m_val != NULL)
|
||||
delete[] m_val;
|
||||
if(m_row != NULL)
|
||||
delete[] m_row;
|
||||
if(m_pb != NULL)
|
||||
delete[] m_pb;
|
||||
if(this->m_val != NULL)
|
||||
delete[] this->m_val;
|
||||
if(this->m_row != NULL)
|
||||
delete[] this->m_row;
|
||||
if(this->m_pb != NULL)
|
||||
delete[] this->m_pb;
|
||||
}
|
||||
else if (m_format == MatrixFormat::matrixFormatSparseBlockCol || m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
else if (this->m_format == MatrixFormat::matrixFormatSparseBlockCol || this->m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
if(m_blockVal != NULL)
|
||||
delete[] m_blockVal;
|
||||
if(m_blockIds != NULL)
|
||||
delete[] m_blockIds;
|
||||
if(this->m_blockVal != NULL)
|
||||
delete[] this->m_blockVal;
|
||||
if(this->m_blockIds != NULL)
|
||||
delete[] this->m_blockIds;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -167,76 +176,76 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
template<class ElemType>
|
||||
void CPUSparseMatrix<ElemType>::SetValue(const size_t rIdx, const size_t cIdx, const ElemType v)
|
||||
{
|
||||
if(m_format != MatrixFormat::matrixFormatSparseCSC && m_format != MatrixFormat::matrixFormatSparseCSR)
|
||||
if(this->m_format != MatrixFormat::matrixFormatSparseCSC && this->m_format != MatrixFormat::matrixFormatSparseCSR)
|
||||
{
|
||||
throw std::logic_error("CPUSparseMatrix: unsupported SetValue() call.");
|
||||
}
|
||||
|
||||
if(m_elemSizeAllocated < m_nz +1) {
|
||||
if(this->m_elemSizeAllocated < this->m_nz +1) {
|
||||
throw std::logic_error("CPUSparseMatrix: allocated size is too small.");
|
||||
}
|
||||
|
||||
if(rIdx < 0 || rIdx >= m_numRows) {
|
||||
if(rIdx < 0 || rIdx >= this->m_numRows) {
|
||||
throw std::logic_error("CPUSparseMatrix: SetValue() invalid row id");
|
||||
}
|
||||
|
||||
if(cIdx < 0 || cIdx >= m_numCols) {
|
||||
if(cIdx < 0 || cIdx >= this->m_numCols) {
|
||||
throw std::logic_error("CPUSparseMatrix: SetValue() invalid column id");
|
||||
}
|
||||
|
||||
size_t r = (m_format == matrixFormatSparseCSC) ? rIdx: cIdx;
|
||||
size_t c = (m_format == matrixFormatSparseCSC) ? cIdx: rIdx;
|
||||
size_t r = (this->m_format == matrixFormatSparseCSC) ? rIdx: cIdx;
|
||||
size_t c = (this->m_format == matrixFormatSparseCSC) ? cIdx: rIdx;
|
||||
|
||||
m_val[m_nz] = v;
|
||||
m_row[m_nz] = r;
|
||||
this->m_val[this->m_nz] = v;
|
||||
this->m_row[this->m_nz] = r;
|
||||
|
||||
//consistency check
|
||||
if(c == m_colIdx && r <= m_row[m_nz-1])
|
||||
if(c == this->m_colIdx && r <= this->m_row[this->m_nz-1])
|
||||
{
|
||||
throw std::logic_error("CPUSparseMatrix: SetValue is not called properly");
|
||||
}
|
||||
|
||||
if (c != m_colIdx)
|
||||
if (c != this->m_colIdx)
|
||||
{
|
||||
m_pb[c] = m_nz;
|
||||
m_colIdx = (int) c;
|
||||
}
|
||||
m_pb[c+1] = m_nz+1;
|
||||
m_nz++;
|
||||
this->m_pb[c+1] = this->m_nz+1;
|
||||
this->m_nz++;
|
||||
}
|
||||
|
||||
template<class ElemType>
|
||||
ElemType* CPUSparseMatrix<ElemType>::BufferPointer() const
|
||||
{
|
||||
if(m_format == MatrixFormat::matrixFormatSparseCSC || m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseCSC || this->m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
{
|
||||
return m_val;
|
||||
return this->m_val;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_blockVal;
|
||||
return this->m_blockVal;
|
||||
}
|
||||
}
|
||||
|
||||
template<class ElemType>
|
||||
void CPUSparseMatrix<ElemType>::Resize(const size_t numRows, const size_t numCols, size_t size)
|
||||
{
|
||||
m_nz = 0;
|
||||
m_colIdx = -1;
|
||||
m_numRows = numRows;
|
||||
m_numCols = numCols;
|
||||
this->m_nz = 0;
|
||||
this->m_colIdx = -1;
|
||||
this->m_numRows = numRows;
|
||||
this->m_numCols = numCols;
|
||||
|
||||
if(m_elemSizeAllocated < size)
|
||||
if(this->m_elemSizeAllocated < size)
|
||||
{
|
||||
m_elemSizeAllocated = size;
|
||||
if(m_format == MatrixFormat::matrixFormatSparseCSC || m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
this->m_elemSizeAllocated = size;
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseCSC || this->m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
{
|
||||
if(m_val != NULL)
|
||||
delete[] m_val;
|
||||
if(m_row != NULL)
|
||||
delete[] m_row;
|
||||
if(m_pb != NULL)
|
||||
delete[] m_pb;
|
||||
if(this->m_val != NULL)
|
||||
delete[] this->m_val;
|
||||
if(this->m_row != NULL)
|
||||
delete[] this->m_row;
|
||||
if(this->m_pb != NULL)
|
||||
delete[] this->m_pb;
|
||||
|
||||
//int len = m_format == MatrixFormat::matrixFormatSparseCSC ? numCols : numRows;
|
||||
size_t len = numCols > numRows ? numCols : numRows;
|
||||
|
@ -245,12 +254,12 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
m_pb = new size_t[len+1];
|
||||
|
||||
}
|
||||
else if(m_format == MatrixFormat::matrixFormatSparseBlockCol || m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
else if(this->m_format == MatrixFormat::matrixFormatSparseBlockCol || this->m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
if(m_blockVal != NULL)
|
||||
delete[] m_blockVal;
|
||||
if(m_blockIds != NULL)
|
||||
delete[] m_blockIds;
|
||||
if(this->m_blockVal != NULL)
|
||||
delete[] this->m_blockVal;
|
||||
if(this->m_blockIds != NULL)
|
||||
delete[] this->m_blockIds;
|
||||
|
||||
size_t max = numCols > numRows ? numCols : numRows;
|
||||
m_blockVal = new ElemType[size];
|
||||
|
@ -263,9 +272,9 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
template<class ElemType>
|
||||
void CPUSparseMatrix<ElemType>::Reset()
|
||||
{
|
||||
m_nz = 0;
|
||||
m_colIdx = -1;
|
||||
m_blockSize = 0;
|
||||
this->m_nz = 0;
|
||||
this->m_colIdx = -1;
|
||||
this->m_blockSize = 0;
|
||||
}
|
||||
|
||||
//c = op(a) * op(this) or c += op(a) * op(this)
|
||||
|
@ -489,7 +498,11 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else
|
||||
{
|
||||
#ifndef LINUX
|
||||
throw std::exception("CPUSparseMatrix:: ScaleAndAdd() Not implemented");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -509,7 +522,11 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
throw std::logic_error("AssignSoftmaxOf: Matrix a, class, idx2cls or label is empty.");
|
||||
|
||||
if(etp.GetFormat() != MatrixFormat::matrixFormatSparseCSC)
|
||||
#ifndef LINUX
|
||||
throw std::exception("CPUSparseMatrix:: ClassEntropy() only support CSC");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
|
||||
size_t nC = cls.GetNumCols();
|
||||
size_t nV = label.GetNumRows() - nC;
|
||||
|
@ -682,7 +699,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
c.SetValue(0.0);
|
||||
}
|
||||
|
||||
if(m_format == MatrixFormat::matrixFormatSparseBlockCol || m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseBlockCol || this->m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
for(size_t j = 0; j < m_blockSize; j++)
|
||||
{
|
||||
|
@ -701,7 +718,11 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else
|
||||
{
|
||||
#ifndef LINUX
|
||||
throw std::exception("CPUSparseMatrix:: NormalGrad() only support block sparse format");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -716,7 +737,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
|
||||
const ElemType floor = 1e-16f;
|
||||
if(m_format == MatrixFormat::matrixFormatSparseCSC || m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseCSC || this->m_format == MatrixFormat::matrixFormatSparseCSR)
|
||||
{
|
||||
size_t col_num = (m_format == MatrixFormat::matrixFormatSparseCSC) ? GetNumCols() : GetNumRows();
|
||||
for(size_t j = 0; j < col_num; j++)
|
||||
|
@ -737,7 +758,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
c(row, col) = adenorm;
|
||||
}
|
||||
}
|
||||
} else if(m_format == MatrixFormat::matrixFormatSparseBlockCol || m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
} else if(this->m_format == MatrixFormat::matrixFormatSparseBlockCol || this->m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
for(size_t j = 0; j < m_blockSize; j++)
|
||||
{
|
||||
|
@ -746,7 +767,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
size_t start = j* len;
|
||||
for(size_t p = start; p < start+len; p++)
|
||||
{
|
||||
ElemType val = m_blockVal[p];
|
||||
ElemType val = this->m_blockVal[p];
|
||||
|
||||
size_t row = (m_format == MatrixFormat::matrixFormatSparseBlockCol) ? (p - start) : i;
|
||||
size_t col = (m_format == MatrixFormat::matrixFormatSparseBlockCol) ? i : (p - start);
|
||||
|
@ -763,7 +784,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
template<class ElemType>
|
||||
CPUSparseMatrix<ElemType>& CPUSparseMatrix<ElemType>::InplaceTruncate (const ElemType threshold)
|
||||
{
|
||||
if(m_format == MatrixFormat::matrixFormatSparseBlockCol || m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
if(this->m_format == MatrixFormat::matrixFormatSparseBlockCol || this->m_format == MatrixFormat::matrixFormatSparseBlockRow)
|
||||
{
|
||||
ElemType locThresholdPos = abs(threshold);
|
||||
ElemType locTHresholdNeg = -locThresholdPos;
|
||||
|
@ -774,20 +795,24 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
size_t start = j* len;
|
||||
for (size_t p = start; p < start+len; p++)
|
||||
{
|
||||
if (m_blockVal[p] > locThresholdPos)
|
||||
if (this->m_blockVal[p] > locThresholdPos)
|
||||
{
|
||||
m_blockVal[p] = locThresholdPos;
|
||||
this->m_blockVal[p] = locThresholdPos;
|
||||
}
|
||||
else if (m_blockVal[p] < locTHresholdNeg)
|
||||
else if (this->m_blockVal[p] < locTHresholdNeg)
|
||||
{
|
||||
m_blockVal[p] = locTHresholdNeg;
|
||||
this->m_blockVal[p] = locTHresholdNeg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifndef LINUX
|
||||
throw std::exception("CPUSparseMatrix:: InplaceTruncate() only support block based sparse matrix");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
|
|
@ -6,15 +6,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "cpumatrix.h"
|
||||
#include "CPUMatrix.h"
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifndef LINUX
|
||||
#ifdef MATH_EXPORTS
|
||||
#define MATH_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATH_API __declspec(dllimport)
|
||||
#endif
|
||||
#endif /* Linux - already defined in CPUMatrix.h */
|
||||
|
||||
namespace Microsoft { namespace MSR { namespace CNTK {
|
||||
|
||||
|
@ -24,6 +26,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
private:
|
||||
void ZeroInit();
|
||||
void CheckInit(const MatrixFormat format);
|
||||
|
||||
public:
|
||||
CPUSparseMatrix(const MatrixFormat format);
|
||||
|
@ -37,7 +40,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
void ShiftBy(int /*numShift*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
size_t BufferSize() const {return m_elemSizeAllocated*sizeof(ElemType);}
|
||||
size_t BufferSize() const {return this->m_elemSizeAllocated*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const;
|
||||
|
||||
void SetGaussianRandomValue(const ElemType /*mean*/, const ElemType /*sigma*/, unsigned long /*seed*/) { NOT_IMPLEMENTED; }
|
||||
|
@ -46,14 +49,14 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
const CPUSparseMatrix<ElemType> & label, const CPUMatrix<ElemType>& cls,
|
||||
const CPUMatrix<ElemType>& idx2cls, CPUSparseMatrix<ElemType>& etp, CPUMatrix<ElemType>& entropyScore);
|
||||
|
||||
static void CPUSparseMatrix<ElemType>::ClassEntropyError(CPUSparseMatrix<ElemType>& a);
|
||||
static void ClassEntropyError(CPUSparseMatrix<ElemType>& a);
|
||||
|
||||
static void CPUSparseMatrix<ElemType>::ClassEntropyGradientOfInput(
|
||||
static void ClassEntropyGradientOfInput(
|
||||
const CPUSparseMatrix<ElemType>& error,
|
||||
const CPUMatrix<ElemType>& weight,
|
||||
CPUMatrix<ElemType>& grd);
|
||||
|
||||
static void CPUSparseMatrix<ElemType>::ClassEntropyGradientOfWeight(
|
||||
static void ClassEntropyGradientOfWeight(
|
||||
const CPUSparseMatrix<ElemType>& error,
|
||||
const CPUMatrix<ElemType>& input,
|
||||
const CPUSparseMatrix<ElemType> & label,
|
||||
|
|
|
@ -6,6 +6,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef LINUX
|
||||
#define wcsnlen_s wcsnlen /* Not sure if this is best replacement... Malcolm */
|
||||
#endif
|
||||
|
||||
#define AUTOPLACEMATRIX 1000 // used in parameters only
|
||||
#define MANAGEDEXTERN -2 // managed externally (i.e. PTask)
|
||||
|
@ -77,7 +82,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
void SetMatrixName(const wchar_t* s)
|
||||
{
|
||||
Clear();
|
||||
if (s!=nullptr)
|
||||
if (s!=NULL)
|
||||
{
|
||||
size_t n = wcsnlen_s(s, SIZE_MAX);
|
||||
m_matrixName = new wchar_t[n+1];
|
||||
|
@ -102,10 +107,10 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
protected:
|
||||
void Clear()
|
||||
{
|
||||
if (m_matrixName!=nullptr)
|
||||
if (m_matrixName!=NULL)
|
||||
{
|
||||
delete[] m_matrixName;
|
||||
m_matrixName = nullptr;
|
||||
m_matrixName = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,4 +125,4 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
size_t m_nz; //Number of non-zero elements for sparse matrices (unused in other formats)
|
||||
wchar_t* m_matrixName;
|
||||
};
|
||||
}}}
|
||||
}}}
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -17,11 +17,17 @@ typedef struct cublasContext *cublasHandle_t;
|
|||
struct CUstream_st;
|
||||
typedef struct CUstream_st *cudaStream_t;
|
||||
|
||||
#ifndef LINUX
|
||||
#ifndef MATH_API
|
||||
#ifdef MATH_EXPORTS
|
||||
#define MATH_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATH_API __declspec(dllimport)
|
||||
#endif
|
||||
#endif /* MATH_API */
|
||||
#else /* LINUX */
|
||||
#define MATH_API
|
||||
#endif
|
||||
|
||||
#ifndef USE_TIME_BASED_SEED
|
||||
#define USE_TIME_BASED_SEED ULONG_MAX
|
||||
|
@ -45,10 +51,12 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
public:
|
||||
DeviceBoundNumber() {m_data=NULL;};
|
||||
DeviceBoundNumber(const DeviceBoundNumber<ElemType> &deepCopy);
|
||||
#ifndef LINUX
|
||||
DeviceBoundNumber(DeviceBoundNumber<ElemType> &&shallowCopy);
|
||||
#endif
|
||||
~DeviceBoundNumber();
|
||||
int GetDeviceId() const {return m_computeDevice;}
|
||||
ElemType* ExposePointer2Value() const {return m_data;}
|
||||
ElemType* ExposePointer2Value() const {return this->m_data;}
|
||||
//performs shallow copy only
|
||||
void ShallowCopyFrom(ElemType* newVal,int newValsDevceId);
|
||||
};
|
||||
|
@ -76,8 +84,10 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
GPUMatrix(const size_t numRows, const size_t numCols, ElemType *pArray, const size_t matrixFlags=matrixFlagNormal,int deviceId=0);
|
||||
GPUMatrix(const GPUMatrix<ElemType>& deepCopyFrom);
|
||||
GPUMatrix<ElemType>& operator=(const GPUMatrix<ElemType>& deepCopyFrom); //assignment operator, deep copy
|
||||
#ifndef LINUX
|
||||
GPUMatrix(GPUMatrix<ElemType>&& moveFrom);
|
||||
GPUMatrix<ElemType>& operator=(GPUMatrix<ElemType>&& moveFrom); //move assignment operator, shallow copy
|
||||
#endif /* LINUX */
|
||||
~GPUMatrix(void);
|
||||
|
||||
static int GetBestGPUDeviceId();
|
||||
|
@ -95,8 +105,8 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
GPUMatrix<ElemType> ColumnSlice(size_t startColumn, size_t numCols) const;
|
||||
GPUMatrix<ElemType>& AssignColumnSlice(const GPUMatrix<ElemType>& fromMatrix, size_t startColumn, size_t numCols);
|
||||
|
||||
size_t BufferSize() const {return m_numRows*m_numCols*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const {return m_pArray;}
|
||||
size_t BufferSize() const {return this->m_numRows*this->m_numCols*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const {return this->m_pArray;}
|
||||
|
||||
void Adagrad(GPUMatrix<ElemType>& gradients);
|
||||
void RmsProp(GPUMatrix<ElemType>& gradients,
|
||||
|
@ -109,8 +119,8 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
void Reshape(const size_t numRows, const size_t numCols);
|
||||
void Resize(const size_t numRows, const size_t numCols, bool growOnly = true); //by default we only reallocate if need to grow
|
||||
|
||||
ElemType& operator() (const size_t /*row*/, const size_t /*col*/) { throw std::exception("GPUMatrix doesn't support this"); }
|
||||
const ElemType& operator() (const size_t /*row*/, const size_t /*col*/) const { throw std::exception("GPUMatrix doesn't support this"); }
|
||||
ElemType& operator() (const size_t /*row*/, const size_t /*col*/) { throw std::logic_error("GPUMatrix doesn't support this"); }
|
||||
const ElemType& operator() (const size_t /*row*/, const size_t /*col*/) const { throw std::logic_error("GPUMatrix doesn't support this"); }
|
||||
ElemType Get00Element() const;
|
||||
|
||||
void SetValue(const ElemType v);
|
||||
|
@ -262,7 +272,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
GPUMatrix<ElemType>& AssignInnerProductOfMatrices(const GPUMatrix<ElemType>& a, const GPUMatrix<ElemType>& b);
|
||||
|
||||
void Print(const char* matrixName, size_t rowStart, size_t rowEnd, size_t colStart, size_t colEnd) const;
|
||||
void Print(const char* matrixName = nullptr) const; //print whole matrix. can be expensive
|
||||
void Print(const char* matrixName = NULL) const; //print whole matrix. can be expensive
|
||||
|
||||
void ReadFromFile(FILE* f, const char * matrixName); //matrixName is used to verify that correct matrix is read.
|
||||
void WriteToFile(FILE* f, const char * matrixName); //matrixName is used to verify that correct matrix is read.
|
||||
|
@ -334,7 +344,11 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
size_t elsize;
|
||||
stream>>elsize;
|
||||
if (sizeof(ElemType)!=elsize)
|
||||
#ifndef LINUX
|
||||
throw std::exception("Template argument size doesn't match those in file");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif
|
||||
std::wstring matrixName;
|
||||
size_t numRows, numCols;
|
||||
int format;
|
||||
|
|
|
@ -2626,7 +2626,11 @@ __global__ void _normalGrad(
|
|||
}
|
||||
}
|
||||
|
||||
#ifndef LINUX
|
||||
static __inline__ __device__ double atomicAdd(double* address, double val)
|
||||
#else
|
||||
static __device__ double atomicAdd(double* address, double val)
|
||||
#endif
|
||||
{
|
||||
unsigned long long int* address_as_ull = (unsigned long long int*)address;
|
||||
unsigned long long int old = *address_as_ull, assumed;
|
||||
|
@ -3223,4 +3227,4 @@ else
|
|||
d_tmp[0] = max((ElemType)0, d_tmp[0]/max((ElemType)1.0e-10,sqrt(d_tmp[1]))/max((ElemType)1.0e-10,sqrt(d_tmp[2])));
|
||||
}
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -27,7 +27,9 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
void performInplaceFunction(int kind);
|
||||
void DeepCopy(const GPUSparseMatrix<ElemType>& deepCopyFrom);
|
||||
void Clear();
|
||||
#ifndef LINUX
|
||||
void PrepareBuffer(size_t m, size_t n, bool canReuseBuffer, std::function<size_t (int* csrRowPtrC)> func);
|
||||
#endif
|
||||
size_t ElemCountFromBufferSize(size_t totalBufferSize);
|
||||
void PrepareDevice(short deviceId=-1) const;
|
||||
|
||||
|
@ -39,7 +41,9 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
GPUSparseMatrix(const GPUSparseMatrix<ElemType>&);
|
||||
GPUSparseMatrix(const GPUMatrix<ElemType>&);
|
||||
#ifndef LINUX
|
||||
GPUSparseMatrix(GPUSparseMatrix<ElemType>&&);
|
||||
#endif /* LINUX */
|
||||
~GPUSparseMatrix();
|
||||
public:
|
||||
void Resize(const size_t numRows, const size_t numCols, size_t size = 0);
|
||||
|
@ -50,22 +54,22 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
// in memory format is always in the following order:
|
||||
// Non-zero data elements, Full index locations, compressed index locations
|
||||
// In CSR row data is compressed, in CSC col data is compressed
|
||||
const ElemType* NzLocation() const {return m_pArray;}
|
||||
ElemType* NzLocation() {return m_pArray;}
|
||||
size_t NzCount() const {return m_nz;}
|
||||
size_t NzSize() const {return sizeof(ElemType)*m_nz;} // actual number of element bytes in use
|
||||
int* IndexLocation() const {return (int*)(m_pArray+m_elemSizeAllocated);}
|
||||
size_t IndexSize() const {return sizeof(int)*m_nz;} // actual number of index bytes in use
|
||||
int* CompressedIndexLocation() const {return IndexLocation() + m_elemSizeAllocated;}
|
||||
const ElemType* NzLocation() const {return this->m_pArray;}
|
||||
ElemType* NzLocation() {return this->m_pArray;}
|
||||
size_t NzCount() const {return this->m_nz;}
|
||||
size_t NzSize() const {return sizeof(ElemType)*this->m_nz;} // actual number of element bytes in use
|
||||
int* IndexLocation() const {return (int*)(this->m_pArray+this->m_elemSizeAllocated);}
|
||||
size_t IndexSize() const {return sizeof(int)*this->m_nz;} // actual number of index bytes in use
|
||||
int* CompressedIndexLocation() const {return IndexLocation() + this->m_elemSizeAllocated;}
|
||||
size_t CompressedIndexCount() const
|
||||
{
|
||||
if (m_format&matrixFormatCompressed)
|
||||
if (this->m_format&matrixFormatCompressed)
|
||||
{
|
||||
size_t cnt = (m_format&matrixFormatRowMajor)?m_numRows:m_numCols;
|
||||
size_t cnt = (this->m_format&matrixFormatRowMajor)?this->m_numRows:this->m_numCols;
|
||||
if (cnt) cnt++; // add an extra element on the end for the "max" value
|
||||
return cnt;
|
||||
}
|
||||
return m_nz; // COO format
|
||||
return this->m_nz; // COO format
|
||||
}
|
||||
// get size for compressed index
|
||||
size_t CompressedIndexSize() const {return (CompressedIndexCount())*sizeof(int);}
|
||||
|
@ -73,10 +77,10 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
ElemType* BufferPointer() const;
|
||||
|
||||
// the column and row locations will swap based on what format we are in. Full index always follows the data array
|
||||
int* RowLocation() const {return (m_format&matrixFormatRowMajor)?CompressedIndexLocation():IndexLocation();}
|
||||
size_t RowSize() const {return (m_format&matrixFormatRowMajor)?CompressedIndexSize():IndexSize();}
|
||||
int* ColLocation() const {return (m_format&matrixFormatRowMajor)?IndexLocation():CompressedIndexLocation();}
|
||||
size_t ColSize() const {return (m_format&matrixFormatRowMajor)?IndexSize():CompressedIndexSize();} // actual number of row bytes in use
|
||||
int* RowLocation() const {return (this->m_format&matrixFormatRowMajor)?CompressedIndexLocation():IndexLocation();}
|
||||
size_t RowSize() const {return (this->m_format&matrixFormatRowMajor)?CompressedIndexSize():IndexSize();}
|
||||
int* ColLocation() const {return (this->m_format&matrixFormatRowMajor)?IndexLocation():CompressedIndexLocation();}
|
||||
size_t ColSize() const {return (this->m_format&matrixFormatRowMajor)?IndexSize():CompressedIndexSize();} // actual number of row bytes in use
|
||||
|
||||
void SetValue(const GPUSparseMatrix<ElemType>& deepCopyFrom);
|
||||
void SetValue(const GPUMatrix<ElemType>& denseMatrix);
|
||||
|
@ -89,7 +93,9 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
GPUMatrix<ElemType> CopyToDenseMatrix();
|
||||
GPUSparseMatrix<ElemType>& operator=(const GPUSparseMatrix<ElemType>& deepCopy);
|
||||
#ifndef LINUX
|
||||
GPUSparseMatrix<ElemType>& operator=(GPUSparseMatrix<ElemType>&& moveFrom);
|
||||
#endif /* LINUX */
|
||||
GPUSparseMatrix<ElemType> operator+ (const GPUSparseMatrix<ElemType>& a) const;
|
||||
GPUSparseMatrix<ElemType> operator- (const GPUSparseMatrix<ElemType>& a) const;
|
||||
GPUSparseMatrix<ElemType>& operator^= (ElemType alpha); //element-wise power
|
||||
|
@ -102,7 +108,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
bool IsEqualTo(const GPUMatrix<ElemType>& a, const ElemType threshold = 1e-8) const;
|
||||
public:
|
||||
int GetComputeDeviceId(void) const;
|
||||
size_t GetNZElements() const {return m_nz;}
|
||||
size_t GetNZElements() const {return this->m_nz;}
|
||||
//Sets sparse matrix in CSR format. this acts as deep copy
|
||||
void SetMatrixFromCSRFormat(int *h_CSRRow, int *h_Col, ElemType *h_Val, size_t nz, size_t numRows, size_t numCols, bool IsOnDevice=false, int devId=0);
|
||||
void SetMatrixFromCSCFormat(size_t *h_row, size_t *h_rowIdx, size_t size, size_t blockSize);
|
||||
|
@ -199,10 +205,11 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
|
||||
public:
|
||||
template <class ElemType>
|
||||
friend MATH_API File& operator>>(File& stream, GPUSparseMatrix<ElemType>& us);
|
||||
template <class ElemType>
|
||||
friend MATH_API File& operator<<(File& stream, const GPUSparseMatrix<ElemType>& us);
|
||||
// See: http://stackoverflow.com/questions/4660123/overloading-friend-operator-for-template-class/4661372#4661372
|
||||
template <class ElemTypeDummy>
|
||||
friend MATH_API File& operator>>(File& stream, GPUSparseMatrix<ElemTypeDummy>& us);
|
||||
template <class ElemTypeDummy>
|
||||
friend MATH_API File& operator<<(File& stream, const GPUSparseMatrix<ElemTypeDummy>& us);
|
||||
|
||||
bool m_legacy;
|
||||
int m_colIdx; //used to SetValue()
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#include "CPUMatrix.cpp"
|
||||
#include "Matrix.cpp"
|
||||
|
||||
#include "..\..\common\include\fileutil.cpp"
|
||||
#include "..\..\common\include\File.cpp"
|
||||
#include "..\..\Common\Include\fileutil.cpp"
|
||||
#include "..\..\Common\Include\File.cpp"
|
||||
|
||||
//don't treat it as sample code. some code does not make sense
|
||||
//only used to force compiler to build the code
|
||||
|
@ -343,4 +343,4 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
CallEverythingInMatrix<double>();
|
||||
}
|
||||
#pragma endregion instantiate all classes
|
||||
}}}
|
||||
}}}
|
||||
|
|
|
@ -8,11 +8,17 @@
|
|||
#include "fileutil.h"
|
||||
#include "Matrix.h"
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
|
||||
#pragma warning (disable: 4127) // conditional expression is constant; "if (sizeof(ElemType)==sizeof(float))" triggers this
|
||||
#pragma warning (disable: 4239) // nonstandard extension; triggered by this pattern: "auto& second = transposeB ? b.m_GPUMatrix->Transpose() : *b.m_GPUMatrix;"
|
||||
#pragma warning (disable: 4702) // unreachable code; triggered for unknown reasons
|
||||
|
||||
#ifndef min
|
||||
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
|
||||
//before calling the following macro the current matrix location and matrix type on MatrixPointerToCheck must have been set correctly
|
||||
#define DISPATCH_MATRIX_ON_FLAG(MatrixPointerToCheck, MatrixPointerToSetFlag, CPUDense, GPUDense, CPUSparse, GPUSparse) \
|
||||
{ \
|
||||
|
@ -218,7 +224,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
//matrixName is used to verify that correct matrix is read.
|
||||
template<class ElemType>
|
||||
Matrix<ElemType>::Matrix(FILE* f, const char * matrixName, short deviceId=AUTOPLACEMATRIX, const MatrixType matrixType = DENSE)
|
||||
Matrix<ElemType>::Matrix(FILE* f, const char * matrixName, short deviceId, const MatrixType matrixType)
|
||||
{
|
||||
if (deviceId == MANAGEDEXTERN)
|
||||
throw runtime_error("Externally Managed Matrix must use the basic constructor, then SetValue()\n");
|
||||
|
@ -256,7 +262,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
|
||||
template<class ElemType>
|
||||
Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, short deviceId=AUTOPLACEMATRIX, const MatrixType matrixType = DENSE)
|
||||
Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, short deviceId, const MatrixType matrixType)
|
||||
{
|
||||
if (deviceId == MANAGEDEXTERN)
|
||||
throw runtime_error("Externally Managed Matrix must use the basic constructor, then SetValue(), or the full constructor\n");
|
||||
|
@ -294,7 +300,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
|
||||
template<class ElemType>
|
||||
Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, ElemType *pArray, const size_t matrixFlags, short deviceId=AUTOPLACEMATRIX, const size_t nnz=0)
|
||||
Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, ElemType *pArray, const size_t matrixFlags, short deviceId, const size_t nnz)
|
||||
{
|
||||
Init(deviceId);
|
||||
|
||||
|
@ -332,7 +338,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
//copy constructor, deep copy
|
||||
template<class ElemType>
|
||||
Matrix<ElemType>::Matrix(const Matrix<ElemType>& deepCopyFrom, short deviceId=AUTOPLACEMATRIX)
|
||||
Matrix<ElemType>::Matrix(const Matrix<ElemType>& deepCopyFrom, short deviceId)
|
||||
{
|
||||
if (deviceId == MANAGEDEXTERN)
|
||||
throw runtime_error("Externally Managed Matrix must use the basic constructor, then SetValue(), or the full constructor\n");
|
||||
|
@ -519,7 +525,13 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
NOT_IMPLEMENTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifndef LINUX
|
||||
throw std::exception("Unknown matrix type");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
}
|
||||
}
|
||||
|
||||
template<class ElemType>
|
||||
|
@ -608,7 +620,13 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
NOT_IMPLEMENTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifndef LINUX
|
||||
throw std::exception("Unknown matrix type");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
}
|
||||
|
||||
return slice;
|
||||
}
|
||||
|
@ -820,7 +838,14 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
DISPATCH_MATRIX_ON_FLAG(this,
|
||||
this,
|
||||
m_CPUMatrix->SetValue(*db_number.ExposePointer2Value()),
|
||||
if (GetDeviceId()!=db_number.GetDeviceId()) throw std::exception("Matrix and device bound number must be on the same device");
|
||||
if (GetDeviceId()!=db_number.GetDeviceId())
|
||||
{
|
||||
#ifndef LINUX
|
||||
throw std::exception("Matrix and device bound number must be on the same device");
|
||||
#else
|
||||
throw std::exception();
|
||||
#endif /* LINUX */
|
||||
}
|
||||
m_GPUMatrix->SetValue(db_number.ExposePointer2Value()),
|
||||
NOT_IMPLEMENTED,
|
||||
NOT_IMPLEMENTED
|
||||
|
@ -889,7 +914,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
//WARNING: what's the exact meaning of MANAGEDEXTERN here? This is not handled currently
|
||||
template<class ElemType>
|
||||
void Matrix<ElemType>::SetValue(const size_t numRows, const size_t numCols, ElemType *pArray, const size_t matrixFlags, int deviceId=MANAGEDEXTERN)
|
||||
void Matrix<ElemType>::SetValue(const size_t numRows, const size_t numCols, ElemType *pArray, const size_t matrixFlags, int deviceId)
|
||||
{
|
||||
if (pArray == nullptr)
|
||||
throw std::invalid_argument("Invalid pArray.");
|
||||
|
@ -1054,7 +1079,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
//maskRate: percentage of values masked out (similar to dropout rate)
|
||||
//scaleValue: which scale value to set to the left ones (unmasked items).
|
||||
template<class ElemType>
|
||||
void Matrix<ElemType>::SetUniformRandomMask(const ElemType maskRate, const ElemType scaleValue, unsigned long seed=USE_TIME_BASED_SEED)
|
||||
void Matrix<ElemType>::SetUniformRandomMask(const ElemType maskRate, const ElemType scaleValue, unsigned long seed)
|
||||
{
|
||||
if (IsEmpty())
|
||||
throw std::logic_error("SetUniformRandomMask: Matrix is empty.");
|
||||
|
@ -2308,24 +2333,24 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
if (sizeof(ElemType)==sizeof(float))
|
||||
{
|
||||
if (!_finitef((float)threshold))
|
||||
if (!isfinite((float)threshold))
|
||||
return *this;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_finite(threshold))
|
||||
if (!isfinite(threshold))
|
||||
return *this;
|
||||
}
|
||||
|
||||
DISPATCH_MATRIX_ON_FLAG(this,
|
||||
this,
|
||||
this->m_CPUMatrix->InplaceTruncate(threshold),
|
||||
this->m_GPUMatrix->InplaceTruncateTop(abs(threshold)); this->m_GPUMatrix->InplaceTruncateBottom(-abs(threshold)),
|
||||
this->m_GPUMatrix->InplaceTruncateTop(fabs(threshold)); this->m_GPUMatrix->InplaceTruncateBottom(-fabs(threshold)),
|
||||
this->m_CPUSparseMatrix->InplaceTruncate(threshold),
|
||||
if(this->m_GPUSparseMatrix->m_legacy)
|
||||
{
|
||||
this->m_GPUSparseMatrix->InplaceTruncateTop(abs(threshold));
|
||||
this->m_GPUSparseMatrix->InplaceTruncateBottom(-abs(threshold));
|
||||
this->m_GPUSparseMatrix->InplaceTruncateTop(fabs(threshold));
|
||||
this->m_GPUSparseMatrix->InplaceTruncateBottom(-fabs(threshold));
|
||||
}
|
||||
else //new GPU Sparse matrix
|
||||
{
|
||||
|
@ -2345,12 +2370,12 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
if (sizeof(ElemType)==sizeof(float))
|
||||
{
|
||||
if (!_finitef((float)threshold))
|
||||
if (!isfinite((float)threshold))
|
||||
return *this;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_finite(threshold))
|
||||
if (!isfinite(threshold))
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -2374,7 +2399,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
if (sizeof(ElemType)==sizeof(float))
|
||||
{
|
||||
if (!_finitef((float)threshold))
|
||||
if (!isfinite((float)threshold))
|
||||
{
|
||||
(*this) = a;
|
||||
return *this;
|
||||
|
@ -2382,7 +2407,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else
|
||||
{
|
||||
if (!_finite(threshold))
|
||||
if (!isfinite(threshold))
|
||||
{
|
||||
(*this) = a;
|
||||
return *this;
|
||||
|
@ -2412,12 +2437,12 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
if (sizeof(ElemType)==sizeof(float))
|
||||
{
|
||||
if (!_finitef((float)threshold))
|
||||
if (!isfinite((float)threshold))
|
||||
return *this;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_finite(threshold))
|
||||
if (!isfinite(threshold))
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -2440,7 +2465,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
|
||||
if (sizeof(ElemType)==sizeof(float))
|
||||
{
|
||||
if (!_finitef((float)threshold))
|
||||
if (!isfinite((float)threshold))
|
||||
{
|
||||
(*this) = a;
|
||||
return *this;
|
||||
|
@ -2448,7 +2473,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else
|
||||
{
|
||||
if (!_finite(threshold))
|
||||
if (!isfinite(threshold))
|
||||
{
|
||||
(*this) = a;
|
||||
return *this;
|
||||
|
@ -3396,7 +3421,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else if (a.m_matrixType==MatrixType::SPARSE && b.m_matrixType==c.m_matrixType && b.m_matrixType==MatrixType::DENSE) //Sparse*Dense+Dense
|
||||
{
|
||||
auto& second = transposeB ? b.m_GPUMatrix->Transpose() : *b.m_GPUMatrix;
|
||||
GPUMatrix<ElemType> second = transposeB ? b.m_GPUMatrix->Transpose() : *b.m_GPUMatrix;
|
||||
GPUSparseMatrix<ElemType>::MultiplyAndWeightedAdd(alpha,*a.m_GPUSparseMatrix,transposeA,second,beta,*c.m_GPUMatrix);
|
||||
c.SetDataLocation(GPU, DENSE);
|
||||
}
|
||||
|
@ -3409,8 +3434,10 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else
|
||||
{
|
||||
auto& first = transposeA ? a.m_GPUMatrix->Transpose()*alpha : (*a.m_GPUMatrix)*alpha;
|
||||
auto& second = transposeB ? b.m_GPUSparseMatrix->Transpose() : *b.m_GPUSparseMatrix;
|
||||
GPUMatrix<ElemType> firstDummy = transposeA ? a.m_GPUMatrix->Transpose()*alpha : (*a.m_GPUMatrix)*alpha;
|
||||
GPUMatrix<ElemType> & first= firstDummy; // By Malcolm.. gcc doesn't support auto
|
||||
GPUSparseMatrix<ElemType> secondDummy = transposeB ? b.m_GPUSparseMatrix->Transpose() : *b.m_GPUSparseMatrix;
|
||||
GPUSparseMatrix<ElemType> & second = secondDummy; // By Malcolm.. gcc doesn't support auto
|
||||
if (beta==0)
|
||||
{
|
||||
GPUSparseMatrix<ElemType>::Multiply(first,second,*c.m_GPUMatrix);
|
||||
|
@ -3432,7 +3459,8 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
else if (a.m_matrixType==b.m_matrixType && b.m_matrixType==c.m_matrixType && a.m_matrixType==MatrixType::SPARSE)
|
||||
{
|
||||
auto& first = alpha==1 ? *a.m_GPUSparseMatrix : (*a.m_GPUSparseMatrix)*alpha;
|
||||
GPUSparseMatrix<ElemType> firstDummy = alpha==1 ? *a.m_GPUSparseMatrix : (*a.m_GPUSparseMatrix)*alpha;
|
||||
GPUSparseMatrix<ElemType> & first = firstDummy; // By Malcolm.. gcc doesn't support auto
|
||||
if (beta==0)
|
||||
{
|
||||
GPUSparseMatrix<ElemType>::Multiply(first,transposeA,*b.m_GPUSparseMatrix,transposeB,*c.m_GPUSparseMatrix);
|
||||
|
@ -3897,18 +3925,16 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
template<class ElemType>
|
||||
bool Matrix<ElemType>::HasNan (const char * name) const
|
||||
{
|
||||
#if 0
|
||||
name;
|
||||
return false;
|
||||
#else
|
||||
const auto & us = *this;
|
||||
// const auto & us = *this;
|
||||
const Matrix<ElemType> & us = *this;
|
||||
|
||||
foreach_coord (i, j, us)
|
||||
if (_isnan (us(i,j)))
|
||||
// if (isnan (us(i,j)))
|
||||
if (isnan (us(i,j)))
|
||||
{
|
||||
fprintf (stderr, "hasnan: NaN detected at %s (%d,%d)\n", name, i, j);
|
||||
fprintf (stderr, "hasnan: NaN detected at %s (%ld,%ld)\n", name, i, j);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
#define CheckNan(m) m.HasNan (#m)
|
||||
|
@ -3924,7 +3950,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
foreach_coord (i, j, us)
|
||||
{
|
||||
auto val = us(i,j);
|
||||
if (_isnan (val) || !_finite (val))
|
||||
if (isnan (val) || !isfinite (val))
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
|
|
|
@ -14,10 +14,10 @@
|
|||
|
||||
#define _CRT_SECURE_NO_WARNINGS // "secure" CRT not available on all platforms --add this at the top of all CPP files that give "function or variable may be unsafe" warnings
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
// Windows Header Files:
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
// TODO: reference additional headers your program requires here
|
||||
|
|
|
@ -5,9 +5,13 @@
|
|||
//
|
||||
#pragma once
|
||||
|
||||
#ifndef LINUX
|
||||
|
||||
// Including SDKDDKVer.h defines the highest available Windows platform.
|
||||
|
||||
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
|
||||
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
|
||||
|
||||
#include <SDKDDKVer.h>
|
||||
|
||||
#endif /* LINUX */
|
||||
|
|
Загрузка…
Ссылка в новой задаче