Skip to content

Commit 5ee5dae

Browse files
committed
Reduce the populate_param_data memory footprint
Use a character buffer to minimize the memory footprint associated with reading the input parameters on root_PE and broadcasting it to all PEs. All answers are bitwise identical.
1 parent eb677eb commit 5ee5dae

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

src/framework/MOM_file_parser.F90

+36-13
Original file line numberDiff line numberDiff line change
@@ -364,25 +364,35 @@ subroutine populate_param_data(iounit, filename, param_data)
364364

365365
! Local variables
366366
character(len=INPUT_STR_LENGTH) :: line
367-
character(len=INPUT_STR_LENGTH), allocatable, dimension(:) :: lines_in
368-
integer :: n, num_lines
367+
character(len=1), allocatable, dimension(:) :: char_buf
368+
integer, allocatable, dimension(:) :: line_len ! The trimmed length of each processed input line
369+
integer :: n, num_lines, total_chars, ch, rsc, llen, int_buf(2)
369370
logical :: inMultiLineComment
370371

372+
character(len=80) :: frag1, frag2
373+
374+
371375
! Find the number of keyword lines in a parameter file
372376
if (all_PEs_read .or. is_root_pe()) then
373377
! rewind the parameter file
374378
rewind(iounit)
375379

376380
! count the number of valid entries in the parameter file
377381
num_lines = 0
382+
total_chars = 0
378383
inMultiLineComment = .false.
379384
do while(.true.)
380385
read(iounit, '(a)', end=8) line
381386
line = replaceTabs(line)
382387
if (inMultiLineComment) then
383388
if (closeMultiLineComment(line)) inMultiLineComment=.false.
384389
else
385-
if (lastNonCommentNonBlank(line)>0) num_lines = num_lines + 1
390+
if (lastNonCommentNonBlank(line)>0) then
391+
line = removeComments(line)
392+
line = simplifyWhiteSpace(line(:len_trim(line)))
393+
num_lines = num_lines + 1
394+
total_chars = total_chars + len_trim(line)
395+
endif
386396
if (openMultiLineComment(line)) inMultiLineComment=.true.
387397
endif
388398
enddo ! while (.true.)
@@ -392,18 +402,22 @@ subroutine populate_param_data(iounit, filename, param_data)
392402
call MOM_error(FATAL, 'MOM_file_parser : A C-style multi-line comment '// &
393403
'(/* ... */) was not closed before the end of '//trim(filename))
394404

395-
param_data%num_lines = num_lines
405+
406+
int_buf(1) = num_lines
407+
int_buf(2) = total_chars
396408
endif ! (is_root_pe())
397409

398410
! Broadcast the number of valid entries in parameter file
399411
if (.not. all_PEs_read) then
400-
call broadcast(param_data%num_lines, root_pe())
412+
call broadcast(int_buf, 2, root_pe())
413+
num_lines = int_buf(1)
414+
total_chars = int_buf(2)
401415
endif
402416

403417
! Set up the space for storing the actual lines.
404-
num_lines = param_data%num_lines
405-
allocate (lines_in(num_lines))
406-
lines_in(:) = ' '
418+
param_data%num_lines = num_lines
419+
allocate (line_len(num_lines), source=0)
420+
allocate (char_buf(total_chars), source=" ")
407421

408422
! Read the actual lines.
409423
if (all_PEs_read .or. is_root_pe()) then
@@ -412,6 +426,7 @@ subroutine populate_param_data(iounit, filename, param_data)
412426

413427
! Populate param_data%fln%line
414428
num_lines = 0
429+
rsc = 0
415430
do while(.true.)
416431
read(iounit, '(a)', end=18) line
417432
line = replaceTabs(line)
@@ -422,7 +437,10 @@ subroutine populate_param_data(iounit, filename, param_data)
422437
line = removeComments(line)
423438
line = simplifyWhiteSpace(line(:len_trim(line)))
424439
num_lines = num_lines + 1
425-
lines_in(num_lines) = line
440+
llen = len_trim(line)
441+
line_len(num_lines) = llen
442+
do ch=1,llen ; char_buf(rsc+ch)(1:1) = line(ch:ch) ; enddo
443+
rsc = rsc + llen
426444
endif
427445
if (openMultiLineComment(line)) inMultiLineComment=.true.
428446
endif
@@ -434,21 +452,26 @@ subroutine populate_param_data(iounit, filename, param_data)
434452
// 'reading of '//trim(filename))
435453
endif ! (is_root_pe())
436454

437-
! Broadcast the populated array lines_in
455+
! Broadcast the populated arrays line_len and char_buf
438456
if (.not. all_PEs_read) then
439-
call broadcast(lines_in, INPUT_STR_LENGTH, root_pe())
457+
call broadcast(line_len, num_lines, root_pe())
458+
call broadcast(char_buf(1:total_chars), 1, root_pe())
440459
endif
441460

442461
! Allocate space to hold contents of the parameter file, including the lines in param_data%fln
443462
allocate(param_data%fln(num_lines))
444463
allocate(param_data%line_used(num_lines))
445464
param_data%line_used(:) = .false.
446465
! Populate param_data%fln%line with the keyword lines from parameter file
466+
rsc = 0
447467
do n=1,num_lines
448-
param_data%fln(n)%line = lines_in(n)
468+
line(1:INPUT_STR_LENGTH) = " "
469+
do ch=1,line_len(n) ; line(ch:ch) = char_buf(rsc+ch)(1:1) ; enddo
470+
param_data%fln(n)%line = trim(line)
471+
rsc = rsc + line_len(n)
449472
enddo
450473

451-
deallocate(lines_in)
474+
deallocate(char_buf) ; deallocate(line_len)
452475

453476
end subroutine populate_param_data
454477

0 commit comments

Comments
 (0)