-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mnt: re-arranged codes and added many new versions
Benchmarks should not rely on convergence, rather a fixed number of iterations is more appropriate. The problem of inconsistent iterations between the C and Fortran versions was also fixed because of an indexing error when calculating the cell coordinates (C is 0-based, F is 1-based). So they now run the same number of iterations, regardless of M. Note, this issue still persists in 2 and 3, but not in 4, I can't seem to figure out why. Moved timings about to make them more comparable. All different benchmarks write out the same format output to more easily compare them. The later versions are more realistic since they use allocate/malloc which is more appropriate. Heavily updated the README.md file to explain what is going on.
- Loading branch information
Showing
17 changed files
with
916 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include "c_common.h" | ||
|
||
// COMMENT | ||
// I know, this is extremely bad practice. | ||
// But to accommodate argument sizes for the arguments | ||
// I felt this was ok. | ||
// It makes it much more versatile when testing | ||
// END COMMENT | ||
void swap(int M, double (*a)[M], double (*b)[M]) | ||
{ | ||
double swp; | ||
for (int i = 0; i < M; i++) { | ||
for (int j = 0; j < M; j++) { | ||
swp = a[i][j]; | ||
a[i][j] = b[i][j]; | ||
b[i][j] = swp; | ||
} | ||
} | ||
} | ||
|
||
// See comment above | ||
double mmax(int M, double (*phi)[M], double (*phip)[M]) { | ||
double max = 0.0; | ||
double diff = 0.0; | ||
for (int i = 0; i < M; i++) { | ||
for (int j = 0; j < M; j++) { | ||
diff = fabs(phi[i][j]-phip[i][j]); | ||
if (diff > max) | ||
max = diff; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
double rho(double x, double y) { | ||
double s1 = 0.6; | ||
double e1 = 0.8; | ||
double s2 = 0.2; | ||
double e2 = 0.4; | ||
|
||
if (x > s1 && x < e1 && y > s1 && y < e1) { | ||
return 1.0; | ||
} else if (x > s2 && x < e2 && y > s2 && y < e2 ) { | ||
return -1.0; | ||
} else { | ||
return 0.0; | ||
} | ||
} | ||
|
||
clock_t run(int M, int N_ITER) { | ||
|
||
// COMMENT: | ||
// I wouldn't even imagine any novice in C would | ||
// do something like this. It really makes no sense :) | ||
// END OF COMMENT | ||
double (*phi)[M]; | ||
double (*phip)[M]; | ||
double (*rhoa)[M]; | ||
phi = malloc(sizeof(double[M][M])); | ||
phip = malloc(sizeof(double[M][M])); | ||
rhoa = malloc(sizeof(double[M][M])); | ||
|
||
clock_t tic = clock(); | ||
|
||
// initialize matrices | ||
memset(phip, 0, sizeof(phip[0][0])*M*M); | ||
memset(phi, 0, sizeof(phi[0][0])*M*M); | ||
|
||
double delta = 1.0; | ||
double a2 = pow(SPACE,2.0); | ||
|
||
int iter = 0; | ||
while (iter < N_ITER ) { | ||
iter += 1; | ||
|
||
for (int i=1; i < M-1; i++) { | ||
for (int j=1; j < M-1; j++) { | ||
phip[i][j] = (phi[i+1][j] + phi[i-1][j] + | ||
phi[i][j+1] + phi[i][j-1])/4.0 + | ||
a2/(4.0 * EPSILON0)*rho(i*SPACE,j*SPACE); | ||
} | ||
} | ||
delta = mmax(M, phi, phip); | ||
swap(M, phi, phip); | ||
|
||
} | ||
|
||
clock_t toc = clock(); | ||
|
||
free(phi); | ||
free(phip); | ||
free(rhoa); | ||
|
||
write_timing(M, iter, delta, toc - tic); | ||
} | ||
|
||
|
||
int main(int argc, char *argv[]) { | ||
|
||
int N, N_ITER; | ||
|
||
// Retrieve arguments | ||
parse_args(argc, argv, &N, &N_ITER); | ||
run(N, N_ITER); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include "c_common.h" | ||
|
||
// COMMENT | ||
// I know, this is extremely bad practice. | ||
// But to accommodate argument sizes for the arguments | ||
// I felt this was ok. | ||
// It makes it much more versatile when testing | ||
// END COMMENT | ||
void swap(int M, double a[M][M], double b[M][M]) { | ||
double swp; | ||
for (int i = 0; i < M; i++) { | ||
for (int j = 0; j < M; j++) { | ||
swp = a[i][j]; | ||
a[i][j] = b[i][j]; | ||
b[i][j] = swp; | ||
} | ||
} | ||
} | ||
|
||
// See comment above | ||
double mmax(int M, double a[M][M], double b[M][M]) { | ||
double max = 0.0; | ||
double diff = 0.0; | ||
for (int i = 0; i < M; i++) { | ||
for (int j = 0; j < M; j++) { | ||
diff = fabs(a[i][j]-b[i][j]); | ||
if (diff > max) | ||
max = diff; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
double rho(double x, double y) { | ||
double s1 = 0.6; | ||
double e1 = 0.8; | ||
double s2 = 0.2; | ||
double e2 = 0.4; | ||
|
||
if (x > s1 && x < e1 && y > s1 && y < e1) { | ||
return 1.0; | ||
} else if (x > s2 && x < e2 && y > s2 && y < e2 ) { | ||
return -1.0; | ||
} else { | ||
return 0.0; | ||
} | ||
} | ||
|
||
void run(int M, int N_ITER) { | ||
|
||
double phi[M][M]; | ||
double phip[M][M]; | ||
|
||
clock_t tic = clock(); | ||
int iter = 0; | ||
|
||
// initialize matrices | ||
memset(&phip[0][0], 0, sizeof(double)*M*M); | ||
memset(&phi[0][0], 0, sizeof(double)*M*M); | ||
|
||
double delta; | ||
double a2 = pow(SPACE,2.0); | ||
|
||
while (iter < N_ITER ) { | ||
iter += 1; | ||
|
||
for (int i=1; i < M-1; i++) { | ||
for (int j=1; j < M-1; j++) { | ||
phip[i][j] = (phi[i+1][j] + phi[i-1][j] + | ||
phi[i][j+1] + phi[i][j-1])/4.0 + | ||
a2/(4.0 * EPSILON0)*rho(i*SPACE,j*SPACE); | ||
} | ||
} | ||
delta = mmax(M, phi, phip); | ||
swap(M, phi, phip); | ||
|
||
} | ||
|
||
clock_t toc = clock(); | ||
|
||
write_timing(M, iter, delta, toc - tic); | ||
} | ||
|
||
|
||
int main(int argc, char *argv[]) { | ||
|
||
int N, N_ITER; | ||
|
||
// Retrieve arguments | ||
parse_args(argc, argv, &N, &N_ITER); | ||
|
||
run(N, N_ITER); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include "c_common.h" | ||
|
||
// COMMENT | ||
// I know, this is extremely bad practice. | ||
// But to accommodate argument sizes for the arguments | ||
// I felt this was ok. | ||
// It makes it much more versatile when testing | ||
// END COMMENT | ||
void swap(int M, double a[M][M], double b[M][M]) { | ||
double swp; | ||
for (int i = 0; i < M; i++) { | ||
for (int j = 0; j < M; j++) { | ||
swp = a[i][j]; | ||
a[i][j] = b[i][j]; | ||
b[i][j] = swp; | ||
} | ||
} | ||
} | ||
|
||
// See comment above | ||
double mmax(int M, double a[M][M], double b[M][M]) { | ||
double max = 0.0; | ||
double diff = 0.0; | ||
for (int i = 0; i < M; i++) { | ||
for (int j = 0; j < M; j++) { | ||
diff = fabs(a[i][j]-b[i][j]); | ||
if (diff > max) | ||
max = diff; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
double rho(double x, double y) { | ||
double s1 = 0.6; | ||
double e1 = 0.8; | ||
double s2 = 0.2; | ||
double e2 = 0.4; | ||
|
||
if (x > s1 && x < e1 && y > s1 && y < e1) { | ||
return 1.0; | ||
} else if (x > s2 && x < e2 && y > s2 && y < e2 ) { | ||
return -1.0; | ||
} else { | ||
return 0.0; | ||
} | ||
} | ||
|
||
void run(int M, int N_ITER) { | ||
|
||
double phi[M][M]; | ||
double phip[M][M]; | ||
double rhoa[M][M]; | ||
|
||
clock_t tic = clock(); | ||
int iter = 0; | ||
|
||
// initialize matrices | ||
memset(&phip[0][0], 0, sizeof(double)*M*M); | ||
memset(&phi[0][0], 0, sizeof(double)*M*M); | ||
for (int i=1; i<M-1; i++) { | ||
for (int j=1; j<M-1; j++) { | ||
rhoa[i][j] = rho(i*SPACE,j*SPACE); | ||
} | ||
} | ||
|
||
double delta; | ||
double a2 = pow(SPACE,2.0); | ||
|
||
while (iter < N_ITER ) { | ||
iter += 1; | ||
|
||
for (int i=1; i < M-1; i++) { | ||
for (int j=1; j < M-1; j++) { | ||
phip[i][j] = (phi[i+1][j] + phi[i-1][j] + | ||
phi[i][j+1] + phi[i][j-1])/4.0 + | ||
a2/(4.0 * EPSILON0)*rhoa[i][j]; | ||
} | ||
} | ||
delta = mmax(M, phi, phip); | ||
swap(M, phi, phip); | ||
|
||
} | ||
|
||
clock_t toc = clock(); | ||
|
||
write_timing(M, iter, delta, toc - tic); | ||
} | ||
|
||
|
||
int main(int argc, char *argv[]) { | ||
|
||
int N, N_ITER; | ||
|
||
// Retrieve arguments | ||
parse_args(argc, argv, &N, &N_ITER); | ||
|
||
run(N, N_ITER); | ||
} |
Oops, something went wrong.