Skip to content

Commit

Permalink
Disposing functions are added.
Browse files Browse the repository at this point in the history
Description is enhanced.
  • Loading branch information
Excel-lent committed May 11, 2022
1 parent 81d29f3 commit 86ac67c
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 72 deletions.
133 changes: 110 additions & 23 deletions C#/ClooWrapperVBA/ProgramDevice.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Cloo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;

Expand Down Expand Up @@ -175,16 +174,42 @@ bool ExecuteAsync(ref int[] globalWorkOffset, ref int[] globalWorkSize, ref int[

#endregion GetArguments

#region Destructors

/// <summary>
/// Device type of used device ("GPU" / "CPU").
/// Disposes kernel memory variable.
/// </summary>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
[DispId(16)]
bool ReleaseMemObject(int argument_index);

/// <summary>
/// Disposes kernel.
/// </summary>
/// <returns>True, if the operation was successful, false otherwise.</returns>
[DispId(17)]
bool ReleaseKernel();

/// <summary>
/// Disposes ComputeProgram, ComputeCommandQueue and CommandQueue.
/// </summary>
/// <returns>True, if the operation was successful, false otherwise.</returns>
[DispId(18)]
bool ReleaseProgram();

#endregion Destructors

/// <summary>
/// Device type of used device ("GPU" / "CPU").
/// </summary>
[DispId(19)]
string DeviceType { get; set; }

/// <summary>
/// Error string.
/// </summary>
[DispId(17)]
[DispId(20)]
string ErrorString { get; set; }
}

Expand All @@ -193,7 +218,7 @@ bool ExecuteAsync(ref int[] globalWorkOffset, ref int[] globalWorkSize, ref int[
[ClassInterface(ClassInterfaceType.None)]
public class ProgramDevice : IProgramDevice
{
public ComputeProgram Prog;
public ComputeProgram ComputeProgram;
public ComputeContext ComputeContext;
public ComputeCommandQueue ComputeCommandQueue = null;
private ComputeKernel kernel;
Expand All @@ -212,7 +237,7 @@ public bool CreateKernel(string method)
{
try
{
kernel = Prog.CreateKernel(method);
kernel = ComputeProgram.CreateKernel(method);
variablePointers = new Dictionary<int, ComputeMemory>();
return true;
}
Expand Down Expand Up @@ -272,11 +297,11 @@ public bool Build(string sourceCode, string options, int platformIndex, int devi
return false;
}

Prog = new ComputeProgram(ComputeContext, sourceCode);
ComputeProgram = new ComputeProgram(ComputeContext, sourceCode);

try
{
Prog.Build(null, options, null, IntPtr.Zero);
ComputeProgram.Build(null, options, null, IntPtr.Zero);
}
catch (Exception e)
{
Expand All @@ -285,7 +310,7 @@ public bool Build(string sourceCode, string options, int platformIndex, int devi
return false;
}

buildLogs = Prog.GetBuildLog(ComputeContext.Devices[deviceTypeIndex]);
buildLogs = ComputeProgram.GetBuildLog(ComputeContext.Devices[deviceTypeIndex]);

return true;
}
Expand All @@ -295,7 +320,7 @@ public bool Build(string sourceCode, string options, int platformIndex, int devi
/// <summary>
/// Writes an array of type "Long" to the device.
/// </summary>
/// <param name="argument_index">The argument index.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="values">Array of "Long".</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool SetMemoryArgument_Long(int argument_index, ref int[] values)
Expand All @@ -320,7 +345,7 @@ public bool SetMemoryArgument_Long(int argument_index, ref int[] values)
/// <summary>
/// Writes an array of type "Single" to the device.
/// </summary>
/// <param name="argument_index">The argument index.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="values">Array of "Single".</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool SetMemoryArgument_Single(int argument_index, ref float[] values)
Expand All @@ -345,7 +370,7 @@ public bool SetMemoryArgument_Single(int argument_index, ref float[] values)
/// <summary>
/// Writes an array of type "Double" to the device.
/// </summary>
/// <param name="argument_index">The argument index.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="values">Array of "Double".</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool SetMemoryArgument_Double(int argument_index, ref double[] values)
Expand All @@ -370,7 +395,7 @@ public bool SetMemoryArgument_Double(int argument_index, ref double[] values)
/// <summary>
/// Sets "Long" argument to the kernel.
/// </summary>
/// <param name="argument_index">The argument index.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="value_long">Argument value as "Long".</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool SetValueArgument_Long(int argument_index, int value_long)
Expand All @@ -395,7 +420,7 @@ public bool SetValueArgument_Long(int argument_index, int value_long)
/// <summary>
/// Sets "Single" argument to the kernel.
/// </summary>
/// <param name="argument_index">The argument index.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="value_single">Argument value as "Single".</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool SetValueArgument_Single(int argument_index, float value_single)
Expand All @@ -420,7 +445,7 @@ public bool SetValueArgument_Single(int argument_index, float value_single)
/// <summary>
/// Sets "Double" argument to the kernel.
/// </summary>
/// <param name="argument_index">The argument index.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="value_double">Argument value as "Double".</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool SetValueArgument_Double(int argument_index, double value_double)
Expand Down Expand Up @@ -587,10 +612,10 @@ private bool InitGlobalArrays(ref int[] globalWorkOffset, ref int[] globalWorkSi
/// <summary>
/// Reads an array of type "Long" from the device.
/// </summary>
/// <param name="varIndex">0-based number of argument in argument list.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="values">Array of "Long".</param>
/// <returns>False in case of error/exception. Otherwise true.</returns>
public bool GetMemoryArgument_Long(int varIndex, ref int[] values)
public bool GetMemoryArgument_Long(int argument_index, ref int[] values)
{
try
{
Expand All @@ -599,7 +624,7 @@ public bool GetMemoryArgument_Long(int varIndex, ref int[] values)
fixed (int* p = (int[])values)
{
IntPtr ptr = (IntPtr)p;
ComputeCommandQueue.Read((ComputeBuffer<int>)variablePointers[varIndex], true, 0L, values.Length, ptr, null);
ComputeCommandQueue.Read((ComputeBuffer<int>)variablePointers[argument_index], true, 0L, values.Length, ptr, null);
}
}
return true;
Expand All @@ -615,10 +640,10 @@ public bool GetMemoryArgument_Long(int varIndex, ref int[] values)
/// <summary>
/// Reads an array of type "Single" from the device.
/// </summary>
/// <param name="varIndex">0-based number of argument in argument list.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="values">Array of "Single".</param>
/// <returns>False in case of error/exception. Otherwise true.</returns>
public bool GetMemoryArgument_Single(int varIndex, ref float[] values)
public bool GetMemoryArgument_Single(int argument_index, ref float[] values)
{
try
{
Expand All @@ -627,7 +652,7 @@ public bool GetMemoryArgument_Single(int varIndex, ref float[] values)
fixed (float* p = (float[])values)
{
IntPtr ptr = (IntPtr)p;
ComputeCommandQueue.Read((ComputeBuffer<float>)variablePointers[varIndex], true, 0L, values.Length, ptr, null);
ComputeCommandQueue.Read((ComputeBuffer<float>)variablePointers[argument_index], true, 0L, values.Length, ptr, null);
}
}
return true;
Expand All @@ -643,10 +668,10 @@ public bool GetMemoryArgument_Single(int varIndex, ref float[] values)
/// <summary>
/// Reads an array of type "Double" from the device.
/// </summary>
/// <param name="varIndex">0-based number of argument in argument list.</param>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <param name="values">Array of "Double".</param>
/// <returns>False in case of error/exception. Otherwise true.</returns>
public bool GetMemoryArgument_Double(int varIndex, ref double[] values)
public bool GetMemoryArgument_Double(int argument_index, ref double[] values)
{
try
{
Expand All @@ -655,7 +680,7 @@ public bool GetMemoryArgument_Double(int varIndex, ref double[] values)
fixed (double* p = (double[])values)
{
IntPtr ptr = (IntPtr)p;
ComputeCommandQueue.Read((ComputeBuffer<double>)variablePointers[varIndex], true, 0L, values.Length, ptr, null);
ComputeCommandQueue.Read((ComputeBuffer<double>)variablePointers[argument_index], true, 0L, values.Length, ptr, null);
}
}
return true;
Expand All @@ -670,6 +695,68 @@ public bool GetMemoryArgument_Double(int varIndex, ref double[] values)

#endregion GetArguments

#region Destructors

/// <summary>
/// Disposes kernel memory variable.
/// </summary>
/// <param name="argument_index">0-based index of argument in argument list.</param>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool ReleaseMemObject(int argument_index)
{
try
{
variablePointers[argument_index].Dispose();
return true;
}
catch (Exception ex)
{
ErrorString += ex.Message;
return false;
}
}

/// <summary>
/// Disposes kernel.
/// </summary>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool ReleaseKernel()
{
try
{
kernel.Dispose();
return true;
}
catch (Exception ex)
{
ErrorString += ex.Message;
return false;
}
}

/// <summary>
/// Disposes ComputeProgram, ComputeCommandQueue and CommandQueue.
/// </summary>
/// <returns>True, if the operation was successful, false otherwise.</returns>
public bool ReleaseProgram()
{
try
{
ComputeProgram.Dispose();
ComputeCommandQueue.Dispose();
ComputeContext.Dispose();

return true;
}
catch (Exception ex)
{
ErrorString += ex.Message;
return false;
}
}

#endregion Destructors

/// <summary>
/// Device type of initialized device ("GPU" / "CPU").
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion C#/Installation script.iss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Name: "{app}\demo\cl"; Permissions: everyone-full
Source: bin\ClooWrapperVBA.dll; DestDir: {app}; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: bin\ClooWrapperVBA_x64.dll; DestDir: {app}; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: bin\Cloo.dll; DestDir: {app}; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\OpenCl v0.01.xlsm; DestDir: {app}\demo; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\OpenCl v0.02.xlsm; DestDir: {app}\demo; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\cl\Performance.cl; DestDir: {app}\demo\cl; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\cl\MatrixMultiplication.cl; DestDir: {app}\demo\cl; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\Configuration.vbs; DestDir: {app}\demo; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Expand Down
27 changes: 18 additions & 9 deletions Excel/Asynchronous.bas
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Sub MainLoop()
While Not allTasks_Completed
For i = 1 To progDevices.Count
If progDevices.Item(i).ProgramDevice.ExecutionCompleted Then
Call progDevices.Item(i).ProgramDevice.GetMemoryArgument_Double(0, vecResp) ' Extract the results and do something with received data here.
result = progDevices.Item(i).ProgramDevice.GetMemoryArgument_Double(0, vecResp) ' Extract the results and do something with received data here.

wsAsynchronous.Cells(logLine, 1) = "Task " & currentTaskId(i) & ", " & progDevices.Item(i).ProgramDevice.deviceType & _
progDevices.Item(i).DeviceId & ": completed"
Expand All @@ -37,13 +37,13 @@ Sub MainLoop()
' Start new task
If startedTasks < MAX_TASKS Then
ReDim vecResp(UBound(vecResp)) ' Erase output vector.
Call progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(0, vecResp)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(0, vecResp)

' If you want to use callbacks, than use function below
' "CPU_Task_Completed" is a function that will obtain the callback.
' Call progDevices.Item(i).ProgramDevice.ExecuteAsync(globalWorkOffset, globalWorkSize, localWorkSize, THREAD_PRIORITY, AddressOf Asynchronous.CPU_Task_Completed)

Call progDevices.Item(i).ProgramDevice.ExecuteBackground(globalWorkOffset, globalWorkSize, localWorkSize, THREAD_PRIORITY)
result = progDevices.Item(i).ProgramDevice.ExecuteBackground(globalWorkOffset, globalWorkSize, localWorkSize, THREAD_PRIORITY)
startedTasks = startedTasks + 1
currentTaskId(i) = startedTasks
Else
Expand Down Expand Up @@ -71,6 +71,15 @@ Sub MainLoop()
Wend

wsAsynchronous.Cells(2, currentProgress).Interior.Color = RGB(255, 255, 255)

For i = 1 To progDevices.Count
result = progDevices.Item(i).ProgramDevice.ReleaseMemObject(3)
result = progDevices.Item(i).ProgramDevice.ReleaseMemObject(2)
result = progDevices.Item(i).ProgramDevice.ReleaseMemObject(1)
result = progDevices.Item(i).ProgramDevice.ReleaseMemObject(0)
result = progDevices.Item(i).ProgramDevice.ReleaseKernel
result = progDevices.Item(i).ProgramDevice.ReleaseProgram
Next i
End Sub

Sub RunAsynchronous()
Expand Down Expand Up @@ -124,17 +133,17 @@ Sub RunAsynchronous()

ReDim currentTaskId(progDevices.Count)
For i = 1 To progDevices.Count
Call progDevices.Item(i).ProgramDevice.CreateKernel("DoubleMatrixMult")
Call progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(0, vecResp)
Call progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(1, vecM1)
Call progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(2, vecM2)
Call progDevices.Item(i).ProgramDevice.SetMemoryArgument_Long(3, vecQ)
result = progDevices.Item(i).ProgramDevice.CreateKernel("DoubleMatrixMult")
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(0, vecResp)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(1, vecM1)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(2, vecM2)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Long(3, vecQ)
Next i

startedTasks = 0
' Start execution on all found devices almost simultaneously.
For i = 1 To progDevices.Count
Call progDevices.Item(i).ProgramDevice.ExecuteBackground(globalWorkOffset, globalWorkSize, localWorkSize, THREAD_PRIORITY)
result = progDevices.Item(i).ProgramDevice.ExecuteBackground(globalWorkOffset, globalWorkSize, localWorkSize, THREAD_PRIORITY)

' If you want to use callbacks, than use function below
' "CPU_Task_Completed" is a function that will obtain the callback.
Expand Down
Loading

0 comments on commit 86ac67c

Please sign in to comment.