VB Flow Control Example

← All NMath Code Examples

 

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

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

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

  ' A .NET example in Visual Basic.
  Module FlowControlExample

    Sub Main()

      ''''''''
      '' Forcing work threads to a compute device.
      ''''''/

      '' By default, the BridgeManager routes work to the most efficient device for the work issued.  However, sometimes you may want to
      '' force all computation on a particular thread to run on the CPU or on a particular GPU.  This is done by called the methods ForceCPU()
      '' or ForceGPU() available on the Bridge class.  Typically this would be used to offload work from the CPU to an available GPU.

      NMathConfiguration.Init()

      '' Check to see if a GPU exists
      If (BridgeManager.Instance.GPUCount = 0) Then

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

      End If

      '' Set up a string writer for logging
      Using Writer As New StringWriter

        '' Enable the CPU /GPU bridge logging
        BridgeManager.Instance.EnableLogging(Writer)

        '' Assign all work for thread 1 to the GPU - never falling back to the CPU
        Dim GPU0 As IComputeDevice = BridgeManager.Instance.GetComputeDevice(0)
        Dim BridgeGPU As Bridge = BridgeManager.Instance.GetBridge(GPU0)
        BridgeGPU.ForceGpuAll()
        BridgeManager.Instance.SetBridge(GPU0, BridgeGPU)

        '' Now run a matrix multiply on compute device 0.  Note that despite of the small size of
        '' of the operation it takes place on the GPU because of the ForceGPUAll().
        Dim Thread1 As New Thread(Sub() TimedMatrixMultiply(101))
        BridgeManager.Instance.SetComputeDevice(GPU0, Thread1)
        Thread1.Start()
        Thread1.Join()

        Console.WriteLine(Writer)
        '' 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-05 10:19:41.596 AM      4       0       dgemm         GPU     Above threshold size n x m 101x101 = 10201 >= 0 (crossover 0)

        '' Now revert back to the default bridge and re-run matrix multiply
        BridgeGPU = BridgeManager.Instance.GetBridge(GPU0)
        BridgeGPU.RestoreDefaultRouting()
        BridgeManager.Instance.SetBridge(GPU0, BridgeGPU)

        '' Now run a similar matrix multiply on compute device 0 again.
        Dim Thread2 As New Thread(Sub() TimedMatrixMultiply(102))
        BridgeManager.Instance.SetComputeDevice(GPU0, Thread2)
        Thread2.Start()
        Thread2.Join()

        Console.WriteLine(Writer)
        ''        Time                    tid   Device#  Function    Device Used                  Reason
        '' 2014-03-05 11:33:53.871 AM      4       0       dgemm          GPU     Above threshold size n x m 101x101 = 10201 >= 0 (crossover 0)
        '' 2014-03-05 11:33:54.127 AM      5       0       dgemm          CPU     Below threshold size n x m 102x102 = 10404 < 640000 (crossover 800)


        '' Disable logging
        BridgeManager.Instance.DisableLogging()

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

      End Using
    End Sub

    Private Sub TimedMatrixMultiply(Size As Integer)

      Dim Timer As New Stopwatch()
      Dim A As New DoubleMatrix(Size, Size, 0, 1)
      Timer.Reset()
      Timer.Start()
      NMathFunctions.Product(A, A)
      Timer.Stop()
      Dim TaskId = Thread.CurrentThread.ManagedThreadId
      Console.WriteLine()
      Console.WriteLine("Finished matrix multiplication on thread " & TaskId & " of size " & Size & " in (" & Timer.ElapsedMilliseconds & " ms).")
      Console.WriteLine()

    End Sub

  End Module

End Namespace



← All NMath Code Examples
Top