irpf90/src/irp_stack.py

159 lines
3.7 KiB
Python
Raw Normal View History

2020-01-27 18:22:35 +01:00
#!/usr/bin/env python3
2009-09-23 12:51:27 +02:00
# IRPF90 is a Fortran90 preprocessor written in Python for programming using
# the Implicit Reference to Parameters (IRP) method.
# Copyright (C) 2009 Anthony SCEMAMA
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Anthony Scemama
# LCPQ - IRSAMC - CNRS
# Universite Paul Sabatier
# 118, route de Narbonne
# 31062 Toulouse Cedex 4
# scemama@irsamc.ups-tlse.fr
2009-09-23 12:51:27 +02:00
2009-09-02 20:45:53 +02:00
2020-01-27 18:43:22 +01:00
import util
from command_line import command_line
2009-09-02 20:45:53 +02:00
do_debug = command_line.do_debug
2009-12-09 09:42:36 +01:00
do_openmp = command_line.do_openmp
2015-04-07 17:25:58 +02:00
do_memory = command_line.do_memory
2009-09-02 20:45:53 +02:00
2020-01-27 18:43:22 +01:00
import irpf90_t
2009-09-02 20:45:53 +02:00
FILENAME = irpf90_t.irpdir+"irp_stack.irp.F90"
def create():
txt = """
module irp_stack_mod
integer, parameter :: STACKMAX=1000
2019-02-06 11:19:26 +01:00
character*(128),allocatable :: irp_stack(:)
double precision,allocatable :: irp_cpu(:)
integer :: stack_index
2009-09-02 20:45:53 +02:00
logical :: alloc = .False.
character*(128) :: white = ''
end module
subroutine irp_enter(irp_where)
use irp_stack_mod
character*(*) :: irp_where
2014-10-23 10:18:03 +02:00
"""
2015-04-07 17:25:58 +02:00
txt += "$1"
if do_memory:
txt+="""
2014-05-23 22:03:23 +02:00
if (.not.alloc) then
2019-02-06 11:19:26 +01:00
print *, 'Allocating irp_stack(',STACKMAX+1,')'
print *, 'Allocating irp_cpu(',STACKMAX+1,')'
2014-05-23 22:03:23 +02:00
endif"""
txt +="""
$2
end subroutine
subroutine irp_enter_f(irp_where)
use irp_stack_mod
character*(*) :: irp_where
2009-09-02 20:45:53 +02:00
$1
2009-11-02 16:16:13 +01:00
"""
2015-04-07 17:25:58 +02:00
if do_memory:
txt+="""
2009-11-02 16:16:13 +01:00
if (.not.alloc) then
2019-02-06 11:19:26 +01:00
print *, 'Allocating irp_stack(',STACKMAX+1,')'
print *, 'Allocating irp_cpu(',STACKMAX+1,')'
2015-04-07 17:25:58 +02:00
endif
2015-04-12 00:08:03 +02:00
"""
txt += """
2009-09-02 20:45:53 +02:00
$2
end subroutine
subroutine irp_leave (irp_where)
use irp_stack_mod
character*(*) :: irp_where
double precision :: cpu
$3
$4
end subroutine
"""
# $1
2017-09-18 15:19:59 +02:00
if do_debug:
2015-04-07 17:25:58 +02:00
s = """
2014-05-23 22:03:23 +02:00
if (.not.alloc) then
2019-02-06 11:19:26 +01:00
allocate(irp_stack(0:STACKMAX))
allocate(irp_cpu(0:STACKMAX))
2015-04-07 17:25:58 +02:00
stack_index = 0
alloc = .True.
endif
2019-02-06 11:19:26 +01:00
stack_index = min(stack_index+1,STACKMAX)
irp_stack(stack_index) = irp_where"""
2015-04-07 17:25:58 +02:00
if do_memory:
txt+="""
2019-02-06 11:19:26 +01:00
print *, 'Allocating irp_stack(',STACKMAX+1,')'
print *, 'Allocating irp_cpu(',STACKMAX+1,')' """
2009-09-02 20:45:53 +02:00
else:
2015-04-07 17:25:58 +02:00
s = ""
txt = txt.replace("$1",s)
2009-09-02 20:45:53 +02:00
# $2
if do_debug:
txt = txt.replace("$2","""
2019-02-06 11:19:26 +01:00
print *, white(1:stack_index)//'-> ', trim(irp_where)
call cpu_time(irp_cpu(stack_index))""")
2009-09-02 20:45:53 +02:00
else:
txt = txt.replace("$2","")
# $3
if do_debug:
txt = txt.replace("$3","""
call cpu_time(cpu)
2019-02-06 11:19:26 +01:00
print *, white(1:stack_index)//'<- ', &
trim(irp_stack(stack_index)), &
cpu-irp_cpu(stack_index)""")
2009-09-02 20:45:53 +02:00
else:
txt = txt.replace("$3","")
# $4
2017-09-18 15:19:59 +02:00
if do_debug:
2009-09-02 20:45:53 +02:00
txt = txt.replace("$4","""
2019-02-06 11:19:26 +01:00
stack_index = max(0,stack_index-1)""")
2009-09-02 20:45:53 +02:00
else:
txt = txt.replace("$4","")
2014-05-23 22:03:23 +02:00
txt += """
2009-09-02 20:45:53 +02:00
subroutine irp_trace
use irp_stack_mod
integer :: i
if (.not.alloc) return
2019-02-06 11:19:26 +01:00
print *, 'Stack trace: '
2009-09-02 20:45:53 +02:00
print *, '-------------------------'
2019-02-06 11:19:26 +01:00
do i=1,stack_index
print *, trim(irp_stack(i))
2009-09-02 20:45:53 +02:00
enddo
print *, '-------------------------'
end subroutine
"""
2009-09-09 00:55:13 +02:00
txt = txt.split('\n')
2020-01-27 18:22:35 +01:00
txt = [x+"\n" for x in txt]
2009-09-02 20:45:53 +02:00
if not util.same_file(FILENAME, txt):
file = open(FILENAME,'w')
2009-09-03 23:12:32 +02:00
file.writelines(txt)
2009-09-02 20:45:53 +02:00
file.close()