Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized access to sparse multiports in the C++ target #1312

Merged
merged 29 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bcf540c
removed +nightly command for cargo
tanneberger Jul 14, 2022
0c96a23
switched to PortBankCallBack
tanneberger Jul 27, 2022
e901802
first working version
tanneberger Jul 31, 2022
75db7c9
add active_ports_indices() to all multiports
tanneberger Aug 1, 2022
d702b0c
checked out multiports branch
tanneberger Aug 1, 2022
b6a93d0
resolved merge conflicts
tanneberger Aug 1, 2022
e54cd9b
fixed shebangs
tanneberger Aug 1, 2022
df1da52
reverted versions of submodules
tanneberger Aug 1, 2022
1de6806
add CallbackTest
tanneberger Aug 3, 2022
bbb7b08
renamed class to Multiport
tanneberger Aug 25, 2022
6361a45
Merge branch 'master' into multiport-cpp
tanneberger Aug 25, 2022
9c93a57
Merge branch 'multiport-cpp' of github.com:revol-xut/lingua-franca in…
tanneberger Sep 3, 2022
9f79e6d
renamed function from active_ports_indices to get_present_port_indices
tanneberger Sep 5, 2022
8d677ce
fixed test
tanneberger Sep 9, 2022
5e1d5ba
merged master
tanneberger Sep 9, 2022
b019a3c
updated cpp submodule
tanneberger Sep 9, 2022
0b8d6f7
updated describtions
tanneberger Sep 13, 2022
5d1dc6f
some minor mistake in main reactor name
tanneberger Sep 13, 2022
d655367
Merge remote-tracking branch 'origin/master' into multiport-cpp
tanneberger Sep 13, 2022
30a50a4
submodule changes
tanneberger Sep 15, 2022
b9bd5d1
reverted submodules
tanneberger Sep 15, 2022
b5419ef
reverted ts submodule
tanneberger Sep 15, 2022
8c407ea
Merge remote-tracking branch 'origin/master' into multiport-cpp
tanneberger Sep 20, 2022
5b01645
formatted tests
tanneberger Sep 21, 2022
9ce3fd6
formatting all tests
tanneberger Sep 21, 2022
390a591
update formatting
tanneberger Sep 21, 2022
f899d7c
removing get_active_ports method
tanneberger Oct 5, 2022
4720215
type cast to BaseMultiport
tanneberger Oct 5, 2022
9e300a1
updated reactor-cpp
tanneberger Oct 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/scripts/launch.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/lfc
tanneberger marked this conversation as resolved.
Show resolved Hide resolved

#============================================================================
# Description: Run the Lingua Franca compiler.
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/lib/py/reactor-c-py
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@ class CppAssembleMethodGenerator(private val reactor: Reactor) {
|}
""".trimMargin()
}
}
}
4 changes: 2 additions & 2 deletions org.lflang/src/org/lflang/generator/cpp/CppPortGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CppPortGenerator(private val reactor: Reactor) {

val dataType = inferredType.cppType
return if (isMultiport) {
"std::vector<$portType<$dataType>>"
"reactor::Multiport<$portType<$dataType>>"
} else {
"$portType<$dataType>"
}
Expand All @@ -66,7 +66,7 @@ class CppPortGenerator(private val reactor: Reactor) {
${name}.reserve($width);
for (size_t __lf_idx = 0; __lf_idx < $width; __lf_idx++) {
std::string __lf_port_name = "${name}_" + std::to_string(__lf_idx);
${name}.emplace_back(__lf_port_name, this);
${name}.emplace_back(__lf_port_name, this, ${name}.get_active_ports(), __lf_idx);
tanneberger marked this conversation as resolved.
Show resolved Hide resolved
}
""".trimIndent()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CppReactionGenerator(
allUncontainedSources.map { "const ${it.cppType}& ${it.name}" } +
allUncontainedEffects.map { "${it.cppType}& ${it.name}" } +
allReferencedContainers.map {
if (it.isBank) "const std::vector<${getViewClassName(it)}>& ${it.name}"
if (it.isBank) "const reactor::Multiport<${getViewClassName(it)}>& ${it.name}"
else "${getViewClassName(it)}& ${it.name}"
}

Expand Down Expand Up @@ -176,7 +176,7 @@ class CppReactionGenerator(
val initializers = variables.map { "${it.variable.name}(reactor->${it.variable.name})" }

val viewDeclaration =
if (container.isBank) "std::vector<$viewClass> $viewInstance;"
if (container.isBank) "reactor::Multiport<$viewClass> $viewInstance;"
else "$viewClass $viewInstance;"

return with(PrependOperator) {
Expand Down Expand Up @@ -247,4 +247,4 @@ class CppReactionGenerator(
/** Get all definitions of deadline handlers. */
fun generateDeadlineHandlerDefinitions() =
reactionsWithDeadlines.joinToString(separator = "\n", postfix = "\n") { generateDeadlineHandlerDefinition(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************/

package org.lflang.generator.cpp

import org.lflang.ErrorReporter
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/rust/RustFileConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RustFileConfig(resource: Resource, srcGenBasePath: Path, useHierarchicalBi
FileConfig(resource, srcGenBasePath, useHierarchicalBin) {

/**
* Clean any artifacts produced by the C++ code generator.
* Clean any artifacts produced by the Rust code generator.
*/
@Throws(IOException::class)
override fun doClean() {
Expand Down
2 changes: 0 additions & 2 deletions test/Cpp/src/multiport/BankSelfBroadcast.lf
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ reactor A(bank_index: size_t(0)) {
reaction(in) {=
for (size_t i = 0; i < in.size(); i++) {
if (in[i].is_present()) {
std::cout << "Reactor " << bank_index << " received "
tanneberger marked this conversation as resolved.
Show resolved Hide resolved
<< *in[i].get() << " on channel " << i << '\n';
if (*in[i].get() != i) {
std::cerr << "ERROR: Expected " << i << '\n';
exit(1);
Expand Down
6 changes: 4 additions & 2 deletions test/Cpp/src/multiport/BankToBankMultiport.lf
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ reactor Destination(width: size_t(1)) {

reaction(in) {=
int sum = 0;
for (size_t i = 0; i < in.size(); i++) {
if (in[i].is_present()) sum += *in[i].get();

for (auto i : in.get_present_port_indices()) {
sum += *in[i].get();
}

std::cout << "Sum of received: " << sum << ".\n";
if (sum != s) {
std::cerr << "ERROR: Expected " << s << ".\n";
Expand Down
5 changes: 3 additions & 2 deletions test/Cpp/src/multiport/BankToBankMultiportAfter.lf
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ reactor Destination(width: size_t(1)) {
}

int sum = 0;
for (size_t i = 0; i < in.size(); i++) {
if (in[i].is_present()) sum += *in[i].get();
for (auto i : in.get_present_port_indices()) {
sum += *in[i].get();
}

std::cout << "Sum of received: " << sum << '\n';
if (sum != s) {
std::cerr << "ERROR: Expected " << s << '\n';
Expand Down
61 changes: 61 additions & 0 deletions test/Cpp/src/multiport/CallbackMultiport.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Test bank of reactors to multiport input with id parameter in the bank.
tanneberger marked this conversation as resolved.
Show resolved Hide resolved
target Cpp;

reactor Test {
input[3000] sink: void;
output[3000] source: void;


reaction (sink) -> source {=
for (auto i: sink.get_present_port_indices()) {
source[i].set();
}
=}
}

main reactor CallbackMultiport {
test = new Test();
state received:bool(false);

reaction (startup) -> test.sink {=
for (auto i = 0; i < 30; i++) {
auto semi_random_index = (i * 100) % 3000;
test.sink[semi_random_index].set();
}
=}

reaction (test.source) {=
received = true;
std::vector<std::size_t> positions;

for (auto i = 0; i < 30; i++) {
auto semi_random_index = (i * 100) % 3000;
positions.push_back(semi_random_index);
}

auto received_indices = test.source.get_present_port_indices();

if (positions.size() != received_indices.size()) {
std::cerr << "positions size:" << positions.size()
<< " indices size:" << received_indices.size() << std::endl;
throw std::runtime_error("not matching sizes");
}

for (auto i = 0; i < positions.size(); i++) {
if (positions[i] != received_indices[i]) {
std::cout << "mismatching:" << positions[i] << "|" << received_indices[i] << std::endl;
throw std::runtime_error("indices do not match");
}
}

std::cout << "[SUCCESS] all indices match" << std::endl;
=}

reaction (shutdown) {=
if (!received) {
std::cerr << "Error: received no input!\n";
exit(2);
}
=}
}

9 changes: 4 additions & 5 deletions test/Cpp/src/multiport/FullyConnected.lf
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ reactor Node(bank_index: size_t(0), num_nodes: size_t(4)) {
std::cout << "Node " << bank_index << " received messages from ";
received = true;
size_t count{0};
for (auto& port : in) {
if (port.is_present()) {
count++;
std::cout << *port.get() << ", ";
}
for (auto i: in.get_present_port_indices()) {
count++;
std::cout << *in[i].get() << ", ";
}

std::cout << '\n';
if (count != num_nodes) {
std::cerr << "ERROR: received less messages than expected!";
Expand Down
11 changes: 5 additions & 6 deletions test/Cpp/src/multiport/FullyConnectedAddressable.lf
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ reactor Node(bank_index: size_t(0), num_nodes: size_t(4)) {
received = true;
size_t count{0};
size_t result{0};
for (auto& port : in) {
if (port.is_present()) {
count++;
result = *port.get();
std::cout << result << ", ";
}
for (auto i : in.get_present_port_indices()) {
count++;
result = *in[i].get();
std::cout << result << ", ";
}

std::cout << '\n';

size_t expected = bank_index == 0 ? num_nodes - 1 : bank_index - 1;
Expand Down
6 changes: 4 additions & 2 deletions test/Cpp/src/multiport/MultiportFromHierarchy.lf
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ reactor Destination {
input[4] in: int

reaction(in) {=

int sum = 0;
for (int i = 0; i < in.size(); i++) {
if (in[i].is_present()) sum += *in[i].get();
for (auto i : in.get_present_port_indices()) {
sum += *in[i].get();
}

std::cout << "Sum of received: " << sum << ".\n";
if (sum != s) {
std::cerr << "ERROR: Expected " << s << ".\n";
Expand Down
4 changes: 2 additions & 2 deletions test/Cpp/src/multiport/MultiportOut.lf
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ reactor Destination {

reaction(in) {=
int sum = 0;
for (int i = 0; i < in.size(); i++) {
if (in[i].is_present()) sum += *in[i].get();
for (auto i : in.get_present_port_indices()) {
sum += *in[i].get();
}
std::cout << "Sum of received: " << sum << ".\n";
if (sum != s) {
Expand Down
5 changes: 3 additions & 2 deletions test/Cpp/src/multiport/MultiportToHierarchy.lf
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ reactor Destination(width: size_t(4)) {

reaction(in) {=
int sum = 0;
for (size_t i = 0; i < in.size(); i++) {
if (in[i].is_present()) sum += *in[i].get();
for (auto i : in.get_present_port_indices()) {
sum += *in[i].get();
}

std::cout << "Sum of received: " << sum << ".\n";
if (sum != s) {
std::cerr << "ERROR: Expected " << s << ".\n";
Expand Down
25 changes: 11 additions & 14 deletions test/Cpp/src/multiport/MultiportToMultiport2.lf
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ reactor Source(width: size_t(2)) {
=}
}

reactor Destination(width: size_t(2)) {
input[width] in: size_t

reaction(in) {=
for (size_t i = 0; i < in.size(); i++) {
if (in[i].is_present()) {
size_t value = *in[i].get();
std::cout << "Received on channel " << i << ": " << value << '\n';
// NOTE: For testing purposes, this assumes the specific
// widths instantiated below.
if (value != i % 3) {
std::cerr << "ERROR: expected " << i % 3 << '\n';
exit(1);
}
reactor Destination(width:size_t(2)) {
input[width] in:size_t;
reaction (in) {=
for (auto i: in.get_present_port_indices()) {
size_t value = *in[i].get();
std::cout << "Received on channel " << i << ": " << value << '\n';
// NOTE: For testing purposes, this assumes the specific
// widths instantiated below.
if (value != i % 3) {
std::cerr << "ERROR: expected " << i % 3 << '\n';
exit(1);
}
}
=}
Expand Down
10 changes: 4 additions & 6 deletions test/Cpp/src/multiport/MultiportToMultiportArray.lf
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ reactor Destination {

reaction(in) {=
int sum = 0;
for (size_t i = 0; i < in.size(); i++) {
if (in[i].is_present()) {
const auto& a = *in[i].get();
for (int j = 0; j < a.size(); j++) {
sum += a[j];
}
for (auto i : in.get_present_port_indices()) {
const auto& a = *in[i].get();
for (int j = 0; j < a.size(); j++) {
sum += a[j];
}
}
std::cout << "Sum of received: " << sum << '\n';
Expand Down