This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest-amxtile.c
168 lines (143 loc) · 3.57 KB
/
test-amxtile.c
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
//==============================================================
// Copyright © 2022 Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#include <immintrin.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdbool.h>
#define MAX 1024
#define MAX_ROWS 16
#define MAX_COLS 64
#define STRIDE 64
#define ARCH_GET_XCOMP_PERM 0x1022
#define ARCH_REQ_XCOMP_PERM 0x1023
#define XFEATURE_XTILECFG 17
#define XFEATURE_XTILEDATA 18
//Define tile config data structure
typedef struct __tile_config
{
uint8_t palette_id;
uint8_t start_row;
uint8_t reserved_0[14];
uint16_t colsb[16];
uint8_t rows[16];
} __tilecfg;
/* Initialize tile config */
static void init_tile_config (__tilecfg *tileinfo)
{
int i;
tileinfo->palette_id = 1;
tileinfo->start_row = 0;
for (i = 0; i < 1; ++i)
{
tileinfo->colsb[i] = MAX_ROWS;
tileinfo->rows[i] = MAX_ROWS;
}
for (i = 1; i < 4; ++i)
{
tileinfo->colsb[i] = MAX_COLS;
tileinfo->rows[i] = MAX_ROWS;
}
_tile_loadconfig (tileinfo);
}
/* Initialize int8_t buffer */
static void init_buffer (int8_t *buf, int8_t value)
{
int rows, colsb, i, j;
rows = MAX_ROWS;
colsb = MAX_COLS;
for (i = 0; i < rows; i++)
for (j = 0; j < colsb; j++)
{
buf[i * colsb + j] = value;
}
}
/* Initialize int32_t buffer */
static void init_buffer32 (int32_t *buf, int32_t value)
{
int rows, colsb, i, j;
rows = MAX_ROWS;
colsb = MAX_COLS;
int colsb2=colsb/4;
for (i = 0; i < rows; i++)
for (j = 0; j < (colsb2); j++)
{
buf[i * colsb2 + j] = value;
}
}
/* Set_tiledata_use() - Invoke syscall to set ARCH_SET_STATE_USE */
static bool set_tiledata_use()
{
if (syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA))
{
printf("\n Fail to do XFEATURE_XTILEDATA \n\n");
return false;
}
else
{
printf("\n TILE DATA USE SET - OK \n\n");
return true;
}
return true;
}
/* Print int8_t buffer */
static void print_buffer(int8_t* buf, int32_t rows, int32_t colsb)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < (colsb); j++)
{
printf("%d ", buf[i * colsb + j]);
}
printf("\n");
}
printf("\n");
}
/* Print int32_t buffer */
static void print_buffer32(int32_t* buf, int32_t rows, int32_t colsb)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < (colsb); j++)
{
printf("%d ", buf[i * colsb + j]);
}
printf("\n");
}
printf("\n");
}
int main(){
__tilecfg tile_data = {0};
int8_t src1[MAX];
int8_t src2[MAX];
int32_t res[MAX/4];
int rows = MAX_ROWS;
int colsb = MAX_COLS;
// Request permission to linux kernel to run AMX
if (!set_tiledata_use())
exit(-1);
// Load tile configuration
init_tile_config (&tile_data);
// Init src matrix buffers with data
init_buffer (src1, 2);
print_buffer(src1, rows, colsb);
init_buffer (src2, 2);
print_buffer(src2, rows, colsb);
// Init dst matrix buffers with data
init_buffer32 (res, 0);
// Load tile rows from memory
_tile_loadd (2, src1, STRIDE);
_tile_loadd (3, src2, STRIDE);
_tile_loadd (1, res, STRIDE);
// Compute dot-product of bytes in tiles
_tile_dpbssd (1, 2, 3);
// Store the tile data to memory
_tile_stored (1, res, STRIDE);
print_buffer32(res, rows, colsb/4);
// Release the tile configuration to return to the init state,
// which releases all storage it currently holds
_tile_release ();
}