Skip to content

Commit aae6f93

Browse files
committed
add Add implementation
1 parent 0d9a25c commit aae6f93

7 files changed

Lines changed: 199 additions & 2 deletions

File tree

Deeploy/Targets/Magia/Bindings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: 2024 ETH Zurich and University of Bologna
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
6+
from Deeploy.AbstractDataTypes import PointerClass
7+
from Deeploy.CommonExtensions.DataTypes import int8_t, int32_t
8+
from Deeploy.DeeployTypes import CodeTransformation, NodeBinding
9+
from Deeploy.Targets.Generic.TypeCheckers import AddChecker
10+
from Deeploy.Targets.Magia.Templates import AddTemplate
11+
12+
BasicTransformer = CodeTransformation([])
13+
14+
MagiaAddBindings = [
15+
NodeBinding(AddChecker([PointerClass(int8_t), PointerClass(int8_t)], [PointerClass(int32_t)]),
16+
AddTemplate.referenceTemplate, BasicTransformer),
17+
]

Deeploy/Targets/Magia/Platform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
from Deeploy.DeeployTypes import ConstantBuffer, DeploymentEngine, DeploymentPlatform, NetworkContext, NodeMapper, \
99
NodeTemplate, StructBuffer, TopologyOptimizer, TransientBuffer, VariableBuffer
10-
from Deeploy.Targets.Generic.Bindings import BasicAddBindings
1110
from Deeploy.Targets.Generic.Layers import AddLayer
1211
from Deeploy.Targets.Generic.Parsers import AddParser
1312
from Deeploy.Targets.Generic.Templates import AllocateTemplate as BasicAllocateTemplate
13+
from Deeploy.Targets.Magia.Bindings import MagiaAddBindings
1414
from Deeploy.Targets.Magia.Templates import AllocateTemplate, FreeTemplate
1515

16-
AddMapper = NodeMapper(AddParser(), BasicAddBindings)
16+
AddMapper = NodeMapper(AddParser(), MagiaAddBindings)
1717

1818
MagiaMapping = {'Add': AddLayer([AddMapper])}
1919

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2024 ETH Zurich and University of Bologna
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from typing import Dict, List, Tuple
6+
7+
from Deeploy.DeeployTypes import NetworkContext, NodeTemplate, OperatorRepresentation
8+
9+
10+
class _MagiaAddTemplate(NodeTemplate):
11+
12+
def alignToContext(self, ctxt: NetworkContext,
13+
operatorRepresentation: OperatorRepresentation) -> Tuple[NetworkContext, Dict, List[str]]:
14+
15+
data_in_1 = ctxt.lookup(operatorRepresentation['data_in_1'])
16+
data_in_2 = ctxt.lookup(operatorRepresentation['data_in_2'])
17+
data_out = ctxt.lookup(operatorRepresentation['data_out'])
18+
19+
input_1_offset = 0
20+
if hasattr(data_in_1, "_signed") and hasattr(data_in_1, "nLevels"):
21+
input_1_offset = (data_in_1._signed == 0) * int(data_in_1.nLevels / 2)
22+
input_2_offset = 0
23+
if hasattr(data_in_2, "_signed") and hasattr(data_in_2, "nLevels"):
24+
input_2_offset = (data_in_2._signed == 0) * int(data_in_2.nLevels / 2)
25+
output_offset = 0
26+
if hasattr(data_out, "_signed") and hasattr(data_out, "nLevels"):
27+
output_offset = -(data_out._signed == 0) * int(data_out.nLevels // 2)
28+
29+
operatorRepresentation['offset'] = input_1_offset + input_2_offset + output_offset
30+
31+
return ctxt, operatorRepresentation, []
32+
33+
34+
referenceTemplate = _MagiaAddTemplate("""
35+
// Magia Add (Name: ${nodeName}, Op: ${nodeOp})
36+
Add(${data_in_1}, ${data_in_2}, ${data_out}, ${size}, ${offset});
37+
""")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2024 ETH Zurich and University of Bologna
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from . import *

DeeployTest/Platforms/Magia/main.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2025 University of Bologna and Fondazione Chips-IT.
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Alberto Dequino <alberto.dequino@unibo.it>
6+
// Alex Marchioni <alex.marchioni@chips.it>
7+
8+
#include <stdint.h>
9+
#include "testinputs.h"
10+
#include "testoutputs.h"
11+
#include "Network.h"
12+
13+
14+
15+
int main(void) {
16+
17+
uint32_t hartid = get_hartid();
18+
uint32_t l1_tile_base = get_l1_base(hartid);
19+
uint32_t cycle_start, cycle_stop;
20+
21+
/* Init tile's iDMA, Redmule, fsync, event-unit */
22+
idma_config_t idma_cfg = {.hartid = hartid};
23+
idma_controller_t idma_ctrl = {
24+
.base = NULL,
25+
.cfg = &idma_cfg,
26+
.api = &idma_api,
27+
};
28+
29+
redmule_config_t redmule_cfg = {.hartid = hartid};
30+
redmule_controller_t redmule_ctrl = {
31+
.base = NULL,
32+
.cfg = &redmule_cfg,
33+
.api = &redmule_api,
34+
};
35+
36+
fsync_config_t fsync_cfg = {.hartid = hartid};
37+
fsync_controller_t fsync_ctrl = {
38+
.base = NULL,
39+
.cfg = &fsync_cfg,
40+
.api = &fsync_api,
41+
};
42+
43+
fsync_init(&fsync_ctrl);
44+
idma_init(&idma_ctrl);
45+
redmule_init(&redmule_ctrl);
46+
47+
eu_config_t eu_cfg = {.hartid = hartid};
48+
eu_controller_t eu_ctrl = {
49+
.base = NULL,
50+
.cfg = &eu_cfg,
51+
.api = &eu_api,
52+
};
53+
eu_init(&eu_ctrl);
54+
eu_fsync_init(&eu_ctrl, 0);
55+
eu_redmule_init(&eu_ctrl, 0);
56+
eu_idma_init(&eu_ctrl, 0);
57+
58+
59+
/* initialization */
60+
InitNetwork();
61+
62+
fsync_sync_level(&fsync_ctrl, MAX_SYNC_LVL - 1, 0);
63+
eu_fsync_wait(&eu_ctrl, WAIT_MODE);
64+
65+
/* input copy */
66+
// TODO: check if memcopy is necessary!!!
67+
if (hartid == 0) {
68+
for (uint32_t buf = 0; buf < num_inputs; buf++) {
69+
memcpy(inputs[buf], _inputs[buf], inputs_bytes[buf]);
70+
}
71+
}
72+
73+
fsync_sync_global(&fsync_ctrl);
74+
eu_fsync_wait(&eu_ctrl, WAIT_MODE);
75+
76+
/* execution */
77+
cycle_start = perf_get_cycles();
78+
RunNetwork();
79+
cycle_stop = perf_get_cycles();
80+
printf("id: %d, cycles: %d\n", hartid, cycle_stop - cycle_start);
81+
82+
fsync_sync_global(&fsync_ctrl);
83+
eu_fsync_wait(&eu_ctrl, WAIT_MODE);
84+
85+
/* comparison */
86+
uint32_t errors = 0;
87+
uint32_t tests = 0;
88+
OUTPUTTYPE *computed_buf;
89+
OUTPUTTYPE *expected_buf;
90+
if (hartid == 0) {
91+
for (uint32_t buf = 0; buf < num_outputs; buf++) {
92+
tests += outputs_bytes[buf] / sizeof(OUTPUTTYPE);
93+
for (uint32_t i = 0; i < outputs_bytes[buf] / sizeof(OUTPUTTYPE); i++) {
94+
OUTPUTTYPE expected = ((OUTPUTTYPE *)_outputs[buf])[i];
95+
OUTPUTTYPE computed = ((OUTPUTTYPE *)outputs[buf])[i];
96+
OUTPUTTYPE diff = (computed > expected) ? (computed - expected) : (expected - computed);
97+
if(diff > 0) {
98+
if (ISOUTPUTFLOAT) {
99+
// printf("Expected %10.6f computed: %10.6f diff: %10.6f (at index %u in output %u)\n",
100+
// expected, computed, diff, i, buf);
101+
} else {
102+
printf("Expected %d computed: %d diff: %d (at index %u in output %u)\n",
103+
expected, computed, diff, i, buf);
104+
}
105+
errors++;
106+
}
107+
}
108+
}
109+
printf("Number of errors: %d\n", errors);
110+
}
111+
112+
return errors;
113+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

TargetLibraries/Magia/src/Add.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 ETH Zurich and University of Bologna
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "tile.h"
8+
9+
void Add(int8_t *pIn1, int8_t *pIn2, int32_t *pOut, uint32_t size, int32_t offset) {
10+
11+
uint32_t hartid = get_hartid();
12+
uint32_t tile_size = (size + NUM_HARTS - 1) / NUM_HARTS;
13+
uint32_t start = tile_size * hartid;
14+
uint32_t stop = tile_size * (hartid + 1);
15+
16+
if (stop > size) {
17+
stop = size;
18+
}
19+
20+
for (uint32_t i = start; i < stop; i++) {
21+
pOut[i] = (int32_t)(pIn1[i]) + (int32_t)(pIn2[i]) + offset;
22+
}
23+
}

0 commit comments

Comments
 (0)