Why does my native library behave differently when wrapped using clibgen?
3 ビュー (過去 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:
- Create a MyClass object in c++ (cppobj)
- Populate the input parameters of cppobj
- Run myFun() from cppobj
- Write the input and output (all elements) of cppobj to binary files
- 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)
- Run myFun() on matobj_matout
- 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?
0 件のコメント
採用された回答
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.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!