-
Notifications
You must be signed in to change notification settings - Fork 7
/
laplacian.cpp
67 lines (52 loc) · 1.38 KB
/
laplacian.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-FileCopyrightText: 2021 CSC - IT Center for Science Ltd. <www.csc.fi>
//
// SPDX-License-Identifier: MIT
#include <cstdio>
#include <omp.h>
#include <vector>
#define NX 4096
#define NY 4096
int main()
{
constexpr int niters = 50;
int nx = NX;
int ny = NY;
// Use static to ensure allocation from heap; allocation from stack can segfault
static double A[NX][NY];
static double L[NX][NY];
double dx = 1.0 / double(nx);
double dy = 1.0 / double(ny);
// Initialize arrays
double x = 0.0;
double y;
for (int i = 0; i < nx; i++)
{
y = 0.0;
for (int j = 0; j < ny; j++)
{
A[i][j] = x*x + y*y;
L[i][j] = 0.0;
y += dy;
}
x += dx;
}
double t0 = omp_get_wtime();
// Compute Laplacian
#pragma nounroll
for (int iter = 0; iter < niters; iter++)
for (int j = 1; j < ny-1; j++)
for (int i = 1; i < nx-1; i++)
L[i][j] = (A[i-1][j] - 2.0*A[i][j] + A[i+1][j]) / (dx*dx) +
(A[i][j-1] - 2.0*A[i][j] + A[i][j+1]) / (dy*dy);
double t1 = omp_get_wtime();
// Check the result
double meanL = 0.0;
for (int i = 1; i < nx-1; i++)
for (int j = 1; j < ny-1; j++)
meanL += L[i][j];
meanL /= ((nx - 1) * (ny - 1));
printf("Numerical solution %6.4f\n", meanL);
printf("Analytical solution %6.4f\n", 4.0);
printf("Time %7.4f\n", t1 - t0);
return 0;
}