summaryrefslogtreecommitdiff
path: root/src/kernels/level3/xgemm_direct.opencl
blob: fb5972ba8f197b1fb25ac559294f1cdc4dd8bfab (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// =================================================================================================
// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This
// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max-
// width of 100 characters per line.
//
// Author(s):
//   Cedric Nugteren <www.cedricnugteren.nl>
//
// This is a generic GEMM kernel that works for all sizes and configurations: it doesn't require any
// pre and and post-processing kernels.
//
// =================================================================================================

// Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string
// literal). Comment-out this line for syntax-highlighting when developing.
R"(

// =================================================================================================

// Caches global off-chip memory into local (shared) memory on-chip. This function is specific for
// caching the A input matrix.
inline void GlobalToLocalDirectA(const __global realM* restrict agm, __local real* alm,
                                 const int a_ld, const int a_offset, const int tid, const int kwg,
                                 const int a_transpose, const int a_conjugate) {
  const int la0 = tid % MDIMA;
  const int la1 = tid / MDIMA;
  #pragma unroll
  for (int mia=0; mia<MWA/VWM; ++mia) {
    #pragma unroll
    for (int kia=0; kia<KWA; ++kia) {

      // Computes the indices for the global memory
      int mg = mia + la0*(MWA/VWM);
      int kg = kia + la1*KWA;
      int idm = (a_transpose) ? mg + kwg/VWM : mg + GetGroupID0()*(MWG/VWM);
      int idk = (a_transpose) ? kg + GetGroupID0()*MWG : kg + kwg;

      // Loads the data from global memory into the local memory
      const realM avec = agm[idk*(a_ld/VWM) + idm + a_offset];
      #if VWM == 1
         alm[kg*MWG + mg] = avec;
      #elif VWM == 2
         alm[kg*MWG + mg*VWM + 0] = avec.x;
         alm[kg*MWG + mg*VWM + 1] = avec.y;
      #elif VWM == 4
         alm[kg*MWG + mg*VWM + 0] = avec.x;
         alm[kg*MWG + mg*VWM + 1] = avec.y;
         alm[kg*MWG + mg*VWM + 2] = avec.z;
         alm[kg*MWG + mg*VWM + 3] = avec.w;
      #elif VWM == 8
         alm[kg*MWG + mg*VWM + 0] = avec.s0;
         alm[kg*MWG + mg*VWM + 1] = avec.s1;
         alm[kg*MWG + mg*VWM + 2] = avec.s2;
         alm[kg*MWG + mg*VWM + 3] = avec.s3;
         alm[kg*MWG + mg*VWM + 4] = avec.s4;
         alm[kg*MWG + mg*VWM + 5] = avec.s5;
         alm[kg*MWG + mg*VWM + 6] = avec.s6;
         alm[kg*MWG + mg*VWM + 7] = avec.s7;
      #elif VWM == 16
         alm[kg*MWG + mg*VWM + 0] = avec.s0;
         alm[kg*MWG + mg*VWM + 1] = avec.s1;
         alm[kg*MWG + mg*VWM + 2] = avec.s2;
         alm[kg*MWG + mg*VWM + 3] = avec.s3;
         alm[kg*MWG + mg*VWM + 4] = avec.s4;
         alm[kg*MWG + mg*VWM + 5] = avec.s5;
         alm[kg*MWG + mg*VWM + 6] = avec.s6;
         alm[kg*MWG + mg*VWM + 7] = avec.s7;
         alm[kg*MWG + mg*VWM + 8] = avec.s8;
         alm[kg*MWG + mg*VWM + 9] = avec.s9;
         alm[kg*MWG + mg*VWM + 10] = avec.sA;
         alm[kg*MWG + mg*VWM + 11] = avec.sB;
         alm[kg*MWG + mg*VWM + 12] = avec.sC;
         alm[kg*MWG + mg*VWM + 13] = avec.sD;
         alm[kg*MWG + mg*VWM + 14] = avec.sE;
         alm[kg*MWG + mg*VWM + 15] = avec.sF;
      #endif
      if (a_conjugate) {
        for (int vm=0; vm<VWM; ++vm) {
          COMPLEX_CONJUGATE(alm[kg*MWG + mg*VWM + vm]);
        }
      }
    }
  }
}

// Same as above, but now for the B input matrix
inline void GlobalToLocalDirectB(const __global realN* restrict bgm, __local real* blm,
                                 const int b_ld, const int b_offset, const int tid, const int kwg,
                                 const int b_transpose, const int b_conjugate) {
  const int lb0 = tid % NDIMB;
  const int lb1 = tid / NDIMB;
  #pragma unroll
  for (int kib=0; kib<KWB; ++kib) {
    #pragma unroll
    for (int nib=0; nib<NWB/VWN; ++nib) {

      // Computes the indices for the global memory
      int ng = nib + lb0*(NWB/VWN);
      int kg = kib + lb1*KWB;
      int idn = (b_transpose) ? ng + kwg/VWN : ng + GetGroupID1()*(NWG/VWN);
      int idk = (b_transpose) ? kg + GetGroupID1()*NWG : kg + kwg;

      // Loads the data from global memory into the local memory
      const realM bvec = bgm[idk*(b_ld/VWN) + idn + b_offset];
      #if VWN == 1
         blm[kg*NWG + ng] = bvec;
      #elif VWN == 2
         blm[kg*NWG + ng*VWN + 0] = bvec.x;
         blm[kg*NWG + ng*VWN + 1] = bvec.y;
      #elif VWN == 4
         blm[kg*NWG + ng*VWN + 0] = bvec.x;
         blm[kg*NWG + ng*VWN + 1] = bvec.y;
         blm[kg*NWG + ng*VWN + 2] = bvec.z;
         blm[kg*NWG + ng*VWN + 3] = bvec.w;
      #elif VWN == 8
         blm[kg*NWG + ng*VWN + 0] = bvec.s0;
         blm[kg*NWG + ng*VWN + 1] = bvec.s1;
         blm[kg*NWG + ng*VWN + 2] = bvec.s2;
         blm[kg*NWG + ng*VWN + 3] = bvec.s3;
         blm[kg*NWG + ng*VWN + 4] = bvec.s4;
         blm[kg*NWG + ng*VWN + 5] = bvec.s5;
         blm[kg*NWG + ng*VWN + 6] = bvec.s6;
         blm[kg*NWG + ng*VWN + 7] = bvec.s7;
      #elif VWN == 16
         blm[kg*NWG + ng*VWN + 0] = bvec.s0;
         blm[kg*NWG + ng*VWN + 1] = bvec.s1;
         blm[kg*NWG + ng*VWN + 2] = bvec.s2;
         blm[kg*NWG + ng*VWN + 3] = bvec.s3;
         blm[kg*NWG + ng*VWN + 4] = bvec.s4;
         blm[kg*NWG + ng*VWN + 5] = bvec.s5;
         blm[kg*NWG + ng*VWN + 6] = bvec.s6;
         blm[kg*NWG + ng*VWN + 7] = bvec.s7;
         blm[kg*NWG + ng*VWN + 8] = bvec.s8;
         blm[kg*NWG + ng*VWN + 9] = bvec.s9;
         blm[kg*NWG + ng*VWN + 10] = bvec.sA;
         blm[kg*NWG + ng*VWN + 11] = bvec.sB;
         blm[kg*NWG + ng*VWN + 12] = bvec.sC;
         blm[kg*NWG + ng*VWN + 13] = bvec.sD;
         blm[kg*NWG + ng*VWN + 14] = bvec.sE;
         blm[kg*NWG + ng*VWN + 15] = bvec.sF;
      #endif
      if (b_conjugate) {
        for (int vn=0; vn<VWN; ++vn) {
          COMPLEX_CONJUGATE(blm[kg*NWG + ng*VWN + vn]);
        }
      }
    }
  }
}

// =================================================================================================

// Caches on-chip local memory into per-thread private memory (registers). This function is specific
// for caching the A input matrix.
inline void LocalToPrivateDirectA(__local real* alm, real apm[MWI], const int kg,
                                  const int a_transpose) {
  #pragma unroll
  for (int mi=0; mi<MWI; ++mi) {
    const int mg = mi + get_local_id(0)*MWI;
    const int index = (a_transpose) ? mg*KWG + kg : kg*MWG + mg;
    apm[mi] = alm[index];
  }
}

// Same as above, but now for the B input matrix
inline void LocalToPrivateDirectB(__local real* blm, real bpm[NWI], const int kg,
                                  const int b_transpose) {
  #pragma unroll
  for (int ni=0; ni<NWI; ++ni) {
    const int ng = ni + get_local_id(1)*NWI;
    const int index = (b_transpose) ? ng*KWG + kg : kg*NWG + ng;
    bpm[ni] = blm[index];
  }
}

// =================================================================================================

// Initializes the accumulation registers to zero
inline void InitAccRegistersDirect(real cpm[NWI][MWI]) {
  #pragma unroll
  for (int mi=0; mi<MWI; ++mi) {
    #pragma unroll
    for (int ni=0; ni<NWI; ++ni) {
      SetToZero(cpm[ni][mi]);
    }
  }
}

// =================================================================================================

// Performs the actual computation: Cpm += Apm * Bpm
inline void MultiplyAccumulateDirect(real cpm[NWI][MWI], real apm[MWI], real bpm[NWI]) {
  #pragma unroll
  for (int ni=0; ni<NWI; ++ni) {
    #pragma unroll
    for (int mi=0; mi<MWI; ++mi) {
      MultiplyAdd(cpm[ni][mi], apm[mi], bpm[ni]);
    }
  }
}

// =================================================================================================

// Merges the results in Cpm with the global array in Cgm. This also performs the multiplication
// with the constants: Cgm = alpha*A*B + beta*Cgm = alpha*Cpm + beta*Cgm
inline void StoreResultsDirect(__global real* cgm, real cpm[NWI][MWI],
                               const int kSizeM, const int kSizeN,
                               const real alpha, const real beta,
                               const int c_ld, const int c_offset, const int c_transpose) {
  #pragma unroll
  for (int ni=0; ni<NWI; ++ni) {
    #pragma unroll
    for (int mi=0; mi<MWI; ++mi) {
      int mg = mi + get_local_id(0)*MWI;
      int ng = ni + get_local_id(1)*NWI;
      int idm = mg + GetGroupID0() * MWG;
      int idn = ng + GetGroupID1() * NWG;

      // Determines the destination index
      const int c_index = (c_transpose) ? idm*c_ld + idn : idn*c_ld + idm;

      // The final multiplication with alpha and the addition with beta*C
      real result;
      AXPBY(result, alpha, cpm[ni][mi], beta, cgm[c_index + c_offset]);
      cgm[c_index + c_offset] = result;
    }
  }
}

// =================================================================================================

// Main entry point of the kernel. This is the direct version without restrictions.
__attribute__((reqd_work_group_size(MDIMC, NDIMC, 1)))
__kernel void XgemmDirect(const int kSizeM, const int kSizeN, const int kSizeK,
                          const real_arg arg_alpha,
                          const real_arg arg_beta,
                          const __global realM* restrict agm, const int a_offset, const int a_ld,
                          const __global realN* restrict bgm, const int b_offset, const int b_ld,
                          __global real* cgm, const int c_offset, const int c_ld,
                          const int a_transpose, const int b_transpose, const int c_transpose,
                          const int a_conjugate, const int b_conjugate) {
  const real alpha = GetRealArg(arg_alpha);
  const real beta = GetRealArg(arg_beta);

  // Extra pointers to scalar versions of global memory
  const __global real* restrict agms = (const __global real* restrict) agm;
  const __global real* restrict bgms = (const __global real* restrict) bgm;

  // Allocates workgroup-private memory (local memory)
  __local real alm[KWG * MWG];
  __local real blm[KWG * NWG];

  // Combined thread identifier (volatile to disable caching)
  volatile int tid = get_local_id(0) + MDIMC*get_local_id(1);

  // Allocates workitem-private memory (registers)
  real apm[MWI];
  real bpm[NWI];
  real cpm[NWI][MWI];

  // Initializes the accumulation registers
  InitAccRegistersDirect(cpm);

  // The faster version of GEMM is not allowed on the (incomplete) borders. Therefore, this section
  // processes only the main parts: output blocks of MWG by NWG.
  const int idm = get_local_id(0) * MWI + GetGroupID0() * MWG;
  const int idn = get_local_id(1) * NWI + GetGroupID1() * NWG;
  if ((idm < (kSizeM/MWG)*MWG) && (idn < (kSizeN/NWG)*NWG) &&
      (a_ld % VWM == 0) && (b_ld % VWN == 0)) {

    // Loops over all complete workgroup tiles
    int kwg = 0;
    for (; kwg < (kSizeK/KWG) * KWG; kwg+=KWG) {

      // Loads data: off-chip --> local (matrix A and B)
      GlobalToLocalDirectA(agm, alm, a_ld, a_offset, tid, kwg, a_transpose, a_conjugate);
      GlobalToLocalDirectB(bgm, blm, b_ld, b_offset, tid, kwg, b_transpose, b_conjugate);
      barrier(CLK_LOCAL_MEM_FENCE);

      // Loops over all workitem tiles, unrolled by a factor KWI
      for (int pwi=0; pwi<KWG; pwi+=KWI) {
        #pragma unroll
        for (int pit=0; pit<KWI; ++pit) {
          int kg = pwi + pit;

          // Loads data: local --> private (matrix A)
          LocalToPrivateDirectA(alm, apm, kg, a_transpose);

          // Loads data: local --> private (matrix B)
          LocalToPrivateDirectB(blm, bpm, kg, b_transpose);

          // Performs the accumulation (Cpm += Apm * Bpm)
          MultiplyAccumulateDirect(cpm, apm, bpm);
        }
      }
      barrier(CLK_LOCAL_MEM_FENCE);
    }

    // Loop over the remaining part (incomplete tile in K-dimension)
    for (; kwg < kSizeK; ++kwg) {
      const int idk = kwg;

      // Loads A into register memory
      #pragma unroll
      for (int mi=0; mi<MWI; ++mi) {
        const int a_index = (a_transpose) ? (idm + mi)*a_ld + idk : idk*a_ld + (idm + mi);
        apm[mi] = agms[a_index + a_offset];
        if (a_conjugate) { COMPLEX_CONJUGATE(apm[mi]); }
      }

      // Loads B into register memory
      #pragma unroll
      for (int ni=0; ni<NWI; ++ni) {
        const int b_index = (b_transpose) ? (idn + ni)*b_ld + idk : idk*b_ld + (idn + ni);
        bpm[ni] = bgms[b_index + b_offset];
        if (b_conjugate) { COMPLEX_CONJUGATE(bpm[ni]); }
      }

      // Performs the accumulation (Cpm += Apm * Bpm)
      MultiplyAccumulateDirect(cpm, apm, bpm);
    }

    #if GLOBAL_MEM_FENCE == 1
      barrier(CLK_GLOBAL_MEM_FENCE);
    #endif

    // Stores a tile of results and performs the multiplication with alpha and beta
    StoreResultsDirect(cgm, cpm, kSizeM, kSizeN, alpha, beta, c_ld, c_offset, c_transpose);
  }

  // Simple but slow version for the parts on the edge (incomplete tiles in M and N-dimensions)
  else {

    // Loop over the K-dimension
    for (int idk = 0; idk < kSizeK; ++idk) {

      // Loads A into register memory
      #pragma unroll
      for (int mi=0; mi<MWI; ++mi) {
        if (idm + mi < kSizeM) {
          const int a_index = (a_transpose) ? (idm + mi)*a_ld + idk : idk*a_ld + (idm + mi);
          apm[mi] = agms[a_index + a_offset];
          if (a_conjugate) { COMPLEX_CONJUGATE(apm[mi]); }
        }
        else {
          SetToZero(apm[mi]);
        }
      }

      // Loads B into register memory
      #pragma unroll
      for (int ni=0; ni<NWI; ++ni) {
        if (idn + ni < kSizeN) {
          const int b_index = (b_transpose) ? (idn + ni)*b_ld + idk : idk*b_ld + (idn + ni);
          bpm[ni] = bgms[b_index + b_offset];
          if (b_conjugate) { COMPLEX_CONJUGATE(bpm[ni]); }
        }
        else {
          SetToZero(bpm[ni]);
        }
      }

      // Performs the accumulation (Cpm += Apm * Bpm)
      MultiplyAccumulateDirect(cpm, apm, bpm);
    }

    // Stores the results
    #pragma unroll
    for (int ni=0; ni<NWI; ++ni) {
      #pragma unroll
      for (int mi=0; mi<MWI; ++mi) {
        if ((idm + mi) < kSizeM && (idn + ni) < kSizeN) {

          // Determines the destination index
          const int c_index = (c_transpose) ? (idm + mi)*c_ld + (idn + ni) : (idn + ni)*c_ld + (idm + mi);

          // Computes and stores the result
          real result;
          AXPBY(result, alpha, cpm[ni][mi], beta, cgm[c_index + c_offset]);
          cgm[c_index + c_offset] = result;
        }
      }
    }
  }
}

// =================================================================================================

// End of the C++11 raw string literal
)"

// =================================================================================================