package.scala 8.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// Copyright (C) 2017 Jens Korinth, TU Darmstadt
//
// This file is part of Tapasco (TPC).
//
// Tapasco is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tapasco is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Tapasco.  If not, see <http://www.gnu.org/licenses/>.
//
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package de.tu_darmstadt.cs.esa.tapasco.jobs
import  de.tu_darmstadt.cs.esa.tapasco.Implicits._
import  de.tu_darmstadt.cs.esa.tapasco.json._
import  de.tu_darmstadt.cs.esa.tapasco.base._
import  de.tu_darmstadt.cs.esa.tapasco.base.json._
import  de.tu_darmstadt.cs.esa.tapasco.dse._
import  de.tu_darmstadt.cs.esa.tapasco.dse.json._
import  play.api.libs.json._
import  play.api.libs.json.Reads._
import  play.api.libs.functional.syntax._
import  java.nio.file._

package object json {
  /* @{ HighLevelSynthesisJob */
  private val highLevelSynthesisJobReads: Reads[Job] = (
    (JsPath \ "Job").read[String] (verifying[String](_.toLowerCase equals "highlevelsynthesis")) ~>
    (JsPath \ "Implementation").readNullable[String].map (_ getOrElse "VivadoHLS") ~
    (JsPath \ "Architectures").readNullable[Seq[String]] ~
    (JsPath \ "Platforms").readNullable[Seq[String]] ~
    (JsPath \ "Kernels").readNullable[Seq[String]]
  ) (HighLevelSynthesisJob.apply _)

  implicit val highLevelSynthesisJobWrites: Writes[HighLevelSynthesisJob] = (
    (JsPath \ "Job").write[String] ~
    (JsPath \ "Implementation").write[String] ~
    (JsPath \ "Architectures").writeNullable[Seq[String]] ~
    (JsPath \ "Platforms").writeNullable[Seq[String]] ~
    (JsPath \ "Kernels").writeNullable[Seq[String]]
  ) (unlift(HighLevelSynthesisJob.unapply _ andThen (_ map ("HighLevelSynthesis" +: _))))
  /* HighLevelSynthesisJob @} */

  /* @{ ImportJob */
  private val importJobReads: Reads[Job] = (
    (JsPath \ "Job").read[String] (verifying[String](_.toLowerCase equals "import")) ~>
    (JsPath \ "Zip").read[Path] ~
    (JsPath \ "Id").read[Int] (verifying[Int](_ > 0)) ~
    (JsPath \ "Description").readNullable[String] ~
    (JsPath \ "Average Clock Cycles").readNullable[Int] (verifying[Int](_ > 0)) ~
57
    (JsPath \ "Skip Evaluation").readNullable[Boolean] ~
58
    (JsPath \ "Synth Options").readNullable[String] ~
59
    (JsPath \ "Architectures").readNullable[Seq[String]] ~
60
61
    (JsPath \ "Platforms").readNullable[Seq[String]] ~
    (JsPath \ "Optimization").readNullable[Int] (verifying[Int](_ >= 0))
62
63
64
65
66
67
68
69
  ) (ImportJob.apply _)

  implicit val importJobWrites: Writes[ImportJob] = (
    (JsPath \ "Job").write[String] ~
    (JsPath \ "Zip").write[Path] ~
    (JsPath \ "Id").write[Int] ~
    (JsPath \ "Description").writeNullable[String] ~
    (JsPath \ "Average Clock Cycles").writeNullable[Int] ~
70
    (JsPath \ "Skip Evaluation").writeNullable[Boolean] ~
71
    (JsPath \ "Synth Options").writeNullable[String] ~
72
    (JsPath \ "Architectures").writeNullable[Seq[String]] ~
73
74
    (JsPath \ "Platforms").writeNullable[Seq[String]] ~
    (JsPath \ "Optimization").writeNullable[Int]
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  ) (unlift(ImportJob.unapply _ andThen (_ map ("Import" +: _))))
  /* ImportJob @} */

  /* @{ BulkImportJob */
  private val bulkImportJobReads: Reads[Job] = (
    (JsPath \ "Job").read[String] (verifying[String](_.toLowerCase equals "bulkimport")) ~>
    (JsPath \ "CSV").read[Path]
  ) .fmap(BulkImportJob.apply _)

  implicit val bulkImportJobWrites: Writes[BulkImportJob] = (
    (JsPath \ "Job").write[String] ~
    (JsPath \ "CSV").write[Path]
  ) (unlift(BulkImportJob.unapply _ andThen (_ map (("BulkImport", _)))))
  /* BulkImportJob @} */

  /* @{ CoreStatisticsJob */
  private val coreStatisticsJobReads: Reads[Job] = (
    (JsPath \ "Job").read[String] (verifying[String](_.toLowerCase equals "corestatistics")) ~>
    (JsPath \ "File Prefix").readNullable[String] ~
    (JsPath \ "Architectures").readNullable[Seq[String]] ~
    (JsPath \ "Platforms").readNullable[Seq[String]]
  ) (CoreStatisticsJob.apply _)

  implicit val coreStatisticsJobWrites: Writes[CoreStatisticsJob] = (
    (JsPath \ "Job").write[String] ~
    (JsPath \ "File Prefix").writeNullable[String] ~
    (JsPath \ "Architectures").writeNullable[Seq[String]] ~
    (JsPath \ "Platforms").writeNullable[Seq[String]]
  ) (unlift(CoreStatisticsJob.unapply _ andThen (_ map ("CoreStatistics" +: _))))
  /* CoreStatisticsJob @} */

  /* @{ ComposeJob */
  private val composeJobReads: Reads[Job] = (
    (JsPath \ "Job").read[String] (verifying[String](_.toLowerCase equals "compose")) ~>
    ((JsPath \ "Composition").read[Composition] | (JsPath \ "Composition").read[Path].map {
      p => Composition.from(p).toTry.get
    }) ~
    (JsPath \ "Design Frequency").read[Heuristics.Frequency] (verifying[Heuristics.Frequency](f => f >= 50 && f <= 500)) ~
    (JsPath \ "Implementation").readNullable[String].map (_ getOrElse "Vivado") ~
    (JsPath \ "Architectures").readNullable[Seq[String]] ~
    (JsPath \ "Platforms").readNullable[Seq[String]] ~
    (JsPath \ "Features").readNullable[Seq[Feature]] ~
    (JsPath \ "DebugMode").readNullable[String]
  ) (ComposeJob.apply _)

  implicit val composeJobWrites: Writes[ComposeJob] = (
    (JsPath \ "Job").write[String] ~
    (JsPath \ "Composition").write[Composition] ~
    (JsPath \ "Design Frequency").write[Heuristics.Frequency] ~
    (JsPath \ "Implementation").write[String] ~
    (JsPath \ "Architectures").writeNullable[Seq[String]] ~
    (JsPath \ "Platforms").writeNullable[Seq[String]] ~
    (JsPath \ "Features").writeNullable[Seq[Feature]] ~
    (JsPath \ "DebugMode").writeNullable[String]
  ) (unlift(ComposeJob.unapply _ andThen (_ map ("Compose" +: _))))
  /* ComposeJob @} */

  /* @{ DesignSpaceExplorationJob */
  private val dseJobReads: Reads[Job] = (
    (JsPath \ "Job").read[String] (verifying[String](_.toLowerCase equals "designspaceexploration")) ~>
    (JsPath \ "Initial Composition").read[Composition] ~
    (JsPath \ "Initial Frequency").readNullable[Heuristics.Frequency].map (_ getOrElse 100.0) ~
137
    (JsPath \ "Dimensions").read[DesignSpace.Dimensions] ~
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    (JsPath \ "Heuristic").read[Heuristics.Heuristic] ~
    (JsPath \ "Batch Size").read[Int] (verifying[Int](_ > 0)) ~
    (JsPath \ "Output Path").readNullable[Path] ~
    (JsPath \ "Architectures").readNullable[Seq[String]] ~
    (JsPath \ "Platforms").readNullable[Seq[String]] ~
    (JsPath \ "Features").readNullable[Seq[Feature]] ~
    (JsPath \ "DebugMode").readNullable[String]
  ) (DesignSpaceExplorationJob.apply _)

  implicit val dseJobWrites: Writes[DesignSpaceExplorationJob] = (
    (JsPath \ "Job").write[String] ~
    (JsPath \ "Initial Composition").write[Composition] ~
    (JsPath \ "Initial Frequency").write[Heuristics.Frequency] ~
    (JsPath \ "Dimensions").write[DesignSpace.Dimensions] ~
    (JsPath \ "Heuristic").write[Heuristics.Heuristic] ~
    (JsPath \ "Batch Size").write[Int] ~
    (JsPath \ "Output Path").writeNullable[Path] ~
    (JsPath \ "Architectures").writeNullable[Seq[String]] ~
    (JsPath \ "Platforms").writeNullable[Seq[String]] ~
    (JsPath \ "Features").writeNullable[Seq[Feature]] ~
    (JsPath \ "DebugMode").writeNullable[String]
  ) (unlift(DesignSpaceExplorationJob.unapply _ andThen (_ map ("DesignSpaceExploration" +: _))))
  /* DesignSpaceExplorationJob @} */

  /* @{ Job */
  implicit val jobReads: Reads[Job] =
    highLevelSynthesisJobReads | importJobReads | bulkImportJobReads | coreStatisticsJobReads |
    composeJobReads | dseJobReads | JsPath.read[Path].map(p => Job.from(p).toTry.get)

  implicit object jobWrites extends Writes[Job] {
    def writes(j: Job): JsValue = j match { // TODO fix this; how to properly dump families in collections?
      case t: BulkImportJob             => bulkImportJobWrites.writes(t)
      case t: ComposeJob                => composeJobWrites.writes(t)
      case t: CoreStatisticsJob         => coreStatisticsJobWrites.writes(t)
      case t: DesignSpaceExplorationJob => dseJobWrites.writes(t)
      case t: HighLevelSynthesisJob     => highLevelSynthesisJobWrites.writes(t)
      case t: ImportJob                 => importJobWrites.writes(t)
    }
  }
  /* Job @} */
}
// vim: foldmethod=marker foldmarker=@{,@} foldlevel=0