cublas_function.h 17.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
 * @file cublas_function.h
 * @brief Implementation of specific cublas function
 */

#pragma once

#include "cublas_benchmark.h"

/**
 * @brief Class of SgemmFunction
 */
class SgemmFunction : public CublasFunction {
17
18
19
20
21
22
23
    float *Parameter_0_0;      ///< the pointer of the first input data
    float *Parameter_1_0;      ///< the pointer of the second input data
    float *Result_3_0;         ///< the pointer of output data
    float *Parameter_0_0_host; ///< the pointer of the first input data on host
    float *Parameter_1_0_host; ///< the pointer of the second input data on host
    float *Result_cpu;

24
25
26
27
28
29
30
31
    /**
     * @brief Execute the kernel/function
     */
    virtual void kernel_entry() {
        sgemm(cublas_handle, this->transa_, this->transb_, this->m_, this->n_, this->k_,
              reinterpret_cast<const float *>(Parameter_0_0), reinterpret_cast<const float *>(Parameter_1_0),
              reinterpret_cast<float *>(Result_3_0));
    }
32
33
34
35
36
37
38
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
        matrix_calculation_on_cpu_with_data(Parameter_0_0_host, Parameter_1_0_host, Result_3_0, &Result_cpu, 1.0f,
                                            1.0f);
    }
39
40
41
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
42
43
44
    virtual void prepare_tensor(bool random) {
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host,
                                random);
45
46
47
48
49
50
51
52
    }
    /**
     * @brief Check the correctness of function calculation result
     */
    virtual int correctness_check() {
        double eps = this->eps == 0.0 ? 1.e-6 : this->eps;
        return check_result(1, Result_3_0, Result_cpu, eps);
    }
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

  public:
    /**
     * @brief Construct a new Sgemm Function object
     */
    SgemmFunction() {
        this->batch_count_ = 1;
        cuda_init(&cublas_handle);
    }
    /**
     * @brief Construct a new Sgemm Function object
     * @param  function         base class CublasFunction object
     */
    SgemmFunction(CublasFunction &function) : CublasFunction(function) {
        this->batch_count_ = 1;
        cuda_init(&cublas_handle);
    }
    /**
     * @brief Destroy the Sgemm Function object
     */
    ~SgemmFunction() {
        // Free contexts
        CUDA_SAFE_CALL(cudaFree(Parameter_0_0));
        CUDA_SAFE_CALL(cudaFree(Parameter_1_0));
        CUDA_SAFE_CALL(cudaFree(Result_3_0));
78
79
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
80
81
82
83
84
85
86
87
88
89
90
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of CgemmFunction
 */
class CgemmFunction : public CublasFunction {
    cuComplex *Parameter_0_0;
    cuComplex *Parameter_1_0;
    cuComplex *Result_3_0;
91
92
93
    cuComplex *Parameter_0_0_host;
    cuComplex *Parameter_1_0_host;
    std::complex<float> *Result_cpu;
94
95
96
97
98
99
100
101
    /**
     * @brief Execute the kernel/function
     */
    virtual void kernel_entry() {
        cgemm(cublas_handle, this->transa_, this->transb_, this->m_, this->n_, this->k_,
              reinterpret_cast<const cuComplex *>(Parameter_0_0), reinterpret_cast<const cuComplex *>(Parameter_1_0),
              reinterpret_cast<cuComplex *>(Result_3_0));
    }
102
103
104
105
106
107
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
        matrix_calculation_on_cpu_with_data(Parameter_0_0_host, Parameter_1_0_host, Result_3_0, &Result_cpu);
    }
108
109
110
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
111
112
113
    virtual void prepare_tensor(bool random) {
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host,
                                random);
114
115
116
117
118
119
120
    }
    /**
     * @brief Check the correctness of function calculation result
     */
    virtual int correctness_check() {
        double eps = this->eps == 0.0 ? 1.e-6 : this->eps;
        return check_result(1, Result_3_0, Result_cpu, eps);
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
    }

  public:
    /**
     * @brief Construct a new Cgemm Function object
     */
    CgemmFunction() {
        this->batch_count_ = 1;
        cuda_init(&cublas_handle);
    }
    /**
     * @brief Construct a new Cgemm Function object
     * @param  function         base class CublasFunction object
     */
    CgemmFunction(CublasFunction &function) : CublasFunction(function) {
        this->batch_count_ = 1;
        cuda_init(&cublas_handle);
    }
    /**
     * @brief Destroy the Cgemm Function object
     */
    ~CgemmFunction() {
        // Free contexts
        CUDA_SAFE_CALL(cudaFree(Parameter_0_0));
        CUDA_SAFE_CALL(cudaFree(Parameter_1_0));
        CUDA_SAFE_CALL(cudaFree(Result_3_0));
147
148
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
149
150
151
152
153
154
155
156
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of GemmExFunction
 */
class GemmExFunction : public CublasFunction {
157
158
159
160
161
162
    void *Parameter_0_0;
    void *Parameter_1_0;
    void *Result_3_0;
    void *Parameter_0_0_host;
    void *Parameter_1_0_host;
    void *Result_cpu;
163
164
165
166
167
168
169
170
171
172
173
    /**
     * @brief Execute the kernel/function
     */
    virtual void kernel_entry() {
        gemmEx(cublas_handle, this->transa_, this->transb_, this->m_, this->n_, this->k_,
               reinterpret_cast<void *>(Parameter_0_0), reinterpret_cast<void *>(Parameter_1_0),
               reinterpret_cast<void *>(Result_3_0), this->datatype_, this->use_tensor_core_);
    }
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
174
    virtual void prepare_tensor(bool random) {
175
        if (this->datatype_.compare("half") == 0) {
176
177
178
            CublasFunction::prepare_tensor_template<half>(
                reinterpret_cast<half **>(&Parameter_0_0), reinterpret_cast<half **>(&Parameter_1_0),
                reinterpret_cast<half **>(&Result_3_0), reinterpret_cast<half **>(&Parameter_0_0_host),
179
                reinterpret_cast<half **>(&Parameter_1_0_host), random);
180
        } else if (this->datatype_.compare("float") == 0) {
181
182
183
            CublasFunction::prepare_tensor_template<float>(
                reinterpret_cast<float **>(&Parameter_0_0), reinterpret_cast<float **>(&Parameter_1_0),
                reinterpret_cast<float **>(&Result_3_0), reinterpret_cast<float **>(&Parameter_0_0_host),
184
                reinterpret_cast<float **>(&Parameter_1_0_host), random);
185
186
187
188
189
190
        }
    }
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
191
        if (this->datatype_.compare("half") == 0) {
192
193
194
            matrix_calculation_on_cpu_with_data(
                reinterpret_cast<half *>(Parameter_0_0_host), reinterpret_cast<half *>(Parameter_1_0_host),
                reinterpret_cast<half *>(Result_3_0), reinterpret_cast<float **>(&Result_cpu));
195
        } else if (this->datatype_.compare("float") == 0) {
196
197
198
199
200
201
202
203
204
205
            matrix_calculation_on_cpu_with_data(
                reinterpret_cast<float *>(Parameter_0_0_host), reinterpret_cast<float *>(Parameter_1_0_host),
                reinterpret_cast<float *>(Result_3_0), reinterpret_cast<float **>(&Result_cpu));
        }
    }
    /**
     * @brief Check the correctness of function calculation result
     */
    virtual int correctness_check() {
        int result = 0;
206
        if (this->datatype_.compare("half") == 0) {
207
208
209
            double eps = this->eps == 0.0 ? 1.e-3 : this->eps;
            result = check_result(this->batch_count_, reinterpret_cast<half *>(Result_3_0),
                                  reinterpret_cast<float *>(Result_cpu), eps);
210
        } else if (this->datatype_.compare("float") == 0) {
211
212
213
214
215
216
            double eps = this->eps == 0.0 ? 1.e-6 : this->eps;
            result = check_result(this->batch_count_, reinterpret_cast<float *>(Result_3_0),
                                  reinterpret_cast<float *>(Result_cpu), eps);
        }
        return result;
    }
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

  public:
    /**
     * @brief Construct a new Gemm Ex Function object
     */
    GemmExFunction() {
        this->batch_count_ = 1;
        cuda_init(&cublas_handle);
    }
    /**
     * @brief Construct a new Gemm Ex Function object
     * @param  function         base class CublasFunction object
     */
    GemmExFunction(CublasFunction &function) : CublasFunction(function) {
        this->batch_count_ = 1;
        cuda_init(&cublas_handle);
    }
    /**
     * @brief Destroy the Gemm Ex Function object
     */
    ~GemmExFunction() {
        // Free contexts
        CUDA_SAFE_CALL(cudaFree(Parameter_0_0));
        CUDA_SAFE_CALL(cudaFree(Parameter_1_0));
        CUDA_SAFE_CALL(cudaFree(Result_3_0));
242
243
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
244
245
246
247
248
249
250
251
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of GemmStridedBatchedExFunction
 */
class GemmStridedBatchedExFunction : public CublasFunction {
252
253
254
255
256
257
    void *Parameter_0_0;
    void *Parameter_1_0;
    void *Result_3_0;
    void *Parameter_0_0_host;
    void *Parameter_1_0_host;
    void *Result_cpu;
258
259
260
261
262
263
264
265
266
267
268
269
    /**
     * @brief Execute the kernel/function
     */
    virtual void kernel_entry() {
        gemmStridedBatchedEx(cublas_handle, this->transa_, this->transb_, this->m_, this->n_, this->k_,
                             reinterpret_cast<void *>(Parameter_0_0), reinterpret_cast<void *>(Parameter_1_0),
                             reinterpret_cast<void *>(Result_3_0), this->datatype_, this->use_tensor_core_,
                             this->batch_count_);
    }
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
270
    virtual void prepare_tensor(bool random) {
271
        if (this->datatype_.compare("half") == 0) {
272
273
274
            prepare_tensor_template<half>(
                reinterpret_cast<half **>(&Parameter_0_0), reinterpret_cast<half **>(&Parameter_1_0),
                reinterpret_cast<half **>(&Result_3_0), reinterpret_cast<half **>(&Parameter_0_0_host),
275
                reinterpret_cast<half **>(&Parameter_1_0_host), random);
276
        } else if (this->datatype_.compare("float") == 0) {
277
278
279
            prepare_tensor_template<float>(
                reinterpret_cast<float **>(&Parameter_0_0), reinterpret_cast<float **>(&Parameter_1_0),
                reinterpret_cast<float **>(&Result_3_0), reinterpret_cast<float **>(&Parameter_0_0_host),
280
                reinterpret_cast<float **>(&Parameter_1_0_host), random);
281
282
283
284
285
286
        }
    }
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
287
        if (this->datatype_.compare("half") == 0) {
288
289
            matrix_calculation_on_cpu_with_data(
                reinterpret_cast<half *>(Parameter_0_0_host), reinterpret_cast<half *>(Parameter_1_0_host),
290
291
                reinterpret_cast<half *>(Result_3_0), reinterpret_cast<float **>(&Result_cpu), 1.0f, 1.0f);
        } else if (this->datatype_.compare("float") == 0) {
292
293
294
295
296
297
298
299
300
301
            matrix_calculation_on_cpu_with_data(
                reinterpret_cast<float *>(Parameter_0_0_host), reinterpret_cast<float *>(Parameter_1_0_host),
                reinterpret_cast<float *>(Result_3_0), reinterpret_cast<float **>(&Result_cpu), 1.0f, 1.0f);
        }
    }
    /**
     * @brief Check the correctness of function calculation result
     */
    virtual int correctness_check() {
        int result = 0;
302
        if (this->datatype_.compare("half") == 0) {
303
304
305
            double eps = this->eps == 0.0 ? 1.e-3 : this->eps;
            result = check_result(this->batch_count_, reinterpret_cast<half *>(Result_3_0),
                                  reinterpret_cast<float *>(Result_cpu), eps);
306
        } else if (this->datatype_.compare("float") == 0) {
307
308
309
310
311
312
            double eps = this->eps == 0.0 ? 1.e-6 : this->eps;
            result = check_result(this->batch_count_, reinterpret_cast<float *>(Result_3_0),
                                  reinterpret_cast<float *>(Result_cpu), eps);
        }
        return result;
    }
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

  public:
    /**
     * @brief Construct a new Gemm Strided Batched Ex Function object
     */
    GemmStridedBatchedExFunction() { cuda_init(&cublas_handle); }
    /**
     * @brief Construct a new Gemm Strided Batched Ex Function object
     * @param  function         base class CublasFunction object
     */
    GemmStridedBatchedExFunction(CublasFunction &function) : CublasFunction(function) { cuda_init(&cublas_handle); }
    /**
     * @brief Destroy the Gemm Strided Batched Ex Function object
     */
    ~GemmStridedBatchedExFunction() {
        // Free contexts
        CUDA_SAFE_CALL(cudaFree(Parameter_0_0));
        CUDA_SAFE_CALL(cudaFree(Parameter_1_0));
        CUDA_SAFE_CALL(cudaFree(Result_3_0));
332
333
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
334
335
336
337
338
339
340
341
342
343
344
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of SgemmStridedBatchedFunction
 */
class SgemmStridedBatchedFunction : public CublasFunction {
    float *Parameter_0_0;
    float *Parameter_1_0;
    float *Result_3_0;
345
346
347
    float *Parameter_0_0_host;
    float *Parameter_1_0_host;
    float *Result_cpu;
348
349
350
351
352
353
354
355
356
357
358
359
    /**
     * @brief Execute the kernel/function
     */
    virtual void kernel_entry() {
        sgemmStridedBatched(cublas_handle, this->transa_, this->transb_, this->m_, this->n_, this->k_,
                            reinterpret_cast<const float *>(Parameter_0_0),
                            reinterpret_cast<const float *>(Parameter_1_0), reinterpret_cast<float *>(Result_3_0),
                            this->batch_count_);
    }
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
360
361
362
    virtual void prepare_tensor(bool random) {
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host,
                                random);
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    }
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
        matrix_calculation_on_cpu_with_data(Parameter_0_0_host, Parameter_1_0_host, Result_3_0, &Result_cpu, 1.0f,
                                            1.0f);
    }
    /**
     * @brief Check the correctness of function calculation result
     */
    virtual int correctness_check() {
        double eps = this->eps == 0.0 ? 1.e-6 : this->eps;
        return check_result(this->batch_count_, Result_3_0, Result_cpu, eps);
    }
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

  public:
    /**
     * @brief Construct a new Sgemm Strided Batched Function object
     */
    SgemmStridedBatchedFunction() { cuda_init(&cublas_handle); }
    /**
     * @brief Construct a new Sgemm Strided Batched Function object
     * @param  function         base class CublasFunction object
     */
    SgemmStridedBatchedFunction(CublasFunction &function) : CublasFunction(function) { cuda_init(&cublas_handle); }
    /**
     * @brief Destroy the Sgemm Strided Batched Function object
     */
    ~SgemmStridedBatchedFunction() {
        // Free contexts
        CUDA_SAFE_CALL(cudaFree(Parameter_0_0));
        CUDA_SAFE_CALL(cudaFree(Parameter_1_0));
        CUDA_SAFE_CALL(cudaFree(Result_3_0));
397
398
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
399
400
401
402
403
404
405
406
407
408
409
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of Cgemm3mStridedBatchedFunction
 */
class Cgemm3mStridedBatchedFunction : public CublasFunction {
    cuComplex *Parameter_0_0;
    cuComplex *Parameter_1_0;
    cuComplex *Result_3_0;
410
411
412
    cuComplex *Parameter_0_0_host;
    cuComplex *Parameter_1_0_host;
    std::complex<float> *Result_cpu;
413
414
415
416
417
418
419
420
421
422
423
424
    /**
     * @brief Execute the kernel/function
     */
    virtual void kernel_entry() {
        cgemm3mStridedBatched(cublas_handle, this->transa_, this->transb_, this->m_, this->n_, this->k_,
                              reinterpret_cast<const cuComplex *>(Parameter_0_0),
                              reinterpret_cast<const cuComplex *>(Parameter_1_0),
                              reinterpret_cast<cuComplex *>(Result_3_0), this->batch_count_);
    }
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
425
426
427
    virtual void prepare_tensor(bool random) {
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host,
                                random);
428
429
430
431
432
433
434
435
436
437
438
439
440
    }
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
        matrix_calculation_on_cpu_with_data(Parameter_0_0_host, Parameter_1_0_host, Result_3_0, &Result_cpu);
    }
    /**
     * @brief Check the correctness of function calculation result
     */
    virtual int correctness_check() {
        double eps = this->eps == 0.0 ? 1.e-6 : this->eps;
        return check_result(this->batch_count_, Result_3_0, Result_cpu, eps);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    }

  public:
    /**
     * @brief Construct a new Cgemm 3m Strided Batched Function object
     */
    Cgemm3mStridedBatchedFunction() { cuda_init(&cublas_handle); }
    /**
     * @brief Construct a new Cgemm 3m Strided Batched Function object according to base class object
     * @param  function         base class CublasFunction object
     */
    Cgemm3mStridedBatchedFunction(CublasFunction &function) : CublasFunction(function) { cuda_init(&cublas_handle); }
    /**
     * @brief Destroy the Cgemm 3m Strided Batched Function object
     */
    ~Cgemm3mStridedBatchedFunction() {
        // Free contexts
        CUDA_SAFE_CALL(cudaFree(Parameter_0_0));
        CUDA_SAFE_CALL(cudaFree(Parameter_1_0));
        CUDA_SAFE_CALL(cudaFree(Result_3_0));
461
462
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
463
464
465
        cuda_free(&cublas_handle);
    }
};