필터 지우기
필터 지우기

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

조회 수: 13 (최근 30일)
Ben
Ben 2024년 7월 9일 18:29
댓글: Ben 2024년 7월 10일 21:25
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일 6:14
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일 21:25
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에서 Build 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