Repository navigation
Expand file tree
/
Copy pathexample.cpp
More file actions
185 lines (159 loc) · 5.59 KB
/
Copy pathexample.cpp
File metadata and controls
185 lines (159 loc) · 5.59 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/******************************************************************************
* Copyright (C) 2025 Advanced Micro Devices, Inc. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
#include "xaiengine.h"
#include <stdio.h>
#ifndef __AIESIM__
#include "xil_printf.h"
#include "xil_cache.h"
#endif
#if AIE_GEN <= 2
#define HW_GEN XAIE_DEV_GEN_AIEML
#else
#define HW_GEN XAIE_DEV_GEN_AIE2PS
#endif
#ifdef __AIELINUX__
#define OUT_COL 35
#elif defined(__AIESIM__)
#define OUT_COL 3
#elif AIE_GEN == 5
#define OUT_COL 10
#else
#define OUT_COL 6
#endif
// these are fallbacks, the actual values are injected via dm_offsets.h by aiehlc
#ifndef CORE_IP_MEM
#define CORE_IP_MEM 0x8000
#endif
#ifndef CORE_OP_MEM
#define CORE_OP_MEM 0x8800
#endif
__global__ void loop_kernel(int *in, int *out) {
for (int i = 0; i < 20; ++i)
*(out++) = 100 + i;
}
void routing(XAie_DevInst *) {}
int test_kernel(XAie_DevInst *DevInst) {
printf("\nLoading kernel 1...\n");
XAie_CoreReset(DevInst, XAie_TileLoc(4,4));
XAie_CoreUnreset(DevInst, XAie_TileLoc(4,4));
AieRC elfrc = XAie_LoadElfMem(DevInst, XAie_TileLoc(4, 4), (unsigned char *)loop_kernel);
printf("Finished (ELF rc=%d). Continuing...\n", (int)elfrc);
printf("\nRouting...\n");
XAie_RoutingInstance* routingInstance = XAie_InitRoutingHandler(DevInst);
XAie_Route(routingInstance, NULL, /*src=*/ XAie_TileLoc(2,0), /*dest=*/ XAie_TileLoc(4,4));
XAie_Route(routingInstance, NULL, /*src=*/ XAie_TileLoc(4,4), /*dest=*/ XAie_TileLoc(OUT_COL,0));
printf("Finished. Continuing...\n");
printf("\nAllocating DDR memory...\n");
int *in_ptr = 0, *out_ptr = 0;
const int len = 20;
XAie_MemInst *in = XAie_MemAllocate(DevInst, len * sizeof(u32), XAIE_MEM_CACHEABLE);
in_ptr = (int*)XAie_MemGetVAddr(in);
XAie_MemInst *out = XAie_MemAllocate(DevInst, len * sizeof(u32), XAIE_MEM_CACHEABLE);
out_ptr = (int*)XAie_MemGetVAddr(out);
printf("Finished. Continuing...\n");
printf("\nInitializing DDR memory...\n");
for(int i = 0; i < len; i++) {
in_ptr[i] = 9999;
out_ptr[i] = 9999;
}
XAie_MemSyncForDev(in);
printf("Finished. Continuing...\n");
printf("\nMoving input data to AIE...\n");
XAie_MoveDataExternal2Aie(routingInstance, /*src=*/ XAie_TileLoc(2,0),
in, len*sizeof(u32),
CORE_IP_MEM, /*dest=*/ XAie_TileLoc(4,4));
#if AIE_GEN == 5
XAie_RouteDmaWait(routingInstance, XAie_TileLoc(2, 0), XAie_TileLoc(4, 4), true);
#endif
printf("Finished. Continuing...\n");
printf("\nRunning kernel 1...\n");
XAie_Run(routingInstance, 1);
while (XAie_CoreWaitForDone(DevInst, XAie_TileLoc(4, 4), 0) != XAIE_OK) {
}
printf("Finished. Continuing...\n");
printf("\nMoving output data to CPU...\n");
XAie_MoveDataAie2External(routingInstance, /*src=*/ XAie_TileLoc(4,4),
CORE_OP_MEM, len*sizeof(u32),
out, /*dest=*/ XAie_TileLoc(OUT_COL,0));
#if AIE_GEN == 5
XAie_RouteDmaWait(routingInstance, XAie_TileLoc(4, 4), XAie_TileLoc(OUT_COL, 0), false);
#endif
XAie_MemSyncForCPU(out);
printf("Finished. Continuing...\n");
printf("\nVerifying output data...\n");
int mismatches = 0;
for (int i = 0; i < len; i++) {
if (100 + i != out_ptr[i]) {
printf("Mismatch at index %d: CPU=%d, AIE=%d\n", i, 100 + i, out_ptr[i]);
mismatches++;
} else {
printf("%d: CPU=%d, AIE=%d\n", i, 100 + i, out_ptr[i]);
}
}
if (mismatches == 0) {
printf("Sucess: CPU result matches AIE.\n");
printf("\n\nDone with kernel 1!\n\n");
return 0;
} else {
printf("Failure: There were %d mismatches.\n", mismatches);
printf("\n\nDone with kernel 1!\n\n");
return -1;
}
}
#define XAIE_BASE_ADDR 0x20000000000
#define XAIE_COL_SHIFT 25
#define XAIE_ROW_SHIFT 20
#define XAIE_NUM_ROWS 11
#define XAIE_NUM_COLS 38
#define XAIE_SHIM_ROW 0
#define XAIE_RES_TILE_ROW_START 1
#define XAIE_RES_TILE_NUM_ROWS 2
#define XAIE_AIE_TILE_ROW_START 3
#define XAIE_AIE_TILE_NUM_ROWS 8
int main(int argc, char* argv[]) {
#ifndef __AIESIM__
Xil_DCacheDisable();
Xil_ICacheDisable();
#endif
XAie_SetupConfig(ConfigPtr, HW_GEN, XAIE_BASE_ADDR,
XAIE_COL_SHIFT, XAIE_ROW_SHIFT,
XAIE_NUM_COLS, XAIE_NUM_ROWS, XAIE_SHIM_ROW,
XAIE_RES_TILE_ROW_START, XAIE_RES_TILE_NUM_ROWS,
XAIE_AIE_TILE_ROW_START, XAIE_AIE_TILE_NUM_ROWS);
XAie_InstDeclare(DevInst, &ConfigPtr);
AieRC RC = XAie_CfgInitialize(&DevInst, &ConfigPtr);
if(RC != XAIE_OK) {
printf("Driver initialization failed.\n");
return -1;
}
#ifdef __AIESIM__
XAie_SetIOBackend(&DevInst, XAIE_IO_BACKEND_SIM);
#else
XAie_SetIOBackend(&DevInst, XAIE_IO_BACKEND_BAREMETAL);
#if AIE_GEN >= 2
if(DevInst.Backend->Type == XAIE_IO_BACKEND_BAREMETAL) {
RC = XAie_UpdateNpiAddr(&DevInst, 0xF6D10000);
if(RC != XAIE_OK) {
printf("Failed to update NPI address\n");
return -1;
}
}
RC = XAie_PartitionInitialize(&DevInst, NULL);
if(RC != XAIE_OK) {
printf("Failed to initialize partition\n");
return -1;
}
#else
XAie_PmRequestTiles(&DevInst, NULL, 0);
#endif
#endif /* __AIESIM__ */
if(test_kernel(&DevInst) == 0){
printf("\nKernel test passed!\n");
return 0;
} else {
printf("\nKernel test failed!\n");
return -1;
}
}