Commit 3a04f817 authored by Christophe Favergeon's avatar Christophe Favergeon
Browse files

Improvement to Python wrapper

Some small changes to Compute graph API to define structured datatype.
Some small changes to take into account google colab new version numbers
for the packages.
parent d760b356
Loading
Loading
Loading
Loading
+18 −14
Original line number Diff line number Diff line
# Generic and functions bodes
# Generic and functions nodes

The generic and function nodes are the basic nodes that you use to create other kind of nodes in the graph.

@@ -16,7 +16,7 @@ There are 3 other classes that can be used to create new nodes from functions:
* `Binary`
* `Dsp`

## Generic Nodes
# Generic Nodes

Any new kind of node must inherit from one of those classes. Those classes are providing the methods `addInput` and/or `addOutput` to define new IOs.

@@ -38,7 +38,7 @@ class ProcessingNode(GenericNode):

See the [simple](../examples/simple/README.md) example for more explanation about how to define a new node.

### Methods
## Methods

The constructor of the node is using the `addInput` and/or `addOutput` to define new IOs.

@@ -66,16 +66,17 @@ def typeName(self):

This method defines the name of the C++ class implementing the wrapper for this node.

### Datatypes
# Datatypes

Datatypes for the IOs are inheriting from `CGStaticType`.

Currently there are two classes defined:
Currently there are 3 classes defined:

* `CType` for the standard CMSIS-DSP types
* `CStructType` for a C struct
* `PythonClassType` to create structured datatype for the Python scheduler

#### CType
## CType

You create such a type with `CType(id)` where `id` is one of the constant coming from the Python wrapper:

@@ -94,25 +95,28 @@ You create such a type with `CType(id)` where `id` is one of the constant coming

For instance, to define a `float32_t` type for an IO you can use `CType(F32)`

#### CStructType
## CStructType

The constructor has the following definition

```python
def __init__(self,name,python_name,size_in_bytes): 
def __init__(self,name,size_in_bytes): 
```

* `name` is the name of the C struct
* `python_name` is the name of the Python class implementing this type (when you generate a Python schedule)
* `size_in_bytes` is the size of the struct. It should take into account padding. It is used in case of buffer sharing since the datatype of the shared buffer is `int8_t`. The Python script must be able to compute the size of those buffers and needs to know the size of the structure.
* `size_in_bytes` is the size of the struct. It should take into account padding. It is used in case of buffer sharing since the datatype of the shared buffer is `int8_t`. The Python script must be able to compute the size of those buffers and needs to know the size of the structure including padding.

In Python, there is no `struct`. This datatype is mapped to an object. Object have reference type. Compute graph FIFOs are assuming a value type semantic.
## PythonClassType

As consequence, in Python side you should never copy those structs since it would copy the reference. You should instead copy the members of the struct.
```python
def __init__(self,python_name)
```

In Python, there is no `struct`. This datatype is mapped to an object. Object have reference type. Compute graph FIFOs are assuming a value type semantic.

If you don't plan on generating a Python scheduler, you can just use whatever name you want for the `python_name`. It will be ignored by the C++ code generation.
As consequence, in Python side you should never copy those structs since it would copy the reference. You should instead copy the members of the struct and they should be scalar values.

## Function and constant nodes
# Function and constant nodes

A Compute graph C++ wrapper is useful when the software components you use have a state that needs to be initialized in the C++ constructor, and preserved between successive calls to the `run` method of the wrapper.

+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ Python APIs to describe the nodes and graph and generate the C++, Python or Grap

1. ## [Graph class](Graph.md)

2. ## [Generic and function nodes](Generic.md)
2. ## [Generic, function nodes and datatypes](Generic.md)

3. ## Scheduler

+3 −0
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@
#include  CMSIS_device_header
#include "cmsis_os2.h"

#if defined(RTE_Compiler_EventRecorder)
#include "EventRecorder.h"
#endif 

void app_main (void *argument)
{
+2 −0
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ using namespace std;
#include <cstdio>
#include "arm_math.h"

#include "cg_status.h"

#include "host/FileSink.h"
#include "host/FileSource.h"
#include "CFFT.h"
+15 −2
Original line number Diff line number Diff line
@@ -11,11 +11,15 @@ This example is illustrating :

It is  possible to use a custom datatype:

For the C++ version of the scheduler it would be:

```python
complexType=CStructType("complex","MyComplex",8)
complexType=CStructType("MyComplex",8)
```

This is defining a new datatype that is mapped to the type `complex` in C/C++ and the class `MyComplex` in Python. The last argument is the size in bytes of the struct in C.
This is defining a new datatype that is mapped to the type `complex` in C/C++.

The last argument is the size in bytes of the struct in C.

The type complex may be defined with:

@@ -26,6 +30,15 @@ typedef struct {
} complex;
```

For the Python version of the scheduler:

```python
complexType=PythonClassType("MyComplex")
```

The class `MyComplex` is used in Python. 


**Note that:**

- The value **must have** value semantic in C/C++. So avoid classes 
Loading