L4_LayerHandler.scala 8.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//=============================================================================
//
//  This file is part of the ExaStencils code generation framework. ExaStencils
//  is free software: you can redistribute it and/or modify it under the terms
//  of the GNU General Public License as published by the Free Software
//  Foundation, either version 3 of the License, or (at your option) any later
//  version.
//
//  ExaStencils 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 General Public License for
//  more details.
//
//  You should have received a copy of the GNU General Public License along
//  with ExaStencils. If not, see <http://www.gnu.org/licenses/>.
//
//=============================================================================

19
20
21
package exastencils.app.l4

import exastencils.app.LayerHandler
22
import exastencils.applications.l4.L4_AddDefaultApplication
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import exastencils.base.ExaRootNode
import exastencils.base.l4._
import exastencils.baseExt.l4._
import exastencils.boundary.ir.L4_ResolveBoundaryHandlingFunctions
import exastencils.communication.l4._
import exastencils.config._
import exastencils.datastructures.StrategyTimer
import exastencils.domain.l4.L4_DomainCollection
import exastencils.field.ir.IR_AddPaddingToFieldLayouts
import exastencils.field.l4._
import exastencils.grid.l4._
import exastencils.interfacing.l4.L4_ExternalFieldCollection
import exastencils.knowledge.l4.L4_KnowledgeContainer._
import exastencils.knowledge.l4._
37
import exastencils.layoutTransformation.l4.L4_AddSoAtoAoSTransformation
38
39
40
41
42
43
44
45
46
47
48
49
50
import exastencils.logger.Logger
import exastencils.operator.l4._
import exastencils.optimization.l4.L4_GeneralSimplify
import exastencils.parsers.l4._
import exastencils.prettyprinting.Indenter
import exastencils.solver.l4._
import exastencils.timing.l4.L4_ResolveTimerFunctions
import exastencils.util.l4._

/// L4_LayerHandler

trait L4_LayerHandler extends LayerHandler

51
/// L4_DummyLayerHandler
Sebastian Kuckuk's avatar
Sebastian Kuckuk committed
52
53
54
55
56
57
58
59

object L4_DummyLayerHandler extends L4_LayerHandler {
  def initialize() : Unit = {}
  def handle() : Unit = {}
  def print() : Unit = {}
  def shutdown() : Unit = {}
}

60
61
62
63
64
65
66
67
68
69
70
71
72
73
/// L4_DefaultLayerHandler

object L4_DefaultLayerHandler extends L4_LayerHandler {
  override def initialize() : Unit = {
    // activate default knowledge collections

    L4_DomainCollection
    L4_FieldLayoutCollection
    L4_FieldCollection
    L4_StencilCollection
    L4_StencilFieldCollection
    L4_VirtualFieldCollection
    L4_ExternalFieldCollection
    L4_EquationCollection
74
    L4_FieldCombinationCollection
75

76

77
    L4_PrepareAccesses.strategies += L4_PrepareMatrixAccesses
78
79
80
81
82
83
  }

  override def shutdown() : Unit = {
    L4_KnowledgeContainer.clear()
  }

84
85
86
87
88
89
90
91
  override def print() : Unit = {
    if (Settings.getDebugL4file.nonEmpty) {
      val outFile = new java.io.FileWriter(Settings.getDebugL4file)
      outFile.write(Indenter.addIndentations(ExaRootNode.l4_root.prettyprint()))
      outFile.close()
    }
  }

92
93
94
  override def handle() : Unit = {
    if (Settings.timeStrategies) StrategyTimer.startTiming("Handling Layer 4")

95
    if (ExaRootNode.l4_root.nodes.nonEmpty) {
96
97
98
99
100
      L4_WrapFieldFieldConvolutions.apply()
      L4_AddLoopsToFieldAssignments.apply()
      L4_AddLoopsToLocalSolve.apply()
      L4_AddCommunicationToLoops.apply()
      L4_AdaptFieldLayoutsForComm.apply()
101
    }
102

Martin Zeus's avatar
Martin Zeus committed
103
    //try {
104
      ExaRootNode.mergeL4(L4_Root(Settings.getL4file.map(L4_Parser.parseFile(_) : L4_Node)))
Martin Zeus's avatar
Martin Zeus committed
105
106
107
    //} catch {
    //  case foo : Exception => Logger.error("first parse")
    //}
108
109
110
111
112
113
114
115
116
117
118
119
120
    ExaRootNode.l4_root.flatten()

    if (true) {
      L4_UnresolveOperatorTimesField.apply()
      L4_UnresolveFieldFieldConvolutions.apply()
      L4_UnresolveStencilAccesses.apply()
      L4_UnresolveStencilFieldAccesses.apply()
      L4_UnresolveFieldAccesses.apply()
      // FIXME: transform back to declarations and re-fold
      L4_ReplaceLevelsInFunctionDecls.apply()
      L4_CombineLeveledFunctionDecls.apply()
      // L4_GenerateLeveledKnowledgeDecls.apply()
    }
121
122
    if (Knowledge.l4_genSoA2AoSTransformation)
      L4_AddSoAtoAoSTransformation.apply()
123

124
125
    print()
    val oldL4Code = ExaRootNode.l4_root.prettyprint()
126

127
128
    // re-parse the code to check for errors - also clear knowledge collections
    L4_KnowledgeContainer.clear()
129

130
    val l4FileName = if (Settings.getDebugL4file.nonEmpty) Settings.getDebugL4file else "debugLayer4"
131
132
133
    try {
      ExaRootNode.l4_root = L4_Parser.parse(oldL4Code, l4FileName)
    } catch {
134
      case foo : Exception => Logger.error("second parse")
135
    }
136
    ExaRootNode.l4_root.flatten()
137

138
    if (ExaRootNode.l4_root.nodes.nonEmpty) {
139
140
      L4_ProcessInlineKnowledge.apply()

141
142
      if (Knowledge.l4_genDefaultApplication)
        L4_AddDefaultApplication.apply()
143

144
      L4_Validation.apply()
145

146
      if (Settings.timeStrategies) StrategyTimer.stopTiming("Handling Layer 4")
147

148
      L4_UnifyGlobalSections.apply()
149

150
      // go to IR
151

152
153
      L4_ResolveColorLoops.apply()

154
155
      // pre-process level specifications in declarations
      L4_ResolveLevelSpecifications.apply()
156

157
158
159
      L4_ResolveFunctionInstantiations.apply()
      L4_UnfoldFunctionDeclarations.apply()
      L4_ProcessFunctionDeclarations.apply()
160

161
      L4_UnfoldKnowledgeDeclarations.apply()
162
      L4_UnfoldLeveledExpressionDeclarations.apply()
163
      L4_UnfoldLeveledVariableDeclarations.apply()
164

165
166
      // resolve current, etc.
      L4_ResolveRelativeLevels.apply()
167

168
169
      L4_ResolveLevelScopes.apply()

170
      L4_PrepareDeclarations.apply()
171

172
      L4_InlineDeclaredExpressions.apply()
173
      L4_PrepareAccesses.apply()
174
      L4_ResolveVariableAccesses.apply()
175

176
      L4_ResolveSpecialConstants.apply()
177
      L4_ResolveFrozenFields.apply()
178
179
180
181
182
      L4_ResolveDslFunctionReferences.apply()
      L4_ResolveEvaluateFunctions.apply()
      L4_ResolveIntegrateFunctions.apply()
      L4_ResolveMathFunctions.apply()
      L4_ResolveTimerFunctions.apply()
183

184
185
186
187
188
      var matches = 0
      do {
        matches = 0
        matches += L4_ProcessDeclarations.applyAndCountMatches()
        matches += L4_ResolveAccesses.applyAndCountMatches()
189

190
191
192
193
        if (Knowledge.experimental_l4_resolveVirtualFields) {
          // integrate before evaluate -> might be nested
          L4_ResolveIntegrateOnGrid.apply()
          matches += (if (L4_ResolveIntegrateOnGrid.results.isEmpty) 0 else L4_ResolveIntegrateOnGrid.results.last._2.matches)
194

195
196
197
198
          L4_ResolveEvaluateOnGrid.apply()
          matches += (if (L4_ResolveEvaluateOnGrid.results.isEmpty) 0 else L4_ResolveEvaluateOnGrid.results.last._2.matches)
        }
      } while (matches > 0)
199

200
201
202
203
204
      if (ExaRootNode.l4_root.nodes.exists(_.isInstanceOf[L4_KnowledgeDecl])) {
        val filtered = ExaRootNode.l4_root.nodes.filter(_.isInstanceOf[L4_KnowledgeDecl])
        Logger.warn(s"L4 root has ${ filtered.length } unprocessed declaration nodes remaining:")
        filtered.foreach(Logger.warn(_))
      }
205

206
207
208
      L4_ResolveVariableAccesses.apply()
      L4_ResolveStencilFunctions.apply()
      L4_ResolveLoopItAccesses.apply()
209
      L4_ResolveNativeFunctions.apply()
210
211
212
      L4_ResolvePrintFunctions.apply()
      L4_ResolveBuildStringFunctions.apply()
      L4_ResolveKnowledgeParameterAccess.apply()
213

214
      L4_GeneralSimplify.doUntilDone()
215

216
      L4_ProcessKnowledgeDeclarations.apply()
217

218
219
      if (Knowledge.l4_genSepLayoutsPerField)
        L4_DuplicateFieldLayoutsForFields.apply()
220

221
      // after L4_ResolveFieldAccesses
Sebastian Kuckuk's avatar
Sebastian Kuckuk committed
222
      L4_ResolveReadFieldFunctions.apply()
223
      L4_ResolvePrintFieldFunctions.apply()
224
      L4_ResolveWriteFieldFunctions.apply()
225

226
227
228
229
      L4_ResolveBoundaryHandlingFunctions.apply()

      L4_ResolveStencilComponentAccesses.apply()
      L4_ResolveStencilFieldComponentAccesses.apply()
230
231
232

      //TODO Zeus: Complex Access
      L4_ValidateComplexAccess.apply()
233
    }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

    if (Settings.timeStrategies) StrategyTimer.startTiming("Progressing from L4 to IR")

    // progress knowledge to IR
    L4_KnowledgeContainer.progress()

    //L4_ProgressKnowledge.apply()

    if (Knowledge.data_alignFieldPointers)
      IR_AddPaddingToFieldLayouts.apply()

    ExaRootNode.progressToIR()

    if (Settings.timeStrategies) StrategyTimer.stopTiming("Progressing from L4 to IR")
  }
}