-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathstencil.py
108 lines (95 loc) · 2.78 KB
/
stencil.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# Copyright 2021 NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import print_function
import argparse
import datetime
import math
from benchmark import run_benchmark
import cunumeric as np
def initialize(N):
print("Initializing stencil grid...")
grid = np.zeros((N + 2, N + 2))
grid[:, 0] = -273.15
grid[:, -1] = -273.15
grid[-1, :] = -273.15
grid[0, :] = 40.0
return grid
def run(grid, I, N): # noqa: E741
print("Running Jacobi stencil...")
center = grid[1:-1, 1:-1]
north = grid[0:-2, 1:-1]
east = grid[1:-1, 2:]
west = grid[1:-1, 0:-2]
south = grid[2:, 1:-1]
for i in range(I):
average = center + north + east + west + south
work = 0.2 * average
# delta = np.sum(np.absolute(work - center))
center[:] = work
total = np.sum(center)
# return total
return total / (N ** 2)
def run_stencil(N, I, timing): # noqa: E741
start = datetime.datetime.now()
grid = initialize(N)
average = run(grid, I, N)
# This will sync the timing because we will need to wait for the result
assert not math.isnan(average)
stop = datetime.datetime.now()
print("Average energy is %.8g" % average)
delta = stop - start
total = delta.total_seconds() * 1000.0
if timing:
print("Elapsed Time: " + str(total) + " ms")
return total
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--iter",
type=int,
default=100,
dest="I",
help="number of iterations to run",
)
parser.add_argument(
"-n",
"--num",
type=int,
default=100,
dest="N",
help="number of elements in one dimension",
)
parser.add_argument(
"-t",
"--time",
dest="timing",
action="store_true",
help="perform timing",
)
parser.add_argument(
"-b",
"--benchmark",
type=int,
default=1,
dest="benchmark",
help="number of times to benchmark this application (default 1 "
"- normal execution)",
)
args = parser.parse_args()
run_benchmark(
run_stencil, args.benchmark, "Stencil", (args.N, args.I, args.timing)
)