-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrindow_openblas.c
246 lines (222 loc) · 7.22 KB
/
rindow_openblas.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* rindow_openblas extension for PHP */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <php.h>
#include <Zend/zend_interfaces.h>
#include <Zend/zend_exceptions.h>
#include <ext/spl/spl_iterators.h>
#include <ext/spl/spl_exceptions.h>
#include "ext/standard/info.h"
#include <Interop/Polite/Math/Matrix.h>
#include "php_rindow_openblas.h"
#if _MSC_VER
extern int rindow_load_openblas_dll();
extern void rindow_unload_openblas_dll();
#endif
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
int php_rindow_openblas_common_dtype_to_valuesize(zend_long dtype)
{
switch (dtype) {
case php_interop_polite_math_matrix_dtype_bool:
case php_interop_polite_math_matrix_dtype_int8:
case php_interop_polite_math_matrix_dtype_uint8:
case php_interop_polite_math_matrix_dtype_float8:
return 1;
case php_interop_polite_math_matrix_dtype_int16:
case php_interop_polite_math_matrix_dtype_uint16:
case php_interop_polite_math_matrix_dtype_float16:
return 2;
case php_interop_polite_math_matrix_dtype_int32:
case php_interop_polite_math_matrix_dtype_uint32:
case php_interop_polite_math_matrix_dtype_float32:
return 4;
case php_interop_polite_math_matrix_dtype_int64:
case php_interop_polite_math_matrix_dtype_uint64:
case php_interop_polite_math_matrix_dtype_float64:
return 8;
}
return 0;
}
int php_rindow_openblas_common_dtype_is_int(zend_long dtype)
{
switch (dtype) {
case php_interop_polite_math_matrix_dtype_int8:
case php_interop_polite_math_matrix_dtype_uint8:
case php_interop_polite_math_matrix_dtype_int16:
case php_interop_polite_math_matrix_dtype_uint16:
case php_interop_polite_math_matrix_dtype_int32:
case php_interop_polite_math_matrix_dtype_uint32:
case php_interop_polite_math_matrix_dtype_int64:
case php_interop_polite_math_matrix_dtype_uint64:
return 1;
}
return 0;
}
int php_rindow_openblas_common_dtype_is_float(zend_long dtype)
{
switch (dtype) {
case php_interop_polite_math_matrix_dtype_float8:
case php_interop_polite_math_matrix_dtype_float16:
case php_interop_polite_math_matrix_dtype_float32:
case php_interop_polite_math_matrix_dtype_float64:
return 1;
}
return 0;
}
int php_rindow_openblas_common_dtype_is_bool(zend_long dtype)
{
switch (dtype) {
case php_interop_polite_math_matrix_dtype_bool:
return 1;
}
return 0;
}
int php_rindow_openblas_assert_shape_parameter(
char* name, zend_long n)
{
if(n<1) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Argument %s must be greater than 0.",name);
return -1;
}
return 0;
}
int php_rindow_openblas_assert_vector_buffer_spec(
char* name,php_interop_polite_math_matrix_linear_buffer_t *buffer,
zend_long n, zend_long offset, zend_long inc)
{
if(buffer->data==NULL) {
zend_throw_exception_ex(spl_ce_DomainException, 0, "buffer%s is not initialized",name);
return -1;
}
if(offset<0) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Argument offset%s must be greater than or equals 0.",name);
return -1;
}
if(inc<1) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Argument inc%s must be greater than 0.",name);
return -1;
}
if(offset+(n-1)*inc >= buffer->size) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Vector specification too large for buffer%s.",name);
return -1;
}
return 0;
}
int php_rindow_openblas_assert_matrix_buffer_spec(
char* name, php_interop_polite_math_matrix_linear_buffer_t *buffer,
zend_long m,zend_long n, zend_long offset, zend_long ld)
{
if(buffer->data==NULL) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "uninitialized array: %s",name);
return -1;
}
if(offset<0) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Argument offset%s must be greater than or equals 0.",name);
return -1;
}
if(ld<1) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Argument ld%s must be greater than 0.",name);
return -1;
}
if(offset+(m-1)*ld+(n-1) >= buffer->size) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Matrix specification too large for buffer%s.",name);
return -1;
}
return 0;
}
int php_rindow_openblas_assert_buffer_size(
php_interop_polite_math_matrix_linear_buffer_t *buffer,
zend_long offset,zend_long size,
char* message)
{
if(size<1 || offset<0 ||
buffer->size < offset+size) {
zend_throw_exception(spl_ce_InvalidArgumentException, message, 0);
return -1;
}
return 0;
}
int php_rindow_openblas_assert_buffer_type(
php_interop_polite_math_matrix_linear_buffer_t *buffer,
char* name)
{
if(!php_interop_polite_math_matrix_is_linear_buffer(buffer)) {
zend_throw_exception_ex(zend_ce_type_error, 0, "%s must implement interface %s",
name,PHP_INTEROP_POLITE_MATH_MATRIX_LINEAR_BUFFER_CLASSNAME);
return 1;
}
return 0;
}
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(rindow_openblas)
{
#if defined(ZTS) && defined(COMPILE_DL_RINDOW_OPENBLAS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(rindow_openblas)
{
php_info_print_table_start();
php_info_print_table_header(2, "rindow_openblas support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ rindow_openblas_functions[]
*/
//static const zend_function_entry rindow_openblas_functions[] = {
//};
/* }}} */
PHP_MINIT_FUNCTION(rindow_openblas)
{
#if _MSC_VER
int rc=rindow_load_openblas_dll();
if(rc!=0) {
rindow_unload_openblas_dll();
return FAILURE;
}
#endif
php_rindow_openblas_buffer_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_rindow_openblas_blas_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_rindow_openblas_lapack_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_rindow_openblas_math_init_ce(INIT_FUNC_ARGS_PASSTHRU);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(rindow_openblas)
{
#if _MSC_VER
rindow_unload_openblas_dll();
#endif
return SUCCESS;
}
/* {{{ php_rindow_openblas_module_entry
*/
zend_module_entry rindow_openblas_module_entry = {
STANDARD_MODULE_HEADER,
"rindow_openblas", /* Extension name */
NULL, /* zend_function_entry */
PHP_MINIT(rindow_openblas), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(rindow_openblas), /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(rindow_openblas), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(rindow_openblas), /* PHP_MINFO - Module info */
PHP_RINDOW_OPENBLAS_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_RINDOW_OPENBLAS
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(rindow_openblas)
#endif