Home | History | Annotate | Download | only in mpi
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifdef TENSORFLOW_USE_MPI
     17 
     18 #include "tensorflow/contrib/mpi/mpi_utils.h"
     19 namespace tensorflow {
     20 
     21 #define max_worker_name_length 128
     22 
     23 MPIUtils::MPIUtils(const std::string& worker_name) {
     24   InitMPI();
     25   // Connect the MPI process IDs to the worker names that are used by TF.
     26   // Gather the names of all the active processes (name can't be longer than
     27   // 128 bytes)
     28   int proc_id = 0, number_of_procs = 1;
     29   char my_name[max_worker_name_length];
     30   MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &proc_id));
     31   MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &number_of_procs));
     32 
     33   CHECK(worker_name.size() < max_worker_name_length)
     34       << "Specified worker name is too long.";
     35   snprintf(my_name, max_worker_name_length, worker_name.c_str());
     36   std::vector<char> worker_names(number_of_procs * max_worker_name_length);
     37   MPI_CHECK(MPI_Allgather(my_name, max_worker_name_length, MPI_CHAR,
     38                           &worker_names[0], max_worker_name_length, MPI_CHAR,
     39                           MPI_COMM_WORLD));
     40 
     41   if (proc_id == 0) LOG(INFO) << "MPI process-ID to gRPC server name map: \n";
     42   for (int i = 0; i < number_of_procs; i++) {
     43     name_to_id_[std::string(&worker_names[i * 128])] = i;
     44     if (proc_id == 0)
     45       LOG(INFO) << "Process: " << i
     46                 << "\tgRPC-name: " << std::string(&worker_names[i * 128])
     47                 << std::endl;
     48   }
     49 }
     50 
     51 void MPIUtils::InitMPI() {
     52   // Initialize the MPI environment if that hasn't been done
     53   int flag = 0;
     54   MPI_CHECK(MPI_Initialized(&flag));
     55   if (!flag) {
     56     int proc_id = 0, number_of_procs = 1, len = -1;
     57     char my_host_name[max_worker_name_length];
     58     // MPI_CHECK(MPI_Init_thread(0, 0, MPI_THREAD_MULTIPLE, &flag));
     59     MPI_CHECK(MPI_Init(0, 0));
     60     MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &proc_id));
     61     MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &number_of_procs));
     62     MPI_CHECK(MPI_Get_processor_name(my_host_name, &len));
     63     fprintf(stderr,
     64             "MPI Environment initialized. Process id: %d Total processes: %d "
     65             "|| Hostname: %s \n",
     66             proc_id, number_of_procs, my_host_name);
     67   }
     68 }
     69 
     70 }  // namespace tensorflow
     71 
     72 #endif  // TENSORFLOW_USE_MPI
     73