1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 137 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
#!/usr/bin/env python3
# encoding: utf-8
"""
_UserCodeBlock.py
Created by Graham Dennis on 2008-09-11.
Copyright (c) 2008-2012, Graham Dennis
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
from xpdeint.ScriptElement import ScriptElement
from xpdeint.Utilities import lazy_property
from xpdeint import CodeParser
from xpdeint.CodeParser import CodeParserException
from xpdeint.CallOnceGuards import callOncePerInstanceGuard
from xpdeint.Vectors.ComputedVector import ComputedVector
import textwrap
class _UserCodeBlock(ScriptElement):
def __init__(self, **KWs):
localKWs = self.extractLocalKWs(['codeString'], KWs)
ScriptElement.__init__(self, **KWs)
if 'codeString' in localKWs: self.codeString = localKWs.get('codeString')
@lazy_property
def codeString(self):
return self.xmlElement.cdataContents()
def transformCodeString(self):
CodeParser.checkForIntegerDivision(self)
if self.codeString.count('\n'):
# Deindent code and add '#line' compiler directives
self.addCompilerLineDirectives()
def addCompilerLineDirectives(self):
"""
Add the #line compiler directives at the start and end so that
any compiler errors are reported in terms of lines in the actual xmds
script itself.
"""
result = [textwrap.dedent(self.codeString)]
writeLineDirectives = not self.getVar('debug') and self.xmlElement
if writeLineDirectives:
result.insert(
0,
'#line %i "%s"\n' % (self.scriptLineNumber, self.getVar('scriptName'))
)
result.append('\n#line _XPDEINT_CORRECT_MISSING_LINE_NUMBER_\n')
self.codeString = ''.join(result)
def preflight(self):
super(_UserCodeBlock, self).preflight()
self.transformCodeString()
class _UserLoopCodeBlock(_UserCodeBlock):
def __init__(self, **KWs):
localKWs = self.extractLocalKWs(['field', 'basis', 'targetVector', 'loopArguments'], KWs)
_UserCodeBlock.__init__(self, **KWs)
self.field = localKWs.get('field')
self.basis = localKWs.get('basis')
self.targetVector = localKWs.get('targetVector', None)
self.loopArguments = localKWs.get('loopArguments', {})
self.bindNamedVectorsCalled = False
self.preflightCalled = False
self.prefixCodeString = ''
self.postfixCodeString = ''
@property
def loopCodeString(self):
return self.prefixCodeString + self.codeString + self.postfixCodeString
def loop(self, codeWrapperFunction = None, **KWs):
loopCode = self.loopCodeString
if codeWrapperFunction: loopCode = codeWrapperFunction(loopCode)
loopKWs = self.loopArguments.copy()
loopKWs.update(KWs)
result = self.transformVectorsToBasis(self.dependencies, self.basis)
if result: result = "// Transforming vectors to basis (%s)\n%s\n" % (', '.join(self.basis), result)
loopingVectors = set(self.dependencies)
if self.targetVector: loopingVectors.add(self.targetVector)
result += self.loopOverFieldInBasisWithVectorsAndInnerContent(self.field, self.basis, loopingVectors, loopCode, **loopKWs)
return result
@lazy_property
def specialTargetsVector(self):
# If all dependencies are of type real, then the special targets vector must be real too
specialTargetsVectorType = 'complex'
if all([v.type == 'real' for v in self.dependencies]):
if not self.targetVector or self.targetVector.type == 'real':
specialTargetsVectorType = 'real'
specialTargetsVector = ComputedVector(name = self.parent.id + "_special_targets", field = self.field,
parent = self, initialBasis = self.basis,
type = specialTargetsVectorType, xmlElement = self.xmlElement,
**self.argumentsToTemplateConstructors)
self.field.temporaryVectors.add(specialTargetsVector)
specialTargetsVector.integratingComponents = False
evaluationCodeBlock = _TargetConstructorCodeBlock(
field = self.field, basis = self.basis,
parent = specialTargetsVector,
**self.argumentsToTemplateConstructors
)
evaluationCodeBlock.targetVector = specialTargetsVector
specialTargetsVector.codeBlocks['evaluation'] = evaluationCodeBlock
# Call necessary preflight functions on the special targets vector
if self.bindNamedVectorsCalled: specialTargetsVector.bindNamedVectors()
if self.preflightCalled: specialTargetsVector.preflight()
self._children.append(specialTargetsVector)
return specialTargetsVector
def addCodeStringToSpecialTargetsVector(self, codeString, containingCodeSlice):
specialTargetsVector = self.specialTargetsVector
specialTargetCodeBlock = specialTargetsVector.codeBlocks['evaluation']
targetCodeBlocks = specialTargetCodeBlock.targetCodeBlocks
codeBlocksWithSameTarget = [codeBlock for codeBlock in targetCodeBlocks if codeBlock.codeString == codeString]
if codeBlocksWithSameTarget:
evaluationCodeBlock = codeBlocksWithSameTarget[0]
targetVariableName = 'target%i' % targetCodeBlocks.index(evaluationCodeBlock)
else:
evaluationCodeBlock = _UserLoopCodeBlock(
field = self.field, basis = self.basis,
parent = specialTargetCodeBlock, xmlElement = self.xmlElement,
**self.argumentsToTemplateConstructors
)
evaluationCodeBlock.codeString = codeString
# This code block could depend on anything we do
evaluationCodeBlock.dependencies.update(self.dependencies)
evaluationCodeBlock.scriptLineNumber = self.scriptLineNumber + self.codeString.count('\n', 0, containingCodeSlice.start)
targetCodeBlocks.append(evaluationCodeBlock)
targetVariableName = 'target%i' % targetCodeBlocks.index(evaluationCodeBlock)
specialTargetsVector.components.append(targetVariableName)
# Call necessary preflight functions on the code block
if self.bindNamedVectorsCalled: evaluationCodeBlock.bindNamedVectors()
if self.preflightCalled: evaluationCodeBlock.preflight()
return targetVariableName
def fixupNonlocallyAccessedComponents(self):
"""
In user code, the user may refer to parts of a vector nonlocally in integer-valued dimensions.
This code translates variables accessed with the ``phi(j: j-3, k:k+5, l:l/2, p:p*p, q:q, r:r)`` notation to a form
that can be used in the C++ source file. The form currently used is ``_phi_jklpqr(j-3, k+5, l/2, p*p, q, r)``.
This function makes an optimisation where if all dimensions are accessed locally,
the ``phi(j: j, k:k, l:l, p:p, q: q, r: r)`` notation is replaced with the string ``phi`` which is a faster
way of accessing the local value than through using the ``_phi_jklpqr(...)`` macro.
"""
vectorsToFix = self.dependencies.copy()
if self.targetVector: vectorsToFix.add(self.targetVector)
nonlocalVariablesCreated = set()
vectorOverrides = self.loopArguments.get('vectorOverrides', [])
simulationDriver = self.getVar('features')['Driver']
for componentName, vector, nonlocalAccessDict, codeSlice in reversed(CodeParser.nonlocalDimensionAccessForVectors(vectorsToFix, self)):
availableDimReps = vector.field.inBasis(self.basis)
validDimensionNames = [dimRep.name for dimRep in availableDimReps]
validDimensionNames.extend([dimRep.name + "_index" for dimRep in availableDimReps])
# If the dict is empty, then it probably means something else
if not nonlocalAccessDict:
continue
if vector in vectorOverrides:
vectorID = vector.id
raise CodeParserException(self, codeSlice.start, "Cannot access vector '%(vectorID)s' non-locally." % locals())
# Check that there are no dimensions listed in the nonlocalAccessDict that don't refer to valid
# dimensions for this vector
for dimName in nonlocalAccessDict.keys():
if not dimName in validDimensionNames:
raise CodeParserException(self, nonlocalAccessDict[dimName][1], "Component '%s' doesn't have dimension '%s'." % (componentName, dimName))
dimRepsNeeded = [dimRep for dimRep in availableDimReps if dimRep.name in nonlocalAccessDict and nonlocalAccessDict[dimRep.name][0] != dimRep.name]
dimRepsNeeded.extend([dimRep for dimRep in availableDimReps if dimRep.name + "_index" in nonlocalAccessDict])
if not dimRepsNeeded:
replacementString = componentName
else:
# Check that the mpi distributed dimension isn't being accessed nonlocally.
if vector.field.isDistributed:
for dimRep in dimRepsNeeded:
if dimRep.hasLocalOffset:
dimRepName = dimRep.name
raise CodeParserException(self, nonlocalAccessDict[dimRepName][1],
"It is illegal to access the dimension '%(dimRepName)s' nonlocally because it is being distributed with MPI.\n"
"Try not using MPI or changing the order of your dimensions." % locals())
nonlocalAccessVariableName = '_%s_' % componentName
nonlocalAccessVariableName += ''.join([dimRep.name for dimRep in dimRepsNeeded])
if not nonlocalAccessVariableName in nonlocalVariablesCreated:
# Populate with whatever we have set for us if it's there.
indexOverrides = self.loopArguments.get('indexOverrides', {}).copy()
for dimRep in dimRepsNeeded:
indexOverrides[dimRep.name] = {vector.field: dimRep.loopIndex}
argumentsString = ', '.join([dimRep.loopIndex for dimRep in dimRepsNeeded])
vectorID = vector.id
componentNumber = vector.components.index(componentName)
defineString = "#define %(nonlocalAccessVariableName)s(%(argumentsString)s) " % locals()
nonlocalAccessString = "_active_%(vectorID)s[%(componentNumber)s + (0" % locals()
for dimRep in vector.field.inBasis(self.basis):
termString = self.explicitIndexPointerTermForVectorAndDimRepWithFieldAndBasis(vector, dimRep, self.field, self.basis, indexOverrides)
nonlocalAccessString += termString.replace('\n', ' \\\n')
nonlocalAccessString += ') * _%(vectorID)s_ncomponents]' % locals()
defineString += nonlocalAccessString + '\n'
undefineString = "#undef %(nonlocalAccessVariableName)s\n" % locals()
featureDict = {
'vector': vector,
'componentName': componentName,
'availableDimReps': availableDimReps,
'dimRepsNeeded': dimRepsNeeded,
'nonlocalAccessVariableName': nonlocalAccessVariableName,
'nonlocalAccessString': nonlocalAccessString,
'defineString': defineString,
'undefineString': undefineString
}
featureOrdering = ['Diagnostics']
self.insertCodeForFeatures('nonlocalAccess', featureOrdering, featureDict)
self.prefixCodeString += featureDict['defineString']
self.postfixCodeString += featureDict['undefineString']
nonlocalVariablesCreated.add(nonlocalAccessVariableName)
arguments = []
for dimRep in dimRepsNeeded:
accessViaIndex = not dimRep.name in nonlocalAccessDict
dimRepVariableName = dimRep.name if not accessViaIndex else dimRep.name + "_index"
accessString = nonlocalAccessDict[dimRepVariableName][0]
dimRepName = dimRep.name
if not accessViaIndex:
argumentValue = dimRep.nonlocalAccessIndexFromStringForFieldInBasis(accessString, self.field, self.basis)
if not argumentValue:
raise CodeParserException(self, nonlocalAccessDict[dimRep.name][1],
"Cannot access the '%(dimRepName)s' dimension nonlocally with the string '%(accessString)s'. Check the documentation." % locals())
else:
argumentValue = accessString
arguments.append('/* %(dimRepVariableName)s => %(accessString)s */ (%(argumentValue)s)' % locals())
argumentsString = ', '.join(arguments)
replacementString = '%(nonlocalAccessVariableName)s(%(argumentsString)s)' % locals()
# Replace the phi(j => j + 7) string with the appropriate string
# i.e. _phi_j(j + 7)
self.codeString = self.codeString[:codeSlice.start] + replacementString + self.codeString[codeSlice.stop:]
def transformCodeString(self):
"""Modify the user code as necessary."""
super(_UserLoopCodeBlock, self).transformCodeString()
self.fixupNonlocallyAccessedComponents()
@callOncePerInstanceGuard
def bindNamedVectors(self):
# This function would be called twice otherwise because it gets called specially
# by the _ScriptElement implementation of bindNamedVectors
super(_UserLoopCodeBlock, self).bindNamedVectors()
self.bindNamedVectorsCalled = True
@callOncePerInstanceGuard
def preflight(self):
super(_UserLoopCodeBlock, self).preflight()
vectors = set(self.dependencies)
if self.targetVector:
vectors.add(self.targetVector)
basis = self.basis
self.registerVectorsRequiredInBasis(vectors, basis)
self.preflightCalled = True
class _TargetConstructorCodeBlock(_UserLoopCodeBlock):
def __init__(self, **KWs):
_UserLoopCodeBlock.__init__(self, **KWs)
self.targetCodeBlocks = []
scriptLineOffset = 0
@property
def children(self):
children = super(_TargetConstructorCodeBlock, self).children
children.extend(self.targetCodeBlocks)
return children
@property
def dependencies(self):
dependencies = set()
# Add all the dependencies of all target code blocks together
for targetCodeBlock in self.targetCodeBlocks:
dependencies.update(targetCodeBlock.dependencies)
return frozenset(dependencies)
@property
def loopCodeString(self):
prefixString = ''.join([cb.prefixCodeString for cb in self.targetCodeBlocks])
writeLineDirectives = not self.getVar('debug')
loopStringTemplate = ''
if writeLineDirectives: loopStringTemplate += '#line %%(lineNumber)i "%s"\n' % self.getVar('scriptName')
loopStringTemplate += 'target%(targetNum)i = %(codeString)s;\n'
loopString = ''.join([loopStringTemplate % dict(lineNumber=cb.scriptLineNumber, targetNum=targetNum, codeString=cb.codeString)\
for targetNum, cb in enumerate(self.targetCodeBlocks)])
if writeLineDirectives:
loopString += '#line _XPDEINT_CORRECT_MISSING_LINE_NUMBER_\n'
postfixString = ''.join([cb.postfixCodeString for cb in self.targetCodeBlocks])
return prefixString + loopString + postfixString
def transformCodeString(self):
# This will be done as needed for all child code blocks
pass
|