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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2010. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Purpose: Simple example of NIFs using resource objects to implement functions
* for matrix calculations.
*/
#include "erl_nif.h"
#include <stddef.h>
#include <assert.h>
typedef struct
{
unsigned nrows;
unsigned ncols;
double* data;
}Matrix;
#define POS(MX, ROW, COL) ((MX)->data[(ROW)* (MX)->ncols + (COL)])
static int get_number(ErlNifEnv* env, ERL_NIF_TERM term, double* dp);
static Matrix* alloc_matrix(ErlNifEnv* env, unsigned nrows, unsigned ncols);
static void matrix_dtor(ErlNifEnv* env, void* obj);
static ErlNifResourceType* resource_type = NULL;
static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
{
ErlNifResourceType* rt = enif_open_resource_type(env, NULL,
"matrix_nif_example",
matrix_dtor,
ERL_NIF_RT_CREATE, NULL);
if (rt == NULL) {
return -1;
}
assert(resource_type == NULL);
resource_type = rt;
return 0;
}
static ERL_NIF_TERM create(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
/* create(Nrows, Ncolumns, [[first row],[second row],...,[last row]]) -> Matrix */
unsigned nrows, ncols;
unsigned i, j;
ERL_NIF_TERM list, row, ret;
Matrix* mx = NULL;
if (!enif_get_uint(env, argv[0], &nrows) || nrows < 1 ||
!enif_get_uint(env, argv[1], &ncols) || ncols < 1) {
goto badarg;
}
mx = alloc_matrix(env, nrows, ncols);
list = argv[2];
for (i = 0; i<nrows; i++) {
if (!enif_get_list_cell(env, list, &row, &list)) {
goto badarg;
}
for (j = 0; j<ncols; j++) {
ERL_NIF_TERM v;
if (!enif_get_list_cell(env, row, &v, &row) ||
!get_number(env, v, &POS(mx,i,j))) {
goto badarg;
}
}
if (!enif_is_empty_list(env, row)) {
goto badarg;
}
}
if (!enif_is_empty_list(env, list)) {
goto badarg;
}
ret = enif_make_resource(env, mx);
enif_release_resource(mx);
return ret;
badarg:
if (mx != NULL) {
enif_release_resource(mx);
}
return enif_make_badarg(env);
}
static ERL_NIF_TERM pos(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
/* pos(Matrix, Row, Column) -> float() */
Matrix* mx;
unsigned i, j;
if (!enif_get_resource(env, argv[0], resource_type, (void**)&mx) ||
!enif_get_uint(env, argv[1], &i) || (--i >= mx->nrows) ||
!enif_get_uint(env, argv[2], &j) || (--j >= mx->ncols)) {
return enif_make_badarg(env);
}
return enif_make_double(env, POS(mx, i,j));
}
static ERL_NIF_TERM add(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
/* add(Matrix_A, Matrix_B) -> Matrix_Sum */
unsigned i, j;
ERL_NIF_TERM ret;
Matrix* mxA = NULL;
Matrix* mxB = NULL;
Matrix* mxS = NULL;
if (!enif_get_resource(env, argv[0], resource_type, (void**)&mxA) ||
!enif_get_resource(env, argv[1], resource_type, (void**)&mxB) ||
mxA->nrows != mxB->nrows ||
mxB->ncols != mxB->ncols) {
return enif_make_badarg(env);
}
mxS = alloc_matrix(env, mxA->nrows, mxA->ncols);
for (i = 0; i < mxA->nrows; i++) {
for (j = 0; j < mxA->ncols; j++) {
POS(mxS, i, j) = POS(mxA, i, j) + POS(mxB, i, j);
}
}
ret = enif_make_resource(env, mxS);
enif_release_resource(mxS);
return ret;
}
static ERL_NIF_TERM size_of(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
/* size(Matrix) -> {Nrows, Ncols} */
Matrix* mx;
if (!enif_get_resource(env, argv[0], resource_type, (void**)&mx)) {
return enif_make_badarg(env);
}
return enif_make_tuple2(env, enif_make_uint(env, mx->nrows),
enif_make_uint(env, mx->ncols));
}
static ERL_NIF_TERM to_term(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
/* to_term(Matrix) -> [[first row], [second row], ...,[last row]] */
unsigned i, j;
ERL_NIF_TERM res;
Matrix* mx = NULL;
if (!enif_get_resource(env, argv[0], resource_type, (void**)&mx)) {
return enif_make_badarg(env);
}
res = enif_make_list(env, 0);
for (i = mx->nrows; i-- > 0; ) {
ERL_NIF_TERM row = enif_make_list(env, 0);
for (j = mx->ncols; j-- > 0; ) {
row = enif_make_list_cell(env, enif_make_double(env, POS(mx,i,j)),
row);
}
res = enif_make_list_cell(env, row, res);
}
return res;
}
static int get_number(ErlNifEnv* env, ERL_NIF_TERM term, double* dp)
{
long i;
return enif_get_double(env, term, dp) ||
(enif_get_long(env, term, &i) && (*dp=(double)i, 1));
}
static Matrix* alloc_matrix(ErlNifEnv* env, unsigned nrows, unsigned ncols)
{
Matrix* mx = enif_alloc_resource(resource_type, sizeof(Matrix));
mx->nrows = nrows;
mx->ncols = ncols;
mx->data = enif_alloc(nrows*ncols*sizeof(double));
return mx;
}
static void matrix_dtor(ErlNifEnv* env, void* obj)
{
Matrix* mx = (Matrix*) obj;
enif_free(mx->data);
mx->data = NULL;
}
static ErlNifFunc nif_funcs[] =
{
{"create", 3, create},
{"pos", 3, pos},
{"add", 2, add},
{"size_of", 1, size_of},
{"to_term", 1, to_term}
};
ERL_NIF_INIT(matrix_nif,nif_funcs,load,NULL,NULL,NULL);
|