handle.cc 3.05 KB
Newer Older
PanZezhongQY's avatar
PanZezhongQY committed
1
#include "infiniop/handle.h"
2
3
4
#include "../../utils.h"
#include "infinirt.h"

PanZezhongQY's avatar
PanZezhongQY committed
5
#ifdef ENABLE_CPU_API
6
#include "cpu/cpu_handle.h"
PanZezhongQY's avatar
PanZezhongQY committed
7
#endif
8
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_ILUVATAR_API) || defined(ENABLE_QY_API) || defined(ENABLE_HYGON_API)
9
#include "nvidia/nvidia_handle.h"
PanZezhongQY's avatar
PanZezhongQY committed
10
#endif
PanZezhong's avatar
PanZezhong committed
11
#ifdef ENABLE_CAMBRICON_API
12
#include "bang/bang_handle.h"
PanZezhongQY's avatar
PanZezhongQY committed
13
#endif
Pan Zezhong's avatar
Pan Zezhong committed
14
#ifdef ENABLE_ASCEND_API
15
#include "ascend/ascend_handle.h"
PanZezhongQY's avatar
PanZezhongQY committed
16
#endif
17
#ifdef ENABLE_MOORE_API
18
#include "moore/moore_handle.h"
19
#endif
20
#ifdef ENABLE_KUNLUN_API
zhangyue's avatar
zhangyue committed
21
#include "kunlun/kunlun_handle.h"
22
#endif
23
#ifdef ENABLE_METAX_API
24
#include "metax/metax_handle.h"
25
#endif
PanZezhongQY's avatar
PanZezhongQY committed
26

27
__C infiniStatus_t infiniopCreateHandle(infiniopHandle_t *handle_ptr) {
PanZezhongQY's avatar
PanZezhongQY committed
28
    if (handle_ptr == nullptr) {
PanZezhong's avatar
PanZezhong committed
29
        return INFINI_STATUS_NULL_POINTER;
PanZezhongQY's avatar
PanZezhongQY committed
30
31
    }

32
33
34
35
    infiniDevice_t device;
    int device_id;
    CHECK_STATUS(infinirtGetDevice(&device, &device_id));

36
37
38
39
#define CREATE(CASE, NAMESPACE) \
    case CASE:                  \
        return device::NAMESPACE::Handle::create(handle_ptr, device_id)

PanZezhongQY's avatar
PanZezhongQY committed
40
41
    switch (device) {
#ifdef ENABLE_CPU_API
42
        CREATE(INFINI_DEVICE_CPU, cpu);
PanZezhongQY's avatar
PanZezhongQY committed
43
#endif
44
#ifdef ENABLE_NVIDIA_API
45
        CREATE(INFINI_DEVICE_NVIDIA, nvidia);
PanZezhongQY's avatar
PanZezhongQY committed
46
#endif
47
#ifdef ENABLE_ILUVATAR_API
Derui Yang's avatar
Derui Yang committed
48
        CREATE(INFINI_DEVICE_ILUVATAR, iluvatar);
49
#endif
50
51
52
#ifdef ENABLE_QY_API
        CREATE(INFINI_DEVICE_QY, qy);
#endif
PanZezhongQY's avatar
PanZezhongQY committed
53
#ifdef ENABLE_CAMBRICON_API
54
        CREATE(INFINI_DEVICE_CAMBRICON, bang::cambricon);
PanZezhongQY's avatar
PanZezhongQY committed
55
56
#endif
#ifdef ENABLE_ASCEND_API
57
        CREATE(INFINI_DEVICE_ASCEND, ascend);
58
#endif
59
#ifdef ENABLE_MOORE_API
60
        CREATE(INFINI_DEVICE_MOORE, moore);
61
#endif
62
#ifdef ENABLE_KUNLUN_API
zhangyue's avatar
zhangyue committed
63
        CREATE(INFINI_DEVICE_KUNLUN, kunlun);
PanZezhongQY's avatar
PanZezhongQY committed
64
#endif
65
#ifdef ENABLE_METAX_API
66
        CREATE(INFINI_DEVICE_METAX, metax);
67
#endif
68
69
70
#ifdef ENABLE_HYGON_API
        CREATE(INFINI_DEVICE_HYGON, hygon);
#endif
71
72
73

    default:
        return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
PanZezhongQY's avatar
PanZezhongQY committed
74
    }
75
76

#undef CREATE
PanZezhongQY's avatar
PanZezhongQY committed
77
78
}

PanZezhong's avatar
PanZezhong committed
79
__C infiniStatus_t infiniopDestroyHandle(infiniopHandle_t handle) {
PanZezhong's avatar
PanZezhong committed
80

81
82
83
84
#define DELETE(CASE, NAMESPACE)                                       \
    case CASE:                                                        \
        delete reinterpret_cast<device::NAMESPACE::Handle *>(handle); \
        return INFINI_STATUS_SUCCESS
PanZezhong's avatar
PanZezhong committed
85

PanZezhongQY's avatar
PanZezhongQY committed
86
87
    switch (handle->device) {
#ifdef ENABLE_CPU_API
88
        DELETE(INFINI_DEVICE_CPU, cpu);
PanZezhongQY's avatar
PanZezhongQY committed
89
#endif
90
#ifdef ENABLE_NVIDIA_API
91
        DELETE(INFINI_DEVICE_NVIDIA, nvidia);
PanZezhongQY's avatar
PanZezhongQY committed
92
#endif
93
#ifdef ENABLE_ILUVATAR_API
Derui Yang's avatar
Derui Yang committed
94
        DELETE(INFINI_DEVICE_ILUVATAR, iluvatar);
95
#endif
96
97
98
#ifdef ENABLE_QY_API
        DELETE(INFINI_DEVICE_QY, qy);
#endif
PanZezhong's avatar
PanZezhong committed
99
#ifdef ENABLE_CAMBRICON_API
100
        DELETE(INFINI_DEVICE_CAMBRICON, bang::cambricon);
PanZezhongQY's avatar
PanZezhongQY committed
101
#endif
Pan Zezhong's avatar
Pan Zezhong committed
102
#ifdef ENABLE_ASCEND_API
103
        DELETE(INFINI_DEVICE_ASCEND, ascend);
104
#endif
105
#ifdef ENABLE_MOORE_API
106
        DELETE(INFINI_DEVICE_MOORE, moore);
107
#endif
108
#ifdef ENABLE_KUNLUN_API
zhangyue's avatar
zhangyue committed
109
        DELETE(INFINI_DEVICE_KUNLUN, kunlun);
110
#endif
111
#ifdef ENABLE_METAX_API
112
        DELETE(INFINI_DEVICE_METAX, metax);
113
114
115
#endif
#ifdef ENABLE_HYGON_API
        DELETE(INFINI_DEVICE_HYGON, hygon);
PanZezhongQY's avatar
PanZezhongQY committed
116
#endif
PanZezhong's avatar
PanZezhong committed
117
118
    default:
        return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
PanZezhongQY's avatar
PanZezhongQY committed
119
    }
120

PanZezhong's avatar
PanZezhong committed
121
#undef DELETE
122
}