static int QPSolver_run(lua_State *L) { SVQP2 *qp = (SVQP2*)luaT_checkudata(L, 1, QPSolver_id); luaL_argcheck(L, lua_isfunction(L, 2) || luaT_isudata(L, 2, torch_Tensor_id), 2, "function or Tensor expected"); if(lua_isfunction(L, 2)) { bool initialize = luaT_optboolean(L, 3, true); bool finalize = luaT_optboolean(L, 4, true); qp->Aclosure = L; qp->Afunction = QPSolver_luaClosure; int result = qp->run(initialize, finalize); if(result == -1) luaL_error(L, qp->errmsg); return 0; } else { bool initialize = luaT_optboolean(L, 4, true); bool finalize = luaT_optboolean(L, 5, true); THTensor *data = (THTensor*)luaT_checkudata(L, 2, torch_Tensor_id); luaL_argcheck(L, data->nDimension == 2, 2, "2D Tensor expected"); luaL_argcheck(L, data->size[1] == qp->n, 2, "invalid size"); struct QPSolver_cClosureParams stuff; lua_getfield(L, 3, "__eval"); bool isCKernel = lua_islightuserdata(L, -1); lua_pop(L, 1); if(isCKernel) { double (*func)(THTensor*, THTensor*, void *) = ( double (*)(THTensor*, THTensor*, void *))luaT_getfieldchecklightudata(L, 3, "__eval"); void *params = luaT_getfieldchecklightudata(L, 3, "__params"); stuff.func = func; stuff.params = params; } else { lua_getfield(L, 3, "eval"); lua_pushvalue(L, 3); stuff.func = NULL; stuff.params = NULL; } stuff.L = L; stuff.data = data; stuff.x1 = THTensor_new(); stuff.x2 = THTensor_new(); luaT_pushudata(L, stuff.x1, torch_Tensor_id); /* auto dealloc */ luaT_pushudata(L, stuff.x2, torch_Tensor_id); /* auto dealloc */ qp->Aclosure = &stuff; qp->Afunction = (stuff.func ? QPSolver_cClosure : QPSolver_cLuaClosure); int result = qp->run(initialize, finalize); if(result == -1) luaL_error(L, qp->errmsg); return 0; } }
long torch_File_readObject(lua_State *L) { int index = lua_gettop(L); if(!luaT_isudata(L, index, torch_File_id)) luaL_error(L, "Internal error in readObject: not a File at stack index %d", index); lua_getfield(L, index, "readObject"); lua_pushvalue(L, index); lua_call(L, 1, 1); return 1; }
static int pa_stream_writeShort(lua_State *L) { pa_Stream *stream = NULL; THShortTensor *data = NULL; long nelem = 0; PaError err = 0; int narg = lua_gettop(L); if(narg == 2 && luaT_isudata(L, 1, "pa.Stream") && luaT_isudata(L, 2, "torch.ShortTensor")) { stream = luaT_toudata(L, 1, "pa.Stream"); data = luaT_toudata(L, 2, "torch.ShortTensor"); } else luaL_error(L, "expected arguments: Stream ShortTensor"); if(!stream->id) luaL_error(L, "attempt to operate on a closed stream"); nelem = THShortTensor_nElement(data); luaL_argcheck(L, (nelem > 0) && (nelem % stream->noutchannel == 0), 2, "invalid data: number of elements must be > 0 and divisible by the number of channels"); luaL_argcheck(L, stream->outsampleformat & paInt16, 1, "stream does not support short data"); data = THShortTensor_newContiguous(data); err = Pa_WriteStream(stream->id, THShortTensor_data(data), nelem/stream->noutchannel); THShortTensor_free(data); if(err == paOutputUnderflowed) lua_pushboolean(L, 0); else if(err == paNoError) lua_pushboolean(L, 1); else pa_checkerror(L, err); return 1; }
static int pa_stream_stop(lua_State *L) { pa_Stream *stream = NULL; int narg = lua_gettop(L); if(narg == 1 && luaT_isudata(L, 1, "pa.Stream")) stream = luaT_toudata(L, 1, "pa.Stream"); else luaL_error(L, "expected arguments: Stream"); if(!stream->id) luaL_error(L, "attempt to operate on a closed stream"); pa_checkerror(L, Pa_StopStream(stream->id)); return 0; }
static int pa_stream_cpuload(lua_State *L) { pa_Stream *stream = NULL; int narg = lua_gettop(L); if(narg == 1 && luaT_isudata(L, 1, "pa.Stream")) stream = luaT_toudata(L, 1, "pa.Stream"); else luaL_error(L, "expected arguments: Stream"); if(!stream->id) luaL_error(L, "attempt to operate on a closed stream"); lua_pushnumber(L, Pa_GetStreamCpuLoad(stream->id)); return 1; }
static int pa_stream_free(lua_State *L) { pa_Stream *stream = NULL; int narg = lua_gettop(L); if(narg == 1 && luaT_isudata(L, 1, "pa.Stream")) stream = luaT_toudata(L, 1, "pa.Stream"); else luaL_error(L, "expected arguments: Stream"); if(stream->id) Pa_CloseStream(stream->id); /* should also free input/output buffers */ luaT_free(L, stream); return 0; }
long torch_File_writeObject(lua_State *L) { int index = lua_gettop(L); if(index < 2) luaL_error(L, "File and object expected"); if(!luaT_isudata(L, index-1, torch_File_id)) luaL_error(L, "Internal error in writeObject: not a File at stack index %d", index-1); lua_getfield(L, index-1, "writeObject"); lua_pushvalue(L, index-1); lua_pushvalue(L, index); lua_call(L, 2, 0); lua_pop(L, 1); /* remove the object from the stack */ return 1; }
static int pa_stream_writeavailable(lua_State *L) { pa_Stream *stream = NULL; int narg = lua_gettop(L); PaError err = 0; if(narg == 1 && luaT_isudata(L, 1, "pa.Stream")) stream = luaT_toudata(L, 1, "pa.Stream"); else luaL_error(L, "expected arguments: Stream"); if(!stream->id) luaL_error(L, "attempt to operate on a closed stream"); err = Pa_GetStreamWriteAvailable(stream->id); if(err >= 0) lua_pushnumber(L, err); else pa_checkerror(L, err); return 1; }
static int pa_stream_callbackerror(lua_State *L) { pa_Stream *stream = NULL; int narg = lua_gettop(L); if(narg == 1 && luaT_isudata(L, 1, "pa.Stream")) stream = luaT_toudata(L, 1, "pa.Stream"); else luaL_error(L, "expected arguments: Stream"); if(!stream->id) luaL_error(L, "attempt to operate on a closed stream"); if(Pa_IsStreamActive(stream->id)) luaL_error(L, "cannot check callback error on an active stream"); if(stream->callbackerror) { lua_pushstring(L, stream->callbackerror); return 1; } return 0; }
static int pa_stream_isactive(lua_State *L) { pa_Stream *stream = NULL; int narg = lua_gettop(L); PaError err = 0; if(narg == 1 && luaT_isudata(L, 1, "pa.Stream")) stream = luaT_toudata(L, 1, "pa.Stream"); else luaL_error(L, "expected arguments: Stream"); if(!stream->id) luaL_error(L, "attempt to operate on a closed stream"); err = Pa_IsStreamActive(stream->id); if(err == 1) lua_pushboolean(L, 1); else if(err == 0) lua_pushboolean(L, 0); else pa_checkerror(L, err); return 1; }