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

193 lines
4.5 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
2010-10-02 23:04:30 +02:00
import os
2011-12-13 21:32:00 +01:00
NTHREADS=1 #int(os.getenv('OMP_NUM_THREADS',1))
2009-09-02 20:45:53 +02:00
2009-09-04 15:11:42 +02:00
def strip(x):
return x.strip()
2009-09-05 16:05:00 +02:00
def lower(x):
return x.lower()
2009-09-02 20:45:53 +02:00
def same_file(filename,txt):
assert isinstance(filename,str)
2013-12-11 14:00:27 +01:00
if (type(txt) == list):
buffer = ''.join(txt)
else:
buffer = txt
2009-09-02 20:45:53 +02:00
try:
file = open(filename,"r")
except IOError:
return False
stream = file.read()
2009-09-02 20:45:53 +02:00
file.close()
if len(stream) != len(buffer):
2009-09-02 20:45:53 +02:00
return False
if stream != buffer:
2013-12-11 14:00:27 +01:00
return False
2009-09-02 20:45:53 +02:00
return True
2009-09-04 15:11:42 +02:00
def build_dim(dim):
if len(dim) == 0:
return ""
else:
return "(%s)"%( ",".join(dim) )
2009-09-25 16:57:22 +02:00
def build_dim_colons(v):
d = v.dim
if d == []:
return ""
else:
x = map(lambda x: ":", d)
return "(%s)"%(','.join(x))
import error
2009-09-04 18:37:10 +02:00
def find_subname(line):
buffer = line.lower
if not buffer.endswith(')'):
buffer += "()"
buffer = buffer.split('(')
buffer = buffer[0].split()
2009-09-04 18:37:10 +02:00
if len(buffer) < 2:
error.fail(line,"Syntax Error")
return buffer[-1]
2009-09-04 23:48:49 +02:00
def make_single(l):
d = {}
for x in l:
d[x] = True
return d.keys()
2009-09-05 15:37:23 +02:00
def flatten(l):
2010-10-02 23:42:55 +02:00
if type(l) == list:
2009-09-05 15:37:23 +02:00
result = []
for i in range(len(l)):
elem = l[i]
result += flatten(elem)
return result
else:
return [l]
2009-09-04 18:37:10 +02:00
2009-09-08 16:00:46 +02:00
def dimsize(x):
assert isinstance(x,str)
buffer = x.split(':')
if len(buffer) == 1:
return x
else:
assert len(buffer) == 2
size = ""
b0, b1 = buffer
if b0.replace('-','').isdigit() and b1.replace('-','').isdigit():
size = str( int(b1) - int(b0) + 1 )
else:
if b0.replace('-','').isdigit():
size = "%s - (%d)"%(b1,int(b0)-1)
elif b1.replace('-','').isdigit():
2015-04-07 19:45:30 +02:00
size = "%d - (%s)"%(int(b1)+1,b0)
2009-09-08 16:00:46 +02:00
else:
2015-04-07 19:45:30 +02:00
size = "%s - (%s) + 1"%(b1,b0)
2009-09-08 16:00:46 +02:00
return size
2009-09-09 00:46:42 +02:00
def put_info(text,filename):
2010-10-02 23:42:55 +02:00
assert type(text) == list
2009-09-09 00:46:42 +02:00
if len(text) > 0:
2010-10-02 23:42:55 +02:00
assert type(text[0]) == tuple
2009-09-09 00:46:42 +02:00
from irpf90_t import Line
2010-10-02 23:42:55 +02:00
assert type(text[0][0]) == list
assert isinstance(text[0][1], Line)
2009-09-09 00:46:42 +02:00
lenmax = 80 - len(filename)
format = "%"+str(lenmax)+"s ! %s:%4s"
for vars,line in text:
line.text = format%(line.text.ljust(lenmax),line.filename,str(line.i))
return text
import cPickle as pickle
import os, sys
def parallel_loop(f,source):
pidlist = range(NTHREADS)
2010-10-02 21:19:04 +02:00
src = [ [] for i in xrange(NTHREADS) ]
index = 0
try:
source = map( lambda x: (len(x[1]),(x[0], x[1])), source )
source.sort()
source = map( lambda x: x[1], source )
except:
pass
2010-10-02 21:19:04 +02:00
for i in source:
2010-10-02 23:04:30 +02:00
index += 1
2010-10-02 21:19:04 +02:00
if index == NTHREADS:
index = 0
src[index].append(i)
2010-10-02 23:04:30 +02:00
thread_id = 0
fork = 1
r = range(0,NTHREADS)
2010-10-02 21:19:04 +02:00
for thread_id in xrange(1,NTHREADS):
r[thread_id], w = os.pipe()
2010-10-02 21:19:04 +02:00
fork = os.fork()
if fork == 0:
os.close(r[thread_id])
w = os.fdopen(w,'w')
2010-10-02 21:19:04 +02:00
break
else:
os.close(w)
r[thread_id] = os.fdopen(r[thread_id],'r')
pidlist[thread_id] = fork
2010-10-02 21:19:04 +02:00
thread_id = 0
result = []
2010-10-02 21:19:04 +02:00
for filename, text in src[thread_id]:
result.append( (filename, f(filename,text)) )
result.sort()
if fork == 0:
pickle.dump(result,w,-1)
w.close()
os._exit(0)
for i in xrange(1,NTHREADS):
result += pickle.load(r[i])
r[i].close()
2011-11-23 18:28:08 +01:00
os.waitpid(pidlist[i],0)[1]
return result
2009-09-02 20:45:53 +02:00
if __name__ == '__main__':
2009-09-08 16:00:46 +02:00
print "10",dimsize("10") #-> "10"
print "0:10",dimsize("0:10") # -> "11"
print "0:x",dimsize("0:x") # -> "x+1"
print "-3:x",dimsize("-3:x") # -> "x+1"
print "x:y",dimsize("x:y") # -> "y-x+1"
print "x:5",dimsize("x:5") # -> "y-x+1"