mpi_utils.cpp 1.8 KB
Newer Older
T
tangwei12 已提交
1 2 3 4
//
// Created by tangwei12 on 2018/3/27.
//

T
tangwei12 已提交
5
#include <stdio.h>
T
tangwei12 已提交
6
#include <string.h>
T
tangwei12 已提交
7

T
tangwei12 已提交
8 9
#include <mpi.h>
#include "mpi_utils.h"
T
tangwei12 已提交
10 11

#define max_worker_name_length 128
T
tangwei12 已提交
12
#define mpi_tag = 2008
T
tangwei12 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

namespace paddle {
namespace operators {
namespace detail {
MPIUtils::MPIUtils(const std::string& worker_name) {
  InitMPI();

  int rank = 0, size = 1;
  char my_name[max_work_group_size];
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  snprintf(my_name, max_worker_name_length, worker_name.c_str());

  std::vector<char> worker_names(size * max_worker_name_length);
  MPI_Allgather(my_name, max_worker_name_length, MPI_CHAR, &worker_names[0],
                max_worker_name_length, MPI_CHAR, MPI_COMM_WORLD);
  for (int i = 0; i < number_of_procs; i++) {
    name_to_id_[std::string(&worker_names[i * 128])] = i;
  }
}

void MPIUtils::InitMPI() {
  int flag = 0;
  MPI_CHECK(MPI_Initialized(&flag));

  if (!flag) {
    int rank = 0, size = 1, len = -1;
    char host_name[max_worker_name_length];

    MPI_Init(0, 0);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Get_processor_name(host_name, &len)
  }
};
T
tangwei12 已提交
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 76 77 78 79 80 81 82 83 84 85 86 87 88

MPIIsend::MPIIsend(int dst, const char* req) {
  done1 = 0;
  done2 = 0;
  length = strlen(req);
  req = req;
}

MPIIsend::Send() {
  MPI_Isend(&req, length, MPI_CHAR, dst, mpi_tag, MPI_COMM_WORLD,
            &msg1_);
  MPI_Test(&msg1_, &done1_, MPI_STATUS_IGNORE)
}

  bool MPIIsend::IsFinished() {
     MPI_Status status;
     if (!done1_) MPI_Test(&msg1_, &done1_, &status);
     return done1;
  }

MPIIsend::~MPIIsend(){
  MPI_Wait(&msg1_, MPI_STATUS_IGNORE);
  MPI_Free_mem(req);
}

MPIIrecv::MPIIrecv(){

}

MPIIrecv::Recv(){

}

MPIIrecv::IsFinished(){

}

MPIIrecv::~MPIIrecv(){

}

T
tangwei12 已提交
89 90 91 92
}  // namespace detail

}  // namespace operators
}  // namespace paddle