-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgemm.cc
More file actions
94 lines (75 loc) · 2.75 KB
/
gemm.cc
File metadata and controls
94 lines (75 loc) · 2.75 KB
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
#include <iostream>
#include <numeric>
#include <variant>
#ifdef WITH_CPU
#include "cpu/gemm/gemm.h"
#endif
#if WITH_NVIDIA
#include "nvidia/gemm/cublas.h"
#endif
#if WITH_ILUVATAR
#include "iluvatar/gemm/cublas.h"
#endif
#if WITH_HYGON
#include "hygon/gemm/cublas.h"
#endif
#if WITH_METAX
#include "metax/gemm/mcblas.h"
#endif
#if WITH_CAMBRICON
#include "cambricon/gemm/cnblas.h"
#endif
#if WITH_MOORE
#include "moore/gemm/mublas.h"
#endif
#include "runtime_api.h"
#include "tensor.h"
int main() {
using namespace infini::ops;
constexpr auto m{2};
constexpr auto k{3};
constexpr auto n{4};
std::vector<Tensor::Size> a_shape{m, k};
std::vector<Tensor::Size> b_shape{k, n};
std::vector<Tensor::Size> c_shape{m, n};
const auto a_num_elements{std::accumulate(a_shape.cbegin(), a_shape.cend(), 1,
std::multiplies<int>())};
const auto b_num_elements{std::accumulate(b_shape.cbegin(), b_shape.cend(), 1,
std::multiplies<int>())};
const auto c_num_elements{std::accumulate(c_shape.cbegin(), c_shape.cend(), 1,
std::multiplies<int>())};
std::vector<float> a_vec(a_num_elements);
std::vector<float> b_vec(b_num_elements);
std::vector<float> c_vec(c_num_elements);
std::iota(a_vec.begin(), a_vec.end(), 0);
std::iota(b_vec.begin(), b_vec.end(), 0);
Device dev{DEFAULT_DEVICE_TYPE};
Tensor a_host{a_vec.data(), a_shape, dev};
Tensor b_host{b_vec.data(), b_shape, dev};
Tensor c_host{c_vec.data(), c_shape, dev};
const auto a_size{a_num_elements * kDataTypeToSize.at(a_host.dtype())};
const auto b_size{b_num_elements * kDataTypeToSize.at(b_host.dtype())};
const auto c_size{c_num_elements * kDataTypeToSize.at(c_host.dtype())};
void *a_ptr, *b_ptr, *c_ptr;
DEVICE_MALLOC(&a_ptr, a_size);
DEVICE_MALLOC(&b_ptr, b_size);
DEVICE_MALLOC(&c_ptr, c_size);
DEVICE_MEMCPY(a_ptr, a_vec.data(), a_size, DEVICE_MEMCPY_HOST_TO_DEVICE);
DEVICE_MEMCPY(b_ptr, b_vec.data(), b_size, DEVICE_MEMCPY_HOST_TO_DEVICE);
DEVICE_MEMSET(c_ptr, 0, c_size);
Tensor a_device{a_ptr, a_host.shape(), a_host.dtype(), a_host.device(),
a_host.strides()};
Tensor b_device{b_ptr, b_host.shape(), b_host.dtype(), a_host.device(),
b_host.strides()};
Tensor c_device{c_ptr, c_host.shape(), c_host.dtype(), a_host.device(),
c_host.strides()};
Gemm::call(a_device, b_device, c_device);
DEVICE_MEMCPY(c_vec.data(), c_ptr, c_size, DEVICE_MEMCPY_DEVICE_TO_HOST);
DEVICE_FREE(a_ptr);
DEVICE_FREE(b_ptr);
DEVICE_FREE(c_ptr);
std::cout << "A: " << a_host.ToString() << "\n";
std::cout << "B: " << b_host.ToString() << "\n";
std::cout << "C: " << c_host.ToString() << "\n";
return 0;
}