UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Visitor jlhx
Visitor
228 Views
Registered: ‎05-20-2014

Compiling HLS Code as Matlab-Mex function - hls_math.h trouble

Jump to solution

Hi,

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.

I'm using Vivado 2018.3, Matlab 2015b and the same version of Mingw64 for both Vivado HLS (C-Simulation) and Matlab (creating .mexw64 file).

 

This works:

If I have a function fpgaFun.cpp, that works perfectly fine in the Vivado HLS C-Simulation, as follows:

 

#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
}

The testbench fpgaFun_tb.cpp only converts the data types and the following Matlab command produces a working .mexw64 file:

 

 

mex -v fpgaFun_tb.cpp fpgaFun.cpp -IC:\Xilinx\Vivado\2018.3\include

The resulting file can be called from Matlab and behaves as the HLS compilation.

 

 

This doesn't work:

Modifying fpgaFun.cpp as follows (additions in red):

#include <stdio.h>
#include "hls_stream.h"
#include "ap_fixed.h"
#include <hls_math.h> void fpgaFun(hls::stream< ap_fixed<32,16> > &dataIn1, float &dataIn2, ap_fixed<32,16> &dataOut) { //code
dataOut = hls::cosf(dataIn2); }

The same Matlab command as used previously now produces:

 

C:\Users\re\AppData\Local\Temp\mex_60795916961408_9996\fpgaFun.obj:fpgaFun.cpp:(.text+0x3f0): undefined
reference to `hls::cosf(float)'

Other functions like hls::absf and hls::sqrtf produce similar errors and changing the data type to ap_(u)fixed also produces such errors.

 

What can I do to fix this?

As far as I understand from looking at the HLS C-Simulation csim.mk and Makefile.rules 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 hls_math.h:

C:\Xilinx\Vivado\2018.3\win64\lib\csim

And one folder above those files seem to be the static library versions libhlsmc++.a and libhlsm.a.

According to the official Matlab documentation the compiler command only accepts external .lib files.

Is this really the problem? If so, can Xilinx provide the definitions for the hls_mat.h functions as a .lib or can anything else be done about it?

I understand that this is a very specific problem, infuritating nonetheless.

 

0 Kudos
1 Solution

Accepted Solutions
Moderator
Moderator
167 Views
Registered: ‎10-04-2011

Re: Compiling HLS Code as Matlab-Mex function - hls_math.h trouble

Jump to solution

Hello @jlhx ,

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:

https://www.xilinx.com/support/documentation/sw_manuals/xilinx2019_1/ug902-vivado-high-level-synthesis.pdf

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. 

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. 

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. 

Can you try that to see if this resolves your issue then?

OK, I hope this helps,
Scott

 

0 Kudos
2 Replies
Moderator
Moderator
168 Views
Registered: ‎10-04-2011

Re: Compiling HLS Code as Matlab-Mex function - hls_math.h trouble

Jump to solution

Hello @jlhx ,

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:

https://www.xilinx.com/support/documentation/sw_manuals/xilinx2019_1/ug902-vivado-high-level-synthesis.pdf

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. 

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. 

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. 

Can you try that to see if this resolves your issue then?

OK, I hope this helps,
Scott

 

0 Kudos
Moderator
Moderator
125 Views
Registered: ‎11-21-2018

Re: Compiling HLS Code as Matlab-Mex function - hls_math.h trouble

Jump to solution

Hi @jlhx 

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)

 

If this is not solved/answered, please reply in the topic giving more information on your current status.

 

Thanks and Regards,

Aoife
Product Application Engineer - Xilinx Technical Support EMEA
**~ Don't forget to reply, give kudos, and accept as solution.~**
0 Kudos