clBLAS 2.0
kerngen.h
1/* ************************************************************************
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * ************************************************************************/
16
17
18/*
19 * Kernel generator related common definitions
20 */
21
22#ifndef KERNGEN_H_
23#define KERNGEN_H_
24
25#include <sys/types.h>
26#include <errno.h>
27
28#if defined (_MSC_VER)
29#include <msvc.h>
30#endif
31
32#include <defbool.h>
33#include <list.h>
34#include <cltypes.h>
35#include <mutex.h>
36#include <granulation.h>
37#include <trace_malloc.h>
38
44
45#ifdef _MSC_VER
46#define SPREFIX "I"
47#else
48#define SPREFIX "z"
49#endif
50
51#define SUBDIM_UNUSED (size_t)-1
52
53enum {
54 MAX_TABS = 16,
55 MAX_STATEMENT_PRIORITY = 63,
56 MAX_STATEMENT_LENGTH = 4096
57};
58
59enum {
60 // maximum subproblem dimensions
61 MAX_SUBDIMS = 3,
62 // maximum code nesting
63 MAX_NESTING = 10,
64 KSTRING_MAXLEN = 256,
65 // generated function name max len
66 FUNC_NAME_MAXLEN = KSTRING_MAXLEN
67};
68
69typedef struct{
70 SubproblemDim subdims[MAX_SUBDIMS];
71 PGranularity pgran;
72}DecompositionStruct;
73
74struct KgenContext;
75struct KgenGuard;
76struct StatementBatch;
77
84
89typedef enum CLMemFence {
91 CLK_LOCAL_MEM_FENCE,
93 CLK_GLOBAL_MEM_FENCE
94} CLMemFence;
95
96// TODO: deprecate
97typedef enum UptrType {
98 UPTR_GLOBAL,
99 UPTR_LOCAL,
100 UPTR_PRIVATE
101} UptrType;
102
107typedef struct Kstring {
109 char buf[KSTRING_MAXLEN];
110} Kstring;
111
116typedef int
117(*LoopUnrollGen)(struct KgenContext *ctx, void *priv);
118
125typedef struct LoopCtl {
126 const char *ocName;
127 union {
128 const char *name;
129 unsigned long val;
130 } outBound;
131 bool obConst;
132 unsigned long inBound;
133} LoopCtl;
134
139typedef struct LoopUnrollers {
141 LoopUnrollGen preUnroll;
143 LoopUnrollGen genSingleVec;
145 LoopUnrollGen genSingle;
147 LoopUnrollGen postUnroll;
149 LoopUnrollGen getVecLen;
150} LoopUnrollers;
151
154static __inline void
155emptyKstring(Kstring *kstr)
156{
157 kstr->buf[0] = '\0';
158}
159
160static __inline bool
161isKstringEmpty(const Kstring *kstr)
162{
163 return (kstr->buf[0] == '\0');
164}
165
172
189struct KgenContext
190*createKgenContext(char *srcBuf, size_t srcBufLen, bool fmt);
191
198void
199destroyKgenContext(struct KgenContext *ctx);
200
210void
211resetKgenContext(struct KgenContext *ctx);
212
232int
233kgenSyncFormatting(
234 struct KgenContext *srcCtx,
235 const struct KgenContext *dstCtx,
236 int nrTabs);
237
250int
251kgenDeclareFunction(struct KgenContext *ctx, const char *decl);
252
263int
264kgenBeginFuncBody(struct KgenContext *ctx);
265
277int
278kgenEndFuncBody(struct KgenContext *ctx);
279
292int
293kgenGetLastFuncName(
294 char *buf,
295 size_t buflen,
296 const struct KgenContext *ctx);
297
312int
313kgenBeginBranch(struct KgenContext *ctx, const char *stmt);
314
329int
330kgenEndBranch(struct KgenContext *ctx, const char *stmt);
331
347int
348kgenAddStmt(struct KgenContext *ctx, const char *stmt);
349
350int
351kgenPrintf(struct KgenContext *ctx, const char *fmt,...);
352
353struct StatementBatch
354*createStmtBatch(void);
355
356int
357kgenAddStmtToBatch(
358 struct StatementBatch *batch,
359 int priority,
360 const char *stmt);
361
362int
363kgenBatchPrintf(
364 struct StatementBatch *batch,
365 int priority,
366 const char *fmt,...);
367
368int
369flushStmtBatch(struct KgenContext *ctx, struct StatementBatch *batch);
370
371void
372destroyStmtBatch(struct StatementBatch *batch);
373
383int
384kgenAddBlankLine(struct KgenContext *ctx);
385
396size_t
397kgenSourceSize(struct KgenContext *ctx);
398
407
417int
418kgenAddBarrier(struct KgenContext *ctx, CLMemFence fence);
419
429int
430kgenAddMemFence(struct KgenContext *ctx, CLMemFence fence);
431
445int
446kgenDeclareLocalID(
447 struct KgenContext *ctx,
448 const char *lidName,
449 const PGranularity *pgran);
450
464int
465kgenDeclareGroupID(
466 struct KgenContext *ctx,
467 const char *gidName,
468 const PGranularity *pgran);
469
470/*
471 * TODO: deprecate when casting is eliminated
472 *
473 * declare unified pointers
474 *
475 * @withDouble: double based types pointers area needed
476 *
477 * On success returns 0, on buffer overflowing returns -EOVERFLOW
478 */
479int
480kgenDeclareUptrs(struct KgenContext *ctx, bool withDouble);
481
490
517int
518kgenLoopUnroll(
519 struct KgenContext *ctx,
520 LoopCtl *loopCtl,
521 DataType dtype,
522 const LoopUnrollers *unrollers,
523 void *priv);
524
540struct KgenGuard
541*createKgenGuard(
542 struct KgenContext *ctx,
543 int (*genCallback)(struct KgenContext *ctx, const void *pattern),
544 size_t patSize);
545
556void
557reinitKgenGuard(
558 struct KgenGuard *guard,
559 struct KgenContext *ctx,
560 int (*genCallback)(struct KgenContext *ctx, const void *pattern),
561 size_t patSize);
562
583int
584findGenerateFunction(
585 struct KgenGuard *guard,
586 const void *pattern,
587 char *name,
588 size_t nameLen);
589
596void
597destroyKgenGuard(struct KgenGuard *guard);
598
607
608void
609kstrcpy(Kstring *kstr, const char *str);
610
611void
612ksprintf(Kstring *kstr, const char *fmt,...);
613
614void
615kstrcatf(Kstring *kstr, const char *fmt,...);
616
617// unified pointer type name
618const char
619*uptrTypeName(UptrType type);
620
633char
634dtypeToPrefix(DataType type);
635
644const char
645*dtypeBuiltinType(DataType dtype);
646
655const char
656*dtypeUPtrField(DataType dtype);
657
666const char
667*strOne(DataType dtype);
668
682void
683getVectorTypeName(
684 DataType dtype,
685 unsigned int vecLen,
686 const char **typeName,
687 const char **typePtrName);
688
691#endif /* KERNGEN_H_ */