MnistComm.h:
#pragma once
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <stddef.h>
#include <map>
#include <string>
using std::map;
using std::string;
// Data structure definitions
typedef enum tagMNIST_ERROR_CODE
{
MNIST_OK = 0,
MNIST_CREATE_SHM_FAILED = 1,
MNIST_OPEN_SHM_FAILED = 2,
MNIST_CREATE_EVT_FAILED = 3,
MNIST_OPEN_EVT_FAILED = 4,
MNIST_SESSION_CREATION_FAILED = 10,
MNIST_MODEL_LOAD_FAILED = 11,
MNIST_GRAPH_CREATION_FAILED = 12,
MNIST_MODEL_RUN_FAILED = 13,
} MNIST_ERROR_CODE;
#define MAX_MSG_SIZ 255
typedef struct tagMNIST_COMM_ERROR
{
MNIST_ERROR_CODE err;
char msg[MAX_MSG_SIZ + 1];
} MNIST_COMM_ERROR;
#define MNIST_IMG_HEIGHT 28
#define MNIST_IMG_WIDTH 28
#define MNIST_IMG_DIM (MNIST_IMG_HEIGHT * MNIST_IMG_WIDTH)
#define MNIST_IMG_SIZ (MNIST_IMG_DIM * sizeof(float))
typedef struct tagMNIST_IMG_LABEL
{
float img[MNIST_IMG_HEIGHT][MNIST_IMG_WIDTH];
int label;
} MNIST_IMG_LABEL;
typedef struct tagMNIST_COMM_SHM_LAYOUT
{
MNIST_COMM_ERROR mnist_err;
MNIST_IMG_LABEL mnist_data;
} MNIST_COMM_SHM_LAYOUT;
class MnistShm
{
public:
MnistShm(bool host);
~MnistShm();
bool operator !()
{
return !m_bInitialized;
}
bool GetError(char* pBuf)
{
CopyMemory(pBuf, m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_err), sizeof(MNIST_COMM_ERROR));
return true;
}
bool SetError(char* pBuf)
{
CopyMemory(m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_err), pBuf, sizeof(MNIST_COMM_ERROR));
return true;
}
bool GetImageData(char* pBuf)
{
CopyMemory(pBuf, m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data), MNIST_IMG_SIZ);
return true;
}
bool SetImageData(char* pBuf)
{
CopyMemory(m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data), pBuf, MNIST_IMG_SIZ);
return true;
}
bool GetImageLabel(char* pBuf)
{
CopyMemory(pBuf, m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data) + MNIST_IMG_SIZ, sizeof(int));
return true;
}
bool SetImageLabel(char* pBuf)
{
CopyMemory(m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data) + MNIST_IMG_SIZ,
pBuf, sizeof(int));
return true;
}
private:
static wchar_t* MnistShmName;
bool m_bInitialized;
HANDLE m_hMapFile;
char* m_pBuf;
};
typedef enum tagMNIST_COMM_EVENT
{
MNIST_EVENT_NULL = -1,
MNIST_EVENT_QUIT = 0,
MNIST_EVENT_PROC = 1,
MNIST_EVENT_REDY = 2,
MNIST_EVENT_COUNT = 3,
} MNIST_COMM_EVENT;
typedef struct tagMNIST_EVENT_NAME
{
MNIST_COMM_EVENT e;
wchar_t* name;
} MNIST_EVENT_NAME;
typedef HANDLE MNIST_EVENT_HANDLE[MNIST_EVENT_COUNT];
class MnistEvent
{
public:
MnistEvent(bool host);
~MnistEvent();
bool operator !()
{
return !m_bInitialized;
}
bool NotifyQuit()
{
return PulseEvent(m_hEvt[MNIST_EVENT_QUIT]);
}
bool NotifyProc()
{
return PulseEvent(m_hEvt[MNIST_EVENT_PROC]);
}
bool NotifyReady()
{
return PulseEvent(m_hEvt[MNIST_EVENT_REDY]);
}
MNIST_COMM_EVENT WaitForEvent(MNIST_COMM_EVENT event)
{
DWORD dwEvent;
MNIST_COMM_EVENT e;
do
{
dwEvent = WaitForMultipleObjects(MNIST_EVENT_COUNT, m_hEvt, FALSE, INFINITE);
e = static_cast<MNIST_COMM_EVENT>(dwEvent - WAIT_OBJECT_0);
if (e == event)
break;
} while (e != MNIST_EVENT_QUIT);
return e;
}
private:
static MNIST_EVENT_NAME MnistEventName[MNIST_EVENT_COUNT];
bool m_bHost;
bool m_bInitialized;
MNIST_EVENT_HANDLE m_hEvt;
};
class CMnistComm
{
public:
CMnistComm();
~CMnistComm();
static bool Char2WChar(char* cs, wchar_t* wcs, int size)
{
return swprintf(wcs, size, L"%S", cs);
}
static bool WChar2Char(char* cs, wchar_t* wcs, int size)
{
return snprintf(cs, size, "%S", wcs);
}
};
MnistComm.cpp:
#include "MnistComm.h"
wchar_t* MnistShm::MnistShmName = _T("MnistSharedMemory");
MnistShm::MnistShm(bool host) : m_bInitialized(false), m_hMapFile(NULL)
{
if (host)
m_hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, sizeof(MNIST_COMM_SHM_LAYOUT), MnistShmName);
else
m_hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, MnistShmName);
if (m_hMapFile == NULL)
{
OutputDebugString(host ? TEXT("Could not create file mapping object") :
TEXT("Could not open file mapping object"));
return;
}
m_pBuf = reinterpret_cast<char*>(MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(MNIST_COMM_SHM_LAYOUT)));
if (m_pBuf == NULL)
{
OutputDebugString(TEXT("Could not map view of file"));
return;
}
m_bInitialized = true;
}
MnistShm::~MnistShm()
{
if (m_pBuf)
UnmapViewOfFile(m_pBuf);
if (m_hMapFile)
CloseHandle(m_hMapFile);
}
MNIST_EVENT_NAME MnistEvent::MnistEventName[] =
{
{ MNIST_EVENT_QUIT, TEXT("MnistEventQuit") },
{ MNIST_EVENT_PROC, TEXT("MnistEventProc") },
{ MNIST_EVENT_REDY, TEXT("MnistEventRedy") },
};
MnistEvent::MnistEvent(bool host) :
m_bHost(host),
m_bInitialized(false),
m_hEvt{ 0 }
{
if (host)
{
m_hEvt[MNIST_EVENT_QUIT] = CreateEvent(NULL, TRUE, FALSE, MnistEventName[MNIST_EVENT_QUIT].name);
m_hEvt[MNIST_EVENT_PROC] = CreateEvent(NULL, TRUE, FALSE, MnistEventName[MNIST_EVENT_PROC].name);
m_hEvt[MNIST_EVENT_REDY] = CreateEvent(NULL, TRUE, FALSE, MnistEventName[MNIST_EVENT_REDY].name);
}
else
{
m_hEvt[MNIST_EVENT_QUIT] = OpenEvent(EVENT_ALL_ACCESS, FALSE, MnistEventName[MNIST_EVENT_QUIT].name);
m_hEvt[MNIST_EVENT_PROC] = OpenEvent(EVENT_ALL_ACCESS, FALSE, MnistEventName[MNIST_EVENT_PROC].name);
m_hEvt[MNIST_EVENT_REDY] = OpenEvent(EVENT_ALL_ACCESS, FALSE, MnistEventName[MNIST_EVENT_REDY].name);
}
if (m_hEvt[MNIST_EVENT_QUIT] == NULL)
{
OutputDebugString(host ? TEXT("Could not create quit event object") :
TEXT("Could not open quit event object"));
return;
}
if (m_hEvt[MNIST_EVENT_PROC] == NULL)
{
OutputDebugString(host ? TEXT("Could not create processing event object") :
TEXT("Could not open processing event object"));
return;
}
if (m_hEvt[MNIST_EVENT_REDY] == NULL)
{
OutputDebugString(host ? TEXT("Could not create ready event object") :
TEXT("Could not open ready event object"));
return;
}
m_bInitialized = true;
}
MnistEvent::~MnistEvent()
{
if (m_hEvt[MNIST_EVENT_QUIT])
CloseHandle(m_hEvt[MNIST_EVENT_QUIT]);
if (m_hEvt[MNIST_EVENT_PROC])
CloseHandle(m_hEvt[MNIST_EVENT_PROC]);
if (m_hEvt[MNIST_EVENT_REDY])
CloseHandle(m_hEvt[MNIST_EVENT_REDY]);
}
CMnistComm::CMnistComm()
{
}
CMnistComm::~CMnistComm()
{
}
#pragma once
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <stddef.h>
#include <map>
#include <string>
using std::map;
using std::string;
// Data structure definitions
typedef enum tagMNIST_ERROR_CODE
{
MNIST_OK = 0,
MNIST_CREATE_SHM_FAILED = 1,
MNIST_OPEN_SHM_FAILED = 2,
MNIST_CREATE_EVT_FAILED = 3,
MNIST_OPEN_EVT_FAILED = 4,
MNIST_SESSION_CREATION_FAILED = 10,
MNIST_MODEL_LOAD_FAILED = 11,
MNIST_GRAPH_CREATION_FAILED = 12,
MNIST_MODEL_RUN_FAILED = 13,
} MNIST_ERROR_CODE;
#define MAX_MSG_SIZ 255
typedef struct tagMNIST_COMM_ERROR
{
MNIST_ERROR_CODE err;
char msg[MAX_MSG_SIZ + 1];
} MNIST_COMM_ERROR;
#define MNIST_IMG_HEIGHT 28
#define MNIST_IMG_WIDTH 28
#define MNIST_IMG_DIM (MNIST_IMG_HEIGHT * MNIST_IMG_WIDTH)
#define MNIST_IMG_SIZ (MNIST_IMG_DIM * sizeof(float))
typedef struct tagMNIST_IMG_LABEL
{
float img[MNIST_IMG_HEIGHT][MNIST_IMG_WIDTH];
int label;
} MNIST_IMG_LABEL;
typedef struct tagMNIST_COMM_SHM_LAYOUT
{
MNIST_COMM_ERROR mnist_err;
MNIST_IMG_LABEL mnist_data;
} MNIST_COMM_SHM_LAYOUT;
class MnistShm
{
public:
MnistShm(bool host);
~MnistShm();
bool operator !()
{
return !m_bInitialized;
}
bool GetError(char* pBuf)
{
CopyMemory(pBuf, m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_err), sizeof(MNIST_COMM_ERROR));
return true;
}
bool SetError(char* pBuf)
{
CopyMemory(m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_err), pBuf, sizeof(MNIST_COMM_ERROR));
return true;
}
bool GetImageData(char* pBuf)
{
CopyMemory(pBuf, m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data), MNIST_IMG_SIZ);
return true;
}
bool SetImageData(char* pBuf)
{
CopyMemory(m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data), pBuf, MNIST_IMG_SIZ);
return true;
}
bool GetImageLabel(char* pBuf)
{
CopyMemory(pBuf, m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data) + MNIST_IMG_SIZ, sizeof(int));
return true;
}
bool SetImageLabel(char* pBuf)
{
CopyMemory(m_pBuf + offsetof(MNIST_COMM_SHM_LAYOUT, mnist_data) + MNIST_IMG_SIZ,
pBuf, sizeof(int));
return true;
}
private:
static wchar_t* MnistShmName;
bool m_bInitialized;
HANDLE m_hMapFile;
char* m_pBuf;
};
typedef enum tagMNIST_COMM_EVENT
{
MNIST_EVENT_NULL = -1,
MNIST_EVENT_QUIT = 0,
MNIST_EVENT_PROC = 1,
MNIST_EVENT_REDY = 2,
MNIST_EVENT_COUNT = 3,
} MNIST_COMM_EVENT;
typedef struct tagMNIST_EVENT_NAME
{
MNIST_COMM_EVENT e;
wchar_t* name;
} MNIST_EVENT_NAME;
typedef HANDLE MNIST_EVENT_HANDLE[MNIST_EVENT_COUNT];
class MnistEvent
{
public:
MnistEvent(bool host);
~MnistEvent();
bool operator !()
{
return !m_bInitialized;
}
bool NotifyQuit()
{
return PulseEvent(m_hEvt[MNIST_EVENT_QUIT]);
}
bool NotifyProc()
{
return PulseEvent(m_hEvt[MNIST_EVENT_PROC]);
}
bool NotifyReady()
{
return PulseEvent(m_hEvt[MNIST_EVENT_REDY]);
}
MNIST_COMM_EVENT WaitForEvent(MNIST_COMM_EVENT event)
{
DWORD dwEvent;
MNIST_COMM_EVENT e;
do
{
dwEvent = WaitForMultipleObjects(MNIST_EVENT_COUNT, m_hEvt, FALSE, INFINITE);
e = static_cast<MNIST_COMM_EVENT>(dwEvent - WAIT_OBJECT_0);
if (e == event)
break;
} while (e != MNIST_EVENT_QUIT);
return e;
}
private:
static MNIST_EVENT_NAME MnistEventName[MNIST_EVENT_COUNT];
bool m_bHost;
bool m_bInitialized;
MNIST_EVENT_HANDLE m_hEvt;
};
class CMnistComm
{
public:
CMnistComm();
~CMnistComm();
static bool Char2WChar(char* cs, wchar_t* wcs, int size)
{
return swprintf(wcs, size, L"%S", cs);
}
static bool WChar2Char(char* cs, wchar_t* wcs, int size)
{
return snprintf(cs, size, "%S", wcs);
}
};
MnistComm.cpp:
#include "MnistComm.h"
wchar_t* MnistShm::MnistShmName = _T("MnistSharedMemory");
MnistShm::MnistShm(bool host) : m_bInitialized(false), m_hMapFile(NULL)
{
if (host)
m_hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, sizeof(MNIST_COMM_SHM_LAYOUT), MnistShmName);
else
m_hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, MnistShmName);
if (m_hMapFile == NULL)
{
OutputDebugString(host ? TEXT("Could not create file mapping object") :
TEXT("Could not open file mapping object"));
return;
}
m_pBuf = reinterpret_cast<char*>(MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(MNIST_COMM_SHM_LAYOUT)));
if (m_pBuf == NULL)
{
OutputDebugString(TEXT("Could not map view of file"));
return;
}
m_bInitialized = true;
}
MnistShm::~MnistShm()
{
if (m_pBuf)
UnmapViewOfFile(m_pBuf);
if (m_hMapFile)
CloseHandle(m_hMapFile);
}
MNIST_EVENT_NAME MnistEvent::MnistEventName[] =
{
{ MNIST_EVENT_QUIT, TEXT("MnistEventQuit") },
{ MNIST_EVENT_PROC, TEXT("MnistEventProc") },
{ MNIST_EVENT_REDY, TEXT("MnistEventRedy") },
};
MnistEvent::MnistEvent(bool host) :
m_bHost(host),
m_bInitialized(false),
m_hEvt{ 0 }
{
if (host)
{
m_hEvt[MNIST_EVENT_QUIT] = CreateEvent(NULL, TRUE, FALSE, MnistEventName[MNIST_EVENT_QUIT].name);
m_hEvt[MNIST_EVENT_PROC] = CreateEvent(NULL, TRUE, FALSE, MnistEventName[MNIST_EVENT_PROC].name);
m_hEvt[MNIST_EVENT_REDY] = CreateEvent(NULL, TRUE, FALSE, MnistEventName[MNIST_EVENT_REDY].name);
}
else
{
m_hEvt[MNIST_EVENT_QUIT] = OpenEvent(EVENT_ALL_ACCESS, FALSE, MnistEventName[MNIST_EVENT_QUIT].name);
m_hEvt[MNIST_EVENT_PROC] = OpenEvent(EVENT_ALL_ACCESS, FALSE, MnistEventName[MNIST_EVENT_PROC].name);
m_hEvt[MNIST_EVENT_REDY] = OpenEvent(EVENT_ALL_ACCESS, FALSE, MnistEventName[MNIST_EVENT_REDY].name);
}
if (m_hEvt[MNIST_EVENT_QUIT] == NULL)
{
OutputDebugString(host ? TEXT("Could not create quit event object") :
TEXT("Could not open quit event object"));
return;
}
if (m_hEvt[MNIST_EVENT_PROC] == NULL)
{
OutputDebugString(host ? TEXT("Could not create processing event object") :
TEXT("Could not open processing event object"));
return;
}
if (m_hEvt[MNIST_EVENT_REDY] == NULL)
{
OutputDebugString(host ? TEXT("Could not create ready event object") :
TEXT("Could not open ready event object"));
return;
}
m_bInitialized = true;
}
MnistEvent::~MnistEvent()
{
if (m_hEvt[MNIST_EVENT_QUIT])
CloseHandle(m_hEvt[MNIST_EVENT_QUIT]);
if (m_hEvt[MNIST_EVENT_PROC])
CloseHandle(m_hEvt[MNIST_EVENT_PROC]);
if (m_hEvt[MNIST_EVENT_REDY])
CloseHandle(m_hEvt[MNIST_EVENT_REDY]);
}
CMnistComm::CMnistComm()
{
}
CMnistComm::~CMnistComm()
{
}
No comments:
Post a Comment