runtime_environment.cc 4.4 KB
Newer Older
X
xiexionghang 已提交
1
#include <mpi.h>
X
xiexionghang 已提交
2 3 4 5 6 7
#include "paddle/fluid/train/custom_trainer/feed/common/runtime_environment.h"

namespace paddle {
namespace custom_trainer {
namespace feed {

X
xiexionghang 已提交
8 9 10 11 12 13 14 15 16 17 18 19
RuntimeEnvironment::RuntimeEnvironment() {}
RuntimeEnvironment::~RuntimeEnvironment() {}
bool RuntimeEnvironment::is_master_node(EnvironmentRole role) {
    return rank_id(role) == 0;
}
std::string format_timestamp(time_t time, const char* format) {
    std::string result;
    struct tm p = *localtime(&time);
    char time_str_buffer[64];
    int size = strftime (time_str_buffer, 64, format, &p);
    if (size > 0) {
        result.assign(time_str_buffer, size);
X
xiexionghang 已提交
20
    }
X
xiexionghang 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
    return result;
}

struct MpiNodeInfo {
    int rank_id = -1;
    int node_num = 0;
    MPI_Comm mpi_comm;
};

class MPIRuntimeEnvironment : public RuntimeEnvironment {
public:
    MPIRuntimeEnvironment() {}
    virtual ~MPIRuntimeEnvironment() {}
    virtual int initialize(YAML::Node config) {
X
xiexionghang 已提交
35 36
        return 0;
    }
X
xiexionghang 已提交
37 38 39 40 41 42 43 44
    virtual int wireup() {
        int hr = MPI_Init(NULL, NULL);
        if (MPI_SUCCESS != hr) {
            LOG(FATAL) << "MPI_init failed with error code" << hr; 
            return -1;
        }
        _roles_node_info.resize(static_cast<int>(EnvironmentRole::ALL) + 1);
        set_role(EnvironmentRole::ALL);
X
xiexionghang 已提交
45 46
        return 0;
    }
X
xiexionghang 已提交
47 48 49 50 51
    
    virtual paddle::ps::PSEnvironment* ps_environment() {
        static paddle::ps::MpiPSEnvironment ps_environment;
        return &ps_environment;
    }
X
xiexionghang 已提交
52 53 54 55 56 57

    virtual uint32_t rank_id(EnvironmentRole role) {
        return mpi_node_info(role).rank_id;
    }
    virtual uint32_t node_num(EnvironmentRole role) {
        return mpi_node_info(role).node_num;
X
xiexionghang 已提交
58
    }
X
xiexionghang 已提交
59 60 61 62 63 64 65 66 67 68 69
    virtual int set_role(EnvironmentRole role) {
        auto& node_info = mpi_node_info(role);
        if (node_info.rank_id < 0) {
            if (role == EnvironmentRole::ALL) {
                node_info.mpi_comm = MPI_COMM_WORLD;
            } else {
                MPI_Comm_split(MPI_COMM_WORLD, static_cast<int>(role), 
                    mpi_node_info(EnvironmentRole::ALL).rank_id, &(node_info.mpi_comm));
            }
            MPI_Comm_rank(node_info.mpi_comm, &(node_info.rank_id));
            MPI_Comm_size(node_info.mpi_comm, &(node_info.node_num));
X
xiexionghang 已提交
70
        }
X
xiexionghang 已提交
71
        return 0;
X
xiexionghang 已提交
72
    }
X
xiexionghang 已提交
73 74 75 76

    virtual void barrier(EnvironmentRole role) {
        MPI_Barrier(mpi_node_info(role).mpi_comm);
    }
X
xiexionghang 已提交
77

X
xiexionghang 已提交
78 79
    virtual void bcast(paddle::framework::BinaryArchive& ar, int root_id, EnvironmentRole role) {
        auto& node_info = mpi_node_info(role);
X
xiexionghang 已提交
80
        int len = (int)ar.Length();
X
xiexionghang 已提交
81
        MPI_Bcast(&len, 1, MPI_INT, root_id, node_info.mpi_comm);
X
xiexionghang 已提交
82 83 84
        ar.Resize(len);
        ar.SetCursor(ar.Buffer());
        MPI_Bcast(ar.Buffer(), len, MPI_BYTE, root_id, node_info.mpi_comm);
X
xiexionghang 已提交
85
    }
X
xiexionghang 已提交
86

X
xiexionghang 已提交
87
protected:
X
xiexionghang 已提交
88 89 90
    virtual void print_log(EnvironmentRole role, EnvironmentLogType type, 
        EnvironmentLogLevel level,  const std::string& log_str) {
        if (type == EnvironmentLogType::MASTER_LOG && !is_master_node(role)) {
X
xiexionghang 已提交
91 92
            return;
        }
X
xiexionghang 已提交
93 94 95
        VLOG(static_cast<int>(level)) << log_str;
    }

X
xiexionghang 已提交
96 97 98
    inline MpiNodeInfo& mpi_node_info(EnvironmentRole role) {
        return _roles_node_info[static_cast<int>(role)];
    }
X
xiexionghang 已提交
99

X
xiexionghang 已提交
100 101 102
private:
    std::vector<MpiNodeInfo> _roles_node_info;
};
X
xiexionghang 已提交
103
REGIST_CLASS(RuntimeEnvironment, MPIRuntimeEnvironment);
X
xiexionghang 已提交
104 105 106 107 108 109 110 111 112 113 114 115

//用于本地模式单机训练
class LocalRuntimeEnvironment : public RuntimeEnvironment {
public:
    LocalRuntimeEnvironment() {}
    virtual ~LocalRuntimeEnvironment() {}
    virtual int initialize(YAML::Node config) {
        return 0;
    }
    virtual int wireup() {
        return 0;
    }
X
xiexionghang 已提交
116 117 118 119
    virtual paddle::ps::PSEnvironment* ps_environment() {
        static paddle::ps::LocalPSEnvironment ps_environment;
        return &ps_environment;
    }
X
xiexionghang 已提交
120 121 122 123 124 125 126 127 128 129
    virtual uint32_t rank_id(EnvironmentRole role) {
        return 0;
    }
    virtual uint32_t node_num(EnvironmentRole role) {
        return 1;
    }
    virtual int set_role(EnvironmentRole role) {
        return 0;
    }
    virtual void barrier(EnvironmentRole role) {
X
xiexionghang 已提交
130 131
        return;
    }
X
xiexionghang 已提交
132 133 134 135 136 137 138 139 140
    virtual void bcast(paddle::framework::BinaryArchive& ar, int root_id, EnvironmentRole role) {
        return;
    }
protected:
    virtual void print_log(EnvironmentRole role, EnvironmentLogType type, 
        EnvironmentLogLevel level,  const std::string& log_str) {
        VLOG(static_cast<int>(level)) << log_str;
    }
};
X
xiexionghang 已提交
141
REGIST_CLASS(RuntimeEnvironment, LocalRuntimeEnvironment);
X
xiexionghang 已提交
142 143 144 145

}  // namespace feed
}  // namespace custom_trainer
}  // namespace paddle