# Makefile for 2d diffusion example

# Part I. TESTING

# parameters common to both sequential and parallel...
#      D = diffusion constant
#     dt = time step
#     dx = distance between two lattice points
#      r = radius of initial circle in center
# nsteps = number of time steps
#     nx = number of points in x direction
#     ny = number of points in y direction

# additional parameter required for parallel...
# nprocsx = number of procs in the x direction
# nprocsy = number of procs in the y direction
#     nxl = horizontal extent of one process; divides nprocsx
#     nyl = vertical extent of one processl; divides nprocsy

NSTEPS = 10
NPROCS = 4
SEQ = -DD=0.1 -Ddt=1.0 -Ddx=1.0 -Dr=3.0 -Dnsteps=$(NSTEPS) -Dnx=10 -Dny=40
PAR = $(SEQ) -Dnprocsx=2 -Dnxl=5 -Dnprocsy=2 -Dnyl=20

# runs the sequential version writing output files into seqout/
seq: diffusion_seq.c Makefile
	rm -f seqout/out*
	cc $(SEQ) -o diffusion_seq diffusion_seq.c
	./diffusion_seq

# runs first parallel version, writing output files into parout/
par1: diffusion_par1.c Makefile
	rm -f parout/out*
	mpicc $(PAR) -o diffusion_par1 diffusion_par1.c
	mpiexec -n $(NPROCS) ./diffusion_par1

# runs sequential and first parallel and compares final output files
test1: seq par1
	diff seqout/out_$(NSTEPS) parout/out_$(NSTEPS)

# runs second parallel version, writing output files into parout/
par2: diffusion_par2.c Makefile
	rm -f parout/out*
	mpicc $(PAR) -o diffusion_par2 diffusion_par2.c
	mpiexec -n $(NPROCS) ./diffusion_par2

# etc.
test2: seq par2
	diff seqout/out_$(NSTEPS) parout/out_$(NSTEPS)

par3: diffusion_par3.c Makefile
	rm -f parout/out*
	mpicc $(PAR) -o diffusion_par3 diffusion_par3.c
	mpiexec -n $(NPROCS) ./diffusion_par3

test3: seq par3
	diff seqout/out_$(NSTEPS) parout/out_$(NSTEPS)


# PART II. VERIFICATION of COMMUNICATION SKELETON

MS     = ms
MSCC   = mscc -DSAFETY -DCOLLAPSE


# par1, 2x1 grid, 3 steps, chansize 2.
v1: diffusion_dl1.prom Makefile
	$(MS) -dl -np=2 -block -chansize=2 -Dnprocsx=2 -Dnprocsy=1 \
          -Dnyl=1 -Dnsteps=3 -noanysource diffusion_dl1.prom
	$(MSCC)
	./pan -n

# par1, 3x1 grid, 3 steps, chansize 2.
v2: diffusion_dl1.prom Makefile
	$(MS) -dl -np=3 -block -chansize=2 -Dnprocsx=3 -Dnprocsy=1 \
          -Dnyl=1 -Dnsteps=3 -noanysource diffusion_dl1.prom
	$(MSCC)
	./pan -n

# par1, 2x3 grid, 3 steps, chansize 1.
v3: diffusion_dl1.prom Makefile
	$(MS) -dl -np=6 -block -chansize=1 -Dnprocsx=2 -Dnprocsy=3 \
          -Dnyl=2 -Dnsteps=3 -noanysource diffusion_dl1.prom
	$(MSCC) -DVECTORSZ=2048
	./pan -n

# par1, 3x3 grid, 3 steps, chansize 0.
v4: diffusion_dl1.prom Makefile
	$(MS) -dl -np=9 -block -chansize=0 -Dnprocsx=3 -Dnprocsy=3 \
          -Dnyl=2 -Dnsteps=3 -noanysource diffusion_dl1.prom
	$(MSCC) -DVECTORSZ=10000
	./pan -n

# par1, 3x4 grid, 2 steps, nonblocking, buf=0. too big?
v5: diffusion_dl1.prom Makefile
	$(MS) -dl -np=12 -buf=0 -req=24 -Dnprocsx=3 -Dnprocsy=4 \
          -Dnyl=2 -Dnsteps=2 -noanysource -notest -nocancel -noprobe \
          diffusion_dl1.prom
	$(MSCC) -DVECTORSZ=4000
	./pan -n

# par2, 2x1 grid, 3 steps, chansize 2.
v6: diffusion_dl2.prom Makefile
	$(MS) -dl -np=2 -block -chansize=2 -Dnprocsx=2 -Dnprocsy=1 \
          -Dnyl=1 -Dnsteps=3 diffusion_dl2.prom
	$(MSCC)
	./pan -n

# par2, 3x1 grid, 2 steps, chansize 2.  Counterexample.
v7: diffusion_dl2.prom Makefile
	$(MS) -dl -np=3 -block -chansize=2 -Dnprocsx=3 -Dnprocsy=1 \
          -Dnyl=1 -Dnsteps=2 diffusion_dl2.prom
	$(MSCC) -DREACH
	./pan -n -i
	./pan -n -r

# par3, 3x1, 10 steps, chansize 2.
v8: diffusion_dl3.prom Makefile
	$(MS) -dl -np=3 -block -chansize=2 -Dnprocsx=3 -Dnprocsy=1 \
          -Dnyl=1 -Dnsteps=10 diffusion_dl3.prom
	$(MSCC)
	./pan -n

# par3, 2x3 grid, 2 steps, chansize 1.  Verifies.
v9: diffusion_dl3.prom Makefile
	$(MS) -dl -np=6 -block -chansize=1 -Dnprocsx=2 -Dnprocsy=3 \
          -Dnyl=1 -Dnsteps=2 diffusion_dl3.prom
	$(MSCC) -DVECTORSZ=2000
	./pan -n


# PART III. VERIFICATION OF FUNCTIONAL EQUIVALENCE

v10: diffusion_sym.prom diffusion_sym.c Makefile
	$(MS) -v diffusion_sym.prom -block -chansize=0 -Dnsteps=1  \
          -np=4 -Dnprocsx=2 -Dnprocsy=2                         \
          -Dnx=4 -Dny=4 -Dnxl=2 -Dnyl=2 -Dnxlg=4 -Dnylg=4
	$(MSCC)
	./pan -n

v11: diffusion_sym.prom diffusion_sym.c Makefile
	$(MS) diffusion_sym.prom -block -chansize=0 -Dnsteps=2  \
          -np=4 -Dnprocsx=2 -Dnprocsy=2                         \
          -Dnx=4 -Dny=4 -Dnxl=2 -Dnyl=2 -Dnxlg=4 -Dnylg=4
	$(MSCC)
	./pan -n

v12: diffusion_sym.prom diffusion_sym.c Makefile
	$(MS) diffusion_sym.prom -block -chansize=0 -Dnsteps=3  \
          -np=6 -Dnprocsx=2 -Dnprocsy=3                         \
          -Dnx=4 -Dny=6 -Dnxl=2 -Dnyl=2 -Dnxlg=4 -Dnylg=4
	$(MSCC) -DVECTORSZ=4000
	./pan -n

clean:
	rm -f diffusion_seq diffusion_par1 diffusion_par2 diffusion_par3 \
          parout/out* seqout/out* pan* *~ mpi-spin-init.c *.trail
