common.h 4.0 KB
Newer Older
M
Megvii Engine Team 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
 * \file src/common.h
 * MegRay is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#pragma once

14 15
#include <errno.h>

M
Megvii Engine Team 已提交
16 17 18 19 20 21 22 23
#include "cuda_runtime.h"

#include "debug.h"

namespace MegRay {

typedef enum {
    MEGRAY_OK = 0,
24 25 26 27 28 29 30 31 32
    MEGRAY_SYS_ERROR = 1,
    MEGRAY_CUDA_ERR = 2,
    MEGRAY_NCCL_ERR = 3,
    MEGRAY_UCX_ERR = 4,
    MEGRAY_ENV_ERROR = 5,
    MEGRAY_INVALID_ARGUMENT = 6,
    MEGRAY_INVALID_USAGE = 7,
    MEGRAY_UNEXPECTED_ERR = 8,
    MEGRAY_NOT_IMPLEMENTED = 9
M
Megvii Engine Team 已提交
33 34 35 36 37 38 39 40 41 42 43
} Status;

#define MEGRAY_CHECK(expr)                              \
    do {                                                \
        Status status = (expr);                         \
        if (status != MEGRAY_OK) {                      \
            MEGRAY_ERROR("error [%d]", status);         \
            return status;                              \
        }                                               \
    } while (0)

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
#define SYS_CHECK_RET(expr, errval, retval)             \
    do {                                                \
        retval = (expr);                                \
        if (retval == errval) {                         \
            MEGRAY_ERROR("system error [%d]: %s",       \
                errno, strerror(errno));                \
            return MEGRAY_SYS_ERROR;                    \
        }                                               \
    } while (0)

#define SYS_CHECK(expr, errval)                         \
    do {                                                \
        int retval;                                     \
        SYS_CHECK_RET(expr, errval, retval);            \
    } while (0)

#define SYS_ASSERT_RET(expr, errval, retval)            \
    do {                                                \
        retval = (expr);                                \
        if (retval == errval) {                         \
            MEGRAY_ERROR("system error [%d]: %s",       \
                errno, strerror(errno));                \
            MEGRAY_THROW("system error");               \
        }                                               \
    } while (0)

#define SYS_ASSERT(expr, errval)                        \
    do {                                                \
        int retval;                                     \
        SYS_ASSERT_RET(expr, errval, retval);           \
    } while (0)

M
Megvii Engine Team 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#define CUDA_CHECK(expr)                                \
    do {                                                \
        cudaError_t status = (expr);                    \
        if (status != cudaSuccess) {                    \
            MEGRAY_ERROR("cuda error [%d]: %s", status, \
                cudaGetErrorString(status));            \
            return MEGRAY_CUDA_ERR;                     \
        }                                               \
    } while (0)

#define CUDA_ASSERT(expr)                               \
    do {                                                \
        cudaError_t status = (expr);                    \
        if (status != cudaSuccess) {                    \
            MEGRAY_ERROR("cuda error [%d]: %s", status, \
                cudaGetErrorString(status));            \
            MEGRAY_THROW("cuda error");                 \
        }                                               \
    } while (0)

typedef enum {
    MEGRAY_NCCL = 0,
98
    MEGRAY_UCX = 1
M
Megvii Engine Team 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
} Backend;

typedef enum {
    MEGRAY_INT8 = 0,
    MEGRAY_UINT8 = 1,
    MEGRAY_INT32 = 2,
    MEGRAY_UINT32 = 3,
    MEGRAY_INT64 = 4,
    MEGRAY_UINT64 = 5,
    MEGRAY_FLOAT16 = 6,
    MEGRAY_FLOAT32 = 7,
    MEGRAY_FLOAT64 = 8
} DType;

size_t get_dtype_size(DType dtype);

typedef enum {
    MEGRAY_SUM = 0,
    MEGRAY_MAX = 1,
    MEGRAY_MIN = 2
} ReduceOp;

} // namespace MegRay