summaryrefslogtreecommitdiff
path: root/external/clBLAS/src/library/blas/gens/fetch.h
blob: fe33fe03d953a96ff6c2076b8502e5b712c8ea15 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* ************************************************************************
 * Copyright 2013 Advanced Micro Devices, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ************************************************************************/


#ifndef FETCH_H_
#define FETCH_H_

/**
 * @internal
 * @defgroup FETCH_GEN Generating fetches from memory
 * @ingroup BLAS_GENERATORS
 */

/*@{*/

/**
 * @internal
 * @brief Context for the fetch generator
 */
struct FetchContext;

struct BlasGenSettings;
//enum TailStatus;

// FIXME: Deprecated. Throw later
struct TileMulOpts;

/**
 * @internal
 * @brief Optimization levels for the fetch generator with witch the caller
 *        can control some aspects of the code generation.
 *
 * !!NOTE: At expanding this list, the levels must be placed in ascending
 *         order of their importance.
 *
 * @ingroup BLAS_MAJOR_SUBGENS
 */
typedef enum FetchOptLevel {
    /** Expand the fetch loop in the way providing a prefetch effect */
    FOPTLEV_PREFETCH = 0x01,
    /**
     * Can share temporary coordinates for A and B. Usable in case when
     * A and fetches are fired sequentially and hence in some cases can
     * share the same temporary coordinates. Must be set only if fetch
     * has been already fired for one of the tiles. Otherwise result is
     * undefined.
     */
    FOPTLEV_CAN_SHARE_TMP_AB = 0x02,
    /**
     * Reorder generated statements so as fethes would be groupped
     * all together
     */
    FOPTLEV_MERGE_FETCHES = 0x04,
    /** Enable using of temporary precomputed coordinates */
    FOPTLEV_TMP_COORD_PRECOMPUTING = 0x08,
    /** Enable using of persistent precomputed coordinates */
    FOPTLEV_PERS_COORD_PRECOMPUTING = 0x10
} FetchOptLevel;

/**
 * @internal
 * @brief Addressing modes for the fetch generator
 */
typedef enum FetchAddrMode {
    /**
     * Normal mode. Fetching is performed only with full vectors.
     * Physical coordinates in memory are absolute for the matrices and
     * evaluated only based on the logical coordinates along rows of the
     * matrix \b A, columns of the matrix \b B and coordinate along K
     */
    FETCH_ADDR_NORMAL = 0,
    /**
     * Pointer for the matrix A is set at start of the tile panel.
     * All resulting coordinates will be relative against this base.
     * KernelVarNames::CoordA the generator settings structure is not used
     */
    FETCH_ADDR_A_RELATIVE = 0x01,
    /**
     * Pointer for the matrix B is set at start of the tile panel.
     * All resulting coordinates will be relative against this base.
     * KernelVarNames::CoordB the generator settings structure is not used
     */
    FETCH_ADDR_B_RELATIVE = 0x02,
    /**
     * Pointers for A and B match the current coordinate along dimension K and
     * thus set at the beginning of the tile. All resulting coordinates will be
     * relative against the current value of the pointers.
     * KernelVarNames::CoordA, KernelVarNames::coordB and KernelVarNames
     * accessible via the generator settings structure are not used
     */
    FETCH_ADDR_K_RELATIVE = 0x04,
    /**
     * Cyclical addressing along rows of \b A. That means substracting
     * number of rows from the coordinate in case of exceeding it.
     */
    FETCH_ADDR_A_CYCLICAL = 0x08,
    /** Cyclical addressing along columns of B */
    FETCH_ADDR_B_CYCLICAL = 0x10,
    /** Cyclical addressing along K dimension */
    FETCH_ADDR_K_CYCLICAL = 0x20,
    /**
     * Perform padding of the trailing part along dimension K.
     * That allows perform a vectorized fetch of tail including a piece being
     * outside the size along K. It affects only if K expands along the leading
     * dimension
     */
    FETCH_ADDR_TAILK_PADD = 0x40,
    /*
     * Expand loop with stride equal to witdth of the top level block
     */
    FETCH_ADDR_BW_STRIDE = 0x80
} FetchAddrMode;


// FIXME: Deprecated and should be thrown away later
union FetchTmpVarName {
    const char *idx;
    const char *uptr;
};

/**
 * @internal
 * @brief Specific settings for the fetching generator
 * @ingroup BLAS_MAJOR_SUBGENS
 */
typedef struct FetchOpts {
    MatrixRole mrole;
    CLMemType memA;             /**< type of memory matrix A is located on */
    CLMemType memB;             /**< type of memory matrix B is located on */
    unsigned int lineOffset;
    unsigned int linesNum;
    const char *regName;        // TODO: the field is deprecated. Remove it

    /*
     * FIXME: one more klugde for backward compatibility; get addressing
     *        mode from the options of tilemul
     */
    const struct TileMulOpts *mulOpts;

    // TODO: All the following fields are deprecated. Remove it
    union FetchTmpVarName tmpYvar;
    union FetchTmpVarName tmpXvar;
    const char *alvM;     /**< vecLen-aligned M in vectors */
    const char *alvN;     /**< vecLen-aligned N in vectors */
    const char *alvKA;    /**< vecLen-aligned K in vectors of A */
    const char *avlKB;    /**< vecLen-aligned K in vectors of B */
    const char *ax;       /**< matrix A x coordinate, in vectors */
    const char *ay;       /**< matrix A y coordinate */
    const char *bx;       /**< matrix B x coordinate, in vectors */
    const char *by;       /**< matrix B y coordinate */
    const char *ldav;     /**< matrix A leading dimension, in vectors */
    const char *ldbv;     /**< matrix B leading dimension, in vectors */
    const char *skewArow; /**< matrix A rows skew */
    const char *skewAcol; /**< matrix A columns skew, in vectors */
    const char *skewBrow; /**< matrix A rows skew */
    const char *skewBcol; /**< matrix A columns skew, in vectors */
} FetchOpts;


/**
 * @internal
 * @brief Create context for the fetch generator
 *
 * After creation there are enabled optimization levels relating
 * to precomputing with storing to temporary coordinates.
 * Addressing mode is set to ::FETCH_ADDR_NORMAL
 *
 * @return pointer to a new context object on success, NULL otherwise
 */
struct FetchContext
*createFetchContext(void);

/**
 * @internal
 * @brief Destroy fetch generator context
 *
 * @param[out] fctx            Fetch generator context to destroy
 */
void
destroyFetchContext(struct FetchContext *fctx);

/**
 * @internal
 * @brief Get current fetch optimization levels
 *
 * @param[in] fctx              Fetch context
 */
FetchOptLevel
getFetchOptLevels(struct FetchContext *fctx);

/**
 * @internal
 * @brief Enable needed code optimization levels the fetch generator
 *
 * @param[out] ctx              Generator context
 * @param[in] opts              Fetch Options
 */
void
enableFetchOptLevels(struct FetchContext *fctx, FetchOptLevel levels);

/**
 * @internal
 * @brief Disable unneeded code optimization levels for the fetch generator
 *
 * @param[out] ctx              Generator context
 * @param[in] opts              Fetch Options
 */
void
disableFetchOptLevels(struct FetchContext *fctx, FetchOptLevel levels);

/**
 * @internal
 * @brief Get current addressing mode used by the fetch generator
 *
 * @param[in] fctx              Fetch context
 */
FetchAddrMode
getFetchAddrMode(const struct FetchContext *fctx);

/**
 * @internal
 * @brief Set addressing mode for the fetch generator
 *
 * @param[out] fctx             Fetch context
 * @param[in]  mode             Addressing mode to set
 */
void
setFetchAddrMode(struct FetchContext *fctx, FetchAddrMode mode);

/**
 * @internal
 * @brief Set default fetch addressing mode based on the problem specifics
 *
 * @param[out] fctx             Fetch context
 * @param[in]  gset             Generator settings
 * @param[in]  mask             Addressing mode mask
 * @param[in]  tailStatus       Tails handling status
 * @param[in]  processTailK     Flag showing if the tail part along the
 *                              dimension K is picked up or not.
 *
 * Primarily, the function checks if there are tails along rows of A,
 * columns of B, dimension K and if some tails are raised or not.
 * Based on this info and also taking into account fetch vector length,
 * it set appropriate addressing mode to don't exceed matrix bounds during
 * the fetch operations. If there are not "small" tails for rows of A and
 * columns of B is selects relative addressing for them. If there are not
 * "small" tails along K, it selects relative addressing for this dimension
 * as well.
 *
 * The addressing mode mask passed via the \b mask parameter is used to
 * not set addressing modes not suitable for callers. Resulting addressing
 * mode which is set is presented as bitwise AND of  a default value selected
 * by the function and bitwise negated value of the mask
 *
 * \b tailStatus is a bit mask of values consisting the #TailStatus enumeration.
 *
 * @return Addressing mode the function set during the last call.
 */
FetchAddrMode
setDefaultFetchAddrMode(
    struct FetchContext *fctx,
    const struct BlasGenSettings *gset,
    FetchAddrMode mask,
    int tailStatus,
    bool processTailK);

/**
 * @internal
 * @brief Prepare the fetch generator to generate efficient fetches
 *        within the K loop
 *
 * @param[out] genCtx           Generator context
 * @param[out] fetchCtx         Fetch context
 * @param[in] gset              Generator settings
 * @param[in] memA              Type of memory the matrix A is stored in
 * @param[in] memB              Type of memory the matrix B is stored in
 *
 * Basically, the function lets to declare all needed for work of the fetch
 * generator. If a user lots upon efficient fetching within the tilemul loop,
 * he should call the function before generating that loop.
 * If it is not invoked, the fetch generator produces a code in some default
 * way which may be far from efficient. The stuff prepared with the function is
 * valid only for one fetch call. If the user needs to use the same once again,
 * it may use revalidateFetchContext().
 */
int
prepareFetchLoop(
    struct KgenContext *genCtx,
    struct FetchContext *fetchCtx,
    const struct BlasGenSettings *gset,
    CLMemType memA,
    CLMemType memB);

/**
 * @internal
 * @brief Revalidate fetch context
 *
 * @param[out] fctx             Fetch context
 * @param[in]  mrole            Matrix to revalidate the context for
 *
 * Enable the fetch generator to use the stuff produces with the last call
 * of prepareFetch() once again.
 */
void
revalidateFetchContext(struct FetchContext *fctx, MatrixRole mrole);

/**
 * @internal
 * @brief Tile fetching generator
 *
 * @param[out] genCtx         Generator context
 * @param[in]  fetchCtx       FetchContext
 * @param[in]  gset           Generator settings
 * @param[in]  fetchOpts      Fetch-specific generator options
 *
 * This function generates code which fetches tile a or b from global or local
 * memory into private memory.\n
 * Generated code fetches tiles by vectors using coordinate values in vectors
 * from @ref FetchOpts.
 * Complex types and conjugated tiles are supported. Global cycling is supported
 * for global memory fetching - this mean that if tile overlaps matrix
 * the tail of tile will be fetched from the beginning instead of accessing
 * memory outside the matrix.\n
 * Second level of subdimensions is used for tile sizes.\n
 * Tile can be fetched from global memory or from local memory.
 * If tile is fetched from local memory then leading dimensions for local
 * memory area are taken from first level subdimensions.\n
 * Post-fetch callback generator function can be called after fetching tile
 * for zeroing tails or setting diagonal elements to one. This function is
 * provided by caller in @ref TileMulOpts.postFetch.\n
 * After the function completes its work it invalidates the fetch context, and
 * all the stuff that has been prepared before, will not be used in the next
 * fetch transaction.
 *
 * @return 0 on success
 * @return -EOVERFLOW on source buffer overflowing
 */
int
genFetchInputTile(
    struct KgenContext *genCtx,
    struct FetchContext *fetchCtx,
    const struct BlasGenSettings *gset,
    const FetchOpts *fetchOpts);

/**
 * @internal
 * @brief Fetch input tile
 *
 * @param[out] batch                    Statement batch
 * @param[in]  gset                     Generator settings
 * @param[in]  fetchOpts                Fetch Options
 *
 * The function has the same effect and semantics as the previous one,
 * but put the code to the intermediate statement batch rather than a target
 * generator context.
 */
void
genFetchInputTileBatch(
    struct StatementBatch *batch,
    struct FetchContext *fctx,
    const struct BlasGenSettings *gset,
    const FetchOpts *fetchOpts);

/*@}*/

#endif /* FETCH_H_ */