Skip to content

Commit

Permalink
* Issues with old graphic cards is solved:
Browse files Browse the repository at this point in the history
* CL sources should be saved as plain text (ANSI, not UTF). Some old OpenCL compilers return error in case of UTF files.
* Some graphic cards (Intel(R) HD Graphics 4400) cannot use doubles (no "cl_khr_fp64" extension). Therefore the CL sources were divided to float and double versions. The buttons "Run VBA performance test" and "Asynchronous execution" runs the "float" versions of CL sources because they can be executed at all kind of devices (hopefully).
* Old version of Inno setup was used to compile the installation. Latest Inno setup version created the installation that was falsely detected as virus.
  • Loading branch information
Excel-lent committed Dec 24, 2023
1 parent 42c075c commit efee8aa
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 152 deletions.
10 changes: 6 additions & 4 deletions C#/Installation script.iss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AppName=ClooWrapperVBA
AppVerName=ClooWrapperVBA
DefaultDirName={pf}\ClooWrapperVBA
DefaultGroupName=ClooWrapperVBA
Compression=lzma
Compression=zip
SolidCompression=yes
SourceDir=.\
PrivilegesRequired=poweruser
Expand All @@ -22,9 +22,11 @@ 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.05.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\OpenCl example.xlsm; DestDir: {app}\demo; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\cl\FloatPerformance.cl; DestDir: {app}\demo\cl; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\cl\DoublePerformance.cl; DestDir: {app}\demo\cl; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\cl\FloatMatrixMultiplication.cl; DestDir: {app}\demo\cl; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: ..\Excel\cl\DoubleMatrixMultiplication.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;
Source: bin\register.bat; DestDir: {app}; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Source: bin\unregister.bat; DestDir: {app}; Flags: ignoreversion recursesubdirs overwritereadonly; Permissions: everyone-full;
Expand Down
18 changes: 9 additions & 9 deletions Excel/Asynchronous.bas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Const THREAD_PRIORITY = 0
Dim progDevices As Collection
Dim currentTaskId&()

Dim vecResp#()
Dim vecResp!()
Dim wsAsynchronous As Worksheet
Dim globalWorkSize&(1), localWorkSize&(), globalWorkOffset&()
Dim logLine%
Expand All @@ -26,7 +26,7 @@ Sub MainLoop()
While Not allTasks_Completed
For i = 1 To progDevices.Count
If progDevices.Item(i).ProgramDevice.ExecutionCompleted Then
result = progDevices.Item(i).ProgramDevice.GetMemoryArgument_Double(0, vecResp) ' Extract the results and do something with received data here.
result = progDevices.Item(i).ProgramDevice.GetMemoryArgument_Single(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,7 +37,7 @@ Sub MainLoop()
' Start new task
If startedTasks < MAX_TASKS Then
ReDim vecResp(UBound(vecResp)) ' Erase output vector.
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Double(0, vecResp)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Single(0, vecResp)

' If you want to use callbacks, than use function below
' "CPU_Task_Completed" is a function that will obtain the callback.
Expand Down Expand Up @@ -83,14 +83,14 @@ Sub MainLoop()
End Sub

Sub RunAsynchronous()
Dim vecM1#(), vecM2#()
Dim vecM1!(), vecM2!()
Dim vecQ&(1)
Dim i&, j&, p&, q&, r&, nRows&
Dim buildLogs$, sources$

Set wsAsynchronous = ThisWorkbook.Worksheets("Asynchronous")

Open Application.ActiveWorkbook.Path & "\cl\MatrixMultiplication.cl" For Binary As #1
Open Application.ActiveWorkbook.Path & "\cl\FloatMatrixMultiplication.cl" For Binary As #1
sources = Space$(LOF(1))
Get #1, , sources
Close #1
Expand Down Expand Up @@ -133,10 +133,10 @@ Sub RunAsynchronous()

ReDim currentTaskId(progDevices.Count)
For i = 1 To progDevices.Count
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.CreateKernel("FloatMatrixMult")
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Single(0, vecResp)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Single(1, vecM1)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Single(2, vecM2)
result = progDevices.Item(i).ProgramDevice.SetMemoryArgument_Long(3, vecQ)
Next i

Expand Down
34 changes: 17 additions & 17 deletions Excel/HelloWorld.bas
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Sub HelloWorld()
Dim nRows%, currentRow&, nPlatforms&, nDevices&, i&, j&, result As Boolean
Dim deviceType$, platformName$, platformVendor$, platformVersion$, deviceVendor$, deviceVersion$, driverVersion$, openCLCVersionString$
Dim maxComputeUnits&, globalMemorySize#, maxClockFrequency#, maxMemoryAllocationSize#, deviceName$, sources$, cpuCounter&, gpuCounter&
Dim buildLogs$, platformId&, deviceId&, errorString$
Dim buildLogs$, platformId&, DeviceId&, errorString$
Dim deviceAvailable As Boolean, compilerAvailable As Boolean
Dim m1#(1, 1), m2#(1, 1), vecM1#(), vecM2#(), vecQ&(0), vecResp#(3), globalWorkOffset&(), globalWorkSize&(1), localWorkSize&()
Dim p&, q&, r&, resp#()
Dim m1!(1, 1), m2!(1, 1), vecM1!(), vecM2!(), vecQ&(0), vecResp!(3), globalWorkOffset&(), globalWorkSize&(1), localWorkSize&()
Dim p&, q&, r&, resp!()

Dim clooConfiguration As New ClooWrapperVBA.Configuration
Dim progDevice As ClooWrapperVBA.ProgramDevice
Expand Down Expand Up @@ -76,7 +76,7 @@ Sub HelloWorld()

' Multiplication of two matrices.
' Read the OpenCL sources.
Open Application.ActiveWorkbook.Path & "\cl\MatrixMultiplication.cl" For Binary As #1
Open Application.ActiveWorkbook.Path & "\cl\FloatMatrixMultiplication.cl" For Binary As #1
sources = Space$(LOF(1))
Get #1, , sources
Close #1
Expand All @@ -87,13 +87,13 @@ Sub HelloWorld()
result = clooConfiguration.SetPlatform(platformId)
cpuCounter = 0
gpuCounter = 0
For deviceId = 0 To clooConfiguration.Platform.Devices - 1
result = clooConfiguration.Platform.SetDevice(deviceId)
For DeviceId = 0 To clooConfiguration.Platform.Devices - 1
result = clooConfiguration.Platform.SetDevice(DeviceId)

If clooConfiguration.Platform.device.compilerAvailable Then
If clooConfiguration.Platform.device.deviceType = "CPU" Then
Set progDevice = New ClooWrapperVBA.ProgramDevice
result = progDevice.Build(sources, "", platformId, deviceId, cpuCounter, buildLogs)
result = progDevice.Build(sources, "", platformId, DeviceId, cpuCounter, buildLogs)
If result Then
Exit Do
Else
Expand All @@ -102,7 +102,7 @@ Sub HelloWorld()
End If
If clooConfiguration.Platform.device.deviceType = "GPU" Then
Set progDevice = New ClooWrapperVBA.ProgramDevice
result = progDevice.Build(sources, "", platformId, deviceId, gpuCounter, buildLogs)
result = progDevice.Build(sources, "", platformId, DeviceId, gpuCounter, buildLogs)
gpuCounter = gpuCounter + 1
If result Then
Exit Do
Expand All @@ -111,12 +111,12 @@ Sub HelloWorld()
End If
End If
End If
Next deviceId
Next DeviceId
platformId = platformId + 1
Loop

errorString = progDevice.errorString
result = progDevice.CreateKernel("DoubleMatrixMult")
result = progDevice.CreateKernel("FloatMatrixMult")

' Initialization of arrays:
p = 2: q = 2: r = 2
Expand All @@ -125,28 +125,28 @@ Sub HelloWorld()
m1(i, j) = wsHelloWorld.Cells(i + 1, j + 7)
Next j
Next i
vecM1 = MatrixToVector(m1, p, q)
vecM1 = MatrixToVectorSingle(m1, p, q)
For i = 0 To q - 1
For j = 0 To r - 1
m2(i, j) = wsHelloWorld.Cells(i + 3, j + 7)
Next j
Next i
vecM2 = MatrixToVector(m2, q, r)
vecM2 = MatrixToVectorSingle(m2, q, r)
vecQ(0) = q

result = progDevice.SetMemoryArgument_Double(0, vecResp)
result = progDevice.SetMemoryArgument_Double(1, vecM1)
result = progDevice.SetMemoryArgument_Double(2, vecM2)
result = progDevice.SetMemoryArgument_Single(0, vecResp)
result = progDevice.SetMemoryArgument_Single(1, vecM1)
result = progDevice.SetMemoryArgument_Single(2, vecM2)
result = progDevice.SetMemoryArgument_Long(3, vecQ)

globalWorkSize(0) = p
globalWorkSize(1) = r

result = progDevice.ExecuteSync(globalWorkOffset, globalWorkSize, localWorkSize)

result = progDevice.GetMemoryArgument_Double(0, vecResp)
result = progDevice.GetMemoryArgument_Single(0, vecResp)

resp = VectorToMatrix(vecResp, p, r)
resp = VectorToMatrixSingle(vecResp, p, r)

For i = 0 To p - 1
For j = 0 To r - 1
Expand Down
42 changes: 36 additions & 6 deletions Excel/Helpers.bas
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ Function CreateDeviceCollection(sources$)

If clooConfiguration.Platform.device.deviceType = "CPU" Then
result = progDevice.ProgramDevice.Build(sources, "", i - 1, j - 1, cpuCounter, buildLogs)
progDevice.deviceId = cpuCounter
progDevice.DeviceId = cpuCounter
progDevice.deviceType = "CPU"
If result = True Then cpuCounter = cpuCounter + 1
ElseIf clooConfiguration.Platform.device.deviceType = "GPU" Then
result = progDevice.ProgramDevice.Build(sources, "", i - 1, j - 1, gpuCounter, buildLogs)
progDevice.deviceId = gpuCounter
progDevice.DeviceId = gpuCounter
progDevice.deviceType = "GPU"
If result = True Then gpuCounter = gpuCounter + 1
Else
Expand All @@ -62,7 +62,37 @@ Function CreateDeviceCollection(sources$)
End If
End Function

Function MatrixToVector(m() As Double, maxi As Long, maxj As Long) As Double()
Function MatrixToVectorSingle(m() As Single, maxi As Long, maxj As Long) As Single()
Dim v() As Single
Dim i&, j&

ReDim v(maxi * maxj - 1)

For i = 0 To maxi - 1
For j = 0 To maxj - 1
v(i + maxi * j) = m(i, j)
Next j
Next i

MatrixToVectorSingle = v
End Function

Function VectorToMatrixSingle(v() As Single, maxi As Long, maxj As Long) As Single()
Dim i&, j&
Dim m() As Single

ReDim m(maxi - 1, maxj - 1)

For i = 0 To maxi - 1
For j = 0 To maxj - 1
m(i, j) = v(i + maxi * j)
Next j
Next i

VectorToMatrixSingle = m
End Function

Function MatrixToVectorDouble(m() As Double, maxi As Long, maxj As Long) As Double()
Dim v() As Double
Dim i&, j&

Expand All @@ -74,10 +104,10 @@ Function MatrixToVector(m() As Double, maxi As Long, maxj As Long) As Double()
Next j
Next i

MatrixToVector = v
MatrixToVectorDouble = v
End Function

Function VectorToMatrix(v() As Double, maxi As Long, maxj As Long) As Double()
Function VectorToMatrixDouble(v() As Double, maxi As Long, maxj As Long) As Double()
Dim i&, j&
Dim m() As Double

Expand All @@ -89,5 +119,5 @@ Function VectorToMatrix(v() As Double, maxi As Long, maxj As Long) As Double()
Next j
Next i

VectorToMatrix = m
VectorToMatrixDouble = m
End Function
Binary file modified Excel/OpenCl example.xlsm
Binary file not shown.
Loading

0 comments on commit efee8aa

Please sign in to comment.