aboutsummaryrefslogtreecommitdiff
path: root/nerv/matrix/init.lua
blob: 5d0544acdf8623654a303a6f471b56442da99c71 (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
--- Implements a fraction of matrix operations (methods) in Lua, while
-- others are implemented in C extension.
-- @author Ted Yin <ted.sybil@gmail.com>


--- 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()
    local dim = self:dim()
    local strt = {}
    local fmt
    if self.fmt then
        fmt = self.fmt
    else
        fmt = "%.8f "
    end
    if (dim == 2) then
        for row = 0, nrow - 1 do
            local rp = self[row]
            for col = 0, ncol - 1 do
                table.insert(strt, string.format(fmt, rp[col]))
            end
            table.insert(strt, "\n")
        end
    else
        for col = 0, ncol - 1 do
            table.insert(strt, string.format(fmt, self[col]))
        end
        table.insert(strt, "\n")
    end
    table.insert(strt, string.format(
        "[%s %d x %d]", self.__typename, nrow, ncol))
    return table.concat(strt)
end

function nerv.Matrix:_generate(gen)
    if (self:dim() == 2) then
        for i = 0, self:nrow() - 1 do
            local row = self[i]
            for j = 0, self:ncol() - 1 do
                row[j] = gen(i, j)
            end
        end
    else
        for j = 0, self:ncol() - 1 do
            self[j] = gen(j)
        end
    end
end

--- 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)
    local tmp
    if nerv.is_type(self, 'nerv.CuMatrixFloat') then
        tmp = nerv.MMatrixFloat(self:nrow(), self:ncol())
    elseif nerv.is_type(self, 'nerv.CuMatrixDouble') then
        tmp = nerv.MMatrixDouble(self:nrow(), self:ncol())
    else
        tmp = self
    end
    tmp:_generate(gen)
    if nerv.is_type(self, 'nerv.CuMatrix') then
        self:copy_fromh(tmp)
    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 "

--- Operator overloading of `+`.
function nerv.Matrix:__add__(b)
    c = self:create()
    c:add(self, b, 1.0, 1.0)
    return c
end

--- Operator overloading of `-`.
function nerv.Matrix:__sub__(b)
    c = self:create()
    c:add(self, b, 1.0, -1.0)
    return c
end

--- 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

--- A wrapper function for `copy_from`
function nerv.Matrix:copy_to(b, ...)
    b:copy_from(self, ...)
end

--- The base class for all device (in-GPU) matrices
-- @type nerv.CuMatrix

--- A wrapper function for `copy_fromd`
nerv.CuMatrix.copy_tod = nerv.Matrix.copy_to

--- 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`
nerv.MMatrix.copy_toh = nerv.Matrix.copy_to

--- A wrapper function for `nerv.CuMatrix` copy
function nerv.MMatrix:copy_fromd(b, ...)
    b:copy_toh(self, ...)
end

--- A wrapper function for `nerv.CuMatrix` copy
function nerv.MMatrix:copy_tod(b, ...)
    b:copy_fromh(self, ...)
end