handle.cc 2.65 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
8
#endif
#ifdef ENABLE_CUDA_API
9
#include "cuda/cuda_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_KUNLUN_API
zhangyue's avatar
zhangyue committed
18
#include "kunlun/kunlun_handle.h"
19
#endif
20
#ifdef ENABLE_METAX_API
21
22
#include "maca/maca_handle.h"
#endif
PanZezhongQY's avatar
PanZezhongQY committed
23

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

29
30
31
32
    infiniDevice_t device;
    int device_id;
    CHECK_STATUS(infinirtGetDevice(&device, &device_id));

33
34
35
36
#define CREATE(CASE, NAMESPACE) \
    case CASE:                  \
        return device::NAMESPACE::Handle::create(handle_ptr, device_id)

PanZezhongQY's avatar
PanZezhongQY committed
37
38
    switch (device) {
#ifdef ENABLE_CPU_API
39
        CREATE(INFINI_DEVICE_CPU, cpu);
PanZezhongQY's avatar
PanZezhongQY committed
40
41
#endif
#ifdef ENABLE_CUDA_API
42
        CREATE(INFINI_DEVICE_NVIDIA, cuda::nvidia);
PanZezhongQY's avatar
PanZezhongQY committed
43
44
#endif
#ifdef ENABLE_CAMBRICON_API
45
    case INFINI_DEVICE_CAMBRICON: {
46
        return createBangHandle((infiniopBangHandle_t *)handle_ptr);
47
    }
PanZezhongQY's avatar
PanZezhongQY committed
48
49
#endif
#ifdef ENABLE_ASCEND_API
50
    case INFINI_DEVICE_ASCEND: {
51
        return createAscendHandle((infiniopAscendHandle_t *)handle_ptr);
52
    }
53
54
55
56
57
#endif
#ifdef ENABLE_KUNLUN_API
    case INFINI_DEVICE_KUNLUN: {
        return createKunlunHandle((infiniopKunlunHandle_t *)handle_ptr);
    }
PanZezhongQY's avatar
PanZezhongQY committed
58
#endif
59
#ifdef ENABLE_METAX_API
60
61
        CREATE(INFINI_DEVICE_METAX, maca);
#endif
62
63
64

    default:
        return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
PanZezhongQY's avatar
PanZezhongQY committed
65
    }
66
67

#undef CREATE
PanZezhongQY's avatar
PanZezhongQY committed
68
69
}

PanZezhong's avatar
PanZezhong committed
70
__C infiniStatus_t infiniopDestroyHandle(infiniopHandle_t handle) {
PanZezhong's avatar
PanZezhong committed
71

72
73
74
75
#define DELETE(CASE, NAMESPACE)                                       \
    case CASE:                                                        \
        delete reinterpret_cast<device::NAMESPACE::Handle *>(handle); \
        return INFINI_STATUS_SUCCESS
PanZezhong's avatar
PanZezhong committed
76

PanZezhongQY's avatar
PanZezhongQY committed
77
78
    switch (handle->device) {
#ifdef ENABLE_CPU_API
79
        DELETE(INFINI_DEVICE_CPU, cpu);
PanZezhongQY's avatar
PanZezhongQY committed
80
81
#endif
#ifdef ENABLE_CUDA_API
82
        DELETE(INFINI_DEVICE_NVIDIA, cuda::nvidia);
PanZezhongQY's avatar
PanZezhongQY committed
83
#endif
PanZezhong's avatar
PanZezhong committed
84
#ifdef ENABLE_CAMBRICON_API
85
86
87
    case INFINI_DEVICE_CAMBRICON: {
        return destroyBangHandle((infiniopBangHandle_t)handle);
    }
PanZezhongQY's avatar
PanZezhongQY committed
88
#endif
Pan Zezhong's avatar
Pan Zezhong committed
89
#ifdef ENABLE_ASCEND_API
90
91
92
    case INFINI_DEVICE_ASCEND: {
        return destroyAscendHandle((infiniopAscendHandle_t)handle);
    }
93
94
95
96
97
#endif
#ifdef ENABLE_KUNLUN_API
    case INFINI_DEVICE_KUNLUN: {
        return destroyKunlunHandle((infiniopKunlunHandle_t)handle);
    }
98
#endif
99
#ifdef ENABLE_METAX_API
100
        DELETE(INFINI_DEVICE_METAX, maca);
PanZezhongQY's avatar
PanZezhongQY committed
101
#endif
PanZezhong's avatar
PanZezhong committed
102
103
    default:
        return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
PanZezhongQY's avatar
PanZezhongQY committed
104
    }
105

PanZezhong's avatar
PanZezhong committed
106
#undef DELETE
107
}