-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-example_8cpp-example.html
110 lines (108 loc) · 15.5 KB
/
basic-example_8cpp-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CYNQ: basic-example.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="logo.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">CYNQ
 <span id="projectnumber">0.1.0</span>
</div>
<div id="projectbrief">Framework to develop FPGA applications in C++ with the easiness of PYNQ</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('basic-example_8cpp-example.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">basic-example.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>This is a sample use case for the CYNQ. It represents a matrix-multiplication from FAL</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * See LICENSE for more information about licensing</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> * Copyright 2023</span></div><div class="line"><span class="comment"> * Author: Luis G. Leon-Vega <[email protected]></span></div><div class="line"><span class="comment"> * Diego Arturo Avila Torres <[email protected]></span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <cstdint></span></div><div class="line"><span class="preprocessor">#include <iostream></span></div><div class="line"><span class="preprocessor">#include <memory></span></div><div class="line"><span class="preprocessor">#include <string></span></div><div class="line"></div><div class="line"><span class="preprocessor">#include <cynq/accelerator.hpp></span></div><div class="line"><span class="preprocessor">#include <cynq/datamover.hpp></span></div><div class="line"><span class="preprocessor">#include <cynq/hardware.hpp></span></div><div class="line"><span class="preprocessor">#include <cynq/memory.hpp></span></div><div class="line"></div><div class="line"><span class="preprocessor">#if !defined(EXAMPLE_BITSTREAM_LOCATION) || \</span></div><div class="line"><span class="preprocessor"> !defined(EXAMPLE_DEFAULT_XCLBIN_LOCATION)</span></div><div class="line"><span class="preprocessor">#error "Missing location macros for example"</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="comment">// Given by the example</span></div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">char</span> kBitstream[] = EXAMPLE_BITSTREAM_LOCATION;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">char</span> kXclBin[] = EXAMPLE_DEFAULT_XCLBIN_LOCATION;</div><div class="line"></div><div class="line"><span class="comment">// Given by the design</span></div><div class="line"><span class="keyword">static</span> constexpr uint64_t kAccelAddress = EXAMPLE_ACCEL_ADDR;</div><div class="line"><span class="keyword">static</span> constexpr uint64_t kDmaAddress = EXAMPLE_DMA_ADDR;</div><div class="line"><span class="keyword">static</span> constexpr uint64_t kAddrWriteInputCols = 24;</div><div class="line"><span class="keyword">static</span> constexpr uint64_t kAddrWriteOutputCols = 40;</div><div class="line"><span class="keyword">static</span> constexpr uint64_t kAddrReadInputCols = 32;</div><div class="line"><span class="keyword">static</span> constexpr uint64_t kAddrReadOutputCols = 48;</div><div class="line"></div><div class="line"><span class="comment">// Data parameters</span></div><div class="line"><span class="keyword">using</span> DataType = uint16_t;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> input_a_cols = 400;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> input_a_rows = 2;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> input_b_cols = 4;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> input_b_rows = input_a_cols;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> output_cols = input_b_cols;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> output_rows = input_a_rows;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> word_size = <span class="keyword">sizeof</span>(DataType);</div><div class="line"></div><div class="line"><span class="comment">// Fill data</span></div><div class="line"><span class="keywordtype">void</span> FillData(DataType* A, DataType* B, DataType* C) {</div><div class="line"> <span class="keywordflow">for</span> (uint32_t row = 0; row < input_b_cols; ++row) {</div><div class="line"> <span class="keywordflow">for</span> (uint32_t col = 0; col < input_a_cols; ++col) {</div><div class="line"> A[((row % input_a_rows) * input_a_cols) + (col % input_a_cols)] =</div><div class="line"> row * col;</div><div class="line"> B[((col % input_b_rows) * input_b_cols) + (row % input_b_cols)] =</div><div class="line"> row * col;</div><div class="line"> C[((row % output_rows) * output_cols) + (col % output_cols)] = 0;</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Print results</span></div><div class="line"><span class="keywordtype">void</span> PrintData(DataType* C) {</div><div class="line"> std::cout << <span class="stringliteral">"Output: "</span> << std::endl;</div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < output_rows; ++i) {</div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < output_cols; ++j) {</div><div class="line"> std::cout << C[i * output_cols + j] << <span class="stringliteral">" "</span>;</div><div class="line"> }</div><div class="line"> std::cout << std::endl;</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() {</div><div class="line"> <span class="comment">// NOTE: This is a basic example. Error checking has been removed to keep</span></div><div class="line"> <span class="comment">// simplicity but it is always recommended</span></div><div class="line"> <span class="keyword">using namespace </span><a class="code" href="namespacecynq.html">cynq</a>; <span class="comment">// NOLINT</span></div><div class="line"></div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">size_t</span> input_size =</div><div class="line"> (input_a_cols * input_a_rows + input_b_cols * input_b_rows) * word_size;</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">size_t</span> output_size = output_cols * output_rows * word_size;</div><div class="line"></div><div class="line"> <span class="comment">// Create the platform</span></div><div class="line"> std::cout << <span class="stringliteral">"----- Initialising platform -----"</span> << std::endl;</div><div class="line"> std::shared_ptr<IHardware> platform =</div><div class="line"> <a name="a0"></a><a class="code" href="classcynq_1_1IHardware.html#ab3cc41fbed5a6da306d8ae51153a3065">IHardware::Create</a>(HardwareArchitecture::UltraScale, kBitstream, kXclBin);</div><div class="line"></div><div class="line"> <span class="comment">// Get an accelerator</span></div><div class="line"> std::shared_ptr<IAccelerator> accel = platform->GetAccelerator(kAccelAddress);</div><div class="line"> <span class="comment">// Get a data mover</span></div><div class="line"> std::shared_ptr<IDataMover> mover = platform->GetDataMover(kDmaAddress);</div><div class="line"></div><div class="line"> <span class="comment">// Create buffers for input and output</span></div><div class="line"> std::cout << <span class="stringliteral">"----- Creating memory -----"</span> << std::endl;</div><div class="line"> std::shared_ptr<IMemory> in_mem = mover->GetBuffer(input_size);</div><div class="line"> std::shared_ptr<IMemory> out_mem = mover->GetBuffer(output_size);</div><div class="line"></div><div class="line"> <span class="comment">// Get the host pointers for input/outut</span></div><div class="line"> std::cout << <span class="stringliteral">"----- Loading input -----"</span> << std::endl;</div><div class="line"> DataType* A = in_mem->HostAddress<DataType>().<span class="keyword">get</span>();</div><div class="line"> DataType* B = A + (input_a_cols * input_a_rows);</div><div class="line"> DataType* C = out_mem->HostAddress<DataType>().<span class="keyword">get</span>();</div><div class="line"></div><div class="line"> <span class="comment">// Fill the data</span></div><div class="line"> FillData(A, B, C);</div><div class="line"></div><div class="line"> <span class="comment">// Synchronise data buffer</span></div><div class="line"> std::cout << <span class="stringliteral">"----- Synchronise Input -----"</span> << std::endl;</div><div class="line"> in_mem->Sync(SyncType::HostToDevice);</div><div class="line"></div><div class="line"> <span class="comment">// Start the accel</span></div><div class="line"> accel->Start(StartMode::Continuous);</div><div class="line"> <span class="comment">// Check the control register</span></div><div class="line"> <span class="keyword">auto</span> devstatus = accel->GetStatus();</div><div class="line"> std::cout << <span class="stringliteral">"\tAccel Status: "</span> << <span class="keyword">static_cast<</span><span class="keywordtype">int</span><span class="keyword">></span>(devstatus) << std::endl;</div><div class="line"></div><div class="line"> std::cout << <span class="stringliteral">"----- Configuring accelerator -----"</span> << std::endl;</div><div class="line"> <span class="comment">// Configure params</span></div><div class="line"> accel->Write(kAddrWriteInputCols, &input_a_cols, 1);</div><div class="line"> accel->Write(kAddrWriteOutputCols, &output_cols, 1);</div><div class="line"> <span class="comment">// Check params</span></div><div class="line"> <span class="keywordtype">int</span> res_input_a_cols = 0;</div><div class="line"> <span class="keywordtype">int</span> res_output_cols = 0;</div><div class="line"> accel->Read(kAddrReadInputCols, &res_input_a_cols, 1);</div><div class="line"> accel->Read(kAddrReadOutputCols, &res_output_cols, 1);</div><div class="line"></div><div class="line"> std::cout << <span class="stringliteral">"----- Moving the data -----"</span> << std::endl;</div><div class="line"> <span class="comment">// Move the data</span></div><div class="line"> mover->Upload(in_mem, in_mem->Size(), 0, ExecutionType::Sync);</div><div class="line"> mover->Download(out_mem, out_mem->Size(), 0, ExecutionType::Sync);</div><div class="line"></div><div class="line"> <span class="comment">// Stop the accel</span></div><div class="line"> accel->Stop();</div><div class="line"></div><div class="line"> std::cout << <span class="stringliteral">"----- Synchronising output -----"</span> << std::endl;</div><div class="line"> out_mem->Sync(SyncType::DeviceToHost);</div><div class="line"></div><div class="line"> <span class="comment">// Read the output on *out_data*</span></div><div class="line"> PrintData(C);</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.13 </li>
</ul>
</div>
</body>
</html>