Upload ex6 and ex7

This commit is contained in:
jakob.schratter 2026-01-04 20:15:55 +01:00
commit 6c2d96ff4d
44 changed files with 15291 additions and 0 deletions

83
ex6/adaptivity_schemes.py Normal file
View file

@ -0,0 +1,83 @@
import numpy as np
def flux_jumps(mesh, u):
N = len(mesh) - 1 # number of elements
jumps = np.zeros(N + 1) # N-1 edges
for i in range(1, N):
upper = (u[i + 1] - u[i])/(mesh[i + 1] - mesh[i])
lower = (u[i] - u[i - 1])/(mesh[i] - mesh[i - 1])
jumps[i] = upper - lower
return jumps
def residual_errors(mesh, u):
N = len(mesh) - 1 # number of elements
errors = np.zeros(N)
jumps = flux_jumps(mesh, u)
for i in range(N):
errors[i] = np.sqrt((jumps[i]**2 + jumps[i + 1]**2)/2) # Braess (8.10)
#print("errors:\n", errors)
return errors
def adapt_h(mesh, u, alpha):
N = len(mesh) - 1 # number of elements
errors = residual_errors(mesh, u)
threshhold = alpha * abs(max(errors))
# refine mesh
refined_mesh = [mesh[0]]
for i in range(N):
if abs(errors[i]) <= threshhold:
refined_mesh.append(mesh[i + 1])
else:
refined_mesh.append(mesh[i] + (mesh[i + 1] - mesh[i])/2)
refined_mesh.append(mesh[i + 1])
#print("refined mesh:\n", refined_mesh)
return refined_mesh
def adapt_r(mesh, u):
N = len(mesh) - 1 # number of elements
rho = np.abs(flux_jumps(mesh, u)) # rho ... mesh density function
p = np.zeros(N) # piecewise constant function on the mesh elements
for i in range(N):
p[i] = (rho[i] + rho[i + 1])/2
P = np.zeros(N + 1) # \int_0^{x_j} p(x) dx for j = 1,...,N
for j in range(1, N + 1):
h_j = mesh[j] - mesh[j - 1]
P[j] = P[j - 1] + h_j*p[j - 1] # add integral over j-th interval
moved_mesh = np.zeros(N + 1)
moved_mesh[0] = mesh[0]
moved_mesh[-1] = mesh[-1]
for j in range(1, N): # calculate the new nodes with De Boor's algorithm
xi_j = j/N
k = np.searchsorted(P, xi_j*P[-1], side="left") # searches for index k, such that xi_j*P[-1] > P[k]
assert(P[k - 1] < xi_j*P[-1])
assert(xi_j*P[-1] <= P[k])
moved_mesh[j] = mesh[k - 1] + (xi_j*P[-1] - P[k - 1])/p[k - 1]
print("orign_mesh[j] =", mesh[j])
print("moved_mesh[j] =", moved_mesh[j])
print("done\n")
#print("moved mesh:\n", moved_mesh)
return moved_mesh

139
ex6/ex_6A.py Normal file
View file

@ -0,0 +1,139 @@
import numpy as np
import scipy.integrate as integrate
import matplotlib.pyplot as plt
import adaptivity_schemes
np.set_printoptions(precision=2)
def Solve_6A(mesh, p):
N = len(mesh) - 1 # number of elements
f = lambda x : 2*p**3*x/((p**2*x**2 + 1)**2)
g_b = p/(p**2 + 1)
A = np.zeros((N + 1, N + 1))
f_vec = np.zeros(N + 1)
for i in range(1, N + 1):
h = mesh[i] - mesh[i - 1]
a_11 = 1./h
a_12 = -1./h
a_21 = -1./h
a_22 = 1./h
A[i - 1, i - 1] += a_11
A[i - 1, i] += a_12
A[i, i - 1] += a_21
A[i, i] += a_22
phi_lower = lambda x : (mesh[i] - x)/h
f_vec[i-1] += integrate.quad(lambda x : f(x)*phi_lower(x), mesh[i - 1], mesh[i])[0]
phi_upper = lambda x : (x - mesh[i - 1])/h
f_vec[i] += integrate.quad(lambda x : f(x)*phi_upper(x), mesh[i - 1], mesh[i])[0]
# take Neumann data into account
A[N, N] += 0
f_vec[N] += g_b
# take Dirichlet data into account
u_g = np.zeros(N + 1)
u_g[0] = -np.arctan(p)
#print("u_g =\n", u_g)
# remove first row of A
A_g = A[1:N+1, :]
#print("A_g =\n", A_g)
# remove first row of f_vec
f_g = f_vec[1:N+1]
# assemble RHS with dirichlet data
f_g -= A_g.dot(u_g)
#print("f_g =\n", f_g)
# matrix for the inner nodes (excluding nodes with dirichlet bcs)
A_0 = A[1:N+1, 1:N+1]
#print(A_0)
# solve for u_0 (free dofs)
u_0 = np.linalg.solve(A_0, f_g)
# assemble "u = u_0 + u_g"
u = np.concatenate([[u_g[0]], u_0])
return u
p = 100
########## h-adaptivity ##########
N = 5 # number of elements
mesh = np.linspace(-1, 1, N + 1)
u = Solve_6A(mesh, p)
plt.plot(mesh, u, '-o')
plt.grid()
plt.xlabel('x')
plt.ylabel('u_h(x)')
plt.title("h-adaptivity")
N_vec = ["0 refinements, " + str(N) + " elements"]
refinements = 4 # number of refinements
for i in range(refinements):
mesh = adaptivity_schemes.adapt_h(mesh, u, 0.7)
u = Solve_6A(mesh, p)
plt.plot(mesh, u, '-o')
N_vec.append(str(i + 1) + " refinements, " + str(len(mesh) - 1) + " elements")
# plot exact solution
x = np.linspace(-1, 1, 50)
plt.plot(x, np.arctan(p*x))
N_vec.append("exact")
plt.legend(N_vec)
plt.show()
# ########## r-adaptivity ##########
N = 5
mesh = np.linspace(-1, 1, N + 1)
u = Solve_6A(mesh, p)
plt.plot(mesh, u, '-o')
title = "r-adaptivity with " + str(N) + " elements"
plt.title(title)
adaptations_vec = ["0 adaptations"]
adaptations = 5 # number of iterations
for i in range(adaptations):
mesh = adaptivity_schemes.adapt_r(mesh, u)
u = Solve_6A(mesh, p)
plt.plot(mesh, u, '-o')
adaptations_vec.append(str(i + 1) + " adaptations")
# plot exact solution
x = np.linspace(-1, 1, 50)
plt.plot(x, np.arctan(p*x))
adaptations_vec.append("exact")
plt.legend(adaptations_vec)
plt.xlabel('x')
plt.ylabel('u_h(x)')
plt.grid()
plt.show()

120
ex6/ex_6B.py Normal file
View file

@ -0,0 +1,120 @@
import numpy as np
import matplotlib.pyplot as plt
import adaptivity_schemes
np.set_printoptions(precision=2)
def lam_func(x):
n = len(x)
lam_vec = np.zeros(n)
for i in range(n):
if (x[i] > 1/np.sqrt(2)):
lam_vec[i] = 10
else:
lam_vec[i] = 1
return lam_vec
def Solve_6B(mesh):
N = len(mesh) - 1 # number of elements
A = np.zeros((N + 1, N + 1))
lam_vec = lam_func(mesh)
for i in range(1, N + 1):
h = mesh[i] - mesh[i - 1]
a_11 = lam_vec[i]/h
a_12 = -lam_vec[i]/h
a_21 = -lam_vec[i]/h
a_22 = lam_vec[i]/h
A[i - 1, i - 1] += a_11
A[i - 1, i] += a_12
A[i, i - 1] += a_21
A[i, i] += a_22
#print("A =\n", A)
# take dirichlet data into account
u_g = np.zeros(N + 1)
u_g[0] = 0
u_g[N] = 1
#print("u_g =\n", u_g)
# remove first and last row of A
A_g = A[1:N, :]
#print("A_g =\n", A_g)
# assemble RHS with dirichlet data
f = -A_g.dot(u_g)
#print(f)
# matrix for the inner nodes (excluding nodes with dirichlet bcs)
A_0 = A[1:N, 1:N]
#print(A_0)
# solve for u_0 (free dofs)
u_0 = np.linalg.solve(A_0, f)
# assemble "u = u_0 + u_g"
u = np.concatenate([[0], u_0, [1]])
#print("u =\n", u)
return u
########## h-adaptivity ##########
N = 2 # number of elements
mesh = np.linspace(0, 1, N + 1)
u = Solve_6B(mesh)
plt.plot(mesh, u, '-o')
plt.grid()
plt.xlabel('x')
plt.ylabel('u_h(x)')
plt.title("h-adaptivity")
N_vec = ["0 refinements, " + str(N) + " elements"]
refinements = 5 # number of refinements
for i in range(refinements):
mesh = adaptivity_schemes.adapt_h(mesh, lam_func(mesh)*u, 0.9)
u = Solve_6B(mesh)
plt.plot(mesh, u, '-o')
N_vec.append(str(i + 1) + " refinements, " + str(len(mesh) - 1) + " elements")
plt.legend(N_vec)
plt.show()
########## r-adaptivity ##########
N = 5
mesh = np.linspace(0, 1, N + 1)
u = Solve_6B(mesh)
plt.plot(mesh, u, '-o')
title = "r-adaptivity with " + str(N) + " elements"
plt.title(title)
adaptations_vec = ["0 adaptations"]
adaptations = 4 # number of iterations
for i in range(adaptations):
mesh = adaptivity_schemes.adapt_r(mesh, lam_func(mesh)*u)
u = Solve_6B(mesh)
plt.plot(mesh, u, '-o')
adaptations_vec.append(str(i + 1) + " adaptations")
plt.legend(adaptations_vec)
plt.xlabel('x')
plt.ylabel('u_h(x)')
plt.grid()
plt.show()

127
ex6/ex_6C.py Normal file
View file

@ -0,0 +1,127 @@
import numpy as np
import matplotlib.pyplot as plt
import adaptivity_schemes
np.set_printoptions(precision=2)
def Solve_6C(mesh, p):
N = len(mesh) - 1 # number of elements
A = np.zeros((N + 1, N + 1))
for i in range(1, N + 1):
h = mesh[i] - mesh[i - 1]
a_11 = 1./h - p/2.
a_12 = -1./h + p/2.
a_21 = -1./h - p/2.
a_22 = 1./h + p/2.
A[i - 1, i - 1] += a_11
A[i - 1, i] += a_12
A[i, i - 1] += a_21
A[i, i] += a_22
#print("A =\n", A)
# take dirichlet data into account
u_g = np.zeros(N + 1)
u_g[0] = 0
u_g[N] = 1
#print("u_g =\n", u_g)
# remove first and last row of A
A_g = A[1:N, :]
#print("A_g =\n", A_g)
# assemble RHS with dirichlet data
f = -A_g.dot(u_g)
#print(f)
# matrix for the inner nodes (excluding nodes with dirichlet bcs)
A_0 = A[1:N, 1:N]
#print(A_0)
# solve for u_0 (free dofs)
u_0 = np.linalg.solve(A_0, f)
# assemble "u = u_0 + u_g"
u = np.concatenate([[0], u_0, [1]])
return u
p = 70
######### h-adaptivity ##########
N = 5 # number of elements
mesh = np.linspace(0, 1, N + 1)
u = Solve_6C(mesh, p)
plt.plot(mesh, u, '-o')
plt.grid()
plt.xlabel('x')
plt.ylabel('u_h(x)')
plt.title("h-adaptivity")
N_vec = ["0 refinements, " + str(N) + " elements"]
refinements = 4 # number of refinements
for i in range(refinements):
mesh = adaptivity_schemes.adapt_h(mesh, u, 0.7)
u = Solve_6C(mesh, p)
plt.plot(mesh, u, '-o')
N_vec.append(str(i + 1) + " refinements, " + str(len(mesh) - 1) + " elements")
# plot exact solution
x = np.linspace(0, 1, 50)
plt.plot(x, (np.exp(p*x) - 1.)/(np.exp(p) - 1.))
N_vec.append("exact")
plt.legend(N_vec)
plt.show()
########## r-adaptivity ##########
N = 10
mesh = np.linspace(0, 1, N + 1)
u = Solve_6C(mesh, p)
plt.plot(mesh, u, '-o')
title = "r-adaptivity with " + str(N) + " elements"
plt.title(title)
adaptations_vec = ["0 adaptations"]
adaptations = 4 # number of iterations
for i in range(adaptations):
mesh = adaptivity_schemes.adapt_r(mesh, u)
u = Solve_6C(mesh, p)
plt.plot(mesh, u, '-o')
adaptations_vec.append(str(i + 1) + " adaptations")
# plot exact solution
x = np.linspace(0, 1, 50)
plt.plot(x, (np.exp(p*x) - 1.)/(np.exp(p) - 1.))
adaptations_vec.append("exact")
plt.legend(adaptations_vec)
plt.xlabel('x')
plt.ylabel('u_h(x)')
plt.grid()
plt.show()

154
ex7/ex7_2/GCC_default.mk Normal file
View file

@ -0,0 +1,154 @@
# Basic Defintions for using GNU-compiler suite sequentially
# requires setting of COMPILER=GCC_
#startmake as follows to avoid warnings caused by OpenMPI code
# make 2>&1 | grep -v openmpi
MPI_ROOT=/usr/bin/
CC = ${MPI_ROOT}mpicc
CXX = ${MPI_ROOT}mpicxx
F77 = ${MPI_ROOT}mpif77
LINKER = ${CXX}
# If you 'mpirun ...' reports some error "... not enough slots .." then use the option '--oversubscribe'
MPIRUN = ${MPI_ROOT}mpirun --oversubscribe -display-map
#MPIRUN = ${MPI_ROOT}mpiexec
# 2023, Oct 23: ""WARNING: There is at least non-excluded one OpenFabrics device found,"
# solution according to https://github.com/open-mpi/ompi/issues/11063
MPIRUN += -mca btl ^openib
# KFU:sauron
CXXFLAGS += -I/software/boost/1_72_0/include
WARNINGS = -Wall -pedantic -Woverloaded-virtual -Wfloat-equal -Wshadow \
-Wredundant-decls -Wunreachable-code -Winline -fmax-errors=1
# WARNINGS += -Weffc++ -Wextra
# -Wno-pragmas
CXXFLAGS += -std=c++17 -ffast-math -O3 -march=native ${WARNINGS}
# -ftree-vectorizer-verbose=5 -DNDEBUG
# -ftree-vectorizer-verbose=2
# CFLAGS = -ffast-math -O3 -DNDEBUG -msse3 -fopenmp -fdump-tree-vect-details
# CFLAGS = -ffast-math -O3 -funroll-loops -DNDEBUG -msse3 -fopenmp -ftree-vectorizer-verbose=2
# info on vectorization
#VECTORIZE = -ftree-vectorize -fdump-tree-vect-blocks=foo.dump
#-fdump-tree-pre=stderr
VECTORIZE = -ftree-vectorize -fopt-info -ftree-vectorizer-verbose=5
#CXXFLAGS += ${VECTORIZE}
# -funroll-all-loops -msse3
#GCC -march=knl -march=broadwell -march=haswell
# for debugging purpose (save code)
# -fsanitize=leak # only one out the trhee can be used
# -fsanitize=address
# -fsanitize=thread
SANITARY = -fsanitize=address -fsanitize=undefined -fsanitize=null -fsanitize=return \
-fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow \
-fsanitize=bool -fsanitize=enum -fsanitize=vptr
#CXXFLAGS += ${SANITARY}
#LINKFLAGS +=${SANITARY}
# OpenMP
CXXFLAGS += -fopenmp
LINKFLAGS += -fopenmp
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
$(LINKER) $^ ${LINKFLAGS} -o $@
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
@rm -f ${PROGRAM} ${OBJECTS} gmon.out
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar *.orig
@rm -rf html latex
run: ${PROGRAM}
${MPIRUN} -np 8 ./$^
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
zip: clean
@echo "Zip the directory: " ${MY_DIR}
@cd .. ;\
zip -r ${MY_DIR}.zip ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
$(CXX) -c $(CXXFLAGS) -o $@ $<
# 2>&1 | grep -v openmpi
# special: get rid of compiler warnings genermaeate by openmpi-files
#.cpp.o:
# @$(CXX) -c $(CXXFLAGS) $< 2>/tmp/t.txt || grep -sv openmpi /tmp/t.txt
# |grep -sv openmpi
.c.o:
$(CC) -c $(CFLAGS) -o $@ $<
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# some tools
# Cache behaviour (CXXFLAGS += -g tracks down to source lines; no -pg in linkflags)
cache: ${PROGRAM}
valgrind --tool=callgrind --simulate-cache=yes ./$^
# kcachegrind callgrind.out.<pid> &
kcachegrind `ls -1tr callgrind.out.* |tail -1`
# Check for wrong memory accesses, memory leaks, ...
# use smaller data sets
# no "-pg" in compile/link options
mem: ${PROGRAM}
valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes mpirun -np 8 ./$^
# Graphical interface
# valkyrie
# Simple run time profiling of your code
# CXXFLAGS += -g -pg
# LINKFLAGS += -pg
prof: ${PROGRAM}
perf record ./$^
perf report
# gprof -b ./$^ > gp.out
# kprof -f gp.out -p gprof &
#Trace your heap:
#> heaptrack ./main.GCC_
#> heaptrack_gui heaptrack.main.GCC_.<pid>.gz
heap: ${PROGRAM}
heaptrack ./$^ 11
heaptrack_gui `ls -1tr heaptrack.$^.* |tail -1` &
codecheck: $(SOURCES)
cppcheck --enable=all --inconclusive --std=c++17 --suppress=missingIncludeSystem $^
########################################################################
# get the detailed status of all optimization flags
info:
echo "detailed status of all optimization flags"
$(CXX) --version
$(CXX) -Q $(CXXFLAGS) --help=optimizers

54
ex7/ex7_2/ex7_2/Makefile Executable file
View file

@ -0,0 +1,54 @@
#
# use GNU-Compiler tools
COMPILER=GCC_
# COMPILER=GCC_SEQ_
# alternatively from the shell
# export COMPILER=GCC_
# or, alternatively from the shell
# make COMPILER=GCC_
MAIN = main
SOURCES = ${MAIN}.cpp greetings.cpp
OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = ${MAIN}.${COMPILER}
# uncomment the next to lines for debugging and detailed performance analysis
CXXFLAGS += -g
# -DNDEBUG
# -pg slows down the code on my laptop when using CLANG_
LINKFLAGS += -g
#-pg
#CXXFLAGS += -Q --help=optimizers
#CXXFLAGS += -fopt-info
include ../${COMPILER}default.mk
#############################################################################
# additional specific cleaning in this directory
clean_all::
@rm -f t.dat*
#############################################################################
# special testing
# NPROCS = 4
#
TFILE = t.dat
# TTMP = t.tmp
#
graph: $(PROGRAM)
# @rm -f $(TFILE).*
# next two lines only sequentially
./$(PROGRAM)
@mv $(TFILE).000 $(TFILE)
# $(MPIRUN) $(MPIFLAGS) -np $(NPROCS) $(PROGRAM)
# @echo " "; echo "Manipulate data for graphics."; echo " "
# @cat $(TFILE).* > $(TTMP)
# @sort -b -k 2 $(TTMP) -o $(TTMP).1
# @sort -b -k 1 $(TTMP).1 -o $(TTMP).2
# @awk -f nl.awk $(TTMP).2 > $(TFILE)
# @rm -f $(TTMP).* $(TTMP) $(TFILE).*
#
-gnuplot jac.dem

View file

@ -0,0 +1,89 @@
#include "greetings.h"
#include <cassert>
#include <cstring>
#include <iostream>
#include <mpi.h> // MPI
#include <string>
using namespace std;
// see http://www.open-mpi.org/doc/current
// for details on MPI functions
void greetings(MPI_Comm const &icomm)
{
int myrank, numprocs;
MPI_Comm_rank(icomm, &myrank); // my MPI-rank
MPI_Comm_size(icomm, &numprocs); // #MPI processes
char *name = new char [MPI_MAX_PROCESSOR_NAME],
*chbuf = new char [MPI_MAX_PROCESSOR_NAME];
int reslen, ierr;
MPI_Get_processor_name( name, &reslen);
if (0==myrank) {
cout << " " << myrank << " runs on " << name << endl;
for (int i = 1; i < numprocs; ++i) {
MPI_Status stat;
stat.MPI_ERROR = 0; // M U S T be initialized!!
//ierr = MPI_Recv(chbuf, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, icomm, &stat);
ierr = MPI_Recv(chbuf, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, i, i, icomm, &stat);
assert(0==ierr);
cout << " " << stat.MPI_SOURCE << " runs on " << chbuf;
int count;
MPI_Get_count(&stat, MPI_CHAR, &count); // size of received data
cout << " (length: " << count << " )" << endl;
// stat.Get_error() // Error code
}
}
else {
int dest = 0;
ierr = MPI_Send(name, strlen(name) + 1, MPI_CHAR, dest, myrank, icomm);
assert(0==ierr);
}
delete [] chbuf;
delete [] name;
return;
}
void greetings_cpp(MPI_Comm const &icomm)
{
int myrank, numprocs;
MPI_Comm_rank(icomm, &myrank); // my MPI-rank
MPI_Comm_size(icomm, &numprocs); // #MPI processes
string name(MPI_MAX_PROCESSOR_NAME,'#'), // C++
recvbuf(MPI_MAX_PROCESSOR_NAME,'#'); // C++: receive buffer, don't change size
int reslen, ierr;
MPI_Get_processor_name(name.data(), &reslen);
name.resize(reslen); // C++
if (0==myrank) {
cout << " " << myrank << " runs on " << name << endl;
for (int i = 1; i < numprocs; ++i) {
MPI_Status stat;
stat.MPI_ERROR = 0; // M U S T be initialized!!
//ierr = MPI_Recv(recvbuf.data(), MPI_MAX_PROCESSOR_NAME, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, icomm, &stat);
ierr = MPI_Recv(recvbuf.data(), MPI_MAX_PROCESSOR_NAME, MPI_CHAR, i, i, icomm, &stat);
assert(0==ierr);
int count;
MPI_Get_count(&stat, MPI_CHAR, &count); // size of received data
string const chbuf(recvbuf,0,count); // C++
cout << " " << stat.MPI_SOURCE << " runs on " << chbuf;
cout << " (length: " << count << " )" << endl;
// stat.Get_error() // Error code
}
}
else {
int dest = 0;
ierr = MPI_Send(name.data(), name.size(), MPI_CHAR, dest, myrank, icomm);
assert(0==ierr);
}
return;
}

View file

@ -0,0 +1,11 @@
// general header for all functions in directory
#ifndef GREETINGS_FILE
#define GREETINGS_FILE
#include <mpi.h>
void greetings(MPI_Comm const &icomm);
void greetings_cpp(MPI_Comm const &icomm);
#endif

40
ex7/ex7_2/ex7_2/main.cpp Normal file
View file

@ -0,0 +1,40 @@
#include <iostream>
#include <mpi.h>
#include "greetings.h"
using namespace std;
int main(int argc , char **argv )
{
// -------------------- E2 --------------------
MPI_Init(&argc, &argv); // Initializes the MPI execution environment
// -------------------- E1 --------------------
MPI_Comm const icomm(MPI_COMM_WORLD); // MPI_COMM_WORLD ... all processes
// -------------------- E3 --------------------
int rank;
MPI_Comm_rank(icomm, &rank); // Determines the rank of the calling process in the communicator.
if (rank == 0)
{
int size;
MPI_Comm_size(icomm, &size); // Returns the size of the group associated with a communicator.
cout << "Process " << rank << " says: " << size << " proesses are running." << endl;
}
// To vary number of processes: changed number in GCC_default.mk file
// alternatively, call in terminal:
// /usr/bin/mpirun --oversubscribe -display-map -mca btl ^openib -np 4 ./main.GCC_
// or
// /usr/bin/mpirun --oversubscribe -display-map -mca btl ^openib -np 8 ./main.GCC_
// -------------------- E4 --------------------
greetings_cpp(MPI_COMM_WORLD); // greetings with sorted output
MPI_Finalize(); // Terminates MPI execution environment
return 0;
}

154
ex7/ex7_3/GCC_default.mk Normal file
View file

@ -0,0 +1,154 @@
# Basic Defintions for using GNU-compiler suite sequentially
# requires setting of COMPILER=GCC_
#startmake as follows to avoid warnings caused by OpenMPI code
# make 2>&1 | grep -v openmpi
MPI_ROOT=/usr/bin/
CC = ${MPI_ROOT}mpicc
CXX = ${MPI_ROOT}mpicxx
F77 = ${MPI_ROOT}mpif77
LINKER = ${CXX}
# If you 'mpirun ...' reports some error "... not enough slots .." then use the option '--oversubscribe'
MPIRUN = ${MPI_ROOT}mpirun --oversubscribe -display-map
#MPIRUN = ${MPI_ROOT}mpiexec
# 2023, Oct 23: ""WARNING: There is at least non-excluded one OpenFabrics device found,"
# solution according to https://github.com/open-mpi/ompi/issues/11063
MPIRUN += -mca btl ^openib
# KFU:sauron
CXXFLAGS += -I/software/boost/1_72_0/include
WARNINGS = -Wall -pedantic -Woverloaded-virtual -Wfloat-equal -Wshadow \
-Wredundant-decls -Wunreachable-code -Winline -fmax-errors=1
# WARNINGS += -Weffc++ -Wextra
# -Wno-pragmas
CXXFLAGS += -std=c++17 -ffast-math -O3 -march=native ${WARNINGS}
# -ftree-vectorizer-verbose=5 -DNDEBUG
# -ftree-vectorizer-verbose=2
# CFLAGS = -ffast-math -O3 -DNDEBUG -msse3 -fopenmp -fdump-tree-vect-details
# CFLAGS = -ffast-math -O3 -funroll-loops -DNDEBUG -msse3 -fopenmp -ftree-vectorizer-verbose=2
# info on vectorization
#VECTORIZE = -ftree-vectorize -fdump-tree-vect-blocks=foo.dump
#-fdump-tree-pre=stderr
VECTORIZE = -ftree-vectorize -fopt-info -ftree-vectorizer-verbose=5
#CXXFLAGS += ${VECTORIZE}
# -funroll-all-loops -msse3
#GCC -march=knl -march=broadwell -march=haswell
# for debugging purpose (save code)
# -fsanitize=leak # only one out the trhee can be used
# -fsanitize=address
# -fsanitize=thread
SANITARY = -fsanitize=address -fsanitize=undefined -fsanitize=null -fsanitize=return \
-fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow \
-fsanitize=bool -fsanitize=enum -fsanitize=vptr
#CXXFLAGS += ${SANITARY}
#LINKFLAGS +=${SANITARY}
# OpenMP
CXXFLAGS += -fopenmp
LINKFLAGS += -fopenmp
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
$(LINKER) $^ ${LINKFLAGS} -o $@
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
@rm -f ${PROGRAM} ${OBJECTS} gmon.out
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar *.orig
@rm -rf html latex
run: ${PROGRAM}
${MPIRUN} -np 4 ./$^
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
zip: clean
@echo "Zip the directory: " ${MY_DIR}
@cd .. ;\
zip -r ${MY_DIR}.zip ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
$(CXX) -c $(CXXFLAGS) -o $@ $<
# 2>&1 | grep -v openmpi
# special: get rid of compiler warnings genermaeate by openmpi-files
#.cpp.o:
# @$(CXX) -c $(CXXFLAGS) $< 2>/tmp/t.txt || grep -sv openmpi /tmp/t.txt
# |grep -sv openmpi
.c.o:
$(CC) -c $(CFLAGS) -o $@ $<
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# some tools
# Cache behaviour (CXXFLAGS += -g tracks down to source lines; no -pg in linkflags)
cache: ${PROGRAM}
valgrind --tool=callgrind --simulate-cache=yes ./$^
# kcachegrind callgrind.out.<pid> &
kcachegrind `ls -1tr callgrind.out.* |tail -1`
# Check for wrong memory accesses, memory leaks, ...
# use smaller data sets
# no "-pg" in compile/link options
mem: ${PROGRAM}
valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes mpirun -np 4 ./$^
# Graphical interface
# valkyrie
# Simple run time profiling of your code
# CXXFLAGS += -g -pg
# LINKFLAGS += -pg
prof: ${PROGRAM}
perf record ./$^
perf report
# gprof -b ./$^ > gp.out
# kprof -f gp.out -p gprof &
#Trace your heap:
#> heaptrack ./main.GCC_
#> heaptrack_gui heaptrack.main.GCC_.<pid>.gz
heap: ${PROGRAM}
heaptrack ./$^ 11
heaptrack_gui `ls -1tr heaptrack.$^.* |tail -1` &
codecheck: $(SOURCES)
cppcheck --enable=all --inconclusive --std=c++17 --suppress=missingIncludeSystem $^
########################################################################
# get the detailed status of all optimization flags
info:
echo "detailed status of all optimization flags"
$(CXX) --version
$(CXX) -Q $(CXXFLAGS) --help=optimizers

54
ex7/ex7_3/ex7_3/Makefile Executable file
View file

@ -0,0 +1,54 @@
#
# use GNU-Compiler tools
COMPILER=GCC_
# COMPILER=GCC_SEQ_
# alternatively from the shell
# export COMPILER=GCC_
# or, alternatively from the shell
# make COMPILER=GCC_
MAIN = main
SOURCES = ${MAIN}.cpp vector_operations.cpp
OBJECTS = $(SOURCES:.cpp=.o)
PROGRAM = ${MAIN}.${COMPILER}
# uncomment the next to lines for debugging and detailed performance analysis
CXXFLAGS += -g
# -DNDEBUG
# -pg slows down the code on my laptop when using CLANG_
LINKFLAGS += -g
#-pg
#CXXFLAGS += -Q --help=optimizers
#CXXFLAGS += -fopt-info
include ../${COMPILER}default.mk
#############################################################################
# additional specific cleaning in this directory
clean_all::
@rm -f t.dat*
#############################################################################
# special testing
# NPROCS = 4
#
TFILE = t.dat
# TTMP = t.tmp
#
graph: $(PROGRAM)
# @rm -f $(TFILE).*
# next two lines only sequentially
./$(PROGRAM)
@mv $(TFILE).000 $(TFILE)
# $(MPIRUN) $(MPIFLAGS) -np $(NPROCS) $(PROGRAM)
# @echo " "; echo "Manipulate data for graphics."; echo " "
# @cat $(TFILE).* > $(TTMP)
# @sort -b -k 2 $(TTMP) -o $(TTMP).1
# @sort -b -k 1 $(TTMP).1 -o $(TTMP).2
# @awk -f nl.awk $(TTMP).2 > $(TFILE)
# @rm -f $(TTMP).* $(TTMP) $(TFILE).*
#
-gnuplot jac.dem

107
ex7/ex7_3/ex7_3/main.cpp Normal file
View file

@ -0,0 +1,107 @@
#include <iostream>
#include <mpi.h>
#include <vector>
#include "vector_operations.h"
using namespace std;
int main(int argc , char **argv)
{
MPI_Init(&argc, &argv); // Initializes the MPI execution environment
MPI_Comm const icomm(MPI_COMM_WORLD);
int myrank;
MPI_Comm_rank(icomm, &myrank);
int n = 20;
vector<double> x(n);
vector<double> y = x;
for (int i = 0; i < n; ++i)
{
x[i] = myrank*100 + (i % 5)*10 + i;
y[i] = 1.0/(x[i]);
}
if(myrank == 0) // so scalar product is well defined (avoid division by 0)
y[0] = 0;
// -------------------- E5 --------------------
if (myrank == 0) cout << "-------------------- E5 --------------------" << endl;
DebugVector(x, icomm);
cout.flush();
MPI_Barrier(icomm);
// -------------------- E6 --------------------
if (myrank == 0) cout << "-------------------- E6 --------------------" << endl;
double scalar_product = par_scalar(x, y, icomm);
if (myrank == 0)
{
cout << "<x,y> = " << scalar_product << endl << endl;
}
cout.flush();
MPI_Barrier(icomm);
// -------------------- E7 --------------------
if (myrank == 0) cout << "-------------------- E7 --------------------" << endl;
double xmin, xmax;
par_minmax(x, xmin, xmax, icomm);
if (myrank == 0)
{
cout << "Global min: " << xmin << endl;
cout << "Global max: " << xmax << endl << endl;
}
cout.flush();
MPI_Barrier(icomm);
// -------------------- E8 --------------------
if (myrank == 0) cout << "-------------------- E8 --------------------" << endl;
vector<double> x_new(n);
cout.flush();
MPI_Barrier(icomm);
// All to all
if (myrank == 0) cout << "----- All to all -----" << endl;
auto sendbuf = x.data();
int sendcount = 5;
auto recvbuf = x_new.data();
int recvcount = 5;
MPI_Alltoall(sendbuf, sendcount, MPI_DOUBLE, recvbuf, recvcount, MPI_DOUBLE, icomm);
DebugVector(x_new, icomm);
cout.flush();
MPI_Barrier(icomm);
// All to all v
if (myrank == 0) cout << "----- All to all v -----" << endl;
int sendcounts[4] = {5, 5, 5, 5};
int senddispls[4] = {0, 5, 10, 15};
int rcvcounts[4] = {5, 5, 5, 5};
int rcvdispls[4] = {0, 5, 10, 15};
MPI_Alltoallv(x.data(), sendcounts, senddispls, MPI_DOUBLE, x_new.data(), rcvcounts, rcvdispls, MPI_DOUBLE, icomm);
DebugVector(x_new, icomm);
cout.flush();
MPI_Barrier(icomm);
// All to all (in place), sendcount and sendtype are ignored
if (myrank == 0) cout << "----- All to all (in place) -----" << endl;
MPI_Alltoall(MPI_IN_PLACE, sendcount, MPI_DOUBLE, x.data(), recvcount, MPI_DOUBLE, icomm);
DebugVector(x, icomm);
MPI_Finalize(); // Terminates MPI execution environment
return 0;
}

View file

@ -0,0 +1,110 @@
#include "vector_operations.h"
#include <cassert>
#include <cfloat>
void DebugVector(const vector<double> &xin, const MPI_Comm &icomm)
{
int myrank, numprocs;
MPI_Comm_rank(icomm, &myrank); // my MPI-rank
MPI_Comm_size(icomm, &numprocs); // #MPI processes
int ierr;
int n = xin.size();
int chosen_process;
for (int k = 0; k < numprocs; ++k)
{
MPI_Barrier(icomm);
if (myrank == 0)
{
cout << "Choose next process: ";
cin >> chosen_process;
}
ierr = MPI_Bcast(&chosen_process, 1, MPI_INT, 0, icomm); // broadcast value of "chosen_process" to all processes
assert(ierr == 0);
MPI_Barrier(icomm);
if (chosen_process == myrank)
{
for (int i = 0; i < n; ++i)
{
cout << "x_" << i << " = " << xin[i] << "\t(Process " << myrank << ")" << endl;
}
cout.flush();
}
}
return;
}
double par_scalar(const vector<double> &x, const vector<double> &y, const MPI_Comm &icomm)
{
int n = x.size();
assert(n == (int)y.size());
double sum = 0.0;
double local_sum = 0.0;
for (int i = 0; i < n; ++i)
{
local_sum += x[i]*y[i];
}
int ierr = MPI_Allreduce(&local_sum, &sum, 1, MPI_DOUBLE, MPI_SUM, icomm); // reduce local sums to global sum
assert(ierr == 0);
return sum;
}
void par_minmax(const vector<double> &x, double &global_min, double &global_max, const MPI_Comm &icomm)
{
int myrank, numprocs;
MPI_Comm_rank(icomm, &myrank); // my MPI-rank
MPI_Comm_size(icomm, &numprocs); // #MPI processes
int n = x.size();
double local_min = DBL_MAX;
double local_max = -DBL_MAX;
for (int i = 0; i < n; ++i)
{
if (x[i] < local_min)
local_min = x[i];
if (x[i] > local_max)
local_max = x[i];
}
vector<double> local_mins(numprocs);
vector<double> local_maxs(numprocs);
MPI_Gather(&local_min, 1, MPI_DOUBLE, local_mins.data(), 1, MPI_DOUBLE, 0, icomm);
MPI_Gather(&local_max, 1, MPI_DOUBLE, local_maxs.data(), 1, MPI_DOUBLE, 0, icomm);
if (myrank == 0)
{
global_min = DBL_MAX;
global_max = -DBL_MAX;
for (int i = 0; i < numprocs; ++i)
{
if (local_mins[i] < global_min)
global_min = local_mins[i];
if (local_maxs[i] > global_max)
global_max = local_maxs[i];
}
}
MPI_Bcast(&global_min, 1, MPI_DOUBLE, 0, icomm); // make sure every process is up to date
MPI_Bcast(&global_max, 1, MPI_DOUBLE, 0, icomm);
return;
}

View file

@ -0,0 +1,10 @@
#include <mpi.h>
#include <vector>
using namespace std;
void DebugVector(const vector<double> &xin, const MPI_Comm &icomm);
double par_scalar(const vector<double> &x, const vector<double> &y, const MPI_Comm &icomm);
void par_minmax(const vector<double> &x, double &global_min, double &global_max, const MPI_Comm &icomm);

154
ex7/ex7_4/GCC_default.mk Normal file
View file

@ -0,0 +1,154 @@
# Basic Defintions for using GNU-compiler suite sequentially
# requires setting of COMPILER=GCC_
#startmake as follows to avoid warnings caused by OpenMPI code
# make 2>&1 | grep -v openmpi
MPI_ROOT=/usr/bin/
CC = ${MPI_ROOT}mpicc
CXX = ${MPI_ROOT}mpicxx
F77 = ${MPI_ROOT}mpif77
LINKER = ${CXX}
# If you 'mpirun ...' reports some error "... not enough slots .." then use the option '--oversubscribe'
MPIRUN = ${MPI_ROOT}mpirun --oversubscribe -display-map
#MPIRUN = ${MPI_ROOT}mpiexec
# 2023, Oct 23: ""WARNING: There is at least non-excluded one OpenFabrics device found,"
# solution according to https://github.com/open-mpi/ompi/issues/11063
MPIRUN += -mca btl ^openib
# KFU:sauron
CXXFLAGS += -I/software/boost/1_72_0/include
WARNINGS = -Wall -pedantic -Woverloaded-virtual -Wfloat-equal -Wshadow \
-Wredundant-decls -Wunreachable-code -Winline -fmax-errors=1
# WARNINGS += -Weffc++ -Wextra
# -Wno-pragmas
CXXFLAGS += -std=c++17 -ffast-math -O3 -march=native ${WARNINGS}
# -ftree-vectorizer-verbose=5 -DNDEBUG
# -ftree-vectorizer-verbose=2
# CFLAGS = -ffast-math -O3 -DNDEBUG -msse3 -fopenmp -fdump-tree-vect-details
# CFLAGS = -ffast-math -O3 -funroll-loops -DNDEBUG -msse3 -fopenmp -ftree-vectorizer-verbose=2
# info on vectorization
#VECTORIZE = -ftree-vectorize -fdump-tree-vect-blocks=foo.dump
#-fdump-tree-pre=stderr
VECTORIZE = -ftree-vectorize -fopt-info -ftree-vectorizer-verbose=5
#CXXFLAGS += ${VECTORIZE}
# -funroll-all-loops -msse3
#GCC -march=knl -march=broadwell -march=haswell
# for debugging purpose (save code)
# -fsanitize=leak # only one out the trhee can be used
# -fsanitize=address
# -fsanitize=thread
SANITARY = -fsanitize=address -fsanitize=undefined -fsanitize=null -fsanitize=return \
-fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow \
-fsanitize=bool -fsanitize=enum -fsanitize=vptr
#CXXFLAGS += ${SANITARY}
#LINKFLAGS +=${SANITARY}
# OpenMP
CXXFLAGS += -fopenmp
LINKFLAGS += -fopenmp
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
$(LINKER) $^ ${LINKFLAGS} -o $@
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
@rm -f ${PROGRAM} ${OBJECTS} gmon.out
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar *.orig
@rm -rf html latex
run: ${PROGRAM}
${MPIRUN} -np 4 ./$^
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
zip: clean
@echo "Zip the directory: " ${MY_DIR}
@cd .. ;\
zip -r ${MY_DIR}.zip ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
$(CXX) -c $(CXXFLAGS) -o $@ $<
# 2>&1 | grep -v openmpi
# special: get rid of compiler warnings genereate by openmpi-files
#.cpp.o:
# @$(CXX) -c $(CXXFLAGS) $< 2>/tmp/t.txt || grep -sv openmpi /tmp/t.txt
# |grep -sv openmpi
.c.o:
$(CC) -c $(CFLAGS) -o $@ $<
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# some tools
# Cache behaviour (CXXFLAGS += -g tracks down to source lines; no -pg in linkflags)
cache: ${PROGRAM}
valgrind --tool=callgrind --simulate-cache=yes ./$^
# kcachegrind callgrind.out.<pid> &
kcachegrind `ls -1tr callgrind.out.* |tail -1`
# Check for wrong memory accesses, memory leaks, ...
# use smaller data sets
# no "-pg" in compile/link options
mem: ${PROGRAM}
valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes mpirun -np 4 ./$^
# Graphical interface
# valkyrie
# Simple run time profiling of your code
# CXXFLAGS += -g -pg
# LINKFLAGS += -pg
prof: ${PROGRAM}
perf record ./$^
perf report
# gprof -b ./$^ > gp.out
# kprof -f gp.out -p gprof &
#Trace your heap:
#> heaptrack ./main.GCC_
#> heaptrack_gui heaptrack.main.GCC_.<pid>.gz
heap: ${PROGRAM}
heaptrack ./$^ 11
heaptrack_gui `ls -1tr heaptrack.$^.* |tail -1` &
codecheck: $(SOURCES)
cppcheck --enable=all --inconclusive --std=c++17 --suppress=missingIncludeSystem $^
########################################################################
# get the detailed status of all optimization flags
info:
echo "detailed status of all optimization flags"
$(CXX) --version
$(CXX) -Q $(CXXFLAGS) --help=optimizers

View file

@ -0,0 +1,107 @@
# Basic Defintions for using INTEL-MPI with its compilers
# requires setting of COMPILER=ICC_NATIVE_
# MPI_ROOT should be defined by shell
# path to icpc is contained in $PATH
MPI_BIN = $(shell dirname `which icpc` | sed 's/bin\/intel64/mpi\/intel64\/bin/g')/
MPI_LIB = $(shell echo ${MPI_BIN} | sed 's/bin/lib/g')
# Intel-MPI wrappers used gcc as default !!
CC = ${MPI_BIN}mpicc -cc=icc
CXX = ${MPI_BIN}mpicxx -cxx=icpc
F77 = ${MPI_BIN}mpif77 -f77=ifort
LINKER = ${CXX}
MPIRUN = ${MPI_BIN}mpirun
WARNINGS = -Wall -Wextra -pedantic -Woverloaded-virtual -Wfloat-equal -Wshadow
# -Weffc++ -Wunreachable-code -Winline
CXXFLAGS += -O3 -fargument-noalias -DNDEBUG -std=c++17 ${WARNINGS} ${MPI_COMPILE_FLAGS}
CFLAGS += -O3 -fargument-noalias -DNDEBUG -Wall -Wextra -pedantic -Wfloat-equal \
-Wshadow ${MPI_COMPILE_FLAGS}
# -vec-report=3 -mkl
# -guide -parallel
# -guide-opts=string -guide-par[=n] -guide-vec[=n]
# -auto-p32 -simd
# use MKL by INTEL
LINKFLAGS += -mkl ${MPI_LINK_FLAGS}
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
$(LINKER) $^ ${LINKFLAGS} -o $@
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
rm -f ${PROGRAM} ${OBJECTS}
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar
run: ${PROGRAM}
(export LD_LIBRARY_PATH=${MPI_LIB}:${LD_LIBRARY_PATH} ;${MPIRUN} -np 4 ./$^ ${PROG_ARGS})
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
$(CXX) -c $(CXXFLAGS) -o $@ $<
.c.o:
$(CC) -c $(CFLAGS) -o $@ $<
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# # some tools
# # Cache behaviour (CXXFLAGS += -g tracks down to source lines)
# cache: ${PROGRAM}
# valgrind --tool=callgrind --simulate-cache=yes ./$^
# # kcachegrind callgrind.out.<pid> &
#
# # Check for wrong memory accesses, memory leaks, ...
# # use smaller data sets
# mem: ${PROGRAM}
# valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes ./$^
#
# # Simple run time profiling of your code
# # CXXFLAGS += -g -pg
# # LINKFLAGS += -pg
# prof: ${PROGRAM}
# ./$^
# gprof -b ./$^ > gp.out
# # kprof -f gp.out -p gprof &
#
mem: inspector
prof: amplifier
cache: amplifier
gap_par_report:
${CXX} -c -guide -parallel $(SOURCES) 2> gap.txt
# GUI for performance report
amplifier: ${PROGRAM}
${BINDIR}../vtune_amplifier_xe_2013/bin64/amplxe-gui &
# GUI for Memory and Thread analyzer (race condition)
inspector: ${PROGRAM}
# http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
${BINDIR}../inspector_xe_2013/bin64/inspxe-gui &

112
ex7/ex7_4/ICC_default.mk Normal file
View file

@ -0,0 +1,112 @@
# Basic Defintions for using INTEL compilers with OpenMPI headers and libraries
# requires setting of COMPILER=ICC_
# MPI_ROOT should be defined by shell
MPI_ROOT=/usr
CC = icc
CXX = icpc
F77 = ifort
LINKER = ${CXX}
MPIRUN = ${MPI_ROOT}/bin/mpirun
# no differences when C or C++ is used !! (always used options from mpicxx)
MPI_COMPILE_FLAGS = `${MPI_ROOT}/bin/mpicxx -showme:compile`
MPI_LINK_FLAGS = `${MPI_ROOT}/bin/mpicxx -showme:link`
# MPI_LINK_FLAGS = -pthread -L/usr/lib/openmpi/lib -lmpi_cxx -lmpi -lopen-rte -lopen-pal -ldl -Wl,--export-dynamic -lnsl -lutil -lm -ldl
WARNINGS = -Wall -Wextra -pedantic -Woverloaded-virtual -Wfloat-equal -Wshadow
# -Weffc++ -Wunreachable-code -Winline
CXXFLAGS += -O3 -std=c++17 -fargument-noalias -DNDEBUG ${WARNINGS} ${MPI_COMPILE_FLAGS}
CFLAGS += -O3 -fargument-noalias -DNDEBUG -Wall -Wextra -pedantic -Wfloat-equal \
-Wshadow ${MPI_COMPILE_FLAGS}
# -vec-report=3 -mkl
# -guide -parallel
# -guide-opts=string -guide-par[=n] -guide-vec[=n]
# -auto-p32 -simd
# use MKL by INTEL
LINKFLAGS += -mkl
# use MPI by Compiler
LINKFLAGS += ${MPI_LINK_FLAGS}
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
$(LINKER) $^ ${LINKFLAGS} -o $@
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
rm -f ${PROGRAM} ${OBJECTS}
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar
run: ${PROGRAM}
${MPIRUN} -np 4 ./$^
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
$(CXX) -c $(CXXFLAGS) -o $@ $<
.c.o:
$(CC) -c $(CFLAGS) -o $@ $<
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# # some tools
# # Cache behaviour (CXXFLAGS += -g tracks down to source lines)
# cache: ${PROGRAM}
# valgrind --tool=callgrind --simulate-cache=yes ./$^
# # kcachegrind callgrind.out.<pid> &
#
# # Check for wrong memory accesses, memory leaks, ...
# # use smaller data sets
# mem: ${PROGRAM}
# valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes ./$^
#
# # Simple run time profiling of your code
# # CXXFLAGS += -g -pg
# # LINKFLAGS += -pg
# prof: ${PROGRAM}
# ./$^
# gprof -b ./$^ > gp.out
# # kprof -f gp.out -p gprof &
#
mem: inspector
prof: amplifier
cache: amplifier
gap_par_report:
${CXX} -c -guide -parallel $(SOURCES) 2> gap.txt
# GUI for performance report
amplifier: ${PROGRAM}
${BINDIR}../vtune_amplifier_xe_2013/bin64/amplxe-gui &
# GUI for Memory and Thread analyzer (race condition)
inspector: ${PROGRAM}
# http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
${BINDIR}../inspector_xe_2013/bin64/inspxe-gui &

View file

@ -0,0 +1,128 @@
# Basic Defintions for using OpenMPI with CLANG compilers
# requires setting of COMPILER=OPENMPI_CLANG_
# Pass CLANG Compilers to the OpenMPI wrappers
# see: https://www.open-mpi.org/faq/?category=mpi-apps#override-wrappers-after-v1.0
EXPORT = export OMPI_CXX=clang++; export OMPI_CC=clang; export OMPI_mpifort=flang
CC = mpicc
CXX = mpicxx
F77 = mpifort
LINKER = ${CXX}
MPIRUN = ${MPI_BIN}mpirun
#http://clang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages
SILENCE_MPI = -Wno-weak-vtables -Wno-old-style-cast -Wno-cast-align -Wno-deprecated
SILENCE_MPI+= -Wno-sign-conversion -Wno-reserved-id-macro -Wno-c++98-compat-pedantic
SILENCE_MPI+= -Wno-zero-as-null-pointer-constant -Wno-source-uses-openmp
WARNINGS = -Weverything -Wno-c++98-compat -Wno-weak-vtables -ferror-limit=3 ${SILENCE_MPI}
#-fsyntax-only -Wdocumentation -Wconversion -Wshadow -Wfloat-conversion -pedantic
CXXFLAGS += -Ofast -std=c++17 ${WARNINGS}
#CXXFLAGS += -Ofast -std=c++17
# -ftrapv
#
CFLAGS += -Ofast -Weverything -ferror-limit=3 ${MPI_COMPILE_FLAGS}
# OpenMP
#CXXFLAGS += -fopenmp
#LINKFLAGS += -fopenmp
# tidy_check
SWITCH_OFF=,-readability-magic-numbers,-readability-redundant-control-flow,-readability-redundant-member-init
SWITCH_OFF+=,-readability-redundant-member-init,-readability-isolate-declaration
#READABILITY=,readability*${SWITCH_OFF}
#TIDYFLAGS = -checks=llvm-*,-llvm-header-guard -header-filter=.* -enable-check-profile -extra-arg="-std=c++17" -extra-arg="-fopenmp"
TIDYFLAGS = -checks=llvm-*,-llvm-header-guard${READABILITY} -header-filter=.* -enable-check-profile -extra-arg="-std=c++17" -extra-arg="-fopenmp"
#TIDYFLAGS += -checks='modernize*
MPI_COMPILE_FLAGS = `${MPI_BIN}mpicxx -showme:compile`
MPI_LINK_FLAGS = `${MPI_BIN}mpicxx -showme:link`
#TIDYFLAGS += ${MPI_COMPILE_FLAGS}
TIDYFLAGS += -extra-arg="-I/usr/lib/x86_64-linux-gnu/openmpi/include"
#check:
# echo ${MPI_COMPILE_FLAGS}
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
@( ${EXPORT}; $(LINKER) $^ ${LINKFLAGS} -o $@ )
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
rm -f ${PROGRAM} ${OBJECTS}
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar
codecheck: tidy_check
tidy_check:
clang-tidy ${SOURCES} ${TIDYFLAGS} -- ${SOURCES}
# see also http://clang-developers.42468.n3.nabble.com/Error-while-trying-to-load-a-compilation-database-td4049722.html
run: ${PROGRAM}
${MPIRUN} -np 4 ./$^ ${PROG_ARGS}
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
@( ${EXPORT}; $(CXX) -c $(CXXFLAGS) -o $@ $< )
.c.o:
@( ${EXPORT}; $(CC) -c $(CFLAGS) -o $@ $< )
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# # some tools
# # Cache behaviour (CXXFLAGS += -g tracks down to source lines)
# cache: ${PROGRAM}
# valgrind --tool=callgrind --simulate-cache=yes ./$^
# # kcachegrind callgrind.out.<pid> &
#
# # Check for wrong memory accesses, memory leaks, ...
# # use smaller data sets
# mem: ${PROGRAM}
# valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes ./$^
#
# # Simple run time profiling of your code
# # CXXFLAGS += -g -pg
# # LINKFLAGS += -pg
# prof: ${PROGRAM}
# ./$^
# gprof -b ./$^ > gp.out
# # kprof -f gp.out -p gprof &
#
mem: inspector
prof: amplifier
cache: amplifier
gap_par_report:
${CXX} -c -guide -parallel $(SOURCES) 2> gap.txt
# GUI for performance report
amplifier: ${PROGRAM}
${BINDIR}../vtune_amplifier_xe_2013/bin64/amplxe-gui &
# GUI for Memory and Thread analyzer (race condition)
inspector: ${PROGRAM}
# http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
${BINDIR}../inspector_xe_2013/bin64/inspxe-gui &

View file

@ -0,0 +1,107 @@
# Basic Defintions for using OpenMPI with Intel compilers
# requires setting of COMPILER=OPENMPI_ICC_
# Pass Intel Compilers to the OpenMPI wrappers
# see: https://www.open-mpi.org/faq/?category=mpi-apps#override-wrappers-after-v1.0
EXPORT = export OMPI_CXX=icpc; export OMPI_CC=icc; export OMPI_mpifort=ifort
CC = mpicc
CXX = mpicxx
F77 = mpifort
LINKER = ${CXX}
MPIRUN = ${MPI_BIN}mpirun
WARNINGS = -Wall -Wextra -pedantic -Woverloaded-virtual -Wfloat-equal -Wshadow
# -Weffc++ -Wunreachable-code -Winline
CXXFLAGS += -fast -fargument-noalias -DNDEBUG -std=c++17 ${WARNINGS}
CFLAGS += -O3 -fargument-noalias -DNDEBUG -Wall -Wextra -pedantic -Wfloat-equal -Wshadow
# -vec-report=3 -mkl
# -guide -parallel
# -guide-opts=string -guide-par[=n] -guide-vec[=n]
# -auto-p32 -simd
# use MKL by INTEL
LINKFLAGS += -O3 -mkl ${MPI_LINK_FLAGS}
# ipo: warning #11021: unresolved __GI_memset
# see: https://software.intel.com/en-us/articles/ipo-warning-11021-unresolved-symbols-referenced-a-dynamic-library
LINKFLAGS +=
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
@( ${EXPORT}; $(LINKER) $^ ${LINKFLAGS} -o $@ )
@echo
@echo "Start with : $(MPIRUN) -np num_proc $(MPIFLAGS) $(PROGRAM)"
@echo
clean:
rm -f ${PROGRAM} ${OBJECTS}
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar
run: ${PROGRAM}
${MPIRUN} -np 4 ./$^ ${PROG_ARGS}
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
@( ${EXPORT}; $(CXX) -c $(CXXFLAGS) -o $@ $< )
.c.o:
@( ${EXPORT}; $(CC) -c $(CFLAGS) -o $@ $< )
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# # some tools
# # Cache behaviour (CXXFLAGS += -g tracks down to source lines)
# cache: ${PROGRAM}
# valgrind --tool=callgrind --simulate-cache=yes ./$^
# # kcachegrind callgrind.out.<pid> &
#
# # Check for wrong memory accesses, memory leaks, ...
# # use smaller data sets
# mem: ${PROGRAM}
# valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=$^.addr.out --show-reachable=yes ./$^
#
# # Simple run time profiling of your code
# # CXXFLAGS += -g -pg
# # LINKFLAGS += -pg
# prof: ${PROGRAM}
# ./$^
# gprof -b ./$^ > gp.out
# # kprof -f gp.out -p gprof &
#
mem: inspector
prof: amplifier
cache: amplifier
gap_par_report:
${CXX} -c -guide -parallel $(SOURCES) 2> gap.txt
# GUI for performance report
amplifier: ${PROGRAM}
${BINDIR}../vtune_amplifier_xe_2013/bin64/amplxe-gui &
# GUI for Memory and Thread analyzer (race condition)
inspector: ${PROGRAM}
# http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
${BINDIR}../inspector_xe_2013/bin64/inspxe-gui &

View file

@ -0,0 +1,125 @@
# Use the MPI-wrappers from the PGI compiler suite.
# requires setting of COMPILER=PGI_MPI_
#
# requires
# sudo apt install librdmacm1
# Details for run time information
# export PGI_ACC_TIME=1
# unset PGI_ACC_TIME
# export PGI_ACC_NOTIFY=1
# export PGI_ACC_NOTIFY=3
# unset PGI_ACC_NOTIFY
PGI_PATH = /opt/pgi/linux86-64/2019/bin
#ifeq "$(HOSTNAME)" "mephisto.uni-graz.at"
# # mephisto
# PGI_PATH = /share/apps/pgi/linux86-64/2016/bin
#endif
#MPI_ROOT=${PGI_PATH}mpi/mpich/bin/
MPI_ROOT= ${PGI_PATH}/../mpi/openmpi-3.1.3/bin/
MPIRUN = ${MPI_ROOT}mpirun
CC = ${MPI_ROOT}mpicc
CXX = ${MPI_ROOT}mpicxx
#F77 = ${MPI_ROOT}mpif77
ifndef LINKER
LINKER = ${CC}
endif
LINKER = ${CXX}
WARNINGS = -Minform=warn
PGI_PROFILING += -Minfo=loop,vect,opt,intensity,mp,accel
#PGI_PROFILING += -Mprof=lines –Minfo=ccff
CXXFLAGS += -e3 -std=c++17 -fast ${PGI_PROFILING} ${WARNINGS} -Mnodepchk
CFLAGS += -fast ${PGI_PROFILING} ${WARNINGS} -Mnodepchk
#
# for OpenACC
# Target architecture (nvidia,host)
TA_ARCH = host
#TA_ARCH = nvidia,host
#TA_ARCH = -ta=nvidia:cc2+,cuda5.5,fastmath
#TA_ARCH = -acc -DNDEBUG -ta=nvidia:cc2+,cuda5.5,fastmath,keepgpu
#TA_ARCH = -acc -DNDEBUG -ta=nvidia:cc2+,fastmath,keepgpu
#,keepgpu
# CFLAGS = -O3 -ta=$(TA_ARCH)
#CFLAGS += -B -gopt $(TA_ARCH)
#CXXFLAGS += -B -gopt $(TA_ARCH)
# -Minfo=all
# libcudart.a is needed for direct CUDA calls
#LINKFLAGS = -gopt $(TA_ARCH) -L${BINDIR}../lib $(PGI_PROFILING)
# -lcudart
default: ${PROGRAM}
${PROGRAM}: ${OBJECTS}
$(LINKER) $^ ${LINKFLAGS} -o $@
clean:
rm -f ${PROGRAM} ${OBJECTS} *.gpu *gprof.out
clean_all:: clean
@rm -f *_ *~ *.bak *.log *.out *.tar
#run: clean ${PROGRAM}
run: ${PROGRAM}
${MPIRUN} -np 4 ${OPTIRUN} ./${PROGRAM}
# tar the current directory
MY_DIR = `basename ${PWD}`
tar: clean_all
@echo "Tar the directory: " ${MY_DIR}
@cd .. ;\
tar cf ${MY_DIR}.tar ${MY_DIR} *default.mk ;\
cd ${MY_DIR}
# tar cf `basename ${PWD}`.tar *
doc:
doxygen Doxyfile
#########################################################################
.cpp.o:
$(CXX) -c $(CXXFLAGS) -o $@ $<
.c.o:
$(CC) -c $(CFLAGS) -o $@ $<
.f.o:
$(F77) -c $(FFLAGS) -o $@ $<
##################################################################################################
# # some tools
# # Simple run time profiling of your code
# # CXXFLAGS += -g -pg
# # LINKFLAGS += -pg
# Profiling options PGI, see: pgcollect -help
CPU_PROF = -allcache
GPU_PROF = -cuda=gmem,branch,cc13 -cudainit
#GPU_PROF = -cuda=branch:cc20
#
PROF_FILE = pgprof.out
prof: ${PROGRAM}
# ./$^
# $(CUDA_HOME)/bin/nvvp &
# export LD_LIBRARY_PATH=/state/partition1/apps/pgi/linux86-64/12.9/lib:$LD_LIBRARY_PATH
${OPTIRUN} ${BINDIR}pgcollect $(GPU_PROF) ./$^
${OPTIRUN} ${BINDIR}pgprof -exe ./$^ $(PROF_FILE) &
# Memory checker (slooooow!!!):
# see doc at /usr/local/cuda/doc/cuda-memcheck.pdf
# mem: ${PROGRAM}
# $(CUDA_HOME)memcheck ./$^

Some files were not shown because too many files have changed in this diff Show more