cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

rank_size_proc.cpp (902B)


      1 #include <cassert>
      2 #include <iostream>
      3 #include <mpi.h>
      4 
      5 int main(int argc, char *argv[])
      6 {
      7     int size, rank, len, err;
      8     char hostname[MPI_MAX_PROCESSOR_NAME];
      9 
     10     // initialize MPI
     11     err = MPI_Init(&argc, &argv);
     12     assert(err == MPI_SUCCESS);
     13 
     14     // get number of ranks in the MPI_COMM_WORLD communicator
     15     err = MPI_Comm_size(MPI_COMM_WORLD, &size);
     16     assert(err == MPI_SUCCESS);
     17 
     18     // get my rank
     19     err = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
     20     assert(err == MPI_SUCCESS);
     21 
     22     // query processor name (the return value is implementation dependent)
     23     err = MPI_Get_processor_name(hostname, &len);
     24     assert(err == MPI_SUCCESS);
     25     std::cout << "Rank " << rank << "/" << size << " running on: " << hostname
     26               << '\n';
     27 
     28     // call other MPI routines
     29 
     30     // terminate the MPI environment
     31     err = MPI_Finalize();
     32     assert(err == MPI_SUCCESS);
     33 
     34     return 0;
     35 }