Skip to content

Commit 179a2cf

Browse files
author
Siyuan Cheng
committed
example: add hello_world example to mli module
add hello_world example to mli module Signed-off-by: Siyuan Cheng <[email protected]>
1 parent 3b08b85 commit 179a2cf

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2022 Synopsys
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.13.1)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(hello_world)
8+
9+
zephyr_compile_options("-Wno-ignored-qualifiers")
10+
11+
zephyr_compile_definitions(
12+
V2DSP_XY
13+
)
14+
15+
target_sources(app PRIVATE src/main.c)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. _embarc_mli_hello_world:
2+
3+
Hello_world Example
4+
###################
5+
6+
Quick Start
7+
--------------
8+
9+
Example supports building with [Zephyr Software Development Kit (SDK)](https://docs.zephyrproject.org/latest/getting_started/installation_linux.html#zephyr-sdk) and running with MetaWare Debuger on [nSim simulator](https://www.synopsys.com/dw/ipdir.php?ds=sim_nSIM).
10+
11+
Add embarc_mli module to Zephyr instruction
12+
-------------------------------------------
13+
14+
1. Open command line and change working directory to './zephyrproject/zephyr'
15+
16+
2. Download embarc_mli version 2.0
17+
18+
west update
19+
20+
Build with Zephyr SDK toolchain
21+
-------------------------------
22+
23+
Build requirements:
24+
- Zephyr SDK toolchain version 0.13.2 or higher
25+
- gmake
26+
27+
1. Open command line and change working directory to './zephyrproject/zephyr/samples/modules/embarc_mli/hello_world'
28+
29+
2. Build example
30+
31+
west build -b nsim_em samples/modules/embarc_mli/hello_world
32+
33+
Run example
34+
--------------
35+
36+
1. Run example
37+
38+
west flash
39+
40+
Result shall be:
41+
42+
.. code-block:: console
43+
44+
...
45+
46+
in1:
47+
48+
1 2 3 4 5 6 7 8
49+
50+
in2:
51+
52+
10 20 30 40 50 60 70 80
53+
54+
mli_krn_eltwise_add_fx16 output:
55+
56+
11 22 33 44 55 66 77 88
57+
58+
mli_krn_eltwise_sub_fx16 output:
59+
60+
-9 -18 -27 -36 -45 -54 -63 -72
61+
62+
...
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Copyright (c) 2022 Synopsys
2+
# SPDX-License-Identifier: Apache-2.0
3+
CONFIG_EMBARC_MLI=y
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sample:
2+
description: simple hello_world embarc_mli sample
3+
name: hello_world
4+
tests:
5+
sample.embarc_mli.hello_world:
6+
platform_allow: nsim_em
7+
tags: embarc_mli
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2022 Synopsys
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "mli_api.h"
8+
#include <assert.h>
9+
#include <zephyr.h>
10+
#include <stdio.h>
11+
#include "mli_config.h"
12+
13+
#if (PLATFORM == V2DSP_XY)
14+
#define DATA_ATTR __xy __attribute__((section(".Xdata")))
15+
16+
#elif (PLATFORM == V2DSP_VECTOR)
17+
#define DATA_ATTR __vccm __attribute__((section(".vecmem_data")))
18+
19+
#else
20+
#define DATA_ATTR
21+
22+
#endif
23+
24+
#define NUM_ELEMS 8
25+
26+
int16_t DATA_ATTR data_in1[NUM_ELEMS] = { 1, 2, 3, 4, 5, 6, 7, 8 };
27+
int16_t DATA_ATTR data_in2[NUM_ELEMS] = { 10, 20, 30, 40, 50, 60, 70, 80 };
28+
int16_t DATA_ATTR data_out[NUM_ELEMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
29+
30+
int main() {
31+
mli_tensor in1 = { 0 };
32+
in1.el_type = MLI_EL_FX_16;
33+
in1.rank = 1;
34+
in1.shape[0] = NUM_ELEMS;
35+
in1.mem_stride[0] = 1;
36+
in1.data.capacity = sizeof(data_in1);
37+
in1.data.mem.pi16 = (int16_t *)data_in1;
38+
39+
mli_tensor in2 = { 0 };
40+
in2.el_type = MLI_EL_FX_16;
41+
in2.rank = 1;
42+
in2.shape[0] = NUM_ELEMS;
43+
in2.mem_stride[0] = 1;
44+
in2.data.capacity = sizeof(data_in2);
45+
in2.data.mem.pi16 = (int16_t *)data_in2;
46+
47+
mli_tensor out = { 0 };
48+
out.el_type = MLI_EL_FX_16;
49+
out.rank = 1;
50+
out.shape[0] = NUM_ELEMS;
51+
out.mem_stride[0] = 1;
52+
out.data.capacity = sizeof(data_out);
53+
out.data.mem.pi16 = (int16_t *)data_out;
54+
55+
printf("in1:\n");
56+
for (int i = 0; i < NUM_ELEMS; i++) {
57+
printf("%d ", in1.data.mem.pi16[i]);
58+
}
59+
printf("\nin2:\n");
60+
for (int i = 0; i < NUM_ELEMS; i++) {
61+
printf("%d ", in2.data.mem.pi16[i]);
62+
}
63+
64+
mli_status status;
65+
status = mli_krn_eltwise_add_fx16(&in1, &in2, &out);
66+
assert(status == MLI_STATUS_OK);
67+
68+
printf("\nmli_krn_eltwise_add_fx16 output:\n");
69+
for (int i = 0; i < NUM_ELEMS; i++) {
70+
printf("%d ", out.data.mem.pi16[i]);
71+
}
72+
73+
status = mli_krn_eltwise_sub_fx16(&in1, &in2, &out);
74+
assert(status == MLI_STATUS_OK);
75+
76+
printf("\nmli_krn_eltwise_sub_fx16 output:\n");
77+
for (int i = 0; i < NUM_ELEMS; i++) {
78+
printf("%d ", out.data.mem.pi16[i]);
79+
}
80+
printf("\n");
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)