Skip to content

Commit 3914f15

Browse files
committed
Streaming of preloaded images.
1 parent f73afea commit 3914f15

File tree

5 files changed

+68
-140
lines changed

5 files changed

+68
-140
lines changed

src/ImageProcessing/ImageProcessing.fs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ let applyFilter (filter: float32[][]) (img: byte[,]) =
167167

168168
Array2D.mapi (fun x y _ -> byte (processPixel x y)) img
169169

170-
171-
172170
let applyFilterCpuParallel (filter: float32[][]) (img: byte[,]) =
173171
let imgH = img.GetLength 0
174172
let imgW = img.GetLength 1

src/ImageProcessing/ImageProcessing.fsproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<None Include="App.config" />
2020
<Compile Include="ImageProcessing.fs" />
2121
<Compile Include="Streaming.fs" />
22-
<Compile Include="Matrices.fs" />
2322
<Compile Include="Main.fs" />
2423
</ItemGroup>
2524
<ItemGroup>

src/ImageProcessing/Main.fs

Lines changed: 24 additions & 10 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 | CPUParallel = 2 | NVidia = 3 | IntelGPU = 4 | AnyGPU = 5
7+
type Platforms = CPU = 1 | CPUParallel = 2 | Nvidia = 3 | IntelGPU = 4 | AnyGPU = 5
88

99
[<CliPrefix(CliPrefix.DoubleDash)>]
1010
[<NoAppSettings>]
@@ -13,7 +13,7 @@ type ImageProcessingArguments =
1313
| Output of string
1414
| Platform of Platforms
1515
| WorkGroupSize of uint
16-
| MatrixSize of uint
16+
| Stream of list<Platforms>
1717
with
1818
interface IArgParserTemplate with
1919
member arg.Usage =
@@ -22,7 +22,8 @@ type ImageProcessingArguments =
2222
| Output _ -> "File to store result."
2323
| Platform _ -> "Where to run."
2424
| WorkGroupSize _ -> "Work group size."
25-
| MatrixSize _ -> "Number of columns (or rows). We use square matrices."
25+
| Stream _ -> "Folder processing on multiple devices."
26+
2627
module Main =
2728
//let pathToExamples = "/home/gsv/Projects/TestProj2020/src/ImgProcessing/Examples"
2829
//let inputFolder = System.IO.Path.Combine(pathToExamples, "input")
@@ -40,32 +41,45 @@ module Main =
4041
let output = results.GetResult(Output, defaultValue = "out.jpg")
4142
let platform = results.GetResult(Platform, defaultValue = Platforms.CPU)
4243
let workGroupSize = results.GetResult(WorkGroupSize, defaultValue = 64u)
43-
let matrixSize = results.GetResult(MatrixSize, defaultValue = 512u)
44+
let devicesForStream = results.GetResult(Stream, defaultValue = [])
4445

4546
let filters = [
47+
ImageProcessing.gaussianBlurKernel
48+
ImageProcessing.gaussianBlurKernel
4649
ImageProcessing.gaussianBlurKernel
4750
ImageProcessing.gaussianBlurKernel
4851
ImageProcessing.edgesKernel
4952
]
5053

51-
(*
52-
let applyFiltersOnGPU =
54+
55+
let applyFiltersOnGPU platform =
5356
let device =
5457
match platform with
5558
| Platforms.AnyGPU -> ClDevice.GetFirstAppropriateDevice()
5659
| _ ->
5760
let platform =
5861
match platform with
59-
| Platforms.NVidia -> Platform.Nvidia
62+
| Platforms.Nvidia -> Platform.Nvidia
6063
| Platforms.IntelGPU -> Platform.Intel
6164
ClDevice.GetAvailableDevices(platform = platform)
6265
|> Seq.head
6366
printfn $"Device: %A{device.Name}"
6467

65-
let context = ClContext(device)
68+
let context = ClContext device
6669
ImageProcessing.applyFiltersGPU context 64
67-
*)
6870

71+
match devicesForStream with
72+
| hd :: tl ->
73+
let appliers = List.map applyFiltersOnGPU devicesForStream |> List.map (fun f -> f filters)
74+
let imagesToProcess =
75+
Streaming.listAllFiles input
76+
|> List.map (fun file -> Streaming.Img(ImageProcessing.loadAsImage file))
77+
let start = System.DateTime.Now
78+
Streaming.processAllLoadedFiles imagesToProcess appliers
79+
//Streaming.processAllFiles input output appliers
80+
printfn $"TotalTime = %f{(System.DateTime.Now - start).TotalMilliseconds}"
81+
82+
(*
6983
match platform with
7084
| Platforms.CPU ->
7185
let mutable image = ImageProcessing.loadAs2DArray input
@@ -83,7 +97,7 @@ module Main =
8397
image <- ImageProcessing.applyFilterCpuParallel filter image
8498
printfn $"CPU processing time: {(System.DateTime.Now - start).TotalMilliseconds} ms"
8599
ImageProcessing.save2DByteArrayAsImage image output
86-
100+
*)
87101
(*| _ ->
88102
let start = System.DateTime.Now
89103
let grayscaleImage = ImageProcessing.loadAsImage input

src/ImageProcessing/Matrices.fs

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/ImageProcessing/Streaming.fs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ type msg =
1010
| Img of Image
1111
| EOS of AsyncReplyChannel<unit>
1212

13-
let imgSaver outDir =
14-
let outFile (imgName: string) = System.IO.Path.Combine(outDir, imgName)
15-
13+
let imgSaver saveImage =
14+
1615
MailboxProcessor.Start(fun inbox ->
1716
let rec loop () = async {
1817
let! msg = inbox.Receive()
@@ -23,12 +22,13 @@ let imgSaver outDir =
2322
ch.Reply()
2423
| Img img ->
2524
printfn $"Save: %A{img.Name}"
26-
saveImage img (outFile img.Name)
25+
saveImage img
2726
return! loop ()
2827
}
2928
loop ()
3029
)
3130

31+
3232
let imgProcessor filterApplicator (imgSaver: MailboxProcessor<_>) =
3333
let filter = filterApplicator
3434
MailboxProcessor.Start(fun inbox ->
@@ -50,26 +50,62 @@ let imgProcessor filterApplicator (imgSaver: MailboxProcessor<_>) =
5050
)
5151

5252
let processAllFiles inDir outDir filterApplicators =
53+
5354
let mutable cnt = 0
5455

56+
let outFile (imgName: string) = System.IO.Path.Combine(outDir, imgName)
57+
58+
let saveImageToDir (img:Image) = saveImage img (outFile img.Name)
59+
5560
let imgProcessors =
5661
filterApplicators
5762
|> List.map (fun x ->
58-
let imgSaver = imgSaver outDir
63+
let imgSaver = imgSaver saveImageToDir
5964
imgProcessor x imgSaver
6065
)
6166
|> Array.ofList
6267

6368
let filesToProcess = listAllFiles inDir
6469

70+
while cnt < List.length filesToProcess do
71+
let p = (imgProcessors |> Array.minBy (fun p -> p.CurrentQueueLength))
72+
if p.CurrentQueueLength < 3
73+
then
74+
p.Post (Img(loadAsImage (filesToProcess[cnt])))
75+
cnt <- cnt + 1
76+
77+
(*
6578
for file in filesToProcess do
66-
//while (imgProcessors |> Array.minBy (fun p -> p.CurrentQueueLength)).CurrentQueueLength > 3 do ()
6779
(imgProcessors
6880
|> Array.minBy (fun p -> p.CurrentQueueLength))
6981
.Post(Img(loadAsImage file))
82+
*)
83+
84+
for imgProcessor in imgProcessors do
85+
imgProcessor.PostAndReply EOS
7086

71-
cnt <- cnt + 1
87+
88+
let processAllLoadedFiles inImages filterApplicators =
89+
let result = new ResizeArray<_>()
90+
91+
let mutable cnt = 0
92+
93+
let saveImageToArr (img:Image) = result.Add img
94+
95+
let imgProcessors =
96+
filterApplicators
97+
|> List.map (fun x ->
98+
let imgSaver = imgSaver saveImageToArr
99+
imgProcessor x imgSaver
100+
)
101+
|> Array.ofList
102+
103+
while cnt < List.length inImages do
104+
let p = (imgProcessors |> Array.minBy (fun p -> p.CurrentQueueLength))
105+
if p.CurrentQueueLength < 3
106+
then
107+
p.Post inImages[cnt]
108+
cnt <- cnt + 1
72109

73110
for imgProcessor in imgProcessors do
74111
imgProcessor.PostAndReply EOS
75-
//imgSaver.PostAndReply EOS

0 commit comments

Comments
 (0)