topic Compiling HLS Code as Matlab-Mex function - hls_math.h trouble in High-Level Synthesis (HLS)
https://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1005716#M17644
<P>Hi,</P><P>I want to compile my HLS C++ function as a Matlab .mexw64 file so I can be more flexible building my testbench, but I encouter a compilation problem if I use any function provided by hls_math.h.</P><P>I'm using Vivado 2018.3, Matlab 2015b and the <EM>same</EM> version of Mingw64 for both Vivado HLS (C-Simulation) and Matlab (creating .mexw64 file).</P><P> </P><P><STRONG>This works:</STRONG></P><P>If I have a function <EM>fpgaFun.cpp</EM>, that works perfectly fine in the Vivado HLS C-Simulation, as follows:</P><P> </P><PRE>#include <stdio.h>
#include "hls_stream.h"
#include "ap_fixed.h"
void fpgaFun(hls::stream< ap_fixed<32,16> > &dataIn, float &dataIn2, ap_fixed<32,16> &dataOut) {
//code
}</PRE><P>The testbench <EM>fpgaFun_tb.cpp</EM> only converts the data types and the following Matlab command produces a working .mexw64 file:</P><P> </P><P> </P><PRE>mex -v fpgaFun_tb.cpp fpgaFun.cpp -IC:\Xilinx\Vivado\2018.3\include</PRE><P>The resulting file can be called from Matlab and behaves as the HLS compilation.</P><P> </P><P> </P><P><STRONG>This doesn't work:</STRONG></P><P>Modifying <EM>fpgaFun.cpp</EM> as follows (additions in red):</P><PRE>#include <stdio.h>
#include "hls_stream.h"
#include "ap_fixed.h"<BR /><FONT color="#FF0000">#include <hls_math.h></FONT>
void fpgaFun(hls::stream< ap_fixed<32,16> > &dataIn1, float &dataIn2, ap_fixed<32,16> &dataOut) {
//code<BR /><FONT color="#FF0000">dataOut = hls::cosf(dataIn2);</FONT>
}</PRE><P>The same Matlab command as used previously now produces:</P><P> </P><PRE>C:\Users\re\AppData\Local\Temp\mex_60795916961408_9996\fpgaFun.obj:fpgaFun.cpp:(.text+0x3f0): undefined
reference to `hls::cosf(float)'</PRE><P>Other functions like hls::absf and hls::sqrtf produce similar errors and changing the data type to ap_(u)fixed also produces such errors.</P><P> </P><P>What can I do to fix this?</P><P>As far as I understand from looking at the HLS C-Simulation <EM>csim.mk</EM> and <EM>Makefile.rules</EM> files in the HLS project it seems like that, depending on the compiling language (C/C++), system architecture (32/64-bit) and compiler (clang/gcc) one of the files in the following folder is linked as a library when using <EM>hls_math.h</EM>:</P><P>C:\Xilinx\Vivado\2018.3\win64\lib\csim</P><P>And one folder above those files seem to be the static library versions <EM>libhlsmc++.a</EM> and <EM>libhlsm.a</EM>.</P><P>According to the official <A href="https://de.mathworks.com/help/matlab/ref/mex.html#btw17rw-1-option1optionN" target="_self">Matlab documentation</A> the compiler command only accepts external <EM>.lib</EM> files.</P><P>Is this really the problem? If so, can Xilinx provide the definitions for the <EM>hls_mat.h</EM> functions as a <EM>.lib</EM> or can anything else be done about it?</P><P>I understand that this is a very specific problem, infuritating nonetheless.</P><P> </P>Thu, 08 Aug 2019 10:11:50 GMTjlhx2019-08-08T10:11:50ZCompiling HLS Code as Matlab-Mex function - hls_math.h trouble
https://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1005716#M17644
<P>Hi,</P><P>I want to compile my HLS C++ function as a Matlab .mexw64 file so I can be more flexible building my testbench, but I encouter a compilation problem if I use any function provided by hls_math.h.</P><P>I'm using Vivado 2018.3, Matlab 2015b and the <EM>same</EM> version of Mingw64 for both Vivado HLS (C-Simulation) and Matlab (creating .mexw64 file).</P><P> </P><P><STRONG>This works:</STRONG></P><P>If I have a function <EM>fpgaFun.cpp</EM>, that works perfectly fine in the Vivado HLS C-Simulation, as follows:</P><P> </P><PRE>#include <stdio.h>
#include "hls_stream.h"
#include "ap_fixed.h"
void fpgaFun(hls::stream< ap_fixed<32,16> > &dataIn, float &dataIn2, ap_fixed<32,16> &dataOut) {
//code
}</PRE><P>The testbench <EM>fpgaFun_tb.cpp</EM> only converts the data types and the following Matlab command produces a working .mexw64 file:</P><P> </P><P> </P><PRE>mex -v fpgaFun_tb.cpp fpgaFun.cpp -IC:\Xilinx\Vivado\2018.3\include</PRE><P>The resulting file can be called from Matlab and behaves as the HLS compilation.</P><P> </P><P> </P><P><STRONG>This doesn't work:</STRONG></P><P>Modifying <EM>fpgaFun.cpp</EM> as follows (additions in red):</P><PRE>#include <stdio.h>
#include "hls_stream.h"
#include "ap_fixed.h"<BR /><FONT color="#FF0000">#include <hls_math.h></FONT>
void fpgaFun(hls::stream< ap_fixed<32,16> > &dataIn1, float &dataIn2, ap_fixed<32,16> &dataOut) {
//code<BR /><FONT color="#FF0000">dataOut = hls::cosf(dataIn2);</FONT>
}</PRE><P>The same Matlab command as used previously now produces:</P><P> </P><PRE>C:\Users\re\AppData\Local\Temp\mex_60795916961408_9996\fpgaFun.obj:fpgaFun.cpp:(.text+0x3f0): undefined
reference to `hls::cosf(float)'</PRE><P>Other functions like hls::absf and hls::sqrtf produce similar errors and changing the data type to ap_(u)fixed also produces such errors.</P><P> </P><P>What can I do to fix this?</P><P>As far as I understand from looking at the HLS C-Simulation <EM>csim.mk</EM> and <EM>Makefile.rules</EM> files in the HLS project it seems like that, depending on the compiling language (C/C++), system architecture (32/64-bit) and compiler (clang/gcc) one of the files in the following folder is linked as a library when using <EM>hls_math.h</EM>:</P><P>C:\Xilinx\Vivado\2018.3\win64\lib\csim</P><P>And one folder above those files seem to be the static library versions <EM>libhlsmc++.a</EM> and <EM>libhlsm.a</EM>.</P><P>According to the official <A href="https://de.mathworks.com/help/matlab/ref/mex.html#btw17rw-1-option1optionN" target="_self">Matlab documentation</A> the compiler command only accepts external <EM>.lib</EM> files.</P><P>Is this really the problem? If so, can Xilinx provide the definitions for the <EM>hls_mat.h</EM> functions as a <EM>.lib</EM> or can anything else be done about it?</P><P>I understand that this is a very specific problem, infuritating nonetheless.</P><P> </P>Thu, 08 Aug 2019 10:11:50 GMThttps://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1005716#M17644jlhx2019-08-08T10:11:50ZRe: Compiling HLS Code as Matlab-Mex function - hls_math.h trouble
https://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1008845#M17724
<P>Hello <LI-USER uid="63638"></LI-USER> ,</P>
<P>I think this is a bit outside of the supported flow for HLS, but will try and provide some guidance if I can. The document I want to refer you to is located here:</P>
<P><A href="https://www.xilinx.com/support/documentation/sw_manuals/xilinx2019_1/ug902-vivado-high-level-synthesis.pdf" target="_blank" rel="noopener">https://www.xilinx.com/support/documentation/sw_manuals/xilinx2019_1/ug902-vivado-high-level-synthesis.pdf</A></P>
<P>The hls_math.h library is discussed on page 225, with all the supported functions listed on page 227. You can see there that the cosf function is not one of the ones that is listed. "cosf" comes from the standard c++ "cmath" library. </P>
<P>One caveat to using that C++ library is that C simulation and Co-simualtion can be different due to underlying algorithm differences in HLS, and that is discussed on page 231. This is where the recommondation to use hls_math.h comes in. What is confusing is that our documents do show the hls::cosf function in an example on page 234. Again, this function is not in the current hls_math.h library, and may have been an artifact of a prior release - the document does need to be changed here. I think this is the reason you are getting the function definition not found errors because it is true, that function is not in the library. </P>
<P>In the current library, the hls_math "cos" function can take a floating point value as an input, so I am hoping that if you replace the "cosf" function with "cos" you may be able to resolve this issue. </P>
<P>Can you try that to see if this resolves your issue then?</P>
<P>OK, I hope this helps,<BR />Scott</P>
<P> </P>Fri, 16 Aug 2019 17:15:49 GMThttps://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1008845#M17724scampbell2019-08-16T17:15:49ZRe: Compiling HLS Code as Matlab-Mex function - hls_math.h trouble
https://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1012010#M17779
<P>Hi <LI-USER uid="63638"></LI-USER> </P>
<P style="margin: 0in; font-family: arial; font-size: 11.0pt;">If your question is answered or your issue is solved, please kindly mark the response which helped as solution (click on "Accept as solution" button below the reply)</P>
<P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"> </P>
<P style="margin: 0in; font-family: arial; font-size: 11.0pt;">If this is not solved/answered, please reply in the topic giving more information on your current status.</P>
<P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;"> </P>
<P style="margin: 0in; font-family: arial; font-size: 11.0pt;">Thanks and Regards,</P>Mon, 26 Aug 2019 10:56:00 GMThttps://forums.xilinx.com/t5/High-Level-Synthesis-HLS/Compiling-HLS-Code-as-Matlab-Mex-function-hls-math-h-trouble/m-p/1012010#M17779aoifem2019-08-26T10:56:00Z