Why does my native library behave differently when wrapped using clibgen?

4 次查看(过去 30 天)
I have a native Windows library written in C++ and compiled with the MSVC compiler which contains one class (we'll call it MyClass since I can't provide the actual code for security reasons). MyClass has some std::vector<double> members that serve as input data to a MyClass member function (void myFun()--no input or output args) and also has some std::vector<double> members that serve as output parameters from that function.
I have also used clibgen to build a shared library so that MATLAB can use MyClass. However, I need to make sure that the outputs I get when both matlab and C++ call the function are EXACTLY the same, which I haven't been able to achieve. To test equality between matlab and c++ I am doing the following:
  1. Create a MyClass object in c++ (cppobj)
  2. Populate the input parameters of cppobj
  3. Run myFun() from cppobj
  4. Write the input and output (all elements) of cppobj to binary files
  5. Load the data of these binary files into two matlab objects, one with both inputs and outputs (matobj_cppout), and one with just inputs (matobj_matout)
  6. Run myFun() on matobj_matout
  7. Compare the output parameters of matobj_cppout and matobj_matout
I've calculated small differences in the output values even though they SHOULD be exactly the same, since the function is deterministic and the input parameters in matobj_cppout and matobj_matout are identical. This leads me to believe that MATLAB is doing some kind of conversion either when the binary files or read or when the values are stored to the native MyClass object using the "clib" functionality. I've verified that I'm using fp:precise both when building the native library and when running clibgen.
Any ideas of what could be causing these discrepancies?

采纳的回答

Amish
Amish 2024-7-10
Hi Ben,
I see that you’ve been very thorough in your approach. The small discrepancies you’re observing could indeed be due to several factors related to floating-point precision and data conversion between MATLAB and C++.
Here are some potential causes and and fixes you can try to resolve the issue.
1. Make sure that both environments are using the same floating-point precision and no intermediate computations are causing rounding differences.
2. In MATLAB, use the appropriate precision when reading the binary files. MATLAB might be converting data types when interfacing with C++ through clibgen.
Verify that the data types are correctly mapped. You can refer to the following document that mentions how MATLAB converts C/C++ data into equivalent MATLAB data types:
3. You can further try debugging and check to see if the intermediate values are getting altered to identify where the discrepancies start.
4. Alternatively, you can use MATLAB’s "mex" function to directly call the C++ function from MATLAB, bypassing clibgen.
More information on 'mex' can be found at: https://in.mathworks.com/help/matlab/call-mex-file-functions.html
Hope this helps.
  1 个评论
Ben
Ben 2024-7-10
Thanks Amish!
I ended up discovering that my issue was that I compiled the C++ code (which called my MSVC-compiled library) using the intel compiler. There were some optimizations that the intel compiler was doing that didn't happen when the library was called from matlab (since it used the MSVC compiler all the way down).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Use Prebuilt MATLAB Interface to C++ Library 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by