@*
ChunkedOutput.tmpl

Created by Graham Dennis on 2010-09-17.

Copyright (c) 2010-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/>.

*@
@extends xpdeint.Features._ChunkedOutput

@def description: Chunked Output
@attr featureName = 'ChunkedOutput'

@def defines
  @#
  @super
  @#

#define _CHUNK_SIZE ((long)$chunkSize)
  @#
@end def

@def preAllocation($dict)
  @#
  @for mg in $momentGroups
    @set propDimRep = mg.propDimRep
    @if not propDimRep
      @continue
    @end if
${propDimRep.localLattice} = ${propDimRep.globalLattice};
if (${mg.processedVector.sizeInBasis(mg.codeBlocks['sampling'].basis)})
  ${propDimRep.localLattice} = MIN((_CHUNK_SIZE-1) / (${mg.processedVector.sizeInBasis(mg.codeBlocks['sampling'].basis)} / ${propDimRep.globalLattice} * sizeof(${mg.processedVector.type})) + 1, ${propDimRep.globalLattice});
${insertCodeForFeatures('findMax', ['Driver'], {'variable': '&' + propDimRep.localLattice, 'count': 1, 'op': 'min', 'type': 'long'}), autoIndent=True}@slurp
// Now find the minimum and broadcast it.
  @end for
  @# Initialise output files
  @set outputFeature = $features['Output']
${outputFeature.outputFormat.writeOutSetup(c'("${outputFeature.filename}" + gsArgsAndValues).c_str()', outputFeature)}@slurp
${outputFeature.outputFormat.writeOutTearDown}@slurp
  @#
@end def

@def sampleFunctionEnd($dict)
  @#
  @set mg = dict['caller']
  @set propDimRep = mg.propDimRep
  @#
  @if not propDimRep
    @return
  @end if
if (${propDimRep.index} == ${propDimRep.localLattice}
    && (${propDimRep.index} + ${propDimRep.localOffset}) != ${propDimRep.globalLattice})
{
  ${mg.functions['writeOut'].call(_outfile='NULL'), autoIndent=True}
  _LOG(_SIMULATION_LOG_LEVEL, "Written a chunk of moment group ${mg.number+1}\n");
  ${propDimRep.localOffset} += ${propDimRep.index};
  ${propDimRep.index} = 0;
  @if mg.integratingComponents
  ${mg.rawVector.initialise, autoIndent=True}@slurp
  @end if
}
@end def

@def writeOutBegin($dict)
  @#
  @for mg in $momentGroups
    @set propDimRep = mg.propDimRep
    @if not propDimRep
      @continue
    @end if
${propDimRep.localLattice} = ${propDimRep.index};
  @end for
  @#
@end def
