Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream output files instead of writing them in-memory first #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions PhysicsPackages/eigenPhysicsPackage_class.f90
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ subroutine collectResults(self)
type(outputFile) :: out
character(nameLen) :: name

call out % init(self % outputFormat)
call out % init(self % outputFormat, filename=self % outputFile)

name = 'seed'
call out % printValue(self % pRNG % getSeed(),name)
Expand Down Expand Up @@ -359,8 +359,6 @@ subroutine collectResults(self)
call self % activeTally % print(out)
call out % endBlock()

call out % writeToFile(self % outputFile)

end subroutine collectResults


Expand Down
4 changes: 1 addition & 3 deletions PhysicsPackages/fixedSourcePhysicsPackage_class.f90
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ subroutine collectResults(self)
type(outputFile) :: out
character(nameLen) :: name

call out % init(self % outputFormat)
call out % init(self % outputFormat, filename=self % outputFile)

name = 'seed'
call out % printValue(self % pRNG % getSeed(),name)
Expand All @@ -287,8 +287,6 @@ subroutine collectResults(self)
! Print tally
call self % tally % print(out)

call out % writeToFile(self % outputFile)

end subroutine collectResults


Expand Down
3 changes: 2 additions & 1 deletion UserInterface/fileOutput/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_sources( ./outputFile_class.f90
./asciiOutputFactory_func.f90
./asciiMATLAB_class.f90
./asciiJSON_class.f90
./dummyPrinter_class.f90)
./dummyPrinter_class.f90
./delayedStream_class.f90)

add_unit_tests (./Tests/outputFile_test.f90)
85 changes: 38 additions & 47 deletions UserInterface/fileOutput/asciiJSON_class.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module asciiJSON_class
!! There is indentation for each block and entry, but long multi-dimensional (N-D) arrays
!! are printed on a single line. Also N-D arrays are represented as a JSON array of arrays.
!!
!! JSON output is fairly standard so it can be easlily read into Python and other enviroments
!! JSON output is fairly standard so it can be easily read into Python and other environments
!!
!! TODO:
!! Currently there is a dirty fix to remove commas at the end of a block, which requires to
Expand All @@ -39,7 +39,6 @@ module asciiJSON_class
!!
type, public, extends(asciiOutput) :: asciiJSON
private
type(charTape) :: output
integer(shortInt) :: ind_lvl = 0

integer(shortInt), dimension(:), allocatable :: shapeBuffer
Expand All @@ -49,8 +48,8 @@ module asciiJSON_class

contains
procedure :: init
procedure :: endFile
procedure :: extension
procedure :: writeToFile

procedure :: startBlock
procedure :: endBlock
Expand All @@ -71,51 +70,43 @@ subroutine init(self)
class(asciiJSON), intent(inout) :: self

! Add the initial bracket
call self % output % append( "{" // NEWLINE)
call self % append( "{" // NEWLINE)
self % ind_lvl = 1

end subroutine init

!!
!! Return approperiate extension for the file
!! Finalise the printer
!!
!! See asciiOutput_inter for details
!!
pure function extension(self) result(str)
class(asciiJSON), intent(in) :: self
character(:), allocatable :: str

str = 'json'

end function extension

!!
!! Print the output to the given unit
!!
!! See asciiOutput_inter for details
!!
subroutine writeToFile(self, unit)
subroutine endFile(self)
class(asciiJSON), intent(inout) :: self
integer(shortInt), intent(in) :: unit
character(:),allocatable :: form

! Dirty fix
! Remove comma from the last entry by rewind
! Need to check that the character is a comma to avoid removing { when there is an empty block
! TODO: Find better way. Will clash with any proper stream output
if (self % output % get(self % output % length() - 1) == ",") then
call self % output % cut(2)
call self % output % append(NEWLINE)
if (self % peek(2) == ",") then
call self % cut(2)
call self % append(NEWLINE)
end if
call self % append("}" // NEWLINE)

! Close the file
call self % output % append("}")
end subroutine endFile

form = '(A' // numToChar(self % output % length()) // ')'
!!
!! Return appropriate extension for the file
!!
!! See asciiOutput_inter for details
!!
pure function extension(self) result(str)
class(asciiJSON), intent(in) :: self
character(:), allocatable :: str

write(unit,form) self % output % expose()
str = 'json'

end subroutine writeToFile
end function extension

!!
!! Change state to writing new block with "name"
Expand All @@ -127,10 +118,10 @@ subroutine startBlock(self, name)
character(nameLen), intent(in) :: name

! Write indentation
call self % output % append(repeat(BLANK, self % ind_lvl * IND_SIZE))
call self % append(repeat(BLANK, self % ind_lvl * IND_SIZE))

! Open new block and increase indentation
call self % output % append(QUOTE // trim(name) // QUOTE // ":" // "{" // NEWLINE)
call self % append(QUOTE // trim(name) // QUOTE // ":" // "{" // NEWLINE)
self % ind_lvl = self % ind_lvl + 1

end subroutine startBlock
Expand All @@ -147,17 +138,17 @@ subroutine endBlock(self)
! Remove comma from the last entry by rewind
! Need to check that the character is a comma to avoid removing { when there is an empty block
! TODO: Find better way. Will clash with any proper stream output
if (self % output % get(self % output % length() - 1) == ",") then
call self % output % cut(2)
call self % output % append(NEWLINE)
if (self % peek(2) == ",") then
call self % cut(2)
call self % append(NEWLINE)
end if

! Decrease and write indentation
self % ind_lvl = self % ind_lvl - 1
call self % output % append(repeat(BLANK, self % ind_lvl * IND_SIZE))
call self % append(repeat(BLANK, self % ind_lvl * IND_SIZE))

! Close the block
call self % output % append("}" // ","// NEWLINE)
call self % append("}" // ","// NEWLINE)

end subroutine endBlock

Expand All @@ -171,10 +162,10 @@ subroutine startEntry(self, name)
character(*), intent(in) :: name

! Print indentation
call self % output % append(repeat(BLANK, self % ind_lvl * IND_SIZE))
call self % append(repeat(BLANK, self % ind_lvl * IND_SIZE))

! Write the name
call self % output % append(QUOTE // trim(name) // QUOTE // ":")
call self % append(QUOTE // trim(name) // QUOTE // ":")

end subroutine startEntry

Expand All @@ -188,7 +179,7 @@ subroutine endEntry(self)

! End with NEWLINE
! Comma will be written together with a number/char
call self % output % append(NEWLINE)
call self % append(NEWLINE)

end subroutine endEntry

Expand Down Expand Up @@ -235,12 +226,12 @@ subroutine printNum(self, val)
mul = 1
do i = 1, size(self % shapeBuffer)
mul = mul * self % shapeBuffer(i)
if (modulo(self % count, mul) == 0) call self % output % append("[")
if (modulo(self % count, mul) == 0) call self % append("[")
end do
end if

! Print the number
call self % output % append(val)
call self % append(val)

if (self % in_array) then
! Append the count
Expand All @@ -250,12 +241,12 @@ subroutine printNum(self, val)
mul = 1
do i = 1, size(self % shapeBuffer)
mul = mul * self % shapeBuffer(i)
if (modulo(self % count, mul) == 0) call self % output % append("]")
if (modulo(self % count, mul) == 0) call self % append("]")
end do
end if

! Finish entry with a comma
call self % output % append(",")
call self % append(",")

end subroutine printNum

Expand All @@ -274,12 +265,12 @@ subroutine printChar(self, val)
mul = 1
do i = 1, size(self % shapeBuffer)
mul = mul * self % shapeBuffer(i)
if (modulo(self % count, mul) == 0) call self % output % append("[")
if (modulo(self % count, mul) == 0) call self % append("[")
end do
end if

! Print the character
call self % output % append(QUOTE // val // QUOTE)
call self % append(QUOTE // val // QUOTE)

if (self % in_array) then
! Append the count
Expand All @@ -289,12 +280,12 @@ subroutine printChar(self, val)
mul = 1
do i = 1, size(self % shapeBuffer)
mul = mul * self % shapeBuffer(i)
if (modulo(self % count, mul) == 0) call self % output % append("]")
if (modulo(self % count, mul) == 0) call self % append("]")
end do
end if

! Finish entry with a comma
call self % output % append(",")
call self % append(",")

end subroutine printChar

Expand Down
Loading
Loading