Skip to content
Snippets Groups Projects
Commit 5a45dbcb authored by Milan Jaros's avatar Milan Jaros
Browse files

Init

parent f8c27433
No related branches found
Tags v2.0.0-beta.4
No related merge requests found
# heatedplate
# 2D Steady State Heat Equation in a Rectangle
**HEATED\_PLATE** is a C++ program which solves the steady state heat
equation in a 2D rectangular region, and is intended as a starting point
for implementing an OpenMP parallel version.
Heated Plate solves the steady state heat equation in a 2D rectangular region.
The final estimate of the solution is written to a file in a format
suitable for display by GRID\_TO\_BMP.
The sequential version of this program needs approximately 18/epsilon
iterations to complete.
The physical region, and the boundary conditions, are suggested by this
diagram:
W = 0
+------------------+
| |
W = 100 | | W = 100
| |
+------------------+
W = 100
The region is covered with a grid of M by N nodes, and an N by N array W
is used to record the temperature. The correspondence between array
indices and locations in the region is suggested by giving the indices
of the four corners:
I = 0
[0][0]-------------[0][N-1]
| |
J = 0 | | J = N-1
| |
[M-1][0]-----------[M-1][N-1]
I = M-1
The steady state solution to the discrete heat equation satisfies the
following condition at an interior grid point:
> W\[Central\] = (1/4) \* ( W\[North\] + W\[South\] + W\[East\] +
> W\[West\] )
where "Central" is the index of the grid point, "North" is the index of
its immediate neighbor to the "north", and so on.
Given an approximate solution of the steady state heat equation, a
"better" solution is given by replacing each interior point by the
average of its 4 neighbors - in other words, by using the condition as
an ASSIGNMENT statement:
> W\[Central\] <= (1/4) \* ( W\[North\] + W\[South\] + W\[East\] +
> W\[West\] )
If this process is repeated often enough, the difference between
successive estimates of the solution will go to zero.
This program carries out such an iteration, using a tolerance specified
by the user, and writes the final estimate of the solution to a file
that can be used for graphic processing.
## STEP 0: Create git repository (10%)
Your code should be forked from this repository and hosted on code.it4i.cz as a private project with access for all teachers.
## STEP 1: Building the library (10%)
Provide compilation script for your application (the script should run independently on a current path). Script should load all necessary modules and call `cmake`.
## STEP 2: Analysis of the application (10%)
Use `Arm map` (module Forge) to analyze a sequential run of your application with given use case (`tests/large`). Identify the most time consuming regions that can be parallelized by OpenMP.
## STEP 3: Use OpenMP to run the application in parallel (10%)
Put OpenMP pragmas to a correct positions with appropriate variables visibility in order to utilize more threads effectively.
## STEP 4: Test the correctness of the code (10%)
Create script that automatically check correctness of your application for at least 3 different test cases. Comparison can be implemented as comparison of outputs of sequential and parallel runs.
## STEP 5: Test the behavior of the code on the Karolina cluster (40%)
1. Implement time measurement for all parallel regions using omp_get_wtime().
2. Create script for run strong scalability measurement (PBS script).
3. Evaluate strong scalability of measured regions up to 128 threads and different thread affinity (compact, scatter, balanced, none).
4. Evaluate behaviour for domain specific parameters of your applications (project dependent, maybe none). (Blocking implementation for this project)
5. Prepare charts for all measurements.
## STEP 6: Presentation of your project (10%)
Prepare presentation in form of slides (pptx, pdf). The slides should address all topics requested above.
#include "heated_plate.h"
//****************************************************************************80
void solve(int M, int N, double epsilon, const char *output_filename)
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for HEATED_PLATE.
//
// Discussion:
//
// This code solves the steady state heat equation on a rectangular region.
//
// The sequential version of this program needs approximately
// 18/epsilon iterations to complete.
//
//
// The physical region, and the boundary conditions, are suggested
// by this diagram;
//
// W = 0
// +------------------+
// | |
// W = 100 | | W = 100
// | |
// +------------------+
// W = 100
//
// The region is covered with a grid of M by N nodes, and an N by N
// array W is used to record the temperature. The correspondence between
// array indices and locations in the region is suggested by giving the
// indices of the four corners:
//
// I = 0
// [0][0]-------------[0][N-1]
// | |
// J = 0 | | J = N-1
// | |
// [M-1][0]-----------[M-1][N-1]
// I = M-1
//
// The steady state solution to the discrete heat equation satisfies the
// following condition at an interior grid point:
//
// W[Central] = (1/4) * ( W[North] + W[South] + W[East] + W[West] )
//
// where "Central" is the index of the grid point, "North" is the index
// of its immediate neighbor to the "north", and so on.
//
// Given an approximate solution of the steady state heat equation, a
// "better" solution is given by replacing each interior point by the
// average of its 4 neighbors - in other words, by using the condition
// as an ASSIGNMENT statement:
//
// W[Central] <= (1/4) * ( W[North] + W[South] + W[East] + W[West] )
//
// If this process is repeated often enough, the difference between successive
// estimates of the solution will go to zero.
//
// This program carries out such an iteration, using a tolerance specified by
// the user, and writes the final estimate of the solution to a file that can
// be used for graphic processing.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 July 2008
//
// Author:
//
// Original C version by Michael Quinn.
// C++ version by John Burkardt.
//
// Reference:
//
// Michael Quinn,
// Parallel Programming in C with MPI and OpenMP,
// McGraw-Hill, 2004,
// ISBN13: 978-0071232654,
// LC: QA76.73.C15.Q55.
//
// Parameters:
//
// Commandline argument 1, double EPSILON, the error tolerance.
//
// Commandline argument 2, char *OUTPUT_FILENAME, the name of the file into which
// the steady state solution is written when the program has completed.
//
// Local parameters:
//
// Local, double DIFF, the norm of the change in the solution from one iteration
// to the next.
//
// Local, double MEAN, the average of the boundary values, used to initialize
// the values of the solution in the interior.
//
// Local, double U[M][N], the solution at the previous iteration.
//
// Local, double W[M][N], the solution computed at the latest iteration.
//
{
double diff;
FILE* fp;
int i;
int iterations;
int iterations_print;
int j;
double mean;
ofstream output;
int success;
double **u;
double **w;
//Alloc memory
w = new double* [M];
u = new double* [M];
for (i = 0; i < M; i++)
{
w[i] = new double[N];
u[i] = new double[N];
}
cout << "\n";
cout << "HEATED_PLATE\n";
cout << " C++ version\n";
cout << " A program to solve for the steady state temperature distribution\n";
cout << " over a rectangular plate.\n";
cout << "\n";
cout << " Spatial grid of " << M << " by " << N << " points.\n";
cout << "\n";
cout << " The iteration will be repeated until the change is <= "
<< epsilon << "\n";
diff = epsilon;
cout << "\n";
cout << " The steady state solution will be written to \" "
<< output_filename << "\".\n";
//
// Set the boundary values, which don't change.
//
for (i = 1; i < M - 1; i++)
{
w[i][0] = 100.0;
}
for (i = 1; i < M - 1; i++)
{
w[i][N - 1] = 100.0;
}
for (j = 0; j < N; j++)
{
w[M - 1][j] = 100.0;
}
for (j = 0; j < N; j++)
{
w[0][j] = 0.0;
}
//
// Average the boundary values, to come up with a reasonable
// initial value for the interior.
//
mean = 0.0;
for (i = 1; i < M - 1; i++)
{
mean = mean + w[i][0];
}
for (i = 1; i < M - 1; i++)
{
mean = mean + w[i][N - 1];
}
for (j = 0; j < N; j++)
{
mean = mean + w[M - 1][j];
}
for (j = 0; j < N; j++)
{
mean = mean + w[0][j];
}
mean = mean / (double)(2 * M + 2 * N - 4);
//
// Initialize the interior solution to the mean value.
//
for (i = 1; i < M - 1; i++)
{
for (j = 1; j < N - 1; j++)
{
w[i][j] = mean;
}
}
//
// iterate until the new solution W differs from the old solution U
// by no more than EPSILON.
//
iterations = 0;
iterations_print = 1;
cout << "\n";
cout << " Iteration Change\n";
cout << "\n";
while (epsilon <= diff)
{
//
// Save the old solution in U.
//
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
u[i][j] = w[i][j];
}
}
//
// Determine the new estimate of the solution at the interior points.
// The new solution W is the average of north, south, east and west neighbors.
//
diff = 0.0;
for (i = 1; i < M - 1; i++)
{
for (j = 1; j < N - 1; j++)
{
w[i][j] = (u[i - 1][j] + u[i + 1][j] + u[i][j - 1] + u[i][j + 1]) / 4.0;
if (diff < fabs(w[i][j] - u[i][j]))
{
diff = fabs(w[i][j] - u[i][j]);
}
}
}
iterations++;
if (iterations == iterations_print)
{
cout << " " << setw(8) << iterations
<< " " << diff << "\n";
iterations_print = 2 * iterations_print;
}
}
cout << "\n";
cout << " " << setw(8) << iterations
<< " " << diff << "\n";
cout << "\n";
cout << " Error tolerance achieved.\n";
cout << " CPU time = " << ctime << "\n";
//
// Write the solution to the output file.
//
output.open(output_filename);
output << M << "\n";
output << N << "\n";
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
output << " " << w[i][j];
}
output << "\n";
}
output.close();
cout << "\n";
cout << " Solution written to the output file \"" << output_filename << " \" \n";
//
// Terminate.
//
cout << "\n";
cout << "HEATED_PLATE:\n";
cout << " Normal end of execution.\n";
//Free memory
for (i = 0; i < M; i++)
{
delete[] w[i];
delete[] u[i];
}
delete[] w;
delete[] u;
}
//****************************************************************************80
#pragma once
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ctime>
#include <string>
using namespace std;
void solve(int M, int N, double epsilon, const char* output_filename);
\ No newline at end of file
#include "heated_plate.h"
int main(int argc, char* argv[])
{
int M = 128;
int N = 512;
double epsilon = 0.01;
const char* output_filename = "output.txt";
//Check args
if (argc < 2) {
printf("USAGE:\theatedplate 0 (tiny)\n\theatedplate 1 (large)\n");
return 0;
}
// Read Input
int type = atoi(argv[1]);
//tiny
if (type == 0) {
M = 128;
N = 512;
epsilon = 0.01;
}
//large
if (type == 1) {
M = 1024;
N = 2048;
epsilon = 0.001;
}
solve(M, N, epsilon, output_filename);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment