Skip to content

Commit

Permalink
Merge branch 'exp_events' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Rojods committed Jul 5, 2024
2 parents bc3c1f8 + 28cde93 commit f022be0
Show file tree
Hide file tree
Showing 14 changed files with 1,216 additions and 2,051 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cxx = "1.0"
cxx-build = "1.0"
autocxx = "0.26.0"
autocxx-build = "0.26.0"
crossbeam = { version = "0.8.4", features = ["crossbeam-channel"] }
derive_builder = "0.20.0"
downcast-rs = "1.2.0"
jni = { version = "0.21.1", features = ["invocation"] }
Expand Down Expand Up @@ -49,6 +50,6 @@ url = "2.4.1"
zip = "0.6.6"

[workspace.package]
version = "0.8.3"
version = "0.8.4"
authors = ["Rodolfo Jordao"]
edition = "2021"
2 changes: 1 addition & 1 deletion java-core/src/main/java/idesyde/core/Explorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ default ExplorationBidding bid(DecisionModel decisionModel) {
return new ExplorationBidding(false, false, 10.0, Set.of(), Map.of());
}

default Stream<? extends ExplorationSolution> explore(DecisionModel decisionModel,
default Stream<? extends ExplorationEvent> explore(DecisionModel decisionModel,
Set<ExplorationSolution> previousSolutions,
Configuration configuration) {
return Stream.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ default Stream<ExplorationSolution> exploreAADPTM(
.builder(g -> evaluateAADPTM(g, jobs, jobIdxGraph, configuration),
allConstraints.constrain(codec))
.offspringSelector(new TournamentSelector<>(5))
// .executor(Executors.newSingleThreadExecutor())
.executor(Executors.newSingleThreadExecutor())
.survivorsSelector(UFTournamentSelector.ofVec())
.constraint(allConstraints)
.alterers(
Expand All @@ -243,7 +243,9 @@ default Stream<ExplorationSolution> exploreAADPTM(
: timedSolStream;
return limitedImprovementStream
.map(sol -> {
// System.out.println("Decoding");
var decoded = codec.decode(sol.bestPhenotype().genotype());
// System.out.println("Decoded");
var solMap = new HashMap<String, Double>(sol.bestFitness().length());
var bestFit = sol.bestFitness().data();
var i = 0;
Expand All @@ -266,6 +268,7 @@ default Stream<ExplorationSolution> exploreAADPTM(
}
}
}
// System.out.println("Returning");
return new ExplorationSolution(solMap, decoded);
});
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package idesyde.metaheuristics;

import idesyde.common.AperiodicAsynchronousDataflow;
import idesyde.common.AperiodicAsynchronousDataflowToPartitionedMemoryMappableMulticore;
import idesyde.common.AperiodicAsynchronousDataflowToPartitionedTiledMulticore;
import idesyde.common.SDFApplication;
import idesyde.core.AutoRegister;
import idesyde.core.DecisionModel;
import idesyde.core.ExplorationSolution;
import idesyde.core.Explorer;
import idesyde.core.ExplorationBidding;
import idesyde.core.ExplorationEvent;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Stream;

@AutoRegister(MetaHeuristicsExplorationModule.class)
Expand Down Expand Up @@ -61,23 +58,24 @@ public ExplorationBidding bid(DecisionModel decisionModel) {
}

@Override
public Stream<? extends ExplorationSolution> explore(DecisionModel decisionModel,
public Stream<? extends ExplorationEvent> explore(DecisionModel decisionModel,
Set<ExplorationSolution> previousSolutions, Configuration configuration) {
var totalSolutions = new CopyOnWriteArraySet<ExplorationSolution>();
switch (decisionModel.category()) {
case "AperiodicAsynchronousDataflowToPartitionedMemoryMappableMulticore":
return DecisionModel
.cast(decisionModel, AperiodicAsynchronousDataflowToPartitionedMemoryMappableMulticore.class)
.map(m -> exploreAADPMMM(m, previousSolutions, configuration)).orElse(Stream.empty())
.dropWhile(sol -> previousSolutions.contains(sol) ||
previousSolutions.stream().anyMatch(prev -> prev.dominates(sol)));
previousSolutions.stream().anyMatch(prev -> prev.dominates(sol)))
.map(m -> new ExplorationEvent(Optional.of(m), false));
// .peek(s -> totalSolutions.add(s));
case "AperiodicAsynchronousDataflowToPartitionedTiledMulticore":
return DecisionModel
.cast(decisionModel, AperiodicAsynchronousDataflowToPartitionedTiledMulticore.class)
.map(m -> exploreAADPTM(m, previousSolutions, configuration)).orElse(Stream.empty())
.dropWhile(sol -> previousSolutions.contains(sol) ||
previousSolutions.stream().anyMatch(prev -> prev.dominates(sol)));
previousSolutions.stream().anyMatch(prev -> prev.dominates(sol)))
.map(m -> new ExplorationEvent(Optional.of(m), false));
// .peek(s -> totalSolutions.add(s));
default:
return Stream.empty();
Expand Down
47 changes: 41 additions & 6 deletions rust-bridge-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use std::{
};

use idesyde_core::{
DecisionModel, DesignModel, ExplorationBid, ExplorationConfiguration, ExplorationSolution,
Explorer, IdentificationResult, IdentificationRuleLike, LoggedResult, Module,
OpaqueDecisionModel, OpaqueDesignModel, ReverseIdentificationRuleLike,
DecisionModel, DesignModel, ExplorationBid, ExplorationConfiguration, ExplorationEvent, ExplorationSolution, Explorer, IdentificationResult, IdentificationRuleLike, LoggedResult, Module, OpaqueDecisionModel, OpaqueDesignModel, ReverseIdentificationRuleLike
};
use jni::{
objects::{GlobalRef, JObject, JObjectArray, JPrimitiveArray, JString, JValue},
Expand Down Expand Up @@ -403,6 +401,16 @@ impl<'a> FromJava<'a, JObject<'a>> for f64 {
}
}

impl<'a> FromJava<'a, JObject<'a>> for bool {
fn from_java(env: &mut JNIEnv<'a>, obj: JObject<'a>) -> Result<bool, jni::errors::Error> {
env.with_local_frame(32, |inner| {
inner
.call_method(&obj, "booleanValue", "()Z", &[])
.and_then(|x| x.z())
})
}
}

impl<'a> FromJava<'a, JObject<'a>> for String {
fn from_java(env: &mut JNIEnv<'a>, obj: JObject<'a>) -> Result<String, jni::errors::Error> {
env.with_local_frame_returning_local(256, |inner| {
Expand Down Expand Up @@ -707,6 +715,30 @@ impl<'a> FromJava<'a, JObject<'a>> for ExplorationSolution {
}
}


impl<'a> FromJava<'a, JObject<'a>> for ExplorationEvent {
fn from_java(env: &mut JNIEnv<'a>, obj: JObject<'a>) -> Result<Self, jni::errors::Error> {
let solution_opt: JObject = env
.call_method(&obj, "solution", "()Ljava/util/Optional;", &[])?
.l()?;
let solution = if let Ok(true) = env.call_method(&solution_opt, "isPresent", "()Z", &[]).and_then(|x| x.z()) {
let java_model: JObject = env
.call_method(&solution_opt, "get", "()Ljava/lang/Object;", &[])?
.l()?;
ExplorationSolution::from_java(env, java_model).ok()
} else {
None
};
Ok(ExplorationEvent {
solution,
optimality_proved: env.call_method(&obj, "optimalityProved", "()Ljava/lang/Boolean;", &[])
.and_then(|x| x.l())
.and_then(|x| env.call_method(&x, "booleanValue", "()Z", &[]))
.and_then(|x| x.z()).unwrap_or(false),
})
}
}

#[derive(Clone)]
struct JavaModuleIdentificationRule {
pub java_vm: Arc<JavaVM>,
Expand Down Expand Up @@ -875,7 +907,7 @@ pub struct JavaModuleExplorer {
}

impl Iterator for JavaModuleExplorerationIter {
type Item = idesyde_core::ExplorationSolution;
type Item = idesyde_core::ExplorationEvent;

fn next(&mut self) -> Option<Self::Item> {
if let Ok(mut env) = self.java_vm.attach_current_thread_permanently() {
Expand All @@ -885,9 +917,12 @@ impl Iterator for JavaModuleExplorerationIter {
if has_next.map(|x| x == true).unwrap_or(false) {
let next_java_opt = env
.call_method(&self.iter, "next", "()Ljava/lang/Object;", &[])
.inspect_err(|_| {let _ = env.exception_describe();})
.and_then(|x| x.l());
if let Ok(next_java) = next_java_opt {
return ExplorationSolution::from_java(&mut env, next_java).ok();
return ExplorationEvent::from_java(&mut env, next_java).ok();
} else {
let _ = env.exception_describe();
}
}
}
Expand Down Expand Up @@ -953,7 +988,7 @@ impl Explorer for JavaModuleExplorer {
m: Arc<dyn DecisionModel>,
currrent_solutions: &HashSet<idesyde_core::ExplorationSolution>,
exploration_configuration: idesyde_core::ExplorationConfiguration,
) -> Arc<Mutex<dyn Iterator<Item = idesyde_core::ExplorationSolution> + Send + Sync>> {
) -> Arc<Mutex<dyn Iterator<Item = idesyde_core::ExplorationEvent> + Send + Sync>> {
let java_vm = self.java_vm.clone();
let exploration_iter = java_vm.attach_current_thread_permanently().and_then(|mut top_env| {
let java_m = m.into_java(&mut top_env)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ array[Processes] of var int: durationFetch;
array[Processes] of var int: durationRead;
array[Processes] of var int: durationWrite;

array[Firings] of var opt Firings: orderNext;
array[Firings, Firings] of var int: maxPath;
array[Firings, Mappable] of var int: maxPath;

array[Processes] of var int: invThroughput;
var 0..card(Mappable): nUsedPEs;
Expand Down Expand Up @@ -127,39 +126,33 @@ constraint forall(p in Processes)(duration[p] = durationFetch[p] + executionTime

constraint forall(f, ff in Firings where f != ff /\ ff in follows[f]) (processesExecution[firingsActor[f]] = processesExecution[firingsActor[ff]] -> firingsOrdering[f] < firingsOrdering[ff]);

constraint forall(f, ff in Firings where f != ff) (processesExecution[firingsActor[f]] = processesExecution[firingsActor[ff]] -> firingsOrdering[f] != firingsOrdering[ff]);
constraint diffn([processMapping[firingsActor[f]]| f in Firings], [firingsOrdering[f] | f in Firings], [1 | f in Firings], [1 | f in Firings]);

constraint forall(pe in ListSchedulers, f in Firings) (processesExecution[firingsActor[f]] = pe -> count_lt([processesExecution[firingsActor[ff]] | ff in Firings], pe, firingsOrdering[f]));

constraint nvalue(nUsedPEs, processesExecution);

constraint forall(f in Firings) (orderNext[f] != f);
constraint forall(f, ff in Firings where f != ff) (processesExecution[firingsActor[f]] != processesExecution[firingsActor[ff]] -> orderNext[f] != ff);
constraint forall(f, ff in Firings where f != ff) (processesExecution[firingsActor[f]] = processesExecution[firingsActor[ff]] /\ firingsOrdering[f] + 1 = firingsOrdering[ff] <-> orderNext[f] = ff);


constraint forall(f in Firings) (maxPath[f, f] = duration[firingsActor[f]]);
constraint forall(f, ff in Firings where f != ff) (
maxPath[f, ff] >= duration[firingsActor[f]] + max([0] ++ [maxPath[next, ff] | next in follows[f] where ff in follows[next]])
constraint forall(f in Firings, pe in Tiles where firingsOrdering[f] = 0 /\ processMapping[firingsActor[f]] = pe) (
maxPath[f, pe] = duration[firingsActor[f]]
);
constraint forall(f, ff in Firings where f != ff) (
let { var int: pe = processesExecution[firingsActor[f]] } in
if pe in ListSchedulers then
maxPath[f, ff] >= duration[firingsActor[f]] + maxPath[orderNext[f], ff]
endif
constraint forall(f in Firings, pe in Tiles where firingsOrdering[f] = 0 /\ processMapping[firingsActor[f]] != pe) (
maxPath[f, pe] = duration[firingsActor[f]] + max([-maxTh] ++ [maxPath[ff, pe] | ff in Firings where f in follows[ff]])
);
constraint forall(f in Firings, pe in Tiles where firingsOrdering[f] > 0) (
maxPath[f, pe] = duration[firingsActor[f]] + max(
[maxPath[ff, pe] | ff in Firings where f != ff /\ processMapping[firingsActor[f]] = processMapping[firingsActor[ff]] /\ firingsOrdering[f] > firingsOrdering[ff]] ++
[maxPath[ff, pe] | ff in Firings where f != ff /\ processMapping[firingsActor[f]] != processMapping[firingsActor[ff]] /\ f in follows[ff]]
)
);


constraint forall(p, pp in Processes where p != pp) (processesExecution[p] = processesExecution[pp] -> invThroughput[p] = invThroughput[pp]);
constraint forall(p, pp in Processes where p != pp) (connected[p, pp] -> invThroughput[p] = invThroughput[pp]);

constraint forall(a in Processes) (
invThroughput[a] >= duration[a]
let {
var int: maxCycleConnected = max([maxPath[f, processMapping[firingsActor[f]]] | f in Firings where connected[firingsActor[f], a]]);
var int: maxCycleComapped = max([maxPath[f, processMapping[firingsActor[f]]] | f in Firings where processMapping[firingsActor[f]] = processMapping[a]])
} in
invThroughput[a] = max([duration[a], maxCycleConnected, maxCycleComapped])
);

constraint forall(f, ff in Firings where f != ff /\ processesExecution[firingsActor[f]] = processesExecution[firingsActor[ff]] /\ firingsOrdering[f] < firingsOrdering[ff]) (
invThroughput[firingsActor[f]] >= maxPath[f, ff]
);

constraint forall(solNum in 1..nPareto) (
(previousSolutions[solNum, 1] > -1 -> nUsedPEs < previousSolutions[solNum, 1]) \/
Expand Down
Loading

0 comments on commit f022be0

Please sign in to comment.