clBLAS  2.11
 All Functions Typedefs Enumerations Enumerator Groups Pages
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 
53 enum {
54  MAX_TABS = 16,
55  MAX_STATEMENT_PRIORITY = 63,
56  MAX_STATEMENT_LENGTH = 4096
57 };
58 
59 enum {
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 
69 typedef struct{
70  SubproblemDim subdims[MAX_SUBDIMS];
71  PGranularity pgran;
72 }DecompositionStruct;
73 
74 struct KgenContext;
75 struct KgenGuard;
76 struct StatementBatch;
77 
84 
89 typedef enum CLMemFence {
91  CLK_LOCAL_MEM_FENCE,
93  CLK_GLOBAL_MEM_FENCE
94 } CLMemFence;
95 
96 // TODO: deprecate
97 typedef enum UptrType {
98  UPTR_GLOBAL,
99  UPTR_LOCAL,
100  UPTR_PRIVATE
101 } UptrType;
102 
107 typedef struct Kstring {
109  char buf[KSTRING_MAXLEN];
110 } Kstring;
111 
116 typedef int
117 (*LoopUnrollGen)(struct KgenContext *ctx, void *priv);
118 
125 typedef 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 
139 typedef struct LoopUnrollers {
141  LoopUnrollGen preUnroll;
143  LoopUnrollGen genSingleVec;
145  LoopUnrollGen genSingle;
147  LoopUnrollGen postUnroll;
149  LoopUnrollGen getVecLen;
150 } LoopUnrollers;
151 
154 static __inline void
155 emptyKstring(Kstring *kstr)
156 {
157  kstr->buf[0] = '\0';
158 }
159 
160 static __inline bool
161 isKstringEmpty(const Kstring *kstr)
162 {
163  return (kstr->buf[0] == '\0');
164 }
165 
172 
189 struct KgenContext
190 *createKgenContext(char *srcBuf, size_t srcBufLen, bool fmt);
191 
198 void
199 destroyKgenContext(struct KgenContext *ctx);
200 
210 void
211 resetKgenContext(struct KgenContext *ctx);
212 
232 int
233 kgenSyncFormatting(
234  struct KgenContext *srcCtx,
235  const struct KgenContext *dstCtx,
236  int nrTabs);
237 
250 int
251 kgenDeclareFunction(struct KgenContext *ctx, const char *decl);
252 
263 int
264 kgenBeginFuncBody(struct KgenContext *ctx);
265 
277 int
278 kgenEndFuncBody(struct KgenContext *ctx);
279 
292 int
293 kgenGetLastFuncName(
294  char *buf,
295  size_t buflen,
296  const struct KgenContext *ctx);
297 
312 int
313 kgenBeginBranch(struct KgenContext *ctx, const char *stmt);
314 
329 int
330 kgenEndBranch(struct KgenContext *ctx, const char *stmt);
331 
347 int
348 kgenAddStmt(struct KgenContext *ctx, const char *stmt);
349 
350 int
351 kgenPrintf(struct KgenContext *ctx, const char *fmt,...);
352 
353 struct StatementBatch
354 *createStmtBatch(void);
355 
356 int
357 kgenAddStmtToBatch(
358  struct StatementBatch *batch,
359  int priority,
360  const char *stmt);
361 
362 int
363 kgenBatchPrintf(
364  struct StatementBatch *batch,
365  int priority,
366  const char *fmt,...);
367 
368 int
369 flushStmtBatch(struct KgenContext *ctx, struct StatementBatch *batch);
370 
371 void
372 destroyStmtBatch(struct StatementBatch *batch);
373 
383 int
384 kgenAddBlankLine(struct KgenContext *ctx);
385 
396 size_t
397 kgenSourceSize(struct KgenContext *ctx);
398 
407 
417 int
418 kgenAddBarrier(struct KgenContext *ctx, CLMemFence fence);
419 
429 int
430 kgenAddMemFence(struct KgenContext *ctx, CLMemFence fence);
431 
445 int
446 kgenDeclareLocalID(
447  struct KgenContext *ctx,
448  const char *lidName,
449  const PGranularity *pgran);
450 
464 int
465 kgenDeclareGroupID(
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  */
479 int
480 kgenDeclareUptrs(struct KgenContext *ctx, bool withDouble);
481 
490 
517 int
518 kgenLoopUnroll(
519  struct KgenContext *ctx,
520  LoopCtl *loopCtl,
521  DataType dtype,
522  const LoopUnrollers *unrollers,
523  void *priv);
524 
540 struct KgenGuard
541 *createKgenGuard(
542  struct KgenContext *ctx,
543  int (*genCallback)(struct KgenContext *ctx, const void *pattern),
544  size_t patSize);
545 
556 void
557 reinitKgenGuard(
558  struct KgenGuard *guard,
559  struct KgenContext *ctx,
560  int (*genCallback)(struct KgenContext *ctx, const void *pattern),
561  size_t patSize);
562 
583 int
584 findGenerateFunction(
585  struct KgenGuard *guard,
586  const void *pattern,
587  char *name,
588  size_t nameLen);
589 
596 void
597 destroyKgenGuard(struct KgenGuard *guard);
598 
607 
608 void
609 kstrcpy(Kstring *kstr, const char *str);
610 
611 void
612 ksprintf(Kstring *kstr, const char *fmt,...);
613 
614 void
615 kstrcatf(Kstring *kstr, const char *fmt,...);
616 
617 // unified pointer type name
618 const char
619 *uptrTypeName(UptrType type);
620 
633 char
634 dtypeToPrefix(DataType type);
635 
644 const char
645 *dtypeBuiltinType(DataType dtype);
646 
655 const char
656 *dtypeUPtrField(DataType dtype);
657 
666 const char
667 *strOne(DataType dtype);
668 
682 void
683 getVectorTypeName(
684  DataType dtype,
685  unsigned int vecLen,
686  const char **typeName,
687  const char **typePtrName);
688 
691 #endif /* KERNGEN_H_ */