#include <stdio.h>
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <luaT/luaT.h>
#include "ModelSync.h"
#include "../threads/lib/luaTHRD.h"
const char *fastnn_model_sync_tname = "fastnn.CModelSync";
const char *fastnn_xent_tname = "fastnn.CXent";
const char *fastnn_mse_tname = "fastnn.CMse";
const char *fastnn_global_option_tname = "fastnn.CGlobalOption";
static int model_sync_new(lua_State *L)
{
ModelSync *model_sync = NULL;
if(lua_gettop(L) == 0)
{
model_sync = ModelSync_new();
}
else if(lua_gettop(L) == 1)
{
long id = luaL_checklong(L, 1);
model_sync = ModelSync_newWithId(id);
}
else
luaL_error(L, "modelsync: modelsync new invalid arguments");
if(!model_sync)
luaL_error(L, "modelsync: modelsync new failed");
luaTHRD_pushudata(L, model_sync, fastnn_model_sync_tname);
return 1;
}
static int model_sync_tostring(lua_State *L)
{
char str[STRLEN];
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
snprintf(str, STRLEN, "fastnn.modelsync <%lx>", ModelSync_id(model_sync));
lua_pushstring(L, str);
return 1;
}
static int model_sync_id(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
lua_pushinteger(L, ModelSync_id(model_sync));
return 1;
}
static int model_sync_lockmodel(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
if (ModelSync_lockmodel(model_sync))
luaL_error(L, "modelsync: model lock failed");
return 0;
}
static int model_sync_unlockmodel(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
if (ModelSync_unlockmodel(model_sync))
luaL_error(L, "modelsync: model unlock failed");
return 0;
}
static int model_sync_lockstate(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
if (ModelSync_lockstate(model_sync))
luaL_error(L, "modelsync: state lock failed");
return 0;
}
static int model_sync_unlockstate(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
if (ModelSync_unlockstate(model_sync))
luaL_error(L, "modelsync: state unlock failed");
return 0;
}
static int model_sync_free(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
ModelSync_free(model_sync);
return 0;
}
static int model_sync_initbuffer(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
model_sync->dim_ = luaL_checkinteger(L, 2);
ModelSync_initBuffer(model_sync);
return 0;
}
static int model_sync_weightfromd(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
Matrix *dm = luaT_checkudata(L, 2, "nerv.CuMatrixFloat");
ModelSync_weightfromd(model_sync, dm);
return 0;
}
static int model_sync_weighttod(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
Matrix *dm = luaT_checkudata(L, 2, "nerv.CuMatrixFloat");
ModelSync_weighttod(model_sync, dm);
return 0;
}
static int model_sync_initialized(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
lua_pushboolean(L, model_sync->initialized_);
return 1;
}
static int model_sync_setpos(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
int pos = luaL_checkinteger(L, 2);
model_sync->pos_ = pos;
return 0;
}
static int model_sync_threadcount(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
lua_pushinteger(L, ModelSync_threadcount(model_sync));
return 1;
}
static int model_sync_syncinc(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
ModelSync_syncinc(model_sync);
return 0;
}
static int model_sync_syncdec(lua_State *L)
{
ModelSync *model_sync = luaTHRD_checkudata(L, 1, fastnn_model_sync_tname);
ModelSync_syncdec(model_sync);
return 0;
}
//////////////////////////////////////////
static int xent_new(lua_State *L)
{
Xent *xent = NULL;
if(lua_gettop(L) == 0)
{
xent = Xent_new();
}
else if(lua_gettop(L) == 1)
{
long id = luaL_checklong(L, 1);
xent = Xent_newWithId(id);
}
else if(lua_gettop(L) == 4)
{
size_t frames_, correct_;
double loss_, entropy_ ;
frames_ = luaL_checkinteger(L, 1);
correct_ = luaL_checkinteger(L, 2);
loss_ = luaL_checknumber(L, 3);
entropy_ = luaL_checknumber(L, 4);
xent = Xent_newWithParm(frames_, correct_, loss_, entropy_);
}
else
luaL_error(L, "xent: xent new invalid arguments");
if(!xent)
luaL_error(L, "xent: xent new failed");
luaTHRD_pushudata(L, xent, fastnn_xent_tname);
return 1;
}
static int xent_tostring(lua_State *L)
{
char str[STRLEN];
Xent *xent = luaTHRD_checkudata(L, 1, fastnn_xent_tname);
snprintf(str, STRLEN, "fastnn.xent <%lx>", Xent_id(xent));
lua_pushstring(L, str);
return 1;
}
static int xent_totalframes(lua_State *L)
{
Xent *xent = luaTHRD_checkudata(L, 1, fastnn_xent_tname);
lua_pushinteger(L, xent->frames_);
return 1;
}
static int xent_correct(lua_State *L)
{
Xent *xent = luaTHRD_checkudata(L, 1, fastnn_xent_tname);
lua_pushinteger(L, xent->correct_);
return 1;
}
static int xent_loss(lua_State *L)
{
Xent *xent = luaTHRD_checkudata(L, 1, fastnn_xent_tname);
lua_pushnumber(L, xent->loss_);
return 1;
}
static int xent_entropy(lua_State *L)
{
Xent *xent = luaTHRD_checkudata(L, 1, fastnn_xent_tname);
lua_pushnumber(L, xent->entropy_);
return 1;
}
static int xent_id(lua_State *L)
{
Xent *xent = luaTHRD_checkudata(L,