VB Task 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 CenterSpace.NMath.Core
Imports CenterSpace.NMath.Matrix

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

  ' A .NET example in Visual Basic.
  Module TaskControlExample

    Sub Main()

      ''''''''
      '' Routing work routines to devices.
      ''''''/

      '' Using the BridgeManager it's easy to route individual threaded tasks to a given device.  Remember that compute devices encompass both the CPU
      '' and all installed GPU's. In this example we run a task on the CPU in parallel with a task on the GPU.  If multiple GPU's are installed the
      '' same technique can be used to offload tasks to each of the GPUs to leverage all installed hardware in parallel.

      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 an 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
      Dim Writer As New StringWriter()

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

      '' Build the task array and assign matrix multiply jobs to those tasks.  Any number of tasks can be
      '' added here and any number of tasks can be assigned to a particular device.   Note that the task to 
      '' device assignment is done in each task (below).
      Dim Tasks(1) As Task
      Tasks(0) = Task.Factory.StartNew(Sub() TimedMatrixMultiplyCPU(1299))
      Tasks(1) = Task.Factory.StartNew(Sub() TimedMatrixMultiplyGPU(1301))

      '' Block until all tasks complete
      Task.WaitAll(Tasks)

      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-04 02:23:01.915 PM      4       0       dgemm            GPU     Above threshold size n x m 1301x1301 = 1692601 >= 640000 (crossover 800)
      '' 2014-03-04 02:23:01.915 PM      5       -1      dgemm            CPU     All computations routed to CPU
      ''
      '' The log file shows that one matrix multiply (1301 x 1301) was run on the GPU and a second was run on the CPU (always device -1).

      '' Disable logging
      BridgeManager.Instance.DisableLogging()

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

    End Sub

    Private Sub TimedMatrixMultiplyGPU(Size As Integer)

      '' Place this task on the GPU.
      Dim Device As IComputeDevice = BridgeManager.Instance.GetComputeDevice(0)
      BridgeManager.Instance.SetComputeDevice(Device)

      Dim Timer As New Stopwatch()
      Dim A As New DoubleMatrix(Size, Size, 0, 1)
      Timer.Reset()
      Timer.Start()
      NMathFunctions.Product(A, A)
      Timer.Stop()
      Console.WriteLine("Finished matrix multiplication of size " & Size & " on the GPU  (" & Timer.ElapsedMilliseconds & " ms).")
      Console.WriteLine()

    End Sub

    Private Sub TimedMatrixMultiplyCPU(Size As Integer)

      '' Place this task on the CPU.
      BridgeManager.Instance.SetComputeDevice(BridgeManager.Instance.CPU)

      Dim Timer As New Stopwatch()
      Dim A As New DoubleMatrix(Size, Size, 0, 1)
      Timer.Reset()
      Timer.Start()
      NMathFunctions.Product(A, A)
      Timer.Stop()
      Console.WriteLine("Finished matrix multiplication of size " & Size & " on the CPU  (" & Timer.ElapsedMilliseconds & " ms).")
      Console.WriteLine()

    End Sub

  End Module
End Namespace


← All NMath Code Examples
Top