cublas_function.h 17.4 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
45
46
47
48
49
50
51
    virtual void prepare_tensor() {
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host);
    }
    /**
     * @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);
    }
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

  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));
77
78
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
79
80
81
82
83
84
85
86
87
88
89
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of CgemmFunction
 */
class CgemmFunction : public CublasFunction {
    cuComplex *Parameter_0_0;
    cuComplex *Parameter_1_0;
    cuComplex *Result_3_0;
90
91
92
    cuComplex *Parameter_0_0_host;
    cuComplex *Parameter_1_0_host;
    std::complex<float> *Result_cpu;
93
94
95
96
97
98
99
100
    /**
     * @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));
    }
101
102
103
104
105
106
    /**
     * @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);
    }
107
108
109
110
    /**
     * @brief Prepare memory and data of the input and output for kernel running
     */
    virtual void prepare_tensor() {
111
112
113
114
115
116
117
118
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host);
    }
    /**
     * @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);
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
    }

  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));
145
146
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
147
148
149
150
151
152
153
154
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of GemmExFunction
 */
class GemmExFunction : public CublasFunction {
155
156
157
158
159
160
    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;
161
162
163
164
165
166
167
168
169
170
171
    /**
     * @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
     */
172
    virtual void prepare_tensor() {
173
        if (this->datatype_.compare("half") == 0) {
174
175
176
177
            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),
                reinterpret_cast<half **>(&Parameter_1_0_host));
178
        } else if (this->datatype_.compare("float") == 0) {
179
180
181
182
183
184
185
186
187
188
            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),
                reinterpret_cast<float **>(&Parameter_1_0_host));
        }
    }
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
189
        if (this->datatype_.compare("half") == 0) {
190
191
192
            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));
193
        } else if (this->datatype_.compare("float") == 0) {
194
195
196
197
198
199
200
201
202
203
            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;
204
        if (this->datatype_.compare("half") == 0) {
205
206
207
            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);
208
        } else if (this->datatype_.compare("float") == 0) {
209
210
211
212
213
214
            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;
    }
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

  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));
240
241
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
242
243
244
245
246
247
248
249
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of GemmStridedBatchedExFunction
 */
class GemmStridedBatchedExFunction : public CublasFunction {
250
251
252
253
254
255
    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;
256
257
258
259
260
261
262
263
264
265
266
267
    /**
     * @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
     */
268
    virtual void prepare_tensor() {
269
        if (this->datatype_.compare("half") == 0) {
270
271
272
273
            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),
                reinterpret_cast<half **>(&Parameter_1_0_host));
274
        } else if (this->datatype_.compare("float") == 0) {
275
276
277
278
279
280
281
282
283
284
            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),
                reinterpret_cast<float **>(&Parameter_1_0_host));
        }
    }
    /**
     * @brief  Function calculation on CPU side
     */
    virtual void matrix_calculation_on_cpu() {
285
        if (this->datatype_.compare("half") == 0) {
286
287
            matrix_calculation_on_cpu_with_data(
                reinterpret_cast<half *>(Parameter_0_0_host), reinterpret_cast<half *>(Parameter_1_0_host),
288
289
                reinterpret_cast<half *>(Result_3_0), reinterpret_cast<float **>(&Result_cpu), 1.0f, 1.0f);
        } else if (this->datatype_.compare("float") == 0) {
290
291
292
293
294
295
296
297
298
299
            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;
300
        if (this->datatype_.compare("half") == 0) {
301
302
303
            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);
304
        } else if (this->datatype_.compare("float") == 0) {
305
306
307
308
309
310
            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;
    }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

  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));
330
331
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
332
333
334
335
336
337
338
339
340
341
342
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of SgemmStridedBatchedFunction
 */
class SgemmStridedBatchedFunction : public CublasFunction {
    float *Parameter_0_0;
    float *Parameter_1_0;
    float *Result_3_0;
343
344
345
    float *Parameter_0_0_host;
    float *Parameter_1_0_host;
    float *Result_cpu;
346
347
348
349
350
351
352
353
354
355
356
357
    /**
     * @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
     */
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    virtual void prepare_tensor() {
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host);
    }
    /**
     * @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);
    }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

  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));
394
395
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
396
397
398
399
400
401
402
403
404
405
406
        cuda_free(&cublas_handle);
    }
};

/**
 * @brief Class of Cgemm3mStridedBatchedFunction
 */
class Cgemm3mStridedBatchedFunction : public CublasFunction {
    cuComplex *Parameter_0_0;
    cuComplex *Parameter_1_0;
    cuComplex *Result_3_0;
407
408
409
    cuComplex *Parameter_0_0_host;
    cuComplex *Parameter_1_0_host;
    std::complex<float> *Result_cpu;
410
411
412
413
414
415
416
417
418
419
420
421
422
    /**
     * @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
     */
    virtual void prepare_tensor() {
423
424
425
426
427
428
429
430
431
432
433
434
435
436
        prepare_tensor_template(&Parameter_0_0, &Parameter_1_0, &Result_3_0, &Parameter_0_0_host, &Parameter_1_0_host);
    }
    /**
     * @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);
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    }

  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));
457
458
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_0_0_host));
        CUDA_SAFE_CALL(cudaFreeHost(Parameter_1_0_host));
459
460
461
        cuda_free(&cublas_handle);
    }
};