polaroid-pp

Schlieren and contour plot tool
git clone https://git.0xfab.ch/polaroid-pp.git
Log | Files | Refs | Submodules | README | LICENSE

OrganizerMPI.cpp (2412B)


      1 // File       : OrganizerMPI.cpp
      2 // Date       : Thu Apr 28 10:59:03 2016
      3 // Author     : Fabian Wermelinger
      4 // Description: MPI Organizer Implementation
      5 // Copyright 2016 ETH Zurich. All Rights Reserved.
      6 #include <string>
      7 #include <iostream>
      8 #include <fstream>
      9 #include <cstdio>
     10 #include <cstdlib>
     11 #include "OrganizerMPI.h"
     12 #include "ArgumentParser.h"
     13 
     14 using namespace std;
     15 
     16 OrganizerMPI::OrganizerMPI(const int argc, char ** const argv, const MPI_Comm comm) : m_nscenes(0), m_argc(1), m_comm(comm)
     17 {
     18     m_isroot = (rank() == 0) ? true : false;
     19     ArgumentParser p(argc, (const char**)argv);
     20 
     21     // get scenes
     22     bool bfoundScenes = false;
     23     for (int i=1; i < argc; ++i)
     24     {
     25         const string key(argv[i]);
     26         if (key == "-scenes")
     27         {
     28             char ** const scenes = (argv + (i+1));
     29             m_nscenes = argc - (i+1);
     30             bfoundScenes = true;
     31             for (int i = 0; i < m_nscenes; ++i)
     32                 m_scenes.push_back(string(scenes[i]));
     33             break;
     34         }
     35         ++m_argc;
     36     }
     37     if (p.exist("fscenes"))
     38     {
     39         ifstream infile(p("fscenes").asString());
     40         string line;
     41         int count = 0;
     42         while (infile >> line)
     43         {
     44             m_scenes.push_back(line);
     45             ++count;
     46         }
     47         m_nscenes += count;
     48         bfoundScenes = true;
     49     }
     50 
     51     if (!bfoundScenes)
     52     {
     53         // Define input scenes (=input files) at the end of the argument list
     54         // by using the option "-scenes". You can use globbing here if there
     55         // are many scenes.
     56         if (m_isroot)
     57         {
     58             cerr << "ERROR: No input scenes given. Abort..." << endl;
     59             cerr << "     : (Note: -scenes must be the last argument, followed by [list of] input files)" << endl;
     60         }
     61         abort();
     62     }
     63 }
     64 
     65 vector<string> OrganizerMPI::split_work() const
     66 {
     67     const int myrank = rank();
     68     const int nranks = size();
     69     const int myshare = m_nscenes/nranks;
     70     const int rem = m_nscenes - nranks * myshare;
     71     vector<string> myscenes(0);
     72     if (m_nscenes <= nranks)
     73     {
     74         if (myrank < m_nscenes) myscenes.push_back(m_scenes[myrank]);
     75         return myscenes;
     76     }
     77 
     78     for (int i=0; i < myshare; ++i) // try to distribute work equally
     79         myscenes.push_back(m_scenes[i*nranks + myrank]);
     80     if (myrank < rem)
     81         myscenes.push_back(m_scenes[myshare*nranks + myrank]);
     82     return myscenes;
     83 }