aboutsummaryrefslogtreecommitdiff
path: root/fastnn/io/example.c
blob: f2f09167ff83122b0a3c146e00c440d857ece4c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#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); 
}