Commit dbd5e2eb authored by ='s avatar =

Variable precision given in the file can now be a partial map

parent 4c7f41e8
......@@ -175,11 +175,6 @@ object Main {
case _ => false
}
for(opt <- ctx.options) opt match {
case ParamOption("precFile", b) => println(s"precFile is $b")
case _ =>
}
// this is not ideal, using 'magic' strings
if (ctx.hasFlag("dynamic")) {
analysis.SpecsProcessingPhase andThen
......@@ -205,6 +200,7 @@ object Main {
backend.CodeGenerationPhase
} else {
analysis.SpecsProcessingPhase andThen
analysis.ChoosePrecisionPhase andThen
analysis.RangeErrorPhase andThen
InfoPhase
}
......
......@@ -20,13 +20,13 @@ object ChoosePrecisionPhase extends DaisyPhase {
override val name = "Choose Precision"
override val description = "Chooses the precision of each variable"
// todo: need to start timer?
var reporter: Reporter = null
override def run(ctx:Context, prg:Program): (Context, Program) = {
reporter = ctx.reporter
reporter.info(s"\nStarting $name phase")
val timer = ctx.timers.chooseprecision.start
val fncsToConsider: Seq[String] = functionsToConsider(ctx, prg)
......@@ -42,15 +42,16 @@ object ChoosePrecisionPhase extends DaisyPhase {
reporter.fatalError("You can only use precFile XOR randomMP.")
var varPrecisionMap: Map[Identifier, Map[Identifier, Precision]] = Map()
val uniformPrecision = ctx.constantPrecision
if(precFile != ""){
val (fncIdMap, varIdMap) = buildIdentifierMap(prg)
varPrecisionMap = parseFile(precFile, fncIdMap, varIdMap, reporter, prg.id)
// need to check typing and complete it (with max available precision?) if needed.
for (fnc <- prg.defs) if (!fnc.precondition.isEmpty && !fnc.body.isEmpty) {
val oldMap = varPrecisionMap(fnc.id)
varPrecisionMap = varPrecisionMap - fnc.id + (fnc.id -> typeCheckAndComplete(fnc, oldMap, uniformPrecision, uniformPrecision, reporter))
}
} else {
prg.defs.filter(fnc =>
!fnc.precondition.isEmpty &&
......@@ -80,6 +81,7 @@ object ChoosePrecisionPhase extends DaisyPhase {
case None => None }
FunDef(id, retT, params, pre, newb, post, isField)}))
timer.stop
(ctx.copy(precision=varPrecisionMap), newprg)
}
......@@ -141,50 +143,53 @@ object ChoosePrecisionPhase extends DaisyPhase {
prg.defs.map(fnc => (fnc.id.name -> buildIdentifierMapFunction(fnc))).toMap)
}
// def typeCheckAndComplete(fnc:FunDef, varMap: Map[Identifier, Precision], defaultPrecision:Precision, constantPrecision:Precision, reporter:Reporter) : Map[Identifier, Precision] = {
// def recurse(e:Expr, defPrec:Precision, cPrec:Precision, precMap:Map[Identifier, Precision]) : (Map[Identifier, Precision], Precision) = {
// e match {
// case Variable(id) =>
// precMap.get(id) match {
// case Some(m) => (precMap, m)
// case None => (precMap + (id -> defPrec), defPrec)
// }
// case RealLiteral(r) => (precMap, cPrec)
// case ArithOperator(Seq(l, r), recons) =>
// val (map_l, prec_l) = recurse(l, defPrec, cPrec, precMap)
// val (map_r, prec_r) = recurse(r, defPrec, cPrec, map_l)
// val prec = getUpperBound(prec_l, prec_r)
// (map_r, prec)
// case ArithOperator(Seq(u), recons) =>
// recurse(u, defPrec, cPrec, precMap)
// case Let(id, value, body) =>
// val (map_v, prec_v) = recurse(value, defPrec, cPrec, precMap)
// precMap.get(id) match {
// case Some(m) =>
// if(m <= prec_v)
// recurse(body, defPrec, cPrec, map_v)
// else
// reporter.fatalError(s"Contradiction in typing of Let $id.")
// case None =>
// recurse(body, defPrec, cPrec, map_v + (id -> prec_v))
// }
// }
// }
// def checkParams(p:Seq[ValDef], defPrec:Precision, precMap:Map[Identifier, Precision]) : Map[Identifier, Precision] = {
// p.map(e => e match { case ValDef(v) =>
// precMap.get(v) match {
// case None => (v -> defPrec)
// case Some(m) =>
// }}).toMap
// }
// val map_p = checkParams(fnc.params, defaultPrecision, varMap)
// val final_map = recurse(fnc.body
// }
def typeCheckAndComplete(fnc:FunDef, varMap: Map[Identifier, Precision], defaultPrecision:Precision, constantPrecision:Precision, reporter:Reporter) : Map[Identifier, Precision] = {
def recurse(e:Expr, defPrec:Precision, cPrec:Precision, precMap:Map[Identifier, Precision]) : (Map[Identifier, Precision], Precision) = {
e match {
case Variable(id) =>
precMap.get(id) match {
case Some(m) => (precMap, m)
case None => (precMap + (id -> defPrec), defPrec)
}
case RealLiteral(r) => (precMap, cPrec)
case ArithOperator(Seq(l, r), recons) =>
val (map_l, prec_l) = recurse(l, defPrec, cPrec, precMap)
val (map_r, prec_r) = recurse(r, defPrec, cPrec, map_l)
val prec = getUpperBound(prec_l, prec_r)
(map_r, prec)
case ArithOperator(Seq(u), recons) =>
recurse(u, defPrec, cPrec, precMap)
case Let(id, value, body) =>
val (map_v, prec_v) = recurse(value, defPrec, cPrec, precMap)
precMap.get(id) match {
case Some(m) =>
if(m <= prec_v)
recurse(body, defPrec, cPrec, map_v)
else
reporter.fatalError(s"Contradiction in typing of Let $id.")
case None =>
recurse(body, defPrec, cPrec, map_v + (id -> prec_v))
}
}
}
def checkParams(p:Seq[ValDef], defPrec:Precision, precMap:Map[Identifier, Precision]) : Map[Identifier, Precision] = {
p.map(e => e match { case ValDef(v) =>
precMap.get(v) match {
case None => (v -> defPrec)
case Some(m) => (v -> m)
}}).toMap
}
fnc.body match {
case Some(b) =>
val map_p = varMap ++ checkParams(fnc.params, defaultPrecision, varMap)
recurse(b, defaultPrecision, constantPrecision, map_p)._1
case None => varMap
}
}
def parseFile(f:String, fncIdMap:Map[String, Identifier], varIdMap:Map[String, Map[String, Identifier]], reporter:Reporter, dummyId:Identifier) : Map[Identifier, Map[Identifier, Precision]] = {
val bufferedSource = io.Source.fromFile(f)
......@@ -197,7 +202,6 @@ object ChoosePrecisionPhase extends DaisyPhase {
// input format:
// functionName:
// variablei=typei
// todo: regexp?
for (line <- bufferedSource.getLines) {
......@@ -229,6 +233,7 @@ object ChoosePrecisionPhase extends DaisyPhase {
case None => reporter.fatalError(s"${currentTypes(0)} not found in the identifier map of $currentFunction.")
}
}
else if (line.isEmpty || line.trim.isEmpty) {}
else
reporter.fatalError(s"Format error in $f")
}
......
......@@ -54,7 +54,7 @@ object RangeErrorPhase extends DaisyPhase with RoundoffEvaluators with IntervalS
var trackInitialErrs = true
var trackRoundoffErrs = true
var randomMP = false
var mixedPrecision = false
var uniformPrecision: Precision = Float64
......@@ -69,7 +69,8 @@ object RangeErrorPhase extends DaisyPhase with RoundoffEvaluators with IntervalS
}
case FlagOption("noInitialErrors") => trackInitialErrs = false
case FlagOption("noRoundoff") => trackRoundoffErrs = false
case FlagOption("randomMP") => randomMP = true
case FlagOption("randomMP") => mixedPrecision = true
case ParamOption("precFile", _) => mixedPrecision = true
case ChoiceOption("precision", s) => s match {
case "Float32" =>
uniformPrecision = Float32
......@@ -141,7 +142,7 @@ object RangeErrorPhase extends DaisyPhase with RoundoffEvaluators with IntervalS
val (resError: Rational, resRange: Interval, rangeMap:Map[Expr, Interval], errorMap:Map[Expr, Interval]) = (rangeMethod, errorMethod) match {
case ("interval", "interval") =>
if(randomMP) {
if(mixedPrecision) {
mixedPrecRoundoff_IA_IA(fnc.body.get, inputValMap, inputErrorMap, precisionMap, uniformPrecision, trackRoundoffErrs)
} else {
uniformRoundoff_IA_IA(fnc.body.get, inputValMap, inputErrorMap, uniformPrecision, trackRoundoffErrs)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment