cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Observer
Observer
5,641 Views
Registered: ‎09-28-2016

Calloc, Malloc and free

Hello Experts

 

How can I avoid Malloc, Calloc and Free in Vivado HLS.

 

Thanks in Advance.

0 Kudos
14 Replies
Highlighted
Contributor
Contributor
5,624 Views
Registered: ‎04-12-2017

Just create your arrays with the maximum size you will need?!
0 Kudos
Highlighted
Advisor
Advisor
5,613 Views
Registered: ‎04-26-2015

There are two main situations for using dynamic memory allocation.

 

One is where you will definitely have to allocate some RAM, but you don't know how much. For example, you might need to store a few lines of an image, but until the program runs you don't know how long the lines are. In this case you would select a maximum length for those lines (eg. 2048 pixels) and statically allocate a buffer of that size. That is, instead of this:

 

 

pixel * x = malloc(sizeof(pixel) * line_length);

 

you would write:

 

 

pixel x[MAX_LINE_LENGTH];

 

 

The second situation is where you're not sure if you'll need to allocate memory at all, and the maximum size is largely unbounded. For example, you're storing a list of objects found in a video as a linked list. Often there's a desire to have the stored objects be of a variable size, which introduces fragmentation. There's no really good way to deal with this; you must have a bounded size to use HLS, and dealing with fragmentation requires a whole lot of complexity which isn't easy to do in HLS.

Highlighted
Scholar
Scholar
5,593 Views
Registered: ‎03-22-2016

 @shivanagis

Just to complement to @u4223374 excellent response below, when you have the need for an unbounded size what you really want is to store it in RAM because there is very limited on-chip memory on FPGAs.

As alternative, you might be looking at a possible stream processing case so you should consider doing it in blocks, storing intermediate state.

 

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
Highlighted
Advisor
Advisor
5,560 Views
Registered: ‎04-26-2015

I definitely agree with @hbucher regarding storing data in RAM. It might be a bit slower, but it greatly eases the need to make everything a small as possible.

 

 

The really horrible case for HLS is where you have a linked list or tree structure where nodes are randomly added or removed. In normal C it's easy - you just allocate nodes with malloc/calloc and let the OS and hardware memory management take care of finding some space. Most of the time it's not even necessary to actually check that the allocation worked because the OS ensures that you have an almost unlimited amount of RAM to play with.

 

HLS cannot do that at all - you have to write your own memory controller. If the nodes are all the same size then this isn't all that difficult, but it is tedious (and if you're doing it on-chip then you definitely do need to pay attention to how much RAM you're using). If the nodes aren't the same size then it's probably going to be easier to just implement your algorithm on a CPU (Zynq PS or MicroBlaze) and let the Linux memory controller do all the hard work.

Highlighted
Observer
Observer
5,537 Views
Registered: ‎09-28-2016

Hello @u4223374,

 

Thanks for your time.

 

bello two line are in my code for calloc and melloc

1)newdata=calloc((unsigned int)i,elemements);

2)*pEmxArray=(emxArray_int32_T *(malloc (sizeof(emxArray_int32_t)); where emxArray_int32_t is a structure.

 

please help me in how can I replace these two line in code for Vivado HLS syntesis.

 

Thanks in advance. 

0 Kudos
Highlighted
Scholar
Scholar
5,530 Views
Registered: ‎03-22-2016

Very broad answer but you could use streaming. Check the Vivado HLS User Guide UG902 in particular from page 371. https://www.xilinx.com/support/documentation/sw_manuals/xilinx2017_2/ug902-vivado-high-level-synthesis.pdf
vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
Highlighted
Advisor
Advisor
5,526 Views
Registered: ‎04-26-2015

@shivanagis

 

The direct conversion would be something like this:

int newdata[elelemements];
for (int i = 0; i < elelemements; i++) {
newdata[i] = 0;
} emxArray_int32_t pEmxArray;

However, this depends very heavily on the surrounding code.

 

(1) Looks like you may be allocating space on-the-fly to deal with incoming data. HLS cannot do this and the code will require a major rewrite. @hbucher's excellent suggestion to investigate streams is likely to be very relevant.

 

(2) I have a suspicion that what you have here is an array of pointers, each element of which contains pointers. Again, HLS simply cannot handle this. I'm pretty sure that HLS will simply refuse to accept this, and fixing it will require a code redesign.

 

Apart from that, the variable names suggest that you're doing Matlab-to-C-to-HDL. This is rarely a good route to take with automated tools; while the Matlab Coder tool can produce C code that's adequate for a C compiler, it won't be HLS-friendly. It's better to think of HLS as using a different language that happens to share syntax with C, rather than as being a pure C-to-HDL tool.

0 Kudos
Highlighted
Observer
Observer
5,522 Views
Registered: ‎09-28-2016

@u4223374

 

Exactly you are right, the C code is MATLab to C.  In MATLab one keyword called 'bwlabel' is there. Actually I wanted to implement that. Please can you suggest that how can I implement 'bwlabel' in C.

 

 

Thanks in advance.

0 Kudos
Highlighted
Advisor
Advisor
5,514 Views
Registered: ‎04-26-2015

My advice would be "don't bother".

Blob labelling is one of those things that superficially looks perfect for an FPGA (hey, it's image processing, right?) but realistically is not really very well-suited at all. It requires repeated non-sequential access to RAM (perfect for something like a Zynq CPU where you've got a nice cache waiting to serve that data up, not great for the PL unless you do a lot of caching). It needs to do frequent read-modify-write access on RAM (again, great for a CPU with a data cache), and many algorithms (eg. flood-fill, which is a beautiful recursive algorithm on a CPU) have to allocate large amounts of memory on-the-fly (which HLS cannot do).

You can definitely do it in the PL, but I suspect this is a situation where the CPU will actually be faster - and it'll be far less work too.

0 Kudos
Highlighted
Observer
Observer
4,186 Views
Registered: ‎09-28-2016

@u4223374 Yes I am writing the face detection code and choosing the Zedboard(Evaluation board).

 

Thank you in advance.

0 Kudos
Highlighted
Scholar
Scholar
4,180 Views
Registered: ‎03-22-2016

@u4223374  One could use AXI System Cache - as used inside the Microblaze hier - to interface with external DDR. 

I am sure we can do better given the application specifics but this would greatly enhance certain patterns of access for sure.

https://www.xilinx.com/support/documentation/ip_documentation/system_cache/v4_0/pg118-system-cache.pdf

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
Highlighted
Advisor
Advisor
4,165 Views
Registered: ‎04-26-2015

@hbucher I never knew about that IP! It could definitely help. Anything that prevents continual read-write operations from RAM will be beneficial.

 

@shivanagis I suggest reading up on some papers that cover hardware implementation of blob labelling (if you still want to do it in PL). That's likely to give you a far better result than trying to blindly implement Matlab's algorithm.

0 Kudos
Highlighted
Scholar
Scholar
4,163 Views
Registered: ‎03-22-2016

@u4223374  I knew something you did not know? Wow I will take that as a real compliment! 

 

 

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
0 Kudos
Highlighted
Advisor
Advisor
4,149 Views
Registered: ‎04-26-2015

@hbucher I'm sure that you know plenty about this that I don't know! But you only see me posting about things that I do know about.

 

The challenge is to find something that Chapman doesn't know about Picoblaze (he wrote it) or that Austin (Xilinx's Principal Engineer) doesn't know about the FPGA hardware. I think the latter has actually happened once on these forums, although the thread will be deeply buried.

0 Kudos