VB Basic GPU Example

← All NMath Code Examples

 

Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Diagnostics

Imports CenterSpace.NMath.Core
Imports CenterSpace.NMath.Matrix

Namespace CenterSpace.NMath.Core.Examples.VisualBasic.GPU

  ' A .NET example in Visual Basic.
  Module BasicGPUExample

    Private Timer As New Stopwatch()

    Sub Main()

      ''''''''
      '' Out of the box example.
      ''''''/

      '' This causes NMath's dependent DLLs to be loaded into memory.  This isn't necessary, but with this call we can control
      '' when the DLLs are loaded.
      NMathConfiguration.Init()

      If BridgeManager.Instance.GPUCount = 0 Then

        Console.WriteLine("No installed GPU detected.")
        Console.WriteLine("Please install an NVIDIA GPU and re-run this example.")
        Console.WriteLine()
        Console.WriteLine( "Press Enter Key" )
        Console.Read()
        Return

      End If

      '' The BridgeManager controls the assignment of Bridges to hardware devices, either CPUs or GPUs, and controls the assignment
      '' of individual threads to particular devices.  
      Dim Writer As New StringWriter()

      '' Enable logging of routing to CPU/GPU
      BridgeManager.Instance.EnableLogging(Writer)

      '' Out of the box, NMath Premium will run large problems on the NVIDIA device 0 GPU (if installed) and small problems on the CPU.
      '' If no GPU is installed, all computation takes place on the CPU.  

      '' Perform two matrix multiplications one 100 x 100 and one 1400 x 1400
      '' The small one will run on the CPU and the large one on the GPU
      TimedMatrixMultiply(100)
      TimedMatrixMultiply(1400)

      '' Use the logging system to verify where these matrix multiplications ran. Note that dgemm is the LAPACK name for matrix multiplication.  
      '' 
      ''     Time                           tid   Device#  Function    Device Used  Reason
      ''     2014-03-04 01:18:53.210 PM      1       0       dgemm      CPU     Below threshold size n x m 100x100 = 10000 < 640000 (crossover 800)
      ''     2014-03-04 01:18:53.311 PM      1       0       dgemm      GPU     Above threshold size n x m 1400x1400 = 1960000 >= 64000 (crossover 800)
      Console.WriteLine(Writer)

      '' Note that the first matrix multiple ran on the CPU (under Device Used) because the size was "Below threshold" and the second larger
      '' multiple ran on the GPU because the size was "Above threshold".

      '' Disable logging
      BridgeManager.Instance.DisableLogging()
      Writer.Flush()

      ''''''''
      '' Tuning for optimal performance
      ''''''/

      '' For best performance the CPU/GPU bridge system needs to be tuned once.  Once tuned, the bridge can be stored and loaded again
      '' at run time. 

      '' Get the compute device assigned the device number 0.
      Dim Device0 As IComputeDevice = BridgeManager.Instance.GetComputeDevice(0)

      If Not (Device0 Is Nothing) Then

        '' Start with a default bridge
        Dim Bridge As Bridge = BridgeManager.Instance.NewDefaultBridge(Device0)

        '' Tune this bridge for the matrix multiply operation.  Calling TuneAll() would tune all GPU aware operations (for all precisions
        '' single, double, single complex, double complex) in NMath. Depending on the sizes used, this can be an expensive operation.
        '' It is intended to be run once during a calibration phase for a particular CPU/GPU combination. After completion, save
        '' the resulting routing model by persisting the bridge (see below).
        Bridge.Tune(BridgeFunctions.dgemm, Device0, 1200)

        '' Assign the tuned bridge to GPU device 0.  If you have multiple GPU's installed, this tuned bridge could be assigned to each of
        '' those devices.
        BridgeManager.Instance.SetBridge(Device0, Bridge)

        '' Run the matrix multiply again and dump the log file
        Writer = New System.IO.StringWriter()

        BridgeManager.Instance.EnableLogging(Writer)
        TimedMatrixMultiply(100)
        TimedMatrixMultiply(1400)
        Console.WriteLine(Writer)
        BridgeManager.Instance.DisableLogging()

        ''''''''
        '' Bridge Persistence
        ''''''/

        '' Persisting the bridge that was tuned above is done with the BridgeManager.
        '' Note that this overwrites any existing bridge with the same name.
        BridgeManager.Instance.SaveBridge(Bridge, ".\\MyTunedBridge")

        '' Then loading that bridge from disk is simple.
        Dim MyTunedBridge = BridgeManager.Instance.LoadBridge(".\\MyTunedBridge")

        '' To use the bridge, assign it to an available compute device.
        BridgeManager.Instance.SetBridge(Device0, MyTunedBridge)

		Console.WriteLine()
        Console.WriteLine( "Press Enter Key" )
        Console.Read()

      End If

    End Sub

    Private Sub TimedMatrixMultiply(Size As Integer)

      Console.Write("Matrix multiplication of size " & Size)
      Dim A As New DoubleMatrix(Size, Size, New RandGenUniform())
      Timer.Reset()
      Timer.Start()
      NMathFunctions.Product(A, A)
      Timer.Stop()
      Console.WriteLine(" (" & Timer.ElapsedMilliseconds & " ms)\n")

    End Sub

  End Module

End Namespace


← All NMath Code Examples
Top