Skip to content

Commit

Permalink
(hopefully) working MG with RB implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreu Glasmann committed Apr 28, 2016
1 parent a2f7c2d commit 635e8e9
Show file tree
Hide file tree
Showing 12 changed files with 197,335 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CC= gcc
RM= rm -vf
CFLAGS= -Wall -g -std=c99
OPENMPFLAG= -fopenmp
LIBS= -lrt -lm

PROGFILES= mg

main: serial_mg.o parallel_mg.o helpers.o main.c
$(CC) $(CFLAGS) $(OPENMPFLAG) serial_mg.o parallel_mg.o helpers.o main.c -o mg $(LIBS)

serial_mg.o: serial_mg.c serial_mg.h
$(CC) $(CFLAGS) -c serial_mg.c $(LIBS)

parallel_mg.o: parallel_mg.c parallel_mg.h
$(CC) $(CFLAGS) $(OPENMPFLAG) -c parallel_mg.c $(LIBS)

helpers.o: helpers.c helpers.h
$(CC) $(CFLAGS) $(OPENMPFLAG) -c helpers.c $(LIBS)

clean:
$(RM) *.o $(PROGFILES)
3 changes: 3 additions & 0 deletions helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "helpers.h"

int getIndex(int x, int y, int width) { return x*width + y; }
18 changes: 18 additions & 0 deletions helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef HELPERS_H
#define HELPERS_H

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct{
int N;
int Lmax;
int size[20];
double a[20];
double alpha;;
} param_t;

int getIndex(int x, int y, int width);

#endif
100 changes: 100 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*======================================================================================
Rich Brower Sat May 28 00:23:17 EDT 2011
C program based on simple FMV code of S. F. McCormick, Multigrid Methods, Frontiers in Applied! Mathematics,
vol. 3, SIAM Books, Philadelphia, 1987.The code is intentionally put into a single file with no input parameters
to keep is concise. Of course a full code may have more traditional C structures!
We solve the 2-d Laplace problem: A phi = b
(A phi)((x,y) = [4 phi(x,y) - phi(x+1,y) - phi(x-1,y) - phi(x,y+1) -phi(x,y+1)]/a^2 + m^2 phi(x,y) = b(x,y)
Multiply by scale = 1/(4 + a^2 m^2)
phi = (1 - scale*A) phi + scale*b = phi + res
= scale * (phi(x+1,y) + phi(x-1,y) + phi(x,y+1) + phi(x,y+1)) + a^2 scale*b(x,y)
where we use rescaled: res = a^2 scale b - scale* A phi
with scale(lev) = 1/(4 + pow(2,lev)^2 m^2) or the lattice spacing is a(lev) = pow(2,lev)
So the code is really sovling the new matrix iteration:
phi(x,y) = [(1- scale(lev) * A)phi](x,y) + res(x,y)
= scale(lev) * (phi(x+1,y) + phi(x-1,y) + phi(x,y+1) + phi(x,y+1)) + res
At present relex iteration uses Jacobi method.
======================================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "helpers.h"
#include "serial_mg.h"
#include "parallel_mg.h"
#include <omp.h>

double const_func(int x);
double sin_func(int x);
struct timespec diff(struct timespec start, struct timespec end);
int main(int argc, char **argv)
{
// struct timespec time1, time2, total;

// NON-PARALLEL
printf("Running Non-Parallelized Multigrid\n");
// clock_gettime(CLOCK_MONOTONIC, &time1);
double t1, t2;
double duration;
t1 = omp_get_wtime();
multigrid(7, sin_func);
//clock_gettime(CLOCK_MONOTONIC, &time2);
t2 = omp_get_wtime();
duration = (t2 - t1);
printf(" Total time: %f seconds\n", duration);

// PARALLEL
printf("Running Parallelized Multigrid\n");
// clock_gettime(CLOCK_MONOTONIC, &time1);
t1 = omp_get_wtime();
parallel_multigrid(7, sin_func);
//clock_gettime(CLOCK_MONOTONIC, &time2);
t2 = omp_get_wtime();
duration = (t2 - t1);
printf(" Total time: %f seconds\n", duration);

// NON-PARALLEL
// printf("Running Serial Multigrid\n");
// clock_gettime(CLOCK_MONOTONIC, &time1);
// serial_multigrid(7);
// clock_gettime(CLOCK_MONOTONIC, &time2);
// total = diff(time1, time2);
// printf(" Serial Total time: %ld.%ld seconds\n", total.tv_sec, total.tv_nsec);

return 0;
}

double const_func(int x) {
return 4.0;
}

double sin_func(int x) {
return sin( ( (double)(2.0 * 3.14) /256 ) * x );
}

/*
struct timespec diff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
*/
Loading

0 comments on commit 635e8e9

Please sign in to comment.