Every thing are templated

This commit is contained in:
Thomas Applencourt 2017-02-16 19:14:17 -06:00
parent 6bc15db94a
commit 8a31b6302c
12 changed files with 172 additions and 2870 deletions

File diff suppressed because it is too large Load Diff

View File

@ -24,11 +24,7 @@
# 31062 Toulouse Cedex 4
# scemama@irsamc.ups-tlse.fr
try:
import irpy
except:
import lib_irpy as irpy
from lib.manager import irpy
import getopt, sys
from version import version

View File

@ -28,11 +28,7 @@ from irpf90_t import *
from util import *
from command_line import command_line
import sys
try:
import irpy
except:
import lib_irpy as irpy
from lib.manager import irpy
class Entity(object):
'''All lines between BEGIN_PROVIDER and END_PROVIDER included
@ -164,92 +160,35 @@ class Entity(object):
return any(self.d_entity[i].is_written for i in self.parents)
@irpy.lazy_property
def writer(self):
def io_er(self):
if not self.is_main:
result = []
else:
from util import mangled
name = self.name
result = [ \
"subroutine writer_%s(irp_num)"%(name),
" use %s"%(self.fmodule),
" implicit none",
" character*(*), intent(in) :: irp_num",
" logical :: irp_is_open",
" integer :: irp_iunit" ]
if command_line.do_debug:
length = len("writer_%s" % (self.name))
result += [\
" character*(%d) :: irp_here = 'writer_%s'"%(length,name),
" call irp_enter(irp_here)" ]
result += [ \
" if (.not.%s_is_built) then"%(self.same_as),
" call provide_%s"%(self.same_as),
" endif" ]
result += map(lambda x: " call writer_%s(irp_num)" % (x), mangles(self.needs))
result += [ \
" irp_is_open = .True.",
" irp_iunit = 9",
" do while (irp_is_open)",
" irp_iunit = irp_iunit+1",
" inquire(unit=irp_iunit,opened=irp_is_open)",
" enddo" ]
for n in self.l_name:
result += [\
" open(unit=irp_iunit,file='irpf90_%s_'//trim(irp_num),form='FORMATTED',status='UNKNOWN',action='WRITE')"%(n),
" write(irp_iunit,*) %s%s"%(n,build_dim(self.d_entity[n].dim,colons=True)),
" close(irp_iunit)" ]
if command_line.do_debug:
result.append(" call irp_leave(irp_here)")
result.append("end subroutine writer_%s" % (name))
result.append("")
return result
from util import mangled
from util import ashes_env
name = self.name
d_template= {'name':name,
'fmodule':self.fmodule,
'same_as' : self.same_as,
'do_debug':command_line.do_debug,
'children':mangled(self.needs,self.d_entity),
'group_entity': [{'name':n,'dim':build_dim(self.cm_d_variable[n].dim,colons=True)} for n in self.l_name]}
return ashes_env('io.f90',d_template).split('\n')
def reader(self):
return io.er.split('TOKEN_SPLIT')[0]
def writer(self):
return io.er.split('TOKEN_SPLIT')[1]
@irpy.lazy_property_mutable
def is_read(self):
'''Check if it will be read from disk'''
return any(self.d_entity[i].is_read for i in self.parents)
@irpy.lazy_property
def reader(self):
if not self.is_main:
result = []
else:
from util import mangled
name = self.name
result = [ \
"subroutine reader_%s(irp_num)"%(name),
" use %s"%(self.fmodule),
" implicit none",
" character*(*), intent(in) :: irp_num",
" logical :: irp_is_open",
" integer :: irp_iunit" ]
if command_line.do_debug:
length = len("reader_%s" % (name))
result += [\
" character*(%d) :: irp_here = 'reader_%s'"%(length,name),
" call irp_enter(irp_here)" ]
result += map(lambda x: " call reader_%s(irp_num)" % (x), mangled(self.needs))
result += [ \
" irp_is_open = .True.",
" irp_iunit = 9",
" do while (irp_is_open)",
" inquire(unit=irp_iunit,opened=irp_is_open)",
" enddo"]
for n in self.l_name:
result += [\
" open(unit=irp_iunit,file='irpf90_%s_'//trim(irp_num),form='FORMATTED',status='OLD',action='READ')"%(n),
" read(irp_iunit,*) %s%s"%(n,build_dim(self.cm_d_variable[n].dim,colons=True)),
" close(irp_iunit)" ]
result += [ \
" call touch_%s"%(name),
" %s_is_built = .True."%(name) ]
if command_line.do_debug:
result.append(" call irp_leave(irp_here)")
result.append("end subroutine reader_%s" % (name))
result.append("")
return result
@irpy.lazy_property
def is_source_touch(self):
return (Touch in self.d_type_lines or SoftTouch in self.d_type_lines)

View File

@ -32,11 +32,8 @@ from zlib import crc32
import os
irp_id = abs(crc32(os.getcwd()))
try:
import irpy
except:
import lib_irpy as irpy
from lib.manager import irpy
from util import logger
class Line(object):
def __init__(self, i, text, filename):

View File

@ -1,10 +1,8 @@
from util import parmap, lazy_write_file
from util import flatten, listdir
from util import logger
try:
import irpy
except:
import lib_irpy as irpy
from lib.manager import irpy
import os
import irpf90_t
@ -12,9 +10,6 @@ import sys
from command_line import command_line
from util import logger
class Irpy_comm_world(object):
'''Maestro.'''

View File

@ -1,160 +0,0 @@
#Handle the execution stack
from collections import defaultdict
d_path = defaultdict(list)
d_last_caller = defaultdict(lambda: None)
def genealogy(obj, _node, direction,degree_max=100):
"""Return the genealogy of a _node.
Direction is $parents or $children, recurse accordingly"""
def sap(_node, direction, visited=set(), degree=0):
visited.add(_node)
try:
s = getattr(obj, "{0}_{1}".format(_node, direction))
except AttributeError:
s = set()
for next_ in s - visited:
if degree < degree_max:
sap(next_, direction, visited, degree+1)
return visited
s = sap(_node, direction) - set([_node])
return s
def addattr(obj, name, value):
try:
s = getattr(obj, name)
except AttributeError:
setattr(obj, name, set([value]))
else:
setattr(obj, name, s | set([value]))
def removeattr(obj, name, value):
try:
s = getattr(obj, name)
except AttributeError:
pass
else:
setattr(obj, name, s - set([value]))
# _
# | \ _ _ _ ._ _. _|_ _ ._
# |_/ (/_ (_ (_) | (_| |_ (_) |
#
class lazy_property(object):
"""
My little Property
My little Property
My little Property... friend
"""
def __init__(self, provider, init_node=False, immutable=True):
"""Provider: If a function who will be used to compute the node
init_node: If the name of the node
immutable: If immutable is set you cannot set the node"""
self.provider = provider
self.init_node = init_node
self.immutable = immutable
if not self.init_node:
name = provider.__name__
else:
name = self.init_node
#Kind of human readable identifier
self._node = "_%s_%s" % (name, id(provider))
def __get__(self, obj, objtype):
"Get the value of the node and handle the genealogy"
_node = self._node
if d_path[obj]:
_caller = d_path[obj][-1]
if _caller != d_last_caller[obj]:
addattr(obj, "%s_parents" % _node, _caller)
addattr(obj, "%s_children" % _caller, _node)
d_last_caller[obj] = _caller
#Wanted: value. Cached or Computed
try:
value = getattr(obj, _node)
except AttributeError:
d_path[obj].append(_node)
value = self.provider(obj)
setattr(obj, _node, value)
d_path[obj].pop()
return value
def __set__(self, obj, value):
"""Set the value of the node
But wait, init_node are "gradual typed" variable! Youpi!
Idea borrowed from the-worst-programming-language-ever (http://bit.ly/13tc6XW)
"""
_node = self._node
if not self.init_node:
if self.immutable:
raise AttributeError("Immutable Node {0}".format(self._node))
#Set the new value
setattr(obj, _node, value)
#Node ancestor need to be recompute is asked
for _parent in genealogy(obj, _node, "parents"):
if hasattr(obj, _parent): delattr(obj, _parent)
#Node abandoned her children,
for _child in genealogy(obj, _node, "children",degree_max=1):
removeattr(obj, "%s_parents" % _child, _node)
#Indeed node is now a leaf
setattr(obj, "%s_children" % _node, set())
else:
setattr(obj, _node, value)
self.init_node = False
def lazy_property_mutable(provider):
"Return a lazy_property mutable"
return lazy_property(provider=provider, immutable=False)
def lazy_property_leaves(mutables=(), immutables=()):
"Set to properties for the __init__ method"
def leaf_decorator(func):
def func_wrapper(self, *args, **kwargs):
for node in set(immutables) | set(mutables):
def provider(self):
return getattr(self, "_%s" % node)
p = lazy_property(provider=provider,
init_node=node,
immutable=node in immutables)
#If this ugly? Yeah... Is this an issue? I don't really know
setattr(self.__class__, node, p)
return func(self, *args, **kwargs)
return func_wrapper
return leaf_decorator

View File

@ -1,4 +1,4 @@
#!/unr/bin/env python
#!/usr/bin/env python
# IRPF90 is a Fortran90 preprocessor written in Python for programming using
# the Implicit Reference to Parameters (IRP) method.
# Copyright (C) 2009 Anthony SCEMAMA

View File

@ -59,6 +59,7 @@ simple_dict = {
"begin_provider": Begin_provider,
"begin_provider_immutable": Begin_provider,
"&begin_provider": Cont_provider,
"&begin_provider_immutable": Cont_provider,
"end_provider": End_provider,
"end_provider_immutable": End_provider,
"assert": Assert,

View File

@ -28,11 +28,7 @@
from irpf90_t import *
from util import logger
try:
import irpy
except:
import lib_irpy as irpy
from lib.manager import irpy
class Routine(object):
'''

71
src/templates/ioer.f90 Normal file
View File

@ -0,0 +1,71 @@
SUBROUTINE write_{name}(irp_num)
USE {fmodule}
IMPLICIT NONE
CHARACTER*(*), INTENT(IN) :: irp_num
LOGICAL :: irp_is_open = .TRUE.
INTEGER :: irp_iunit = 9
{?do_debug}
CHARACTER*(7+{@size key=name/}),PARAMETER :: irp_here = 'writer_{name}'
{/do_debug}
{?do_debug} CALL irp_enter(irp_here) {/do_debug}
IF (.NOT.{same_as}_is_built) THEN
CALL provide_{same_as}
ENDIF
{children}
CALL write_{.}(irp_num)
{/children}
DO WHILE (irp_is_open)
irp_iunit = irp_inuit + 1
INQUIRE(UNIT=irp_inuit, OPENED=irp_is_open)
END DO
{#group_entity}
OPEN(UNIT=irp_inuit,file='irpf90_{name}_'//trim(irp_num),FROM='FORMATTED',STATUS='UNKNOWN',ACTION='WRITE')
WRITE(irp_inuit,*) {.}{dim}
CLOSE(irp_inuit)
{/group_entity}
{?do_debug} CALL irp_leave(irp_here) {/do_debug}
END SUBROUTINE write_{name}
!TOKEN_SPLIT
SUBROUTINE read_{name}(irp_num)
USE {fmodule}
IMPLICIT NONE
CHARACTER*(*), INTENT(IN) :: irp_num
LOGICAL :: irp_is_open = .TRUE.
INTEGER :: irp_iunit = 9
{?do_debug}
CHARACTER*(5+{@size key=name/}),PARAMETER :: irp_here = 'read_{name}'
{/do_debug}
{?do_debug} CALL irp_enter(irp_here) {/do_debug}
DO WHILE (irp_is_open)
irp_iunit = irp_inuit + 1
INQUIRE(UNIT=irp_inuit, OPENED=irp_is_open)
END DO
{#group_entity}
OPEN(UNIT=irp_inuit,file='irpf90_{name}_'//trim(irp_num),FROM='FORMATTED',STATUS='UNKNOWN',ACTION='WRITE')
READ(irp_inuit,*) {name}{dim}
CLOSE(irp_inuit)
{/group_entity}
CALL touch_{name}
{?do_debug} CALL irp_leave(irp_here) {/do_debug}
END SUBROUTINE read_{name}

69
src/templates/writer.f90 Normal file
View File

@ -0,0 +1,69 @@
SUBROUTINE write_{name}(irp_num)
USE {fmodule}
IMPLICIT NONE
CHARACTER*(*), INTENT(IN) :: irp_num
LOGICAL :: irp_is_open = .TRUE.
INTEGER :: irp_iunit = 9
{?do_debug}
CHARACTER*(7+{@size key=name/}),PARAMETER :: irp_here = 'writer_{name}'
{/do_debug}
{?do_debug} CALL irp_enter(irp_here) {/do_debug}
IF (.NOT.{same_as}_is_built) THEN
CALL provide_{same_as}
ENDIF
{children}
CALL write_{.}(irp_num)
{/children}
DO WHILE (irp_is_open)
irp_iunit = irp_inuit + 1
INQUIRE(UNIT=irp_inuit, OPENED=irp_is_open)
END DO
{#group_entity}
OPEN(UNIT=irp_inuit,file='irpf90_{name}_'//trim(irp_num),FROM='FORMATTED',STATUS='UNKNOWN',ACTION='WRITE')
WRITE(irp_inuit,*) {.}{dim}
CLOSE(irp_inuit)
{/group_entity}
{?do_debug} CALL irp_leave(irp_here) {/do_debug}
END SUBROUTINE write_{name}
SUBROUTINE read_{name}(irp_num)
USE {fmodule}
IMPLICIT NONE
CHARACTER*(*), INTENT(IN) :: irp_num
LOGICAL :: irp_is_open = .TRUE.
INTEGER :: irp_iunit = 9
{?do_debug}
CHARACTER*(5+{@size key=name/}),PARAMETER :: irp_here = 'read_{name}'
{/do_debug}
{?do_debug} CALL irp_enter(irp_here) {/do_debug}
DO WHILE (irp_is_open)
irp_iunit = irp_inuit + 1
INQUIRE(UNIT=irp_inuit, OPENED=irp_is_open)
END DO
{#group_entity}
OPEN(UNIT=irp_inuit,file='irpf90_{name}_'//trim(irp_num),FROM='FORMATTED',STATUS='UNKNOWN',ACTION='WRITE')
READ(irp_inuit,*) {name}{dim}
CLOSE(irp_inuit)
{/group_entity}
CALL touch_{name}
{?do_debug} CALL irp_leave(irp_here) {/do_debug}
END SUBROUTINE read_{name}

View File

@ -46,9 +46,9 @@ logger.setLevel(30)
# ~#~#~#~#~#
# A S H E S _ T E M P L A T E S
# ~#~#~#~#~#
from ashes import AshesEnv
from lib.manager import ashes
import os
ashes_env = AshesEnv([os.path.join(os.path.dirname(__file__),'templates')])
ashes_env = ashes.AshesEnv([os.path.join(os.path.dirname(__file__),'templates')])
def remove_empy_lines(text):
return os.linesep.join([s for s in text.splitlines() if s.strip()])