-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runs slower than actxserver? What am I doing wrong? A: profiler overhead #11
Comments
Hi, @Sigrsteinn, For DSS_MATLAB, we do have a lot more error checking. It you're using too many API calls and they're all checked on the MATLAB side, I imagine it might have a negative impact, especially since MATLAB is not exactly fast for non-numeric code. If that is indeed the case, I could add an alternative that does no checks. More recent versions of MATLAB have faster COM implementations too. Years ago,
Yep, using
At first glance, yes, it would be similar in concept to the official OpenDSSDirect library. Yet, OpenDSSDirect.py and OpenDSSDirect.jl were both migrated from the official library to DSS C-API due to several issues (many related to The faster alternative to
I'm pretty happy at least that worked without issues 😃
In the end, it's hard to say without checking the code. If you can share a small code sample (either here or privately through email), I could try to investigate it. Depending on what we find, I could update the code in DSS_MATLAB, add additional helper functions in DSS C-API, or just propose you change some aspects of your code. |
@Sigrsteinn Interesting, thanks. It looks like the error checking is not the main culprit. Yet, since it is still a few percent and it's a trivial function call, it's worth making it better. Since my other comment, I noticed there is a faster alternative that I forgot to implement here, I'll try that later today or tomorrow. On the main issue, you generate a long DSS script and pass it all line-by-line, right? I think that's a bit unconventional. In those DSS commands, do you create new DSS elements or just update them? Independent of COM or DSS_MATLAB, have you tried writing those lines to a file and then running a I'll try to run some tests that mimic your code -- load a large circuit in memory, feed it line by line to OpenDSS, loop that. It's still possible that your DSS script itself is hitting some slow code in DSS C-API, i.e., it might not be specific to the API calls or MATLAB. Testing should help make this clearer. On the MEX vs. CallLib topic, this comment has some interesting data: CoolProp/CoolProp#1095 (comment) (it's from 2016 though, take it with a grain of salt). As a sidenote, someday we'll probably add a |
@Sigrsteinn It could be the profiler overhead or some background process. I wrote a simple benchmark that loads (no
My conclusion is that MATLAB has a lot of overhead for function calls, be them COM calls or plain C calls via CallLib. I'm now considering investing some time later to create a full implementation of DSS_MATLAB using MEX to reduce the overhead. Probably won't happen for a while though (at least some weeks). My computer at the university still has MATLAB 2018a but I requested the latest installation available and will retry this simple benchmark when I'm able. I'm not sure the prebuilt MEX files would work on other versions of MATLAB, but I uploaded my test MEX files here if you want to give them a try: dss_capi_mex_test.zip. To test them, drop them inside the |
I decided to check most options to get a more clear overall of the speed of each. On the table (running on MATLAB 2018a):
Some observations:
For the time being, my recommendation would be to use DSS_MATLAB, profile (like you already did, @Sigrsteinn), and try to use some
with
This will not have the extra error checking but should be the faster solution besides MEX. For simple calls (single char array, integer, or double arguments) this is feasible. For functions that return arrays/pointers, it becomes more cumbersome. Most functions names are straightforward and you can check them in the matching header file for the DSS C-API version, e.g. https://github.com/dss-extensions/dss_capi/blob/0.10.6/include/v7/dss_capi.h -- I'll make sure to include this in the future releases. As I mentioned in my previous message, I'll redo this on the latest MATLAB whenever I get it, and in the future investigate MEX further -- the MEX C++ alternative in MATLAB 2020a looks much better than the C alternative. My plan would be to keep the current |
I just the MEX files. The
I also did this. It is faster than the line-by-line MEX some of the time, but the
I don't know what's wrong. Maybe there's something wrong with my PC, maybe I need to run more iterations to get more accurate result for each method. But for now, I need to focus on something else. I will probably try this again some time in the future. Thank you for your help. |
@Sigrsteinn If your timings always come from the profiler, try running with the profiler disabled. With
Thus, when using the profiler, the timings are skewed a lot towards COM. A simple tic + toc should not affect the timings though -- use that instead and DSS_MATLAB should be faster. Update (April 2023)
So, in the end, you'll probably end up with faster scripts in certain conditions, or slower if you hit the slower properties. It all depends on how you wrote the script in the first place. Looking for MATLAB code that uses OpenDSS, there are some very bad examples that instead of using the dedicated API functions, just use the Text interface, only to then post-process the Regardless of the interface being used, the general advice for any language is still valid: try to avoid strings when possible and you'll get results faster. For example, consider that strings are generally arrays of characters of varying sizes, so if you activate lines by name in a loop, you're copying all those strings around, including potential reencoding, etc., while if you activate lines by index, a single integer is being copied. And again, regardless of the interface, it doesn't seem like the users quite understand how the classic OpenDSS API organization works. I guess that's something we can try to help though, by adding an overview document. Per #13, we still want to add a MEX option, but it certainly is not because this package "runs slower than actxserver". |
Hello,
I am sorry if this is a stupid question, but:
I need to run OpenDSS with Matlab.
I profiled my code with Matlab, most of the time is spent on sending the commands through OpenDSS COM interface.
I read up about early binding and late binding in OpenDSS Documentation.
I tried the early binding method with DCSL as seen in the documentation to speed up my code.
It didn't work because I don't know how to handle the "variant" data type in Matlab.
That's when I found this Matlab package on GitHub.
I see that the code in this package is somewhat similar to the DCSL method, particularly the "calllib(libname,command,arg)".
I assumed that this package is similar in concept, and therefore faster than the ActiveX COM interface method.
I read the usage guide. Since my code uses "DSSStartup.m", I replaced the object instantiation as suggested.
I didn't change anything else in my code.
When I run my code, it is considerably slower than when I used the original actxserver method.
What am I doing wrong here?
The text was updated successfully, but these errors were encountered: