handle.cc 2.45 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
PanZezhongQY's avatar
PanZezhongQY committed
20

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

26
27
28
29
    infiniDevice_t device;
    int device_id;
    CHECK_STATUS(infinirtGetDevice(&device, &device_id));

30
31
32
33
#define CREATE(CASE, NAMESPACE) \
    case CASE:                  \
        return device::NAMESPACE::Handle::create(handle_ptr, device_id)

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

    default:
        return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
PanZezhongQY's avatar
PanZezhongQY committed
59
    }
60
61

#undef CREATE
PanZezhongQY's avatar
PanZezhongQY committed
62
63
}

PanZezhong's avatar
PanZezhong committed
64
__C infiniStatus_t infiniopDestroyHandle(infiniopHandle_t handle) {
PanZezhong's avatar
PanZezhong committed
65

66
67
68
69
#define DELETE(CASE, NAMESPACE)                                       \
    case CASE:                                                        \
        delete reinterpret_cast<device::NAMESPACE::Handle *>(handle); \
        return INFINI_STATUS_SUCCESS
PanZezhong's avatar
PanZezhong committed
70

PanZezhongQY's avatar
PanZezhongQY committed
71
72
    switch (handle->device) {
#ifdef ENABLE_CPU_API
73
        DELETE(INFINI_DEVICE_CPU, cpu);
PanZezhongQY's avatar
PanZezhongQY committed
74
75
#endif
#ifdef ENABLE_CUDA_API
76
        DELETE(INFINI_DEVICE_NVIDIA, cuda::nvidia);
PanZezhongQY's avatar
PanZezhongQY committed
77
#endif
PanZezhong's avatar
PanZezhong committed
78
#ifdef ENABLE_CAMBRICON_API
79
80
81
    case INFINI_DEVICE_CAMBRICON: {
        return destroyBangHandle((infiniopBangHandle_t)handle);
    }
PanZezhongQY's avatar
PanZezhongQY committed
82
#endif
Pan Zezhong's avatar
Pan Zezhong committed
83
#ifdef ENABLE_ASCEND_API
84
85
86
    case INFINI_DEVICE_ASCEND: {
        return destroyAscendHandle((infiniopAscendHandle_t)handle);
    }
87
88
89
90
91
#endif
#ifdef ENABLE_KUNLUN_API
    case INFINI_DEVICE_KUNLUN: {
        return destroyKunlunHandle((infiniopKunlunHandle_t)handle);
    }
PanZezhongQY's avatar
PanZezhongQY committed
92
#endif
PanZezhong's avatar
PanZezhong committed
93
94
    default:
        return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
PanZezhongQY's avatar
PanZezhongQY committed
95
    }
96

PanZezhong's avatar
PanZezhong committed
97
#undef DELETE
98
}