Skip to content

Commit a208d99

Browse files
committed
Naive parallel CPU processing.
1 parent 9c0f7de commit a208d99

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/ImageProcessing/ImageProcessing.fs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,36 @@ let applyFilter (filter: float32[][]) (img: byte[,]) =
168168
Array2D.mapi (fun x y _ -> byte (processPixel x y)) img
169169

170170

171+
172+
let applyFilterCpuParallel (filter: float32[][]) (img: byte[,]) =
173+
let imgH = img.GetLength 0
174+
let imgW = img.GetLength 1
175+
176+
let img =
177+
[|
178+
let height = img.GetLength 0
179+
for row in 0..height-1 do
180+
yield img.[row,*]
181+
|]
182+
183+
let filterD = (Array.length filter) / 2
184+
185+
let filter = Array.concat filter
186+
187+
let processPixel px py =
188+
let dataToHandle = [|
189+
for i in px - filterD .. px + filterD do
190+
for j in py - filterD .. py + filterD do
191+
if i < 0 || i >= imgH || j < 0 || j >= imgW
192+
then float32 <| img[px][py]
193+
else float32 <| img[i][j]
194+
|]
195+
196+
Array.fold2 (fun s x y -> s + x * y) 0.0f filter dataToHandle
197+
198+
Array.Parallel.mapi (fun x a -> Array.mapi (fun y _ -> byte (processPixel x y)) a ) img
199+
|> array2D
200+
171201
let applyFilterGPUKernel (clContext: ClContext) localWorkSize =
172202

173203

src/ImageProcessing/Main.fs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open Argu
44
open Argu.ArguAttributes
55
open Brahma.FSharp
66

7-
type Platforms = CPU = 1 | NVidia = 2 | IntelGPU = 3 | AnyGPU = 4
7+
type Platforms = CPU = 1 | CPUParallel = 2 | NVidia = 3 | IntelGPU = 4 | AnyGPU = 5
88

99
[<CliPrefix(CliPrefix.DoubleDash)>]
1010
[<NoAppSettings>]
@@ -48,7 +48,7 @@ module Main =
4848
ImageProcessing.edgesKernel
4949
]
5050

51-
51+
(*
5252
let applyFiltersOnGPU =
5353
let device =
5454
match platform with
@@ -64,7 +64,8 @@ module Main =
6464
6565
let context = ClContext(device)
6666
ImageProcessing.applyFiltersGPU context 64
67-
(*
67+
*)
68+
6869
match platform with
6970
| Platforms.CPU ->
7071
let mutable image = ImageProcessing.loadAs2DArray input
@@ -74,7 +75,16 @@ module Main =
7475
image <- ImageProcessing.applyFilter filter image
7576
printfn $"CPU processing time: {(System.DateTime.Now - start).TotalMilliseconds} ms"
7677
ImageProcessing.save2DByteArrayAsImage image output
77-
| _ ->
78+
| Platforms.CPUParallel ->
79+
let mutable image = ImageProcessing.loadAs2DArray input
80+
printfn $"Device: CPU, parallel"
81+
let start = System.DateTime.Now
82+
for filter in filters do
83+
image <- ImageProcessing.applyFilterCpuParallel filter image
84+
printfn $"CPU processing time: {(System.DateTime.Now - start).TotalMilliseconds} ms"
85+
ImageProcessing.save2DByteArrayAsImage image output
86+
87+
(*| _ ->
7888
let start = System.DateTime.Now
7989
let grayscaleImage = ImageProcessing.loadAsImage input
8090
printfn $"Image reading time: {(System.DateTime.Now - start).TotalMilliseconds} ms"
@@ -84,7 +94,8 @@ module Main =
8494
printfn $"GPU processing time: {(System.DateTime.Now - start).TotalMilliseconds} ms"
8595
printfn $"R: %A{result}"
8696
ImageProcessing.saveImage result output
87-
*)
97+
*)
98+
(*
8899
89100
let start = System.DateTime.Now
90101
@@ -97,5 +108,5 @@ module Main =
97108
- start)
98109
.TotalMilliseconds}"
99110
100-
111+
*)
101112
0

0 commit comments

Comments
 (0)