r/vba • u/wenzelja74 • 2d 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.
21
Upvotes
16
u/kay-jay-dubya 17 2d ago edited 2d 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 2d ago edited 2d 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 2d 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 2d 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 2d 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 2d 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.
1
u/Alternative_Corgi_62 2d 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 2d 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 2d 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.