-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleft_matmul.h
More file actions
112 lines (99 loc) · 3.68 KB
/
left_matmul.h
File metadata and controls
112 lines (99 loc) · 3.68 KB
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
#ifndef ATOM_LEFT_MATMUL_H
#define ATOM_LEFT_MATMUL_H
#include "bivariate.h"
#include "common.h"
#include "subexpr.h"
/* Left matrix multiplication: A @ f(x).
*
* Unified binding for both fixed-constant and updatable-parameter cases.
* Python signature:
* make_left_matmul(param_or_none, child, data, indices, indptr, m, n)
*
* - param_or_none: None for fixed constants (a PARAM_FIXED parameter is created
* internally), or an existing parameter capsule for updatable parameters.
* - child: the child expression capsule f(x).
* - data, indices, indptr, m, n: CSR arrays defining the sparsity pattern and
* initial values of the matrix A. */
static PyObject *py_make_left_matmul(PyObject *self, PyObject *args)
{
PyObject *param_obj;
PyObject *child_capsule;
PyObject *data_obj, *indices_obj, *indptr_obj;
int m, n;
if (!PyArg_ParseTuple(args, "OOOOOii", ¶m_obj, &child_capsule, &data_obj,
&indices_obj, &indptr_obj, &m, &n))
{
return NULL;
}
expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
if (!child)
{
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
return NULL;
}
PyArrayObject *data_array =
(PyArrayObject *) PyArray_FROM_OTF(data_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
PyArrayObject *indices_array = (PyArrayObject *) PyArray_FROM_OTF(
indices_obj, NPY_INT32, NPY_ARRAY_IN_ARRAY);
PyArrayObject *indptr_array = (PyArrayObject *) PyArray_FROM_OTF(
indptr_obj, NPY_INT32, NPY_ARRAY_IN_ARRAY);
if (!data_array || !indices_array || !indptr_array)
{
Py_XDECREF(data_array);
Py_XDECREF(indices_array);
Py_XDECREF(indptr_array);
return NULL;
}
double *csr_data = (double *) PyArray_DATA(data_array);
int *csr_indices = (int *) PyArray_DATA(indices_array);
int *csr_indptr = (int *) PyArray_DATA(indptr_array);
int nnz = csr_indptr[m];
/* Build CSR matrix from Python arrays */
CSR_Matrix *A = new_csr_matrix(m, n, nnz);
memcpy(A->p, csr_indptr, (m + 1) * sizeof(int));
memcpy(A->i, csr_indices, nnz * sizeof(int));
memcpy(A->x, csr_data, nnz * sizeof(double));
/* Determine param_node: use passed capsule, or create PARAM_FIXED internally */
expr *param_node;
if (param_obj == Py_None)
{
/* Fixed constant: pass CSR data directly (values are already in CSR order) */
param_node = new_parameter(nnz, 1, PARAM_FIXED, child->n_vars, csr_data);
if (!param_node)
{
free_csr_matrix(A);
Py_DECREF(data_array);
Py_DECREF(indices_array);
Py_DECREF(indptr_array);
PyErr_SetString(PyExc_RuntimeError,
"failed to create matrix parameter node");
return NULL;
}
}
else
{
param_node = (expr *) PyCapsule_GetPointer(param_obj, EXPR_CAPSULE_NAME);
if (!param_node)
{
free_csr_matrix(A);
Py_DECREF(data_array);
Py_DECREF(indices_array);
Py_DECREF(indptr_array);
PyErr_SetString(PyExc_ValueError, "invalid param capsule");
return NULL;
}
}
Py_DECREF(data_array);
Py_DECREF(indices_array);
Py_DECREF(indptr_array);
expr *node = new_left_matmul(param_node, child, A);
free_csr_matrix(A); /* constructor copies it */
if (!node)
{
PyErr_SetString(PyExc_RuntimeError, "failed to create left_matmul node");
return NULL;
}
expr_retain(node); /* Capsule owns a reference */
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
}
#endif /* ATOM_LEFT_MATMUL_H */