diff options
Diffstat (limited to 'nerv/matrix/init.lua')
-rw-r--r-- | nerv/matrix/init.lua | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/nerv/matrix/init.lua b/nerv/matrix/init.lua index 14b83d4..da76e1b 100644 --- a/nerv/matrix/init.lua +++ b/nerv/matrix/init.lua @@ -1,3 +1,12 @@ +--- Implements a fraction of matrix operations (methods) in Lua, while +-- others are implemented in C extension. +-- @author Ted Yin <[email protected]> + + +--- The base class for all matrices. +-- @type nerv.Matrix + +--- Convert the matrix object to a string. function nerv.Matrix:__tostring__() local ncol = self:ncol() local nrow = self:nrow() @@ -28,8 +37,9 @@ function nerv.Matrix:__tostring__() return table.concat(strt) end --- gen: a function takes take indices of the matrix and return the generated --- all entrys in the matrix will be assigned by calling gen(i, j), for a vector, gen(j) will be called. +--- Assign each element in a matrix using the value returned by a callback `gen`. +-- @param gen the callback used to generated the values in the matrix, to which +-- the indices of row and column will be passed (e.g., `gen(i, j)`) function nerv.Matrix:generate(gen) if (self:dim() == 2) then for i = 0, self:nrow() - 1 do @@ -45,50 +55,87 @@ function nerv.Matrix:generate(gen) end end +--- Create a fresh new matrix of the same matrix type (as `self`). +-- @param nrow optional, the number of rows in the created matrix if specified, +-- otherwise `self:nrow()` will be used +-- @param ncol optional, the number of columns in the created matrix if specified, +-- otherwise `self:ncol()` will be used function nerv.Matrix:create(nrow, ncol) return self.__constructor(nrow or self:nrow(), ncol or self:ncol()) end nerv.MMatrixInt.fmt = "%d " -function nerv.CuMatrix:__add__(b) +--- Operator overloading of `+`. +function nerv.Matrix:__add__(b) c = self:create() c:add(self, b, 1.0, 1.0) return c end -function nerv.CuMatrix:__sub__(b) +--- Operator overloading of `-`. +function nerv.Matrix:__sub__(b) c = self:create() c:add(self, b, 1.0, -1.0) return c end -function nerv.CuMatrix:__mul__(b) +--- Operator overloading of `*`. +function nerv.Matrix:__mul__(b) c = nerv.get_type(self.__typename)(self:nrow(), b:ncol()) c:mul(self, b, 1.0, 0.0, 'N', 'N') return c end +--- CUDA float matrices +-- @type nerv.CuMatrixFloat + +--- Create a CUDA matrix copy of the host matrix (in memory) +-- @param mat the host matrix function nerv.CuMatrixFloat.new_from_host(mat) local res = nerv.CuMatrixFloat(mat:nrow(), mat:ncol()) res:copy_fromh(mat) return res end +--- Create a host matrix copy of the CUDA matrix function nerv.CuMatrixFloat:new_to_host() local res = nerv.MMatrixFloat(self:nrow(), self:ncol()) self:copy_toh(res) return res end +--- CUDA double matrices +-- @type nerv.CuMatrixDouble + +--- Create a CUDA matrix copy of the host matrix (in memory) +-- @param mat the host matrix function nerv.CuMatrixDouble.new_from_host(mat) local res = nerv.CuMatrixDouble(mat:nrow(), mat:ncol()) res:copy_fromh(mat) return res end +--- Create a host matrix copy of the CUDA matrix function nerv.CuMatrixDouble:new_to_host() local res = nerv.MMatrixDouble(self:nrow(), self:ncol()) self:copy_toh(res) return res end + +--- The base class for all host (in-memory) matrices +-- @type nerv.MMatrix + +--- A wrapper function for `copy_fromh` +function nerv.MMatrix:copy_toh(b, ...) + b:copy_fromh(self, ...) +end + +--- Print profiling info of host matrices +function nerv.MMatrix.print_profile() + nerv.info("mmatrix profile not available") +end + +--- Clear profiling info of host matrices +function nerv.MMatrix.clear_profile() +end |