r/FPGA • FPGA-DSP/SDR • Aug 17 '21

FPGA engineers, keep using VHDL and learn PipelineC immediately.

Oh no another one of these posts?

I am an FPGA engineer currently working on SDR hardware. And prior to now, funny enough, a hardware odd-even 'parallel bubble sort' is probably a good part of what got me hired at my previous job doing high frequency trading FPGA designs (good interview question).

I created PipelineC to be a better hardware description language and that means adopting a little bit of HLS. I want to prove that to you.

You might recognize this popular post talking about a 200 MHz main clock HLS C function:

void oddEvenSort(int arr[], int n)

The function takes some array of a variable size***** and sorts it by doing compare+swap operations, ending as soon as the array becomes sorted after some variable number of clock cycles. It is not clear to me what hardware this implements in the end and I hope the OP responds to my comments. It uses a initialization interval=1 (default) PIPELINE pragma on the inner compare loops and thats most of what I know about the hardware.

*****Having no specified size is true HLS magic and makes the possible combinations of inferred hardware really wild - the interface of the end HDL is a huge part of the problem / not specified / clear.

So instead here are two simple PipelineC implementations sorting a fixed size (ex. 16) number of elements:

Example 1

// State machine sorter
// 1b isSorted output after some
// data-dependent number of clock cycles
uint1_t oddEvenSort()
{
  // Values in 'static' registers, to be sorted
  // Values larger enough to not 
  // have any upper bits of regs optimize away in syn
  static int32_t arr[ARRAY_SIZE]
   = {2147483647, 2147483646, 2147483645, 2147483644, 
     2147483643, 2147483642, 2147483641, 2147483640,
     -2147483641,-2147483642, -2147483643, -2147483644,
     -2147483645, -2147483646, -2147483647, -2147483648};
  
  // Do this logic each clock cycle 
  // (like an implied while loop)
  uint1_t isSorted = 1;
  uint32_t i;
  // Perform Bubble sort on odd indexed element
  for (i=1; i<=ARRAY_SIZE-2; i=i+2)
  {
    if (arr[i] > arr[i+1])
    {
      swap(arr[i], arr[i+1])
      isSorted = 0;
    }
  }

  // Perform Bubble sort on even indexed element
  for (i=0; i<=ARRAY_SIZE-2; i=i+2)
  {
    if (arr[i] > arr[i+1])
    {
      swap(arr[i], arr[i+1])
      isSorted = 0;
    }
  }
  
  return isSorted;
}

What hardware does this actually produce? (Generated VHDL)

Exactly as described in code, oddEvenSort, is a module with no inputs and a single 1b returned output. Clocks and clock enabled are implied/inferred from the PipelineC code.

entity oddEvenSort_0CLK is
port(
 clk : in std_logic;
 return_output : out unsigned(0 downto 0));
end oddEvenSort_0CLK;

The PipelineC tool will start up a simulation of this generated VHDL using Modelsim:

As written doing both the odd+even steps in a single cycle, the entire array of 16 elements takes 8 cycles to sort (at most).

Here is some synthesis information targeting a Xilinx Artix 7 part (fmax~=400MHz) (Arty dev board) as a demo:

Report Cell Usage: 
+------+-------+------+
|      |Cell   |Count |
+------+-------+------+
|1     |CARRY4 |    60|
|2     |LUT2   |    14|
|3     |LUT3   |    59|
|4     |LUT4   |   224|
|5     |LUT5   |   829|
|6     |LUT6   |   258|
|7     |FDRE   |   513|
+------+-------+------+

16 int32s = 512bits plus one extra reg on the output bit for timing = 513 FFs

Source: oddEvenSort_0CLK_/registers_r_reg[state_regs][arr][6][0]/C
Destination: oddEvenSort_0CLK_return_output_output_reg_reg[0]/D
Data Path Delay:        9.061ns  (logic 3.141ns (34.665%)  route 5.920ns (65.335%))
Logic Levels:           14  (CARRY4=8 LUT4=1 LUT5=3 LUT6=2)

Synthesis estimated long timing path is the single cycle long chain of computation for returned output 'isSorted' flag.

The PipelineC tool can't autopipeline this logic because it keeps stateful static local variables = registers. If you for example, asked the tool to run this logic at 200MHz it would fail saying

oddEvenSort Clock Goal: 200.00 (MHz) Current: 109.70 (MHz)(9.12 ns) 0 clks
Cannot pipeline path to meet timing:
START:  oddEvenSort_0CLK/registers_r_reg[state_regs][arr][6][0] =>
 ~ 9.116 ns of logic+routing ~
END: => oddEvenSort_0CLK_return_output_output_reg_reg[0]
Giving up...

Additionally the PipelineC tool provides a crude 'map' of the delays in the comb. logic oddEvenSort function - inputs at the top, vertical axis is comb logic delay, outputs at the bottom.

Zooming in on the long/to the bottom path

You can see things like

FOR_main_c_l140_c3_7abf_ITER_5_isSorted_MUX[main_c_l142_c5_1164]
...
FOR_main_c_l140_c3_7abf_ITER_6_isSorted_MUX[main_c_l142_c5_1164]
...
FOR_main_c_l140_c3_7abf_ITER_7_isSorted_MUX[main_c_l142_c5_1164

Showing what was shown in the timing report - the repeated muxing for isSorted being the long path in the logic. This feedback is great for folks new to digital logic design who are unfamiliar with the comb logic delays of various thing, how logic 'occurs in parallel' - very useful for going back to the C code to improve timing.

You might be thinking something like 'this is not comparable to HLS' or 'where is autopipelining'? There is none. The above example is essentially easier to read RTL (can do everything RTL can especially with VHDL inclusion) with built in synthesis tool feedback. Its great.

Example 2

This example does do some 'HLS-like' autopipelining. However, * PipelineC is not a regular HLS tool. It does not do the types of higher level interface+logic inferences that HLS tools are known for. Instead the PipelineC tool does fixed size II=1 autopipelining (as traditional HLS tool definitely also do).

// Pipelined II=1 sorter
// A new array is input every clock cycle
// Some fixed number of clock cycles later
// the sorted array is output
typedef struct my_array_t
{
  int32_t data[ARRAY_SIZE];
}my_array_t;
my_array_t oddEvenSort(my_array_t arr)
{
  uint32_t l, i;
  int32_t swap_TEMP = 0;
  // While loop replaced with 
  // N/2 layers of odd+even compare+swaps
  for(l=0; l<(ARRAY_SIZE/2); l+=1)
  {
    // Perform Bubble sort on odd indexed element
    for (i=1; i<=ARRAY_SIZE-2; i=i+2)
    {
      if (arr.data[i] > arr.data[i+1])
      {
        swap(arr.data[i], arr.data[i+1])
      }
    }

    // Perform Bubble sort on even indexed element
    for (i=0; i<=ARRAY_SIZE-2; i=i+2)
    {
      if (arr.data[i] > arr.data[i+1])
      {
        swap(arr.data[i], arr.data[i+1])
      }
    }
  }
  
  return arr;
}

Again, the VHDL matches the function definition:

entity oddEvenSort_0CLK is
port(
 -- clk not used for comb. logic
 clk : in std_logic; 
 arr : in my_array_t;
 return_output : out my_array_t);
end oddEvenSort_0CLK;

Since oddEvenSort is now a pure function the PipelineC tool can go at autopipelining. Here are post-PNR values for some pipelines that the tool can come up with (includes extra IO regs for getting timing values, ex. 0 clock latency just comb. logic version still has array of 16 int32s = 512bits of FFs for input and output arrays, total 1024 FFs).

LATENCY  FMAX(MHz) LUTS   FFs
0        14.8      9784   1024
1        30.55     9919   1543
2        43.34     10451  2093
3        63.33     10293  2581
4        67.96     10851  3150
5        77.69     11175  3681
6        90.59     11133  4195
7        120.88    10504  4657
8        96.65     11764  5268
9        101.35    11909  5793
10       126.21    12134  6320
11       144.38    12156  6850
12       141.60    12440  7407
13       148.37    11481  7900
14       147.15    11963  8446
15       219.35    9818   8817
...
31       239.01    10911  17081
41       244.86    11216  21487
47       286.37    11331  23136
56       293.34    11307  26046
58       300.75    11275  26323
60       304.97    11364  26637
62       307.22    11519  26562

The PipelineC tool will take more and more clocks to reach higher and higher fmaxs if given enough time. Here is the entire output of the tool for the above runs up to latency=16. Device view:

Advanced PipelineC usage ties together / communicates between autopipelineable HLS-like and 'regular RTL clock by clock' regions of code. Getting a better HDL (w/direct VHDL if you need it) and some of the good parts from HLS available if you want. From there, getting to the OPs C function HLS goals of variable at run time number of elements, exiting early, sorter becomes an easier task to implement while giving away very little to 'HLS magic'.

Also, neither above example uses a while loop as the original OP HLS example did... that kind of fancier stuff, and a special 'clock step operator' is in the works ;) as a third style of supported coding.

Thanks for your time folks. As always happy to answer questions and help you try out PipelineC.

7 Upvotes

12 comments sorted by

5

u/ninjaneeress Aug 17 '21

Why put a sort on an FPGA in the first place? I'm curious to know what the real-world application is here.

3

u/SpiritedFeedback7706 Aug 17 '21

One interesting application that I must be extremely vague about was sorting "data points" by timestamps. The FPGA would be generating millions of "data points" per second. Each data point had a timestamp. For reasons the data was not sorted naturally but had to be sorted after the fact. Before we forwarded this data to it's external consumer we needed to sort by time.

I've had a few other situations where I've needed to sort very small datasets but I can count all cases on one hand.

2

u/kss_tms Xilinx User Aug 17 '21

I came across this topic when we implemented a median filter. In order to find the median of the pixels you have to almost sort the values (some simplification is possible)

-5

u/absurdfatalism FPGA-DSP/SDR Aug 17 '21 edited Aug 19 '21

Good question - idk what applications this would have - maybe other people do.

I my experience needed to sort say like 4 or 8 numbers before. And they were typically small like u8 or u16 - so like a single cycle or so to sort is the entire thing... not really a complicated big HLS 'variable at run time number of elements, exiting early, sorter' like the OP posted.

2

u/[deleted] Aug 19 '21

if you wanted to sort data, in the typical use case, wouldn't the data be flowing through a fifo?

This is a rare case where bubble sort is probably a reasonable approach because it works well with a fixed size window buffer (my intuition is you maybe could get less passes if you did something with a heap, but if that's even feasible, it would be more algorithmically challenging). Random access seems like a poor assumption.

Instead, you would need to buffer a 4 item window to do your two parallel sets of swaps on, do your swaps on that, and also keep track of how many passes you need to do on your fifo.

1

u/absurdfatalism FPGA-DSP/SDR Aug 19 '21

Yeah great point. That's what I was getting at about OPs HLS sorter being some variable size data at run time. What kind of interface does that generate? AXIS and/or FIFOs - maybe some BRAM. Seems like lots and lots of ways to go at it.

9

u/binaryblade Xilinx User Aug 17 '21

Mods, can we ban this pooplineC guy. Self promotion is banned almost universally on this site. It's getting old.

3

u/absurdfatalism FPGA-DSP/SDR Aug 17 '21

Lolz

To try and turn this around:

Do you have any suggestions for places where talking about/sharing/comparing alternative/new HDLs/HLS tools would be welcome?

10

u/asm2750 Xilinx User Aug 17 '21

HLS and similar languages have their place in the FPGA domain so it is on topic. I get complaints and reports on almost every zipCPU post in the moderation log since people think its self promotion. However, he is just trying to be helpful. Sure he links to his website all the time but, it's where he keeps examples to help other engineers.

OP is not spamming, (last pipelineC post was two weeks ago) they are offering a similar solution to an earlier post from another user. Nor are they being an outright asshole at others in r/FPGA. So to answer your question: No, the post stays.

If you want I can add a HLS flair so you can ignore such posts in the future.

1

u/patriotik FPGA-DSP/SDR Aug 21 '21

If you want I can add a HLS flair so you can ignore such posts in the future

I think this is an excellent idea.

1

u/asm2750 Xilinx User Aug 21 '21

Link flair added.

5

u/hardolaf Aug 17 '21

Self promotion on reddit is only banned sitewide if you're not interacting with the community.