r/vba • u/wenzelja74 • 7d ago
Discussion How to enable GPU processing in Excel VBA
I create engineering functions in Excel VBA, but want to take advantage of the GPU to speed up calculations. I’ve tried prompting Copilot to ask for help, but it seems resistant to help in that regards. It wants to direct me to creating XLL’s to accelerate calculations, but I don’t know what I’m doing with XLL’s and I think they require digital signing but my work IT doesn’t allow such things.
22
Upvotes
17
u/kay-jay-dubya 17 7d ago edited 7d ago
Yes, this can be done with VBA, and no you dont need to make an XLL to accomplish it.
OpenCL allows you to access the GPU programatically.there is a VB6 32bit demonstration of how to do it on the vbforums sire: https://www.vbforums.com/showthread.php?898360-VB6-How-to-use-GPU-from-VB6-using-opencl
(VB6 and VBA are the same language)
If you go to the bottom of the thread, I posted theVBA 64bit version of the code for anyone who might need it.
Edit: Just to head off any comments from naysayers, OpenCL uses a DLL that ships with Windows. There is nothing that one needs to install. The working 64bit demo I posted includes the necessary 64bit API declarations.
3
u/personalityson 1 7d ago edited 7d ago
What kind of calculations? You can make dll function calls to BLAS and Lapack with for example Openblas or MKL library. Not sure if GPU will offer any more speed, you need to move memory to GPU and then back to CPU.
7
u/Almesii 7d ago
I havent tried to implement Compute Shaders for general purpose programming. But it is possible to work with the gpu via vba. I did it with freeglut to work with opengl. Maybe you will find something useful: https://github.com/Almesi/VBGL
1
u/EbbMother5325 7d ago
I guess with engineering workbooks you are stuck with excel. But instead of VBA, why not write the heavy functions in Python? Add a decorator to your Python function, call it from your workbook as a regular formula. Xlwings can do it.
Or is there an IT policy that is preventing you?
0
u/Thadrea 3 7d ago
You really shouldn't be doing this in Excel. It's not designed for what you're trying to do, nor is VBA.
2
u/kal_at_kalx_net 7d ago
The best way to get high-performance numerical computing is to use the ancient Excel C SDK and the FP datatype to get a pointer the doubles inside of Excel. I wrote xlladdins/xll24: Excel add-in library to make it simple to do this.
Here is an example of how to call BLAS from Excel xll_lapack/xll_blas.cpp at master · xlladdins/xll_lapack
You don't need signing to run on your personal computer. You might have to at work depending on your IT security settings.
2
u/keith-kld 3d ago edited 3d ago
The speed of computation may be also dependent on the algorithms that you apply. Better algorithms may speed up the calculations. In addition, do not always use variant variables in the code if you know what type they are. This will help your script run faster. I have seen a lot of scripts given by AI app. These scripts can be used immediately but they are not optimized and may contain gaps.
2
u/fafalone 4 3d ago
In addition to OpenCL you can also use CUDA, e.g.
There's a sample project on twinBASIC Discord that doesn't use any tB-only features, so it would work to copy paste into VBA.
--
XLLs aren't required for this, but if you did want to go that route they don't require digital signing unless your workplace enforces a non-default option to require that, and you can make them using your existing VBA language skills in twinBASIC... they're just a renamed standard DLL: https://github.com/fafalone/TBXLLUDF
1
u/Alternative_Corgi_62 7d ago
The worst part about VBA is that it sure the code inside the. xlsm document, and there's is no straight forward way to maintain it outside of Excel. Even tried to place your code in git / tfs?
1
u/MerryWalrus 7d ago
You should have switched out of excel and vba a long time before your solution got this complex. The best time to do it is right now.
-7
u/JoseLunaArts 7d ago
GPUs are used for image processing. Unless you manage to use image processing algorithms to approach functions, a GPU is useless for Excel. AI works in ways that are very similar to image processing. This is is why AI can use GPUs.