aboutsummaryrefslogblamecommitdiff
path: root/fastnn/io/example.c
blob: f2f09167ff83122b0a3c146e00c440d857ece4c3 (plain) (tree)
























































































































































































































































                                                                                                            
#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lualib.h>
#include <luaT/luaT.h>



#include "../threads/lib/luaTHRD.h"
#include "Example.h"


const char *fastnn_example_repo_tname = "fastnn.CExamplesRepo";
const char *fastnn_example_tname = "fastnn.CExample";

static int example_repo_new(lua_State *L) 
{
	ExamplesRepository *repo = NULL	;
	int buffersize;
	bool shared;
	long id;
	if(lua_gettop(L) == 2 && lua_isboolean(L, 2))
	{	
		shared = lua_toboolean(L, 2);		 
		if (shared)
		{
			id = luaL_checkinteger(L, 1); 
                	repo = ExamplesRepository_newWithId(id);
		}
		else
		{
			buffersize = luaL_checkinteger(L, 1);
			repo = ExamplesRepository_new(buffersize);
		}
	}
	else 	
		luaL_error(L, "example: example repo new invalid arguments");
	if (!repo)
		luaL_error(L, "example: example new failed");

        luaTHRD_pushudata(L, repo, fastnn_example_repo_tname);

        return 1;
}

static int example_repo_newfromid(lua_State *L)
{
        ExamplesRepository *repo = NULL ;
        if(lua_gettop(L) == 1)
        {           
                long id = luaL_checkinteger(L, 1); 
                repo = ExamplesRepository_newWithId(id);
        }   
        else    
                luaL_error(L, "example: example repo new invalid arguments");
        if (!repo)
                luaL_error(L, "example: example new failed");

        luaTHRD_pushudata(L, repo, fastnn_example_repo_tname);

        return 1;
}	

static int example_repo_tostring(lua_State *L) 
{
	char str[STRLEN];
        ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
        snprintf(str, STRLEN, "%s <%lx>", fastnn_example_repo_tname, ExamplesRepository_id(repo));
        lua_pushstring(L, str);
        return 1;
}

static int example_repo_id(lua_State *L)
{
	ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
	lua_pushinteger(L, ExamplesRepository_id(repo));
	return 1;
}

static int example_repo_get_gpuid(lua_State *L) 
{
        ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
        lua_pushinteger(L, ExamplesRepository_getGpuId(repo));
        return 1;
}

static int example_repo_set_gpuid(lua_State *L)
{
        ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
	int gpuid = luaL_checkinteger(L, 2);
	ExamplesRepository_setGpuId(repo, gpuid);
        return 0;
}

static int example_repo_accept(lua_State *L)
{
	ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
	Example *example = luaTHRD_checkudata(L, 2, fastnn_example_tname);
	AcceptExample(repo, example);
	return 0;
}

static int example_repo_provide(lua_State *L)
{
	ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
	Example *example = ProvideExample(repo);
	if (NULL != example)
	{
		luaTHRD_pushudata(L, example, fastnn_example_tname);
		return 1;
	}
	else
		return 0;
}

static int example_repo_done(lua_State *L)
{
	ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
	ExamplesDone(repo);
	return 0;
}

static int example_repo_destroy(lua_State *L)
{
	ExamplesRepository *repo = luaTHRD_checkudata(L, 1, fastnn_example_repo_tname);
	ExamplesRepository_destroy(repo);
	return 0;
}

/******************************************/

static int example_new(lua_State *L)
{
	
	Example *example = NULL;
	if(lua_gettop(L) == 0)
        {
                example = Example_new();
        }
	else if(lua_gettop(L) == 1)
	{
		long id = luaL_checkinteger(L, 1);
		example = Example_newWithId(id);
	}
        else
                luaL_error(L, "example: example new invalid arguments");
        if (!example)
                luaL_error(L, "example: example failed");

        luaTHRD_pushudata(L, example, fastnn_example_tname);

        return 1;
}

static int example_id(lua_State *L)
{
	Example *example = luaTHRD_checkudata(L, 1, fastnn_example_tname);
	lua_pushinteger(L, Example_id(example));
        return 1;
}	

static int example_tostring(lua_State *L)
{
        char str[STRLEN];
        Example* example = luaTHRD_checkudata(L, 1, fastnn_example_tname);
        snprintf(str, STRLEN, "%s <%lx>", fastnn_example_tname, Example_id(example));
        lua_pushstring(L, str);
        return 1;
}

static int example_size(lua_State *L)
{
	Example *example = luaTHRD_checkudata(L, 1, fastnn_example_tname);
	lua_pushinteger(L, Example_size(example));
        return 1;
}	

static int example_at(lua_State *L)
{
        Example *example = luaTHRD_checkudata(L, 1, fastnn_example_tname);
	int idx = luaL_checkinteger(L, 2);
	Matrix *m = Example_at(example, idx);
	luaTHRD_pushudata(L, m, "nerv.CuMatrixFloat");
	//luaT_pushudata(L, m, "nerv.CuMatrixFloat");
	return 1;
}

static int example_pushback(lua_State *L)
{
	Example *example = luaTHRD_checkudata(L, 1, fastnn_example_tname);
	Matrix *m = luaTHRD_checkudata(L, 2, "nerv.CuMatrixFloat");
	//Matrix *m = luaT_checkudata(L, 2, "nerv.CuMatrixFloat");
	Example_pushback(example, m);
	return 0;
}	

static int example_destroy(lua_State *L)
{
	Example *example = luaTHRD_checkudata(L, 1, fastnn_example_tname);
	Example_destroy(example);
	//printf("example_destroy ... end\n");
	return 0;
}
	

static const struct luaL_Reg example_repo__ [] = { 
  {"new", example_repo_new},
  {"newfromid", example_repo_newfromid},
  {"__tostring", example_repo_tostring},
  {"id", example_repo_id},
  {"get_gpuid", example_repo_get_gpuid},
  {"set_gpuid", example_repo_set_gpuid},
  {"accept", example_repo_accept},
  {"provide", example_repo_provide},
  {"done", example_repo_done},
  {"free", example_repo_destroy},
  {NULL, NULL}
};

static const struct luaL_Reg example__ [] = { 
  {"new", example_new},
  {"__tostring", example_tostring},
  {"id", example_id},
  {"size", example_size},
  {"at", example_at},
  {"pushback", example_pushback},
  {"free", example_destroy},
  {NULL, NULL}
};

void fastnn_init_example(lua_State *L) 
{
        luaT_newmetatable(L, fastnn_example_tname, NULL, example_new, example_destroy, NULL);
        luaL_register(L, NULL, example__);
        lua_pop(L, 1); 
}

void fastnn_init_example_repo(lua_State *L) 
{
        luaT_newmetatable(L, fastnn_example_repo_tname, NULL, example_repo_new, example_repo_destroy, NULL);
        luaL_register(L, NULL, example_repo__);
        lua_pop(L, 1); 
}