10
0
mirror of https://gitlab.com/scemama/irpf90.git synced 2024-11-19 04:22:23 +01:00
irpf90/src/irp_stack.py

203 lines
5.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
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
import util
from command_line import command_line
do_assert = command_line.do_assert
do_debug = command_line.do_debug
2009-12-09 09:42:36 +01:00
do_openmp = command_line.do_openmp
2009-09-02 20:45:53 +02:00
import irpf90_t
FILENAME = irpf90_t.irpdir+"irp_stack.irp.F90"
def create():
txt = """
module irp_stack_mod
integer, parameter :: STACKMAX=1000
character*(128),allocatable :: irp_stack(:,:)
double precision,allocatable :: irp_cpu(:,:)
integer,allocatable :: stack_index(:)
logical :: alloc = .False.
2014-05-23 22:03:23 +02:00
integer :: nthread
2009-09-02 20:45:53 +02:00
character*(128) :: white = ''
end module
subroutine irp_enter(irp_where)
use irp_stack_mod
integer :: ithread
character*(*) :: irp_where
2014-05-23 22:03:23 +02:00
!$ integer, external :: omp_get_thread_num
!$ integer, external :: omp_get_num_threads
ithread = 0
!$ ithread = omp_get_thread_num()
$1
if (ithread /= 0) then
print *, 'Error: Provider is called by thread', ithread
call irp_trace
stop 1
endif
"""
if command_line.do_memory:
txt+="""
if (.not.alloc) then
nthread = 1
!$OMP PARALLEL
!$OMP SINGLE
!$ nthread = omp_get_num_threads()
!$OMP END SINGLE
!$OMP END PARALLEL
print *, 'Allocating irp_stack(',STACKMAX,',',nthread,')'
print *, 'Allocating irp_cpu(',STACKMAX,',',nthread,')'
print *, 'Allocating stack_index(',nthread,')'
endif"""
txt +="""
$2
end subroutine
subroutine irp_enter_f(irp_where)
use irp_stack_mod
integer :: ithread
character*(*) :: irp_where
!$ integer, external :: omp_get_thread_num
!$ integer, external :: omp_get_num_threads
ithread = 0
!$ ithread = omp_get_thread_num()
2009-09-02 20:45:53 +02:00
$1
2009-11-02 16:16:13 +01:00
"""
if command_line.do_memory:
txt+="""
if (.not.alloc) then
2014-05-23 22:03:23 +02:00
!$OMP PARALLEL
!$OMP SINGLE
!$ nthread = omp_get_num_threads()
2009-11-02 16:16:13 +01:00
print *, 'Allocating irp_stack(',STACKMAX,',',nthread,')'
print *, 'Allocating irp_cpu(',STACKMAX,',',nthread,')'
print *, 'Allocating stack_index(',nthread,')'
2014-05-23 22:03:23 +02:00
!$OMP END SINGLE
!$OMP END PARALLEL
2009-11-02 16:16:13 +01:00
endif"""
txt +="""
2009-09-02 20:45:53 +02:00
$2
end subroutine
subroutine irp_leave (irp_where)
use irp_stack_mod
character*(*) :: irp_where
integer :: ithread
double precision :: cpu
2014-05-23 22:03:23 +02:00
!$ integer, external :: omp_get_thread_num
ithread = 0
!$ ithread = omp_get_thread_num()
2009-09-02 20:45:53 +02:00
$3
$4
end subroutine
"""
# $1
if do_assert or do_debug:
txt = txt.replace("$1","""
2014-05-23 22:03:23 +02:00
if (.not.alloc) then
!$OMP PARALLEL
!$OMP SINGLE
!$ nthread = omp_get_num_threads()
!$OMP END SINGLE
!$OMP END PARALLEL
!$OMP CRITICAL
2009-09-02 20:45:53 +02:00
if (.not.alloc) then
2014-10-07 11:41:48 +02:00
allocate(irp_stack(0:STACKMAX,nthread+1))
allocate(irp_cpu(0:STACKMAX,nthread+1))
allocate(stack_index(nthread+1))
stack_index = 0
2009-09-02 20:45:53 +02:00
alloc = .True.
endif
2014-05-23 22:03:23 +02:00
!$OMP END CRITICAL
endif
2014-10-07 11:41:48 +02:00
stack_index(ithread+1) = mod(stack_index(ithread+1)+1,STACKMAX)
2009-09-02 20:45:53 +02:00
irp_stack(stack_index(ithread+1),ithread+1) = irp_where""")
2009-12-09 09:42:36 +01:00
if command_line.do_memory:
txt+="""
print *, 'Allocating irp_stack(',STACKMAX,','nthread,')'
print *, 'Allocating irp_cpu(',STACKMAX,','nthread,')'
print *, 'Allocating stack_index(',nthread,')'"""
2009-09-02 20:45:53 +02:00
else:
txt = txt.replace("$1","")
# $2
if do_debug:
txt = txt.replace("$2","""
print *, ithread, ':', white(1:stack_index(ithread+1))//'-> ', trim(irp_where)
call cpu_time(irp_cpu(stack_index(ithread+1),ithread+1))""")
else:
txt = txt.replace("$2","")
# $3
if do_debug:
txt = txt.replace("$3","""
call cpu_time(cpu)
print *, ithread, ':', white(1:stack_index(ithread+1))//'<- ', &
trim(irp_stack(stack_index(ithread+1),ithread+1)), &
cpu-irp_cpu(stack_index(ithread+1),ithread+1)""")
else:
txt = txt.replace("$3","")
# $4
if do_debug or do_assert:
txt = txt.replace("$4","""
stack_index(ithread+1) = stack_index(ithread+1)-1""")
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 :: ithread
integer :: i
2014-05-23 22:03:23 +02:00
!$ integer, external :: omp_get_thread_num
2009-09-02 20:45:53 +02:00
ithread = 0
2014-05-23 22:03:23 +02:00
!$ ithread = omp_get_thread_num()
2009-09-02 20:45:53 +02:00
if (.not.alloc) return
print *, 'Stack trace: ', ithread
print *, '-------------------------'
do i=1,stack_index(ithread+1)
print *, trim(irp_stack(i,ithread+1))
enddo
print *, '-------------------------'
end subroutine
"""
2009-09-09 00:55:13 +02:00
txt = txt.split('\n')
txt = map(lambda x: x+"\n",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()