heat_2d

2D stencil update with do parallel.

program main {
    int64 nx = 64
    int64 ny = 64
    int64 steps = 40
    real64 u[nx, ny]
    real64 unew[nx, ny]
    int64 i = 0
    int64 j = 0
    int64 step = 0

    do parallel i = 0, nx - 1 {
        do j = 0, ny - 1 {
            u[i, j] = 0.0
            unew[i, j] = 0.0
        }
    }

    do parallel i = 0, nx - 1 {
        u[i, 0] = 1.0
        u[i, ny - 1] = 1.0
    }
    do j = 0, ny - 1 {
        u[0, j] = 1.0
        u[nx - 1, j] = 1.0
    }

    do step = 0, steps - 1 {
        do parallel i = 1, nx - 2 {
            do j = 1, ny - 2 {
                unew[i, j] = 0.25 * (u[i - 1, j] + u[i + 1, j] + u[i, j - 1] + u[i, j + 1])
            }
        }

        do parallel i = 1, nx - 2 {
            do j = 1, ny - 2 {
                u[i, j] = unew[i, j]
            }
        }
    }

    real64 center = u[nx / 2, ny / 2]
    real64 checksum = 0.0
    do i = 0, nx - 1 {
        do j = 0, ny - 1 {
            checksum = checksum + u[i, j]
        }
    }

    string summary = "heat_2d center=" + to_string(center) + " checksum=" + to_string(checksum) + "\n"
    write_file("/tmp/tgfortran_heat_2d.txt", summary)
    print(center, checksum)
}