Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
8
Issues
8
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
NiuTrans
NiuTrans.Tensor
Commits
90599e05
Commit
90599e05
authored
Mar 11, 2021
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add mutexes to XMem
parent
59ccf22d
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
83 行增加
和
34 行删除
+83
-34
source/tensor/XGlobal.h
+30
-0
source/tensor/XMem.cpp
+37
-3
source/tensor/XMem.h
+14
-0
source/tensor/XThread.h
+0
-31
source/train/TTrain.cpp
+2
-0
没有找到文件。
source/tensor/XGlobal.h
查看文件 @
90599e05
...
...
@@ -132,6 +132,36 @@ extern int TRAINING_SAMPLE_BUF_SIZE;
extern
int
CONST_MINUSONE
;
extern
bool
CONST_TRUE
;
//////////////////////////////////////////////////
// mutex
#ifdef WIN32
#define THREAD_HANDLE HANDLE
#define MUTEX_HANDLE CRITICAL_SECTION
#define COND_HANDLE HANDLE
#define MUTEX_INIT( x ) InitializeCriticalSection( &(x) )
#define MUTEX_DELE( x ) DeleteCriticalSection( &(x) )
#define MUTEX_LOCK( x ) EnterCriticalSection( &(x) )
#define MUTEX_UNLOCK( x ) LeaveCriticalSection( &(x) )
#define COND_INIT( x ) ( x = CreateEvent( NULL, false, false, NULL ) )
#define COND_DELE( x ) CloseHandle( (x) )
#define COND_WAIT( x, y ) WaitForSingleObject( (x), INFINITE )
#define COND_SIGNAL( x ) SetEvent( (x) )
#define COND_RESET( x) ResetEvent( (x) )
#else
#define THREAD_HANDLE pthread_t
#define MUTEX_HANDLE pthread_mutex_t
#define COND_HANDLE pthread_cond_t
#define MUTEX_INIT( x ) pthread_mutex_init( &(x), NULL )
#define MUTEX_DELE( x ) pthread_mutex_destroy( &(x) )
#define MUTEX_LOCK( x ) pthread_mutex_lock( &(x) )
#define MUTEX_UNLOCK( x ) pthread_mutex_unlock( &(x) )
#define COND_INIT( x ) pthread_cond_init( &(x), NULL )
#define COND_DELE( x ) pthread_cond_destroy( &(x) )
#define COND_WAIT( x, y ) pthread_cond_wait( &(x), &(y) )
#define COND_SIGNAL( x ) pthread_cond_signal( &(x) )
#define COND_BROADCAST( x ) pthread_cond_broadcast( &(x) )
#endif
//#define USE_CUDA_RESURSION 1
#define NIUTRANSNNDEBUG
...
...
source/tensor/XMem.cpp
查看文件 @
90599e05
...
...
@@ -54,6 +54,8 @@ XMem::XMem()
signature
=
0
;
mergeFreeOTF
=
true
;
isInitialized
=
false
;
MUTEX_INIT
(
allocMutex
);
MUTEX_INIT
(
bufMutex
);
}
/*
...
...
@@ -77,6 +79,8 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum,
strcpy
(
name
,
"xmem"
);
signature
=
0
;
mergeFreeOTF
=
true
;
MUTEX_INIT
(
allocMutex
);
MUTEX_INIT
(
bufMutex
);
Initialize
(
myDevID
,
myMode
,
myBlockSize
,
myBlockNum
,
myBufSize
);
}
...
...
@@ -99,6 +103,8 @@ XMem::~XMem()
delete
[]
memIndex
;
delete
[]
memIndex2
;
delete
[]
minSizeIndex
;
MUTEX_DELE
(
allocMutex
);
MUTEX_DELE
(
bufMutex
);
}
/*
...
...
@@ -379,12 +385,18 @@ require a piece of memory
*/
void
*
XMem
::
Alloc
(
int
myDevID
,
MTYPE
mySize
)
{
void
*
p
=
NULL
;
MUTEX_LOCK
(
allocMutex
);
if
(
mode
==
FREE_ON_THE_FLY
)
return
AllocStandard
(
myDevID
,
mySize
);
p
=
AllocStandard
(
myDevID
,
mySize
);
else
if
(
isStatic
)
return
AllocStatic
(
myDevID
,
mySize
);
p
=
AllocStatic
(
myDevID
,
mySize
);
else
return
AllocDynamic
(
myDevID
,
mySize
);
p
=
AllocDynamic
(
myDevID
,
mySize
);
MUTEX_UNLOCK
(
allocMutex
);
return
p
;
}
/*
...
...
@@ -521,6 +533,11 @@ void * XMem::AllocBuf(int myDevID, MTYPE mySize, int pitch)
{
MTYPE
backOffset
=
0
;
/* NOTE THAT this is tricky because we lock the buffer
but DO NOT unlock it in this function. The unlock would
happans when we call ReleaseBuf() */
//MUTEX_LOCK(bufMutex);
if
(
pitch
>
1
){
MTYPE
address
=
(
MTYPE
)((
char
*
)
buf
+
bufUsed
);
int
offset
=
address
%
pitch
;
...
...
@@ -560,8 +577,10 @@ release a piece of memory
*/
void
XMem
::
Release
(
int
myDevID
,
void
*
p
,
MTYPE
size
)
{
MUTEX_LOCK
(
allocMutex
);
if
(
mode
==
FREE_ON_THE_FLY
)
ReleaseStandard
(
myDevID
,
p
,
size
);
MUTEX_UNLOCK
(
allocMutex
);
}
/*
...
...
@@ -583,6 +602,9 @@ void XMem::ReleaseBuf(int myDevID, MTYPE mySize, int pitch)
}
bufUsed
-=
(
mySize
+
backOffset
);
/* NOTE THAT this is a response to the lock in AllocBuf() */
//MUTEX_UNLOCK(bufMutex);
}
/*
...
...
@@ -825,6 +847,18 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
return
result
;
}
/* lock the buffer mutex */
void
XMem
::
LockBuf
()
{
MUTEX_LOCK
(
bufMutex
);
}
/* unlock the buffer mutex */
void
XMem
::
UnlockBuf
()
{
MUTEX_UNLOCK
(
bufMutex
);
}
/*
find the highest set bit (or most significant set bit) in an integer-64
>> mySize - required size
...
...
source/tensor/XMem.h
查看文件 @
90599e05
...
...
@@ -24,6 +24,7 @@
#ifndef __XMEM_H__
#define __XMEM_H__
#include "XGlobal.h"
#include <stdio.h>
#include <stdlib.h>
...
...
@@ -249,6 +250,13 @@ public:
/* indicates whether we merge free memory pieces on the fly */
bool
mergeFreeOTF
;
private
:
/* a mutex for memory allocation and release */
MUTEX_HANDLE
allocMutex
;
/* a mutex for buffer memory allocation and release */
MUTEX_HANDLE
bufMutex
;
public
:
/* constructor */
...
...
@@ -337,6 +345,12 @@ public:
/* allocate a piece of memory as "malloc" */
void
*
AllocStandard
(
int
myDevID
,
MTYPE
mySize
,
bool
myIsRebuiltIndex
=
false
);
/* lock the buffer mutex */
void
LockBuf
();
/* unlock the buffer mutex */
void
UnlockBuf
();
/* find the highest set bit (or most significant set bit) in an integer-64 */
int
GetMSB
(
MTYPE
mySize
);
...
...
source/tensor/XThread.h
查看文件 @
90599e05
...
...
@@ -54,37 +54,6 @@ namespace nts{
(unsigned)(flag), (unsigned *)(id))
#endif
//////////////////////////////////////////////////
// mutex
#ifdef WIN32
#define THREAD_HANDLE HANDLE
#define MUTEX_HANDLE CRITICAL_SECTION
#define COND_HANDLE HANDLE
#define MUTEX_INIT( x ) InitializeCriticalSection( &(x) )
#define MUTEX_DELE( x ) DeleteCriticalSection( &(x) )
#define MUTEX_LOCK( x ) EnterCriticalSection( &(x) )
#define MUTEX_UNLOCK( x ) LeaveCriticalSection( &(x) )
#define COND_INIT( x ) ( x = CreateEvent( NULL, false, false, NULL ) )
#define COND_DELE( x ) CloseHandle( (x) )
#define COND_WAIT( x, y ) WaitForSingleObject( (x), INFINITE )
#define COND_SIGNAL( x ) SetEvent( (x) )
#define COND_RESET( x) ResetEvent( (x) )
#else
#define THREAD_HANDLE pthread_t
#define MUTEX_HANDLE pthread_mutex_t
#define COND_HANDLE pthread_cond_t
#define MUTEX_INIT( x ) pthread_mutex_init( &(x), NULL )
#define MUTEX_DELE( x ) pthread_mutex_destroy( &(x) )
#define MUTEX_LOCK( x ) pthread_mutex_lock( &(x) )
#define MUTEX_UNLOCK( x ) pthread_mutex_unlock( &(x) )
#define COND_INIT( x ) pthread_cond_init( &(x), NULL )
#define COND_DELE( x ) pthread_cond_destroy( &(x) )
#define COND_WAIT( x, y ) pthread_cond_wait( &(x), &(y) )
#define COND_SIGNAL( x ) pthread_cond_signal( &(x) )
#define COND_BROADCAST( x ) pthread_cond_broadcast( &(x) )
#endif
typedef
void
(
*
TFunction
)
(
volatile
XList
*
);
/*
...
...
source/train/TTrain.cpp
查看文件 @
90599e05
...
...
@@ -80,6 +80,8 @@ void TestTrain()
config
.
Add
(
"lrate"
,
0.001
F
);
config
.
Add
(
"nstep"
,
10000
);
config
.
Add
(
"nepoch"
,
5
);
//config.Add("jobdev0", -1);
//config.Add("jobdev1", -1);
TTDataLoader
loader
;
loader
.
SetFileName
(
"ttrain.txt"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论