Commit 90599e05 by xiaotong

add mutexes to XMem

parent 59ccf22d
...@@ -132,6 +132,36 @@ extern int TRAINING_SAMPLE_BUF_SIZE; ...@@ -132,6 +132,36 @@ extern int TRAINING_SAMPLE_BUF_SIZE;
extern int CONST_MINUSONE; extern int CONST_MINUSONE;
extern bool CONST_TRUE; 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 USE_CUDA_RESURSION 1
#define NIUTRANSNNDEBUG #define NIUTRANSNNDEBUG
......
...@@ -54,6 +54,8 @@ XMem::XMem() ...@@ -54,6 +54,8 @@ XMem::XMem()
signature = 0; signature = 0;
mergeFreeOTF = true; mergeFreeOTF = true;
isInitialized = false; isInitialized = false;
MUTEX_INIT(allocMutex);
MUTEX_INIT(bufMutex);
} }
/* /*
...@@ -77,6 +79,8 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum, ...@@ -77,6 +79,8 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum,
strcpy(name, "xmem"); strcpy(name, "xmem");
signature = 0; signature = 0;
mergeFreeOTF = true; mergeFreeOTF = true;
MUTEX_INIT(allocMutex);
MUTEX_INIT(bufMutex);
Initialize(myDevID, myMode, myBlockSize, myBlockNum, myBufSize); Initialize(myDevID, myMode, myBlockSize, myBlockNum, myBufSize);
} }
...@@ -99,6 +103,8 @@ XMem::~XMem() ...@@ -99,6 +103,8 @@ XMem::~XMem()
delete[] memIndex; delete[] memIndex;
delete[] memIndex2; delete[] memIndex2;
delete[] minSizeIndex; delete[] minSizeIndex;
MUTEX_DELE(allocMutex);
MUTEX_DELE(bufMutex);
} }
/* /*
...@@ -379,12 +385,18 @@ require a piece of memory ...@@ -379,12 +385,18 @@ require a piece of memory
*/ */
void * XMem::Alloc(int myDevID, MTYPE mySize) void * XMem::Alloc(int myDevID, MTYPE mySize)
{ {
void * p = NULL;
MUTEX_LOCK(allocMutex);
if(mode == FREE_ON_THE_FLY) if(mode == FREE_ON_THE_FLY)
return AllocStandard(myDevID, mySize); p = AllocStandard(myDevID, mySize);
else if(isStatic) else if(isStatic)
return AllocStatic(myDevID, mySize); p = AllocStatic(myDevID, mySize);
else 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) ...@@ -521,6 +533,11 @@ void * XMem::AllocBuf(int myDevID, MTYPE mySize, int pitch)
{ {
MTYPE backOffset = 0; 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){ if(pitch > 1){
MTYPE address = (MTYPE)((char*)buf + bufUsed); MTYPE address = (MTYPE)((char*)buf + bufUsed);
int offset = address % pitch; int offset = address % pitch;
...@@ -560,8 +577,10 @@ release a piece of memory ...@@ -560,8 +577,10 @@ release a piece of memory
*/ */
void XMem::Release(int myDevID, void * p, MTYPE size) void XMem::Release(int myDevID, void * p, MTYPE size)
{ {
MUTEX_LOCK(allocMutex);
if(mode == FREE_ON_THE_FLY) if(mode == FREE_ON_THE_FLY)
ReleaseStandard(myDevID, p, size); ReleaseStandard(myDevID, p, size);
MUTEX_UNLOCK(allocMutex);
} }
/* /*
...@@ -583,6 +602,9 @@ void XMem::ReleaseBuf(int myDevID, MTYPE mySize, int pitch) ...@@ -583,6 +602,9 @@ void XMem::ReleaseBuf(int myDevID, MTYPE mySize, int pitch)
} }
bufUsed -= (mySize + backOffset); 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) ...@@ -825,6 +847,18 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
return result; 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 find the highest set bit (or most significant set bit) in an integer-64
>> mySize - required size >> mySize - required size
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#ifndef __XMEM_H__ #ifndef __XMEM_H__
#define __XMEM_H__ #define __XMEM_H__
#include "XGlobal.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -249,6 +250,13 @@ public: ...@@ -249,6 +250,13 @@ public:
/* indicates whether we merge free memory pieces on the fly */ /* indicates whether we merge free memory pieces on the fly */
bool mergeFreeOTF; 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: public:
/* constructor */ /* constructor */
...@@ -337,6 +345,12 @@ public: ...@@ -337,6 +345,12 @@ public:
/* allocate a piece of memory as "malloc" */ /* allocate a piece of memory as "malloc" */
void * AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex = false); 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 */ /* find the highest set bit (or most significant set bit) in an integer-64 */
int GetMSB(MTYPE mySize); int GetMSB(MTYPE mySize);
......
...@@ -54,37 +54,6 @@ namespace nts{ ...@@ -54,37 +54,6 @@ namespace nts{
(unsigned)(flag), (unsigned *)(id)) (unsigned)(flag), (unsigned *)(id))
#endif #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*); typedef void (*TFunction) (volatile XList*);
/* /*
......
...@@ -80,6 +80,8 @@ void TestTrain() ...@@ -80,6 +80,8 @@ void TestTrain()
config.Add("lrate", 0.001F); config.Add("lrate", 0.001F);
config.Add("nstep", 10000); config.Add("nstep", 10000);
config.Add("nepoch", 5); config.Add("nepoch", 5);
//config.Add("jobdev0", -1);
//config.Add("jobdev1", -1);
TTDataLoader loader; TTDataLoader loader;
loader.SetFileName("ttrain.txt"); loader.SetFileName("ttrain.txt");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论