Skip to content

GPU map2 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: gpu
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions MathNet.Numerics.sln
Original file line number Diff line number Diff line change
@@ -28,27 +28,27 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{B54A0B40-DE22-49FB-B1C0-6E5BDA3B0B2F}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
appveyor.yml = appveyor.yml
build.cmd = build.cmd
build.sh = build.sh
Directory.Build.props = Directory.Build.props
global.json = global.json
build\MathNet.Numerics.nuspec = build\MathNet.Numerics.nuspec
build\MathNet.Numerics.FSharp.nuspec = build\MathNet.Numerics.FSharp.nuspec
build\MathNet.Numerics.CUDA.Win.nuspec = build\MathNet.Numerics.CUDA.Win.nuspec
build\MathNet.Numerics.FSharp.nuspec = build\MathNet.Numerics.FSharp.nuspec
build\MathNet.Numerics.MKL.Linux-x64.nuspec = build\MathNet.Numerics.MKL.Linux-x64.nuspec
build\MathNet.Numerics.MKL.Linux-x86.nuspec = build\MathNet.Numerics.MKL.Linux-x86.nuspec
build\MathNet.Numerics.MKL.Linux.nuspec = build\MathNet.Numerics.MKL.Linux.nuspec
build\MathNet.Numerics.MKL.Win-x64.nuspec = build\MathNet.Numerics.MKL.Win-x64.nuspec
build\MathNet.Numerics.MKL.Win-x86.nuspec = build\MathNet.Numerics.MKL.Win-x86.nuspec
build\MathNet.Numerics.MKL.Win.nuspec = build\MathNet.Numerics.MKL.Win.nuspec
build\MathNet.Numerics.nuspec = build\MathNet.Numerics.nuspec
build\MathNet.Numerics.OpenBLAS.Win.nuspec = build\MathNet.Numerics.OpenBLAS.Win.nuspec
build\NativeProvider.targets = build\NativeProvider.targets
NuGet.config = NuGet.config
paket.dependencies = paket.dependencies
paket.lock = paket.lock
build.cmd = build.cmd
build.sh = build.sh
.gitignore = .gitignore
.gitattributes = .gitattributes
NuGet.config = NuGet.config
restore.cmd = restore.cmd
restore.sh = restore.sh
EndProjectSection
@@ -69,6 +69,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data.Text", "src\Data.Text\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data.Tests", "src\Data.Tests\Data.Tests.csproj", "{1FBB8988-3F40-4FCE-BEA8-2C705888804A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8EC462FD-D22E-90A8-E5CE-7E832BA40C5D}"
ProjectSection(SolutionItems) = preProject
kernels.fs = kernels.fs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
4 changes: 2 additions & 2 deletions src/FSharp.Tests/DenseMatrixGPUTests.fs
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ module DenseMatrixGPUTests =

[<Test>]
let ``DenseMatrixGPU.map2`` () =
GPU.Matrix.map2 id largeM largeM |> shouldEqual largeM
GPU.Matrix.map2 <@(+)@> largeM largeM |> shouldEqual (Matrix.map (fun x -> x + x) largeM)

[<Test>]
let ``DenseMatrixGPU.mXm`` () =
GPU.Matrix.mXm largeM largeM |> shouldEqual largeM
GPU.Matrix.mXm largeM largeM |> shouldEqual largeM
2 changes: 1 addition & 1 deletion src/FSharp.Tests/FSharp.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<GenerateProgramFile>false</GenerateProgramFile>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<TargetFrameworks>net8.0;</TargetFrameworks>
<AssemblyName>MathNet.Numerics.FSharp.Tests</AssemblyName>
<RootNamespace>MathNet.Numerics.Tests</RootNamespace>
<IsPackable>false</IsPackable>
9 changes: 7 additions & 2 deletions src/FSharp/FSharp.fsproj
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net8.0;net6.0;net48;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<AssemblyName>MathNet.Numerics.FSharp</AssemblyName>
<RootNamespace>MathNet.Numerics</RootNamespace>
<IsPackable>true</IsPackable>
@@ -17,11 +17,16 @@ many contributions, proper release notes with attributions will follow. thank yo
<PackageTags>fsharp F# math numeric statistics probability integration interpolation regression solve fit linear algebra matrix fft</PackageTags>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<NoWarn>2003;NU1604</NoWarn>
<NoWarn>2003;NU1604;NU1608</NoWarn>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Numerics\Numerics.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Brahma.FSharp">
<Version>2.0.6</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Statistics.fs" />
55 changes: 53 additions & 2 deletions src/FSharp/LinearAlgebra.Matrix.GPU.fs
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ namespace MathNet.Numerics.LinearAlgebra.GPU
open System
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra

open Brahma.FSharp

/// A module which implements functional matrix operations.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
@@ -42,7 +42,58 @@ module Matrix =
let iter f (v: #Matrix<_>) = v

/// !!!
let map2 f (v: #Matrix<_>) (w: #Matrix<_>) = v
let map2 (f: Quotations.Expr<'a -> 'b -> 'c>) (v: #Matrix<'a>) (w: #Matrix<'b>) =
let device = ClDevice.GetFirstAppropriateDevice() // ?
let context = RuntimeContext(device).ClContext

let n = v.RowCount
let m = v.ColumnCount

let m1_gpu = context.CreateClArray<_>(v.ToColumnMajorArray(), HostAccessMode.NotAccessible)
let m2_gpu = context.CreateClArray<_>(w.ToColumnMajorArray(), HostAccessMode.NotAccessible)
let m3_gpu =
context.CreateClArray(
n * m,
HostAccessMode.NotAccessible,
deviceAccessMode=DeviceAccessMode.WriteOnly,
allocationMode = AllocationMode.Default
)

let localWorkSize = 4

let kernel =
<@
fun (r: Range2D) (m1: ClArray<_>) (m2: ClArray<_>) (m3: ClArray<_>) ->
let i = r.GlobalID0
let j = r.GlobalID1

m3.[i * m + j] <- (%f) m1[i * m + j] m2[i * m + j]
@>

let kernel = context.Compile kernel

let ndRange =
Range2D(
n,
m,
localWorkSize,
localWorkSize
)

let commandQueue = context.QueueProvider.CreateQueue()
let kernel = kernel.GetKernel()

commandQueue.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange m1_gpu m2_gpu m3_gpu))
commandQueue.Post(Msg.CreateRunMsg<_, _> kernel)
let result : 'c[] = Array.zeroCreate(n * m)
let result = commandQueue.PostAndReply(fun ch -> Msg.CreateToHostMsg(m3_gpu, result, ch))
commandQueue.Post(Msg.CreateFreeMsg m1_gpu)

commandQueue.Post(Msg.CreateFreeMsg m2_gpu)

commandQueue.Post(Msg.CreateFreeMsg m3_gpu)

result |> DenseMatrix.raw n m

/// !!!
let mXm (v: #Matrix<_>) (w: #Matrix<_>) = v