blob: 02488add8237bff236910ac56cce75f74e59f1d0 [file] [log] [blame]
Louis Verhaard9bfe0f82020-12-03 12:26:25 +01001/*
2 * Copyright (c) 2020 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#define PY_SSIZE_T_CLEAN
20#include <Python.h>
21
22#include <vector>
23#include "search_allocator.h"
24
25
26
27/**
28 * C++ extension wrapper for allocate
29 *
30 * This method is exposed directly in python with the arguments with a
31 * prototype of the form:
32 *
33 * output = tensor_allocator.allocate(input, available_size=0)
34 *
35 * input: [int]
36 * available_size: int
37 * output: [int]
38 */
39static PyObject *method_allocate (PyObject *self, PyObject *args)
40{
41 /* Object to hold the input integer list. */
42 PyObject *input_list_object;
43
44 /* Object to hold the available size */
45 int available_size = 0;
46
47 /* Arguments to the method are delivered as a tuple, unpack the
48 * tuple to get the individual arguments, note the second is
49 * optional.
50 */
51 if (!PyArg_ParseTuple(args, "O|i", &input_list_object, &available_size)) {
52 return NULL;
53 }
54
55 /* Unpack the length of the input integer list. */
Fredrik Svedberg5b513882020-12-11 13:42:22 +010056 int input_length = static_cast<int>(PyObject_Length (input_list_object));
Louis Verhaard9bfe0f82020-12-03 12:26:25 +010057 if (input_length < 0) {
58 input_length = 0;
59 }
60 std::vector<uint32_t> input;
61 std::vector<uint32_t> output;
62 for (int i = 0; i < input_length; ++i) {
63 PyObject *obj = PyList_GetItem(input_list_object, i);
64 uint32_t value = (uint32_t)PyLong_AsLong(obj);
65 input.push_back(value);
66 }
67 allocate(input, available_size, output);
68 PyObject *output_list = PyList_New(output.size());
69 for (size_t i = 0; i < output.size(); ++i) {
70 PyList_SetItem(output_list, i, PyLong_FromLong(output[i]));
71 }
72 return output_list;
73}
74
75/** tensor_allocator method descriptors. */
76static PyMethodDef tensor_allocator_methods[] = {
77 {"allocate", method_allocate, METH_VARARGS, "Python interface for allocate"},
78 {NULL, NULL, 0, NULL}
79};
80
81/** tensor_allocator module descriptor. */
82static struct PyModuleDef tensor_allocatormodule = {
83 PyModuleDef_HEAD_INIT,
84 "tensor_allocator",
85 "Python interface for tensor_allocator",
86 -1,
87 tensor_allocator_methods
88};
89
90PyMODINIT_FUNC PyInit_tensor_allocator(void) {
91 return PyModule_Create(&tensor_allocatormodule);
92}