aboutsummaryrefslogtreecommitdiff
path: root/fastnn/device/device.c
blob: 71d6ec1fbb0c4ff320952f5f32049b3b9e569a9a (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
#include <stdio.h>
#include <stdlib.h>

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

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


const char *fastnn_device_tname = "fastnn.CDevice";


static int device_new(lua_State *L)
{
	Device *device = NULL;
	if(lua_gettop(L) == 0)
        {
                device = Device_new();
        }
        else if(lua_gettop(L) == 1)
        {
                long id = luaL_checkinteger(L, 1);
                device = Device_newWithId(id);
        }
        else
                luaL_error(L, "device: device new invalid arguments");
        if (!device)
                luaL_error(L, "device: device failed");

        luaTHRD_pushudata(L, device, fastnn_device_tname);

        return 1;
}

static int device_init(lua_State *L)
{
	Device *device = luaTHRD_checkudata(L, 1, fastnn_device_tname);
	Status status;
	Initialize(device, &status);
	NERV_LUA_CHECK_STATUS(L, status);
	return 0;
}	

static int device_select_gpu(lua_State *L)
{
	Device *device = luaTHRD_checkudata(L, 1, fastnn_device_tname);
	
	Status status;
	if(lua_gettop(L) == 2)
	{
		int gpuid = luaL_checkinteger(L, 2);
		SelectGPU(device, gpuid, &status);
		NERV_LUA_CHECK_STATUS(L, status);
		return 0;
	}
	else if(lua_gettop(L) == 1)
	{
		int gpuid = AutoSelectGPU(device, &status);
		NERV_LUA_CHECK_STATUS(L, status);
		lua_pushinteger(L, gpuid);
		return 1;
	}
	else
		luaL_error(L, "device: device select gpu failed");
}	

static int device_id(lua_State *L)
{
	Device *device = luaTHRD_checkudata(L, 1, fastnn_device_tname);
	lua_pushinteger(L, Device_id(device));
	return 1;
}

static int device_tostring(lua_State *L)
{
        char str[STRLEN];
        Device* device = luaTHRD_checkudata(L, 1, fastnn_device_tname);
        snprintf(str, STRLEN, "%s <%lx>", fastnn_device_tname, Device_id(device));
        lua_pushstring(L, str);
        return 1;
}

static int device_destroy(lua_State *L)
{
        Device *device = luaTHRD_checkudata(L, 1, fastnn_device_tname);
        Device_destroy(device);
        //printf("device_destroy ... end\n");
        return 0;
}


//////////////////////////////////////////////

static int context_new(lua_State *L) 
{
        CuContext *context = NULL;
        if(lua_gettop(L) == 0)
        {   
                context = CuContext_new();
        }   
        else if(lua_gettop(L) == 1)
        {   
                long id = luaL_checkinteger(L, 1); 
                context = CuContext_newWithId(id);
        }   
        else
                luaL_error(L, "device: context new invalid arguments");
        if (!context)
                luaL_error(L, "device: context failed");

        luaTHRD_pushudata(L, context, nerv_context_tname);

        return 1;
}


static int context_id(lua_State *L)
{
        CuContext *context = luaTHRD_checkudata(L, 1, nerv_context_tname);
        lua_pushinteger(L, CuContext_id(context));
        return 1;
}

static int context_tostring(lua_State *L)
{
        char str[STRLEN];
        CuContext* context = luaTHRD_checkudata(L, 1, nerv_context_tname);
        snprintf(str, STRLEN, "%s <%lx>", nerv_context_tname, CuContext_id(context));
        lua_pushstring(L, str);
        return 1;
}

static int context_destroy(lua_State *L)
{
	CuContext* context = luaTHRD_checkudata(L, 1, nerv_context_tname);
        CuContext_destroy(context);
        return 0;
}


static const struct luaL_Reg device__ [] = {
  {"new", device_new},
  {"__tostring", device_tostring},
  {"id", device_id},
  {"init", device_init},
  {"select_gpu", device_select_gpu},
  {"free", device_destroy},
  {NULL, NULL}
};

static const struct luaL_Reg context__ [] = {
  {"new", context_new},
  {"__tostring", context_tostring},
  {"id", context_id},
  {"free", context_destroy},
  {NULL, NULL}
};


void fastnn_init_device(lua_State *L)
{
        luaT_newmetatable(L, fastnn_device_tname, NULL, device_new, device_destroy, NULL);
        luaL_register(L, NULL, device__);
        lua_pop(L, 1);
}

void fastnn_init_context(lua_State *L)
{
	luaT_newmetatable(L, nerv_context_tname, NULL, context_new, context_destroy, NULL);
        luaL_register(L, NULL, context__);
        lua_pop(L, 1);
}