YaccConstructor / Brahma.FSharp

F# quotation to OpenCL translator and respective runtime to utilize GPGPUs in F# applications.
http://yaccconstructor.github.io/Brahma.FSharp
Eclipse Public License 1.0
74 stars 17 forks source link

Run on macOS/Mono 5+ #81

Closed sergey-tihon closed 7 years ago

sergey-tihon commented 7 years ago

Description

Is possible to use Brahma.FSharp on macOS / Mono 5+?

Repro steps

  1. Compile and execute sample from Home page

Actual behavior

The error that I see (this probably could be more informative)

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'Brahma.Helpers.Timer`1' threw an exception. ---> System.EntryPointNotFoundException: QueryPerformanceFrequency
  at (wrapper managed-to-native) Brahma.Helpers.NativeMethods:QueryPerformanceFrequency (long&)
  at Brahma.Helpers.Timer`1[TMarker]..ctor () [0x0001c] in <40f78615313049f381a4f5be7149ca08>:0
  at Brahma.Helpers.Timer`1[TMarker]..cctor () [0x00000] in <40f78615313049f381a4f5be7149ca08>:0
   --- End of inner exception stack trace ---
  at BrahmaTest.main (System.String[] argv) [0x00030] in <595a5b72d4251104a7450383725b5a59>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Brahma.Helpers.Timer`1' threw an exception. ---> System.EntryPointNotFoundException
: QueryPerformanceFrequency
  at (wrapper managed-to-native) Brahma.Helpers.NativeMethods:QueryPerformanceFrequency (long&)
  at Brahma.Helpers.Timer`1[TMarker]..ctor () [0x0001c] in <40f78615313049f381a4f5be7149ca08>:0
  at Brahma.Helpers.Timer`1[TMarker]..cctor () [0x00000] in <40f78615313049f381a4f5be7149ca08>:0
   --- End of inner exception stack trace ---
  at BrahmaTest.main (System.String[] argv) [0x00030] in <595a5b72d4251104a7450383725b5a59>:0

Additional info

I did try to print platform details before execution of Main method.

    let (platforms, error) = OpenCL.Net.Cl.GetPlatformIDs()
    for pl in platforms do
        printfn "%A" pl
    printfn "Error %A" (error)

I this case I see the following error

Unhandled Exception:
System.DllNotFoundException: opencl.dll
  at (wrapper managed-to-native) OpenCL.Net.Cl:clGetPlatformIDs (uint,OpenCL.Net.Platform[],uint&)
  at OpenCL.Net.Cl.GetPlatformIDs (System.UInt32 numEntries, OpenCL.Net.Platform[] platforms, System.UInt32& numPlatforms) [0x00000] in <77a3ef23da094c79ba35fe25271dc2a6>:0
  at OpenCL.Net.Cl.GetPlatformIDs (OpenCL.Net.ErrorCode& error) [0x00000] in <77a3ef23da094c79ba35fe25271dc2a6>:0
  at BrahmaTest.main (System.String[] argv) [0x00017] in <595a5eb7d4251104a7450383b75e5a59>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.DllNotFoundException: opencl.dll
  at (wrapper managed-to-native) OpenCL.Net.Cl:clGetPlatformIDs (uint,OpenCL.Net.Platform[],uint&)
  at OpenCL.Net.Cl.GetPlatformIDs (System.UInt32 numEntries, OpenCL.Net.Platform[] platforms, System.UInt32& numPlatforms) [0x00000] in <77a3ef23da094c79ba35fe25271dc2a6>:0
  at OpenCL.Net.Cl.GetPlatformIDs (OpenCL.Net.ErrorCode& error) [0x00000] in <77a3ef23da094c79ba35fe25271dc2a6>:0
  at BrahmaTest.main (System.String[] argv) [0x00017] in <595a5eb7d4251104a7450383b75e5a59>:0

Related information

sergey-tihon commented 7 years ago

Tip 1

In order to fix System.DllNotFoundException: opencl.dll the OpenCL.Net.dll.config should exist near exe file with the following content, this actually allows map dll reference to macOS implementation.

<configuration>
  <dllmap os="osx" dll="opencl.dll" target="/System/Library/Frameworks/OpenCL.framework/OpenCL"/>
</configuration>

Tip 2

The current OpenCL platform name is Apple on macOS. So, the following code

    let (platforms, error) = OpenCL.Net.Cl.GetPlatformIDs()
    for pl in platforms do
        let (_,paramSize) = OpenCL.Net.Cl.GetPlatformInfo(pl, PlatformInfo.Name, System.IntPtr.Zero, InfoBuffer.Empty)
        use buffer = new InfoBuffer(paramSize)
        Cl.GetPlatformInfo(pl, PlatformInfo.Name, paramSize, buffer) |> ignore
        printfn "Platform=%A" (buffer)

    let provider = ComputeProvider.Create()//("Apple", DeviceType.Default)
    for d in provider.Devices do
        let getInfo (pName:DeviceInfo)=
            let (_,paramSize) = Cl.GetDeviceInfo(d, pName, System.IntPtr.Zero, InfoBuffer.Empty)
            use buffer = new InfoBuffer(paramSize)
            Cl.GetDeviceInfo(d, pName, paramSize, buffer) |> ignore
            buffer.ToString()
        printfn "Device=[Name=%s;\tDriverVersion=%A]"
            (getInfo DeviceInfo.Name)
            ((getInfo DeviceInfo.DriverVersion))

prints

Platform=Apple Device=[Name=Intel(R) HD Graphics 530; DriverVersion="1.2(Apr 22 2017 16:00:49)"] Device=[Name=AMD Radeon Pro 460 Compute Engine; DriverVersion="1.2 (Apr 27 2017 22:10:57)"]

Tip 3

Static ctor of Brahma.Helpers.Timer<TMarker> do P/Invoke calls to Kernel32.dll, so this Timer cannot be used on macOS and lines (like below) should be commented

        Timer<string>.Global.Start()
        ...
        Timer<string>.Global.Lap("OpenCL")
sergey-tihon commented 7 years ago

When I commented all calls to Timer<string>.Global I see the different error: (Brahma cannot compile command Expr to OpenCL kernel)

Multiplying two 200x200 matrices 10 times using .NET...
done.
Multiplying two 200x200 matrices 10 times using OpenCL and selected platform/device : [Platform: Apple, device type:Gpu]
[Platform: Apple, device type:Gpu]
 ...

Unhandled Exception:
System.Exception:

  at Brahma.FSharp.OpenCL.Core.ComputeProvider.CompileQuery[T] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr lambda, System.Collections.Generic.IEnume
rable`1[T] translatorOptions, Microsoft.FSharp.Collections.FSharpList`1[T] additionalSources) [0x00153] in <594a167316e8481da745038373164a59>:0
  at Brahma.FSharp.OpenCL.Core.ComputeProvider.Compile[TRange,a] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr`1[T] query, Microsoft.FSharp.Core.FShar
pOption`1[T] _options, Microsoft.FSharp.Core.FSharpOption`1[T] translatorOptions, Microsoft.FSharp.Core.FSharpOption`1[T] _outCode, Microsoft.FSharp.Core.FSharpOption`1[T] kernelNa
me, Microsoft.FSharp.Core.FSharpOption`1[T] _additionalSources) [0x0002d] in <594a167316e8481da745038373164a59>:0
  at BrahmaTest.Main (System.String platformName, System.Single[] m1, System.Single[] m2) [0x00219] in <595a6f50d4251104a7450383506f5a59>:0
  at BrahmaTest.main (System.String[] argv) [0x001d3] in <595a6f50d4251104a7450383506f5a59>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception:

  at Brahma.FSharp.OpenCL.Core.ComputeProvider.CompileQuery[T] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr lambda, System.Collections.Generic.IEnume
rable`1[T] translatorOptions, Microsoft.FSharp.Collections.FSharpList`1[T] additionalSources) [0x00153] in <594a167316e8481da745038373164a59>:0
  at Brahma.FSharp.OpenCL.Core.ComputeProvider.Compile[TRange,a] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr`1[T] query, Microsoft.FSharp.Core.FShar
pOption`1[T] _options, Microsoft.FSharp.Core.FSharpOption`1[T] translatorOptions, Microsoft.FSharp.Core.FSharpOption`1[T] _outCode, Microsoft.FSharp.Core.FSharpOption`1[T] kernelNa
me, Microsoft.FSharp.Core.FSharpOption`1[T] _additionalSources) [0x0002d] in <594a167316e8481da745038373164a59>:0
  at BrahmaTest.Main (System.String platformName, System.Single[] m1, System.Single[] m2) [0x00219] in <595a6f50d4251104a7450383506f5a59>:0
  at BrahmaTest.main (System.String[] argv) [0x001d3] in <595a6f50d4251104a7450383506f5a59>:0

Note: This is strange that stack trace does not have line numbers (:0 all the time), this makes troubleshooting more complicated. I guess that it could be fixed if you ship pdb files together with dlls inside NuGet package.

sergey-tihon commented 7 years ago

Error message after executing new sample on macOS using Brahma.FSharp v1.1.2

Multiplying two 300x300 matrices 10 times using .NET...
done.
Multiplying two 300x300 matrices 10 times using OpenCL and selected platform/device : [Platform: Apple, device type:Gpu]
[Platform: Apple, device type:Gpu]
 ...

Unhandled Exception:
System.Exception:

Error code: InvalidBuildOptions
  at Brahma.FSharp.OpenCL.Core+getProgramProcessingError@42TT[a,b].Invoke (a errCode, OpenCL.Net.Program program) [0x000c4] in <595cbbd516e8481da7450383d5bb5c59>:0
  at Microsoft.FSharp.Core.FSharpFunc`2[T,TResult].InvokeFast[V] (Microsoft.FSharp.Core.FSharpFunc`2[T,TResult] func, T arg1, TResult arg2) [0x0000d] in <5939249c904cf4daa74503839c243959>:0
  at Brahma.FSharp.OpenCL.Core.ComputeProvider.CompileQuery[T] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr lambda, System.Collections.Generic.IEnumerable`1[T] translatorOptions, Microsoft.FSharp.Collections.FSharpList`1[T] additionalSources) [0x0010e] in <595cbbd516e8481da7450383d5bb5c59>:0
  at Brahma.FSharp.OpenCL.Core.ComputeProvider.Compile[TRange,a] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr`1[T] query, Microsoft.FSharp.Core.FSharpOption`1[T] _options, Microsoft.FSharp.Core.FSharpOption`1[T] translatorOptions, Microsoft.FSharp.Core.FSharpOption`1[T] _outCode, Microsoft.FSharp.Core.FSharpOption`1[T] kernelName, Microsoft.FSharp.Core.FSharpOption`1[T] _additionalSources) [0x0002d] in <595cbbd516e8481da7450383d5bb5c59>:0
  at BrahmaTest.Main (System.String platformName, System.Int32 mSize) [0x0022d] in <596b094ed4251104a74503834e096b59>:0
  at BrahmaTest.main (System.String[] argv) [0x0000d] in <596b094ed4251104a74503834e096b59>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception:

Error code: InvalidBuildOptions
  at Brahma.FSharp.OpenCL.Core+getProgramProcessingError@42TT[a,b].Invoke (a errCode, OpenCL.Net.Program program) [0x000c4] in <595cbbd516e8481da7450383d5bb5c59>:0
  at Microsoft.FSharp.Core.FSharpFunc`2[T,TResult].InvokeFast[V] (Microsoft.FSharp.Core.FSharpFunc`2[T,TResult] func, T arg1, TResult arg2) [0x0000d] in <5939249c904cf4daa74503839c243959>:0
  at Brahma.FSharp.OpenCL.Core.ComputeProvider.CompileQuery[T] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr lambda, System.Collections.Generic.IEnumerable`1[T] translatorOptions, Microsoft.FSharp.Collections.FSharpList`1[T] additionalSources) [0x0010e] in <595cbbd516e8481da7450383d5bb5c59>:0
  at Brahma.FSharp.OpenCL.Core.ComputeProvider.Compile[TRange,a] (Brahma.OpenCL.ComputeProvider this, Microsoft.FSharp.Quotations.FSharpExpr`1[T] query, Microsoft.FSharp.Core.FSharpOption`1[T] _options, Microsoft.FSharp.Core.FSharpOption`1[T] translatorOptions, Microsoft.FSharp.Core.FSharpOption`1[T] _outCode, Microsoft.FSharp.Core.FSharpOption`1[T] kernelName, Microsoft.FSharp.Core.FSharpOption`1[T] _additionalSources) [0x0002d] in <595cbbd516e8481da7450383d5bb5c59>:0
  at BrahmaTest.Main (System.String platformName, System.Int32 mSize) [0x0022d] in <596b094ed4251104a74503834e096b59>:0
  at BrahmaTest.main (System.String[] argv) [0x0000d] in <596b094ed4251104a74503834e096b59>:0
gsvgit commented 7 years ago

@sergey-tihon Thanks for additional informatuion. Seems, that it was a problem with extra spaces in compiler options. Fixed in Brahma.FSharp v1.1.3.

sergey-tihon commented 7 years ago
2017-07-16_1626

Woohoo !!! Thank you for help @gsvgit .

Brahma.FSharp works on macOS now!!!