Skip to content

Commit

Permalink
Replace 'make' with build.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pvthinker committed Mar 25, 2023
1 parent 7499a82 commit 1e78c5c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
39 changes: 39 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Compile Fluid2d Fortran routines into Python modules
using f2py
March 2023: the compilation now by-pass 'make' and 'Makefile'
"""
import os
import glob
import subprocess

FFLAGS = "-O3 -march=native -fPIC"

srcs = ["core/fortran_advection.f90",
"core/fortran_diag.f90",
"core/fortran_fluxes.f90",
"core/fortran_operators.f90",
"core/gmg/fortran_multigrid.f90"]


pwd = os.getcwd()
null = subprocess.PIPE

print("Compile Fortran routines to Python modules")

for filename in srcs:

src = os.path.basename(filename)
direc = os.path.dirname(filename)
lib = os.path.splitext(src)[0]

localdir = f"{pwd}/{direc}"

cmd = ["f2py", f'--opt={FFLAGS}', "-m", lib, "-c", src]

print(" ".join(cmd))

os.chdir(localdir)
subprocess.call(cmd, stdout=null, stderr=null)
13 changes: 6 additions & 7 deletions core/gmg/halo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

from numpy import zeros,arange,sqrt,int32,meshgrid,where
from time import time
from gmg.fortran_multigrid import *
import gmg.fortran_multigrid as fortmg
import numpy
#from fillhalo import fillhalo_fortran


def set_neighbours(myrank,np,mp,ix,iy):
Expand Down Expand Up @@ -139,7 +138,7 @@ def fill(self,x):
# fill the halo if proc only knows a subdomain
if self.method==0:
for k in range(self.nbhalo):
fillhalo(x,self.nh)
fortmg.fillhalo(x,self.nh)
# x=zeros((self.m,self.n))
elif self.method==1:

Expand Down Expand Up @@ -242,7 +241,7 @@ def fill(self,x):
# b6=self.sbuff[6]
# b7=self.sbuff[7]

halotobuffer(x,
fortmg.halotobuffer(x,
self.sbuff[0],self.sbuff[1],self.sbuff[2],
self.sbuff[3], self.sbuff[4],
self.sbuff[5],self.sbuff[6],self.sbuff[7])
Expand Down Expand Up @@ -285,7 +284,7 @@ def fill(self,x):

# x=buffertohalo(x,b0,b1,b2,b3,b4,b5,b6,b7)

buffertohalo(x,
fortmg.buffertohalo(x,
self.rbuff[0],self.rbuff[1],self.rbuff[2],
self.rbuff[3],self.rbuff[4],
self.rbuff[5],self.rbuff[6],self.rbuff[7])
Expand All @@ -296,7 +295,7 @@ def fill(self,x):
elif self.method==3:
comm = MPI.COMM_WORLD
for l in range(self.nbhalo):
halotobuffer(x,
fortmg.halotobuffer(x,
self.sbuff[0],self.sbuff[1],self.sbuff[2],
self.sbuff[3], self.sbuff[4],
self.sbuff[5],self.sbuff[6],self.sbuff[7])
Expand All @@ -312,7 +311,7 @@ def fill(self,x):
for k in range(8):
req[k].Wait(MPI.Status())

buffertohalo(x,
fortmg.buffertohalo(x,
self.rbuff[0],self.rbuff[1],self.rbuff[2],
self.rbuff[3],self.rbuff[4],
self.rbuff[5],self.rbuff[6],self.rbuff[7])
Expand Down
34 changes: 17 additions & 17 deletions core/gmg/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from numpy import zeros, arange, sqrt, array, meshgrid, ones, concatenate, unique, random, pi, int8, size, isnan, exp, NaN, asarray
from time import time
# from fortran_multigrid import *
from gmg.subdomains import *
from gmg.halo import *
import gmg.subdomains as subdomains
import gmg.fortran_multigrid as fortmg
import gmg.halo as halo
import sys
# from misc_mpi import *
# from plotutils import plot2d
Expand Down Expand Up @@ -201,17 +201,17 @@ def __init__(self, gridinfo):
method = 2 # use MPI communications and Fortran routines

if self.flag == 'peak':
family = set_family(myrank, np0, mp0, ix, iy)
family = subdomains.set_family(myrank, np0, mp0, ix, iy)
reducj = family.shape[0]
reduci = family.shape[1]
# coarser resolution
self.nc = n//reduci+2*nh
self.mc = m//reducj+2*nh
self.subdom = Subdomains(
self.subdom = subdomains.Subdomains(
nh, self.nc, self.mc, comm, family, method=method)

neighbours = set_neighbours(myrank, np0, mp0, ix, iy)
self.halo = Halo(nh, nv, mv, comm, neighbours,
neighbours = halo.set_neighbours(myrank, np0, mp0, ix, iy)
self.halo = halo.Halo(nh, nv, mv, comm, neighbours,
method=method) # check method

self.msk = ones((mv, nv), dtype=int8) # default mask full of ones
Expand Down Expand Up @@ -318,7 +318,7 @@ def compute_A_atcoarser(self, prevlev, msk):
# let's coarsen the matrix, done in Fortran
# we want
# Acoarse = R * Afine * I
A = coarsenmatrix(prevlev.A, prevlev.msk, msk, prevlev.nh)
A = fortmg.coarsenmatrix(prevlev.A, prevlev.msk, msk, prevlev.nh)
# don't forget to fill the halo
for k in range(9):
i, j = k % 3, k//3
Expand Down Expand Up @@ -346,9 +346,9 @@ def smooth(self, x, b, nite):
# MPI.COMM_WORLD.Barrier()
# we apply the smoothing twice
if self.relaxation == 'tridiagonal':
smoothtridiag(self.msk, self.A, x, b)
fortmg.smoothtridiag(self.msk, self.A, x, b)
else:
smoothtwicewitha(self.msk, self.A, x,
fortmg.smoothtwicewitha(self.msk, self.A, x,
b, self.omega, self.yo)

# smoothoncewitha(self.msk,self.A,x,b,self.omega,self.yo)
Expand All @@ -371,7 +371,7 @@ def residual(self, x, b, r):
for l in range(self.nbresidual):
t0 = time()
# MPI.COMM_WORLD.Barrier()
computeresidualwitha(self.msk, self.A, x, b, r)
fortmg.computeresidualwitha(self.msk, self.A, x, b, r)
t1 = time()
self.time['res'] += t1-t0
self.ncalls['res'] += 1
Expand All @@ -394,7 +394,7 @@ def norm(self, x):
t0 = time()
# MPI.COMM_WORLD.Barrier()
if self.typenorm == 'l2':
local_sum = computenorm(self.msk, x, self.nh)
local_sum = fortmg.computenorm(self.msk, x, self.nh)
t1 = time()
self.time['norm'] += t1-t0
self.ncalls['norm'] += 1
Expand All @@ -405,7 +405,7 @@ def norm(self, x):
self.ncalls['reduce'] += 1

if self.typenorm == 'inf':
local_z = computemax(self.msk, x, self.nh)
local_z = fortmg.computemax(self.msk, x, self.nh)
t1 = time()
self.time['norm'] += t1-t0
self.ncalls['norm'] += 1
Expand All @@ -420,7 +420,7 @@ def inner(self, x, y):
nbduplicates = (self.np0*self.mp0)/(self.np*self.mp)
# computenorm is done in Fortran

local_sum = computeinner(self.msk, x, y, self.nh)
local_sum = fortmg.computeinner(self.msk, x, y, self.nh)
z = MPI.COMM_WORLD.allreduce(local_sum, op=MPI.SUM) / nbduplicates

return z
Expand All @@ -430,7 +430,7 @@ def sum(self, x):
t0 = time()
# MPI.COMM_WORLD.Barrier()
nbduplicates = (self.np0*self.mp0)/(self.np*self.mp)
local_sum = computesum(self.msk, x, self.nh)
local_sum = fortmg.computesum(self.msk, x, self.nh)
t1 = time()
self.time['sum'] += t1-t0
self.ncalls['sum'] += 1
Expand All @@ -457,7 +457,7 @@ def coarsetofine(coarse, fine, x, y):
coarse.ncalls['split'] += 1
else:
t0 = time()
interpolate(fine.msk, coarse.msk, x, fine.nh, y)
fortmg.interpolate(fine.msk, coarse.msk, x, fine.nh, y)
t1 = time()
fine.time['interpolate'] += t1-t0
fine.ncalls['interpolate'] += 1
Expand All @@ -480,7 +480,7 @@ def finetocoarse(fine, coarse, x, y):
coarse.ncalls['gather'] += 1
else:
t0 = time()
restrict(coarse.msk, x, coarse.nh, y)
fortmg.restrict(coarse.msk, x, coarse.nh, y)
t1 = time()
coarse.time['restrict'] += t1-t0
coarse.ncalls['restrict'] += 1
Expand Down
5 changes: 3 additions & 2 deletions core/gmg/subdomains.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
exit(0)

from numpy import zeros,arange,ones,cumsum,sqrt,array,meshgrid
from gmg.fortran_multigrid import buffertodomain
#from gmg.fortran_multigrid import buffertodomain
import gmg.fortran_multigrid as fortmg
from time import time
#from plotutils import plot2d

Expand Down Expand Up @@ -111,7 +112,7 @@ def gather(self,x,y):


b = self.rbuff.reshape( (self.mp,self.np,self.m,self.n))
buffertodomain(b,y,self.nh,self.m1,self.n1)
fortmg.buffertodomain(b,y,self.nh,self.m1,self.n1)


def split(self,x,y):
Expand Down
3 changes: 2 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ echo " Compile modules with f2py"
echo ""
{
# try
make
#make
python3 build.py

} || {
#catch
Expand Down

0 comments on commit 1e78c5c

Please sign in to comment.