Skip to content

Commit 20c2322

Browse files
committed
add image support
Signed-off-by: Jade Abraham <jade.abraham@hpe.com>
1 parent 86d6b4e commit 20c2322

8 files changed

+83
-0
lines changed

01-heat-1D-serial.chpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ImageUtils;
2+
13
config const nx = 100000;
24
config const N = 10000;
35
const alpha = 0.1;
@@ -9,9 +11,12 @@ var u: [omega] real = 1.0;
911
u[nx/4..3*nx/4] = 2.0;
1012
var un = u;
1113

14+
writeImage(u);
15+
1216
for 1..N {
1317
un <=> u;
1418
for i in omegaHat do
1519
u[i] = un[i] + alpha *
1620
(un[i-1] - 2*un[i] + un[i+1]);
21+
writeImage(u);
1722
}

02-heat-1D-buggy.chpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ImageUtils;
2+
13
config const nx = 100000;
24
config const N = 10000;
35
const alpha = 0.1;
@@ -9,9 +11,12 @@ var u: [omega] real = 1.0;
911
u[nx/4..3*nx/4] = 2.0;
1012
var un = u;
1113

14+
writeImage(u);
15+
1216
for 1..N {
1317
un <=> u;
1418
for i in omega do
1519
u[i] = un[i] + alpha *
1620
(un[i-1] - 2*un[i] + un[i+1]);
21+
writeImage(u);
1722
}

03-heat-1D.chpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ImageUtils;
2+
13
config const nx = 100000;
24
config const N = 10000;
35
const alpha = 0.1;
@@ -9,9 +11,12 @@ var u: [omega] real = 1.0;
911
u[nx/4..3*nx/4] = 2.0;
1012
var un = u;
1113

14+
writeImage(u);
15+
1216
for 1..N {
1317
un <=> u;
1418
forall i in omegaHat do
1519
u[i] = un[i] + alpha *
1620
(un[i-1] - 2*un[i] + un[i+1]);
21+
writeImage(u);
1722
}

07-heat-1D-block.chpl

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use BlockDist;
2+
use ImageUtils;
23

34
config const nx = 100000;
45
config const N = 10000;
@@ -11,9 +12,12 @@ var u: [omega] real = 1.0;
1112
u[nx/4..3*nx/4] = 2.0;
1213
var un = u;
1314

15+
writeImage(u);
16+
1417
for 1..N {
1518
un <=> u;
1619
forall i in omegaHat do
1720
u[i] = un[i] + alpha *
1821
(un[i-1] - 2*un[i] + un[i+1]);
22+
writeImage(u);
1923
}

08-heat-2D.chpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ImageUtils;
2+
13
config const nx, ny = 1000;
24
config const N = 10000;
35
const alpha = 0.1;
@@ -9,11 +11,14 @@ var u: [omega] real = 1.0;
911
u[nx/4..nx/2, ny/4..ny/2] = 2.0;
1012
var un = u;
1113

14+
writeImage(u);
15+
1216
for 1..N {
1317
un <=> u;
1418
forall (i, j) in omegaHat do
1519
u[i, j] = un[i, j] + alpha * (
1620
un[i-1, j] + un[i, j-1] +
1721
un[i+1, j] + un[i, j+1] -
1822
4 * un[i, j]);
23+
writeImage(u);
1924
}

09-heat-2D-block.chpl

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use BlockDist;
2+
use ImageUtils;
23

34
config const nx, ny = 1000;
45
config const N = 10000;
@@ -11,11 +12,14 @@ var u: [omega] real = 1.0;
1112
u[nx/4..nx/2, ny/4..ny/2] = 2.0;
1213
var un = u;
1314

15+
writeImage(u);
16+
1417
for 1..N {
1518
un <=> u;
1619
forall (i, j) in omegaHat do
1720
u[i, j] = un[i, j] + alpha * (
1821
un[i-1, j] + un[i, j-1] +
1922
un[i+1, j] + un[i, j+1] -
2023
4 * un[i, j]);
24+
writeImage(u);
2125
}

10-heat-2D-stencil.chpl

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use StencilDist;
2+
use ImageUtils;
23

34
config const nx, ny = 1000;
45
config const N = 10000;
@@ -11,11 +12,14 @@ var u: [omega] real = 1.0;
1112
u[nx/4..nx/2, ny/4..ny/2] = 2.0;
1213
var un = u;
1314

15+
writeImage(u);
16+
1417
for 1..N {
1518
un <=> u;
1619
forall (i, j) in omegaHat do
1720
u[i, j] = un[i, j] + alpha * (
1821
un[i-1, j] + un[i, j-1] +
1922
un[i+1, j] + un[i, j+1] -
2023
4 * un[i, j]);
24+
writeImage(u);
2125
}

ImageUtils.chpl

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module ImageUtils {
2+
public use Image;
3+
4+
/* Controls whether an image is printed */
5+
config const image = false;
6+
/* A factor to scale the image by */
7+
config const imageScale = 1;
8+
/* The framerate of the video */
9+
config const framerate = 10;
10+
/* The height of the image, only applicable for 1D input data */
11+
config const imageHeight = 256;
12+
13+
private proc checkDimsHelper(width: int, height: int) {
14+
if height < 64 then
15+
halt("Image of minimum height 64 is required, got ", height);
16+
if width > 4000 then
17+
halt("Image of maximum width 4000 is required, got ", width);
18+
}
19+
private proc checkDims(data: [?d]) where d.isRectangular() && d.rank == 2 do
20+
checkDimsHelper(data.dim(1).size, data.dim(0).size);
21+
private proc checkDims(data: [?d]) where d.isRectangular() && d.rank == 1 do
22+
checkDimsHelper(d.size, imageHeight);
23+
24+
25+
private proc processFrameData(data: [?d]) where d.isRectangular() && d.rank == 1 {
26+
// make the 1d data into 2d data of a specific height
27+
var data2d: [0..#imageHeight, d.lowBound..d.highBound] data.eltType;
28+
for i in 0..#imageHeight do
29+
data2d[i,..] = data;
30+
31+
return processFrameData(data2d);
32+
}
33+
private proc processFrameData(data: [?d]) where d.isRectangular() && d.rank == 2 {
34+
// create a white border around the image and turn raw data into pixels
35+
var image: [data.domain.expand(1)] int = 0xFFFFFF;
36+
image[data.domain] = interpolateColor(data, 0x000000, 0x0000FF);
37+
38+
return scale(image, imageScale);
39+
}
40+
41+
proc writeImage(u: []) throws {
42+
if !image then return;
43+
44+
checkDims(u);
45+
46+
@functionStatic
47+
ref pipe = try! new mediaPipe("heat.mp4", imageType.bmp, framerate);
48+
49+
pipe.writeFrame(processFrameData(u));
50+
}
51+
}

0 commit comments

Comments
 (0)