Skip to content

Commit c1a7909

Browse files
committed
Hello World!
1 parent aac8b07 commit c1a7909

14 files changed

+1351
-0
lines changed

Diff for: MPI_python.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from mpi4py import MPI
2+
3+
comm = MPI.COMM_WORLD
4+
print("%d of %d" % (comm.Get_rank(), comm.Get_size()))

Diff for: OMP_Fortran.f90

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Program Main
2+
!====================================================================
3+
! eigenvalues and eigenvectors of a real symmetric matrix
4+
! Method: calls Jacobi
5+
!====================================================================
6+
implicit none
7+
integer, parameter :: n=3
8+
double precision :: a(n,n), x(n,n)
9+
double precision, parameter:: abserr=1.0e-09
10+
integer i, j
11+
12+
! matrix A
13+
data (a(1,i), i=1,3) / 1.0, 2.0, 3.0 /
14+
data (a(2,i), i=1,3) / 2.0, 2.0, -2.0 /
15+
data (a(3,i), i=1,3) / 3.0, -2.0, 4.0 /
16+
17+
! print a header and the original matrix
18+
write (*,200)
19+
do i=1,n
20+
write (*,201) (a(i,j),j=1,n)
21+
end do
22+
23+
call Jacobi(a,x,abserr,n)
24+
25+
! print solutions
26+
write (*,202)
27+
write (*,201) (a(i,i),i=1,n)
28+
write (*,203)
29+
do i = 1,n
30+
write (*,201) (x(i,j),j=1,n)
31+
end do
32+
33+
200 format (' Eigenvalues and eigenvectors (Jacobi method) ',/, &
34+
' Matrix A')
35+
201 format (6f12.6)
36+
202 format (/,' Eigenvalues')
37+
203 format (/,' Eigenvectors')
38+
end program main
39+
40+
subroutine Jacobi(a,x,abserr,n)
41+
!===========================================================
42+
! Evaluate eigenvalues and eigenvectors
43+
! of a real symmetric matrix a(n,n): a*x = lambda*x
44+
! method: Jacoby method for symmetric matrices
45+
! Alex G. (December 2009)
46+
!-----------------------------------------------------------
47+
! input ...
48+
! a(n,n) - array of coefficients for matrix A
49+
! n - number of equations
50+
! abserr - abs tolerance [sum of (off-diagonal elements)^2]
51+
! output ...
52+
! a(i,i) - eigenvalues
53+
! x(i,j) - eigenvectors
54+
! comments ...
55+
!===========================================================
56+
implicit none
57+
integer i, j, k, n
58+
double precision a(n,n),x(n,n)
59+
double precision abserr, b2, bar
60+
double precision beta, coeff, c, s, cs, sc
61+
62+
! initialize x(i,j)=0, x(i,i)=1
63+
! *** the array operation x=0.0 is specific for Fortran 90/95
64+
x = 0.0
65+
do i=1,n
66+
x(i,i) = 1.0
67+
end do
68+
69+
! find the sum of all off-diagonal elements (squared)
70+
b2 = 0.0
71+
do i=1,n
72+
do j=1,n
73+
if (i.ne.j) b2 = b2 + a(i,j)**2
74+
end do
75+
end do
76+
77+
if (b2 <= abserr) return
78+
79+
! average for off-diagonal elements /2
80+
bar = 0.5*b2/float(n*n)
81+
82+
do while (b2.gt.abserr)
83+
do i=1,n-1
84+
do j=i+1,n
85+
if (a(j,i)**2 <= bar) cycle ! do not touch small elements
86+
b2 = b2 - 2.0*a(j,i)**2
87+
bar = 0.5*b2/float(n*n)
88+
! calculate coefficient c and s for Givens matrix
89+
beta = (a(j,j)-a(i,i))/(2.0*a(j,i))
90+
coeff = 0.5*beta/sqrt(1.0+beta**2)
91+
s = sqrt(max(0.5+coeff,0.0))
92+
c = sqrt(max(0.5-coeff,0.0))
93+
! recalculate rows i and j
94+
do k=1,n
95+
cs = c*a(i,k)+s*a(j,k)
96+
sc = -s*a(i,k)+c*a(j,k)
97+
a(i,k) = cs
98+
a(j,k) = sc
99+
end do
100+
! new matrix a_{k+1} from a_{k}, and eigenvectors
101+
do k=1,n
102+
cs = c*a(k,i)+s*a(k,j)
103+
sc = -s*a(k,i)+c*a(k,j)
104+
a(k,i) = cs
105+
a(k,j) = sc
106+
cs = c*x(k,i)+s*x(k,j)
107+
sc = -s*x(k,i)+c*x(k,j)
108+
x(k,i) = cs
109+
x(k,j) = sc
110+
end do
111+
end do
112+
end do
113+
end do
114+
return
115+
end

Diff for: OpenMP_C.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<stdio.h>
2+
3+
int main( int ac, char **av)
4+
5+
{
6+
7+
#pragma omp parallel // specify the code between the curly brackets is part of an OpenMP parallel section.
8+
9+
{
10+
11+
printf("Hello World!!!\n");
12+
13+
}
14+
15+
return 0;
16+
17+
}

Diff for: gauss_seidel.c

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* L-20 MCS 572 Fri 7 Oct 2016 : gauss_seidel.c
2+
* This program runs the method of Gauss-Seidel on a test system A*x = b,
3+
* where A is diagonally dominant and the exact solution consists
4+
* of all ones. The dimension can be supplied at the command line.
5+
* This program is an adaption of the method of Jacobi of L-19,
6+
* observe the improved convergence rate of Gauss-Seidel. */
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
11+
void test_system
12+
( int n, double **A, double *b );
13+
/*
14+
* Given n on entry,
15+
* On return is an n-by-n matrix A
16+
* with n+1 on the diagonal and 1 elsewhere.
17+
* The elements of the right hand side b
18+
* all equal 2*n, so the exact solution x
19+
* to A*x = b is a vector of ones. */
20+
21+
void run_gauss_seidel_method
22+
( int n, double **A, double *b,
23+
double epsilon, int maxit,
24+
int *numit, double *x );
25+
/*
26+
* Runs the method of Gauss-Seidel for A*x = b.
27+
*
28+
* ON ENTRY :
29+
* n the dimension of the system;
30+
* A an n-by-n matrix A[i][i] /= 0;
31+
* b an n-dimensional vector;
32+
* epsilon accuracy requirement;
33+
* maxit maximal number of iterations;
34+
* x start vector for the iteration.
35+
*
36+
* ON RETURN :
37+
* numit number of iterations used;
38+
* x approximate solution to A*x = b. */
39+
40+
int main ( int argc, char *argv[] )
41+
{
42+
int n,i;
43+
if(argc > 1)
44+
n = atoi(argv[1]);
45+
else
46+
{
47+
printf("give the dimension : ");
48+
scanf("%d",&n);
49+
}
50+
{
51+
double *b;
52+
b = (double*) calloc(n,sizeof(double));
53+
double **A;
54+
A = (double**) calloc(n,sizeof(double*));
55+
for(i=0; i<n; i++)
56+
A[i] = (double*) calloc(n,sizeof(double));
57+
test_system(n,A,b);
58+
double *x;
59+
x = (double*) calloc(n,sizeof(double));
60+
/* we start at an array of all zeroes */
61+
for(i=0; i<n; i++) x[i] = 0.0;
62+
double eps = 1.0e-4;
63+
int maxit = 2*n*n;
64+
int cnt = 0;
65+
run_gauss_seidel_method(n,A,b,eps,maxit,&cnt,x);
66+
printf("computed %d iterations\n",cnt);
67+
double sum = 0.0;
68+
for(i=0; i<n; i++) /* compute the error */
69+
{
70+
double d = x[i] - 1.0;
71+
sum += (d >= 0.0) ? d : -d;
72+
}
73+
printf("error : %.3e\n",sum);
74+
}
75+
return 0;
76+
}
77+
78+
void test_system
79+
( int n, double **A, double *b )
80+
{
81+
int i,j;
82+
for(i=0; i<n; i++)
83+
{
84+
b[i] = 2.0*n;
85+
for(j=0; j<n; j++) A[i][j] = 1.0;
86+
A[i][i] = n + 1.0;
87+
}
88+
}
89+
90+
void run_gauss_seidel_method
91+
( int n, double **A, double *b,
92+
double epsilon, int maxit,
93+
int *numit, double *x )
94+
{
95+
double *dx = (double*) calloc(n,sizeof(double));
96+
int i,j,k;
97+
98+
for(k=0; k<maxit; k++)
99+
{
100+
double sum = 0.0;
101+
for(i=0; i<n; i++)
102+
{
103+
dx[i] = b[i];
104+
for(j=0; j<n; j++)
105+
dx[i] -= A[i][j]*x[j];
106+
dx[i] /= A[i][i]; x[i] += dx[i];
107+
sum += ( (dx[i] >= 0.0) ? dx[i] : -dx[i]);
108+
}
109+
printf("%4d : %.3e\n",k,sum);
110+
if(sum <= epsilon) break;
111+
}
112+
*numit = k+1; free(dx);
113+
}

Diff for: gaussseidel.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def gaussSeidel(A, b, x, N, tol):
2+
maxIterations = 1000000
3+
xprev = [0.0 for i in range(N)]
4+
for i in range(maxIterations):
5+
for j in range(N):
6+
xprev[j] = x[j]
7+
for j in range(N):
8+
summ = 0.0
9+
for k in range(N):
10+
if (k != j):
11+
summ = summ + A[j][k] * x[k]
12+
x[j] = (b[j] - summ) / A[j][j]
13+
diff1norm = 0.0
14+
oldnorm = 0.0
15+
for j in range(N):
16+
diff1norm = diff1norm + abs(x[j] - xprev[j])
17+
oldnorm = oldnorm + abs(xprev[j])
18+
if oldnorm == 0.0:
19+
oldnorm = 1.0
20+
norm = diff1norm / oldnorm
21+
if (norm < tol) and i != 0:
22+
print("Sequence converges to [", end="")
23+
for j in range(N - 1):
24+
print(x[j], ",", end="")
25+
print(x[N - 1], "]. Took", i + 1, "iterations.")
26+
return
27+
print("Doesn't converge.")
28+
29+
matrix2 = [[3.0, 1.0], [2.0, 6.0]]
30+
vector2 = [5.0, 9.0]
31+
guess = [0.0, 0.0]
32+
33+
matrix3 = [[9.0, -3.0], [-2.0, 8.0]]
34+
vector3 = [6.0, -4.0]
35+
36+
matrix4 = [[1.0, -3.0], [-2.0, 8.0]]
37+
38+
gaussSeidel(matrix2, vector2, guess, 2, 0.00000000000001)
39+
gaussSeidel(matrix3, vector3, guess, 2, 0.00000000000001)
40+
gaussSeidel(matrix4, vector3, guess, 2, 0.00000000000001)

Diff for: gs_seq_mpi.c

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <math.h>
5+
#include "mpi.h"
6+
7+
#define MAX_ITER 100
8+
#define MAX 100 //maximum value of the matrix element
9+
#define TOL 0.000001
10+
11+
// Generate a random float number with the maximum value of max
12+
float rand_float(int max){
13+
return ((float)rand()/(float)(RAND_MAX)) * max;
14+
}
15+
16+
17+
// Allocate 2D matrix
18+
void allocate_init_2Dmatrix(float ***mat, int n, int m){
19+
int i, j;
20+
*mat = (float **) malloc(n * sizeof(float *));
21+
for(i = 0; i < n; i++) {
22+
(*mat)[i] = (float *)malloc(m * sizeof(float));
23+
for (j = 0; j < m; j++)
24+
(*mat)[i][j] = rand_float(MAX);
25+
}
26+
}
27+
28+
// solver
29+
void solver(float ***mat, int n, int m){
30+
float diff = 0, temp;
31+
int done = 0, cnt_iter = 0, i, j;
32+
33+
while (!done && (cnt_iter < MAX_ITER)){
34+
diff = 0;
35+
for (i = 1; i < n - 1; i++)
36+
for (j = 1; j < m - 1; j++){
37+
temp = (*mat)[i][j];
38+
(*mat)[i][j] = 0.2 * ((*mat)[i][j] + (*mat)[i][j - 1] + (*mat)[i - 1][j] + (*mat)[i][j + 1] + (*mat)[i + 1][j]);
39+
diff += abs((*mat)[i][j] - temp);
40+
}
41+
if (diff/n/n < TOL)
42+
done = 1;
43+
cnt_iter ++;
44+
}
45+
46+
if (done)
47+
printf("Solver converged after %d iterations\n", cnt_iter);
48+
else
49+
printf("Solver not converged after %d iterations\n", cnt_iter);
50+
51+
}
52+
53+
int main(int argc, char *argv[]) {
54+
int n, communication;
55+
float **a;
56+
57+
MPI_Init(&argc, &argv);
58+
59+
if (argc < 3) {
60+
printf("Call this program with two parameters: matrix_size communication \n");
61+
printf("\t matrix_size: Add 2 to a power of 2 (e.g. : 18, 1026)\n");
62+
printf("\t communication:\n");
63+
printf("\t\t 0: initial and final using point-to-point communication\n");
64+
printf("\t\t 1: initial and final using collective communication\n");
65+
66+
exit(1);
67+
}
68+
69+
n = atoi(argv[1]);
70+
communication = atoi(argv[2]);
71+
printf("Matrix size = %d communication = %d\n", n, communication);
72+
73+
allocate_init_2Dmatrix(&a, n, n);
74+
75+
double tsop = MPI_Wtime();
76+
solver(&a, n, n);
77+
double tfop = MPI_Wtime();
78+
79+
FILE *f;
80+
if (access("seq.csv", F_OK) == -1) {
81+
f = fopen("seq.csv", "a");
82+
fprintf(f, "Operations-time\n");
83+
}
84+
else {
85+
f = fopen("seq.csv", "a");
86+
}
87+
88+
fprintf(f, "%f;\n", tfop - tsop);
89+
fclose(f);
90+
91+
MPI_Finalize();
92+
return 0;
93+
}

0 commit comments

Comments
 (0)