Skip to content

TravelerList ctor: strtok_mtx #274

Closed

Description

@yakra

#272 (comment):

  • C++, 16 threads: ~2.2s -> ~2.1s, ~4.76% faster
  • C++, 4 threads: ~4.4s -> ~3.8s, ~15.8% faster
  • C++, 1 thread: ~15.55s -> ~13.2s, ~17.8% faster

There are a few potential bottlenecks affecting scaling to more threads:

  1. disk access
  2. more competition for the strtok_mtx mutex
  3. execution units, because hyperthreading
  4. edit: can't forget memory bandwidth & cache.

# 2 is the most interesting here. I can remove the need for a mutex by using strcspn instead of strtrok, with no performance hit.
The effect may not even be noticeable, but it's still worth checking out.

A new lab server with 1 or 2 Xeon E5-2420s would make an interesting testbed:

  • decreased disk bottleneck
    • RAID array?
    • solid state drive?
  • increased processing bottleneck
    • less CPU speed
    • less memory bandwidth No, wait...
    • less cache I'd wanna keep this out of the equation...
  • with 2 CPUs, we can see how it scales up to 12 or 24 threads

WRT # 3, I can already take a closer look on lab2 at how it scales from 4->8 threads and from 8->16 threads.

Also, how did things change between 8b2c5cd & f12c775?

  • The outer loop, splitting .list files into lines, was converted to use strcspn instead of strtok. No noticeable speed change, but only tested on BiggaTomato (4 cores) IIRC.
  • The more intensive inner loop, splitting lines into fields, was left alone. This could potentially have more effect.

Activity

  1. self-assigned this
    on Dec 17, 2019
  2. yakra commented on Dec 18, 2019

    @yakra
    ContributorAuthor

    ping @FL0gnap
    I can build one lab server with RAID, and another lab server with SSD, otherwise identically equip them... and still have another lab server left over. ;)

  3. yakra commented on Apr 4, 2020

    @yakra
    Author
  4. yakra commented on May 8, 2020

    @yakra
    ContributorAuthor

    On most of my machines, this saves only ~0.11-0.13s at the highest thread counts. It's only on lab3, at 19+ threads, where we save 0.21-0.27s. Darn close to our margin of error.
    This tells me there's enough going on outside the mutex to make collisions not a significant problem.

    This may be interesting to revisit later on after implementing #278.
    Less going on outside the mutex would mean more opportunity for collisions and the performance degradation they bring, possibly resulting in a bigger improvement to observe with this fix.

    	modified:   classes/HighwaySystem.cpp
    	modified:   classes/TravelerList/TravelerList.cpp
    	modified:   siteupdate.cpp
    	modified:   threads/ReadListThread.cpp
    
    diff --git a/siteupdate/cplusplus/classes/HighwaySystem.cpp b/siteupdate/cplusplus/classes/HighwaySystem.cpp
    index 0d6bc2b..7d22cfb 100644
    --- a/siteupdate/cplusplus/classes/HighwaySystem.cpp
    +++ b/siteupdate/cplusplus/classes/HighwaySystem.cpp
    @@ -1,4 +1,3 @@
    -//FIXME try to break strtok. That goes for all strtok project-wide.
     class HighwaySystem
     {      /* This class encapsulates the contents of one .csv file
            that represents the collection of highways within a system.
    diff --git a/siteupdate/cplusplus/threads/ReadListThread.cpp b/siteupdate/cplusplus/threads/ReadListThread.cpp
    index 923fb45..f48f1af 100644
    --- a/siteupdate/cplusplus/threads/ReadListThread.cpp
    +++ b/siteupdate/cplusplus/threads/ReadListThread.cpp
    @@ -1,7 +1,6 @@
     void ReadListThread
     (      unsigned int id, std::list<std::string> *traveler_ids, std::list<std::string>::iterator *it,
    -       std::list<TravelerList*> *traveler_lists, std::mutex *tl_mtx, std::mutex *strtok_mtx,
    -       Arguments *args, ErrorList *el
    +       std::list<TravelerList*> *traveler_lists, std::mutex *tl_mtx, Arguments *args, ErrorList *el
     )
     {      //printf("Starting ReadListThread %02i\n", id); fflush(stdout);
            while (*it != traveler_ids->end())
    @@ -16,7 +15,7 @@ void ReadListThread
                    //printf("ReadListThread %02i (*it)++\n", id); fflush(stdout);
                    std::cout << tl << ' ' << std::flush;
                    tl_mtx->unlock();
    -               TravelerList *t = new TravelerList(tl, el, args, strtok_mtx);
    +               TravelerList *t = new TravelerList(tl, el, args);
                                      // deleted on termination of program
                    TravelerList::alltrav_mtx.lock();
                    traveler_lists->push_back(t);
    diff --git a/siteupdate/cplusplus/siteupdate.cpp b/siteupdate/cplusplus/siteupdate.cpp
    index 2cc92f3..2e68891 100644
    --- a/siteupdate/cplusplus/siteupdate.cpp
    +++ b/siteupdate/cplusplus/siteupdate.cpp
    @@ -96,7 +96,7 @@ using namespace std;
     int main(int argc, char *argv[])
     {      ifstream file;
            string line;
    -       mutex list_mtx, log_mtx, strtok_mtx;
    +       mutex list_mtx, log_mtx;
            time_t timestamp;
     
            // start a timer for including elapsed time reports in messages
    @@ -406,7 +406,7 @@ int main(int argc, char *argv[])
           #ifdef threading_enabled
            // set up for threaded .list file processing
            for (unsigned int t = 0; t < args.numthreads; t++)
    -               thr[t] = new thread(ReadListThread, t, &traveler_ids, &id_it, &traveler_lists, &list_mtx, &strtok_mtx, &args, &el);
    +               thr[t] = new thread(ReadListThread, t, &traveler_ids, &id_it, &traveler_lists, &list_mtx, &args, &el);
            for (unsigned int t = 0; t < args.numthreads; t++)
                    thr[t]->join();
            for (unsigned int t = 0; t < args.numthreads; t++)
    @@ -414,7 +414,7 @@ int main(int argc, char *argv[])
           #else
            for (string &t : traveler_ids)
            {       cout << t << ' ' << std::flush;
    -               traveler_lists.push_back(new TravelerList(t, &el, &args, &strtok_mtx));
    +               traveler_lists.push_back(new TravelerList(t, &el, &args));
            }
            traveler_ids.clear();
           #endif
    diff --git a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp
    index 7263213..fd01eff 100644
    --- a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp
    +++ b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp
    @@ -29,7 +29,7 @@ class TravelerList
            unsigned int preview_systems_clinched;
            static std::mutex alltrav_mtx;  // for locking the traveler_lists list when reading .lists from disk
     
    -       TravelerList(std::string travname, ErrorList *el, Arguments *args, std::mutex *strtok_mtx)
    +       TravelerList(std::string travname, ErrorList *el, Arguments *args)
            {       active_systems_traveled = 0;
                    active_systems_clinched = 0;
                    preview_systems_traveled = 0;
    @@ -104,9 +104,15 @@ class TravelerList
                            }
                            // process fields in line
                            std::vector<char*> fields;
    -                       strtok_mtx->lock();
    -                       for (char *token = strtok(lines[l], " \t"); token; token = strtok(0, " \t") ) fields.push_back(token);
    -                       strtok_mtx->unlock();
    +                       size_t spn = 0;
    +                       for (char* c = lines[l]; *c; c += spn)
    +                       {       spn = strcspn(c, " \t");
    +                               while (c[spn] == ' ' || c[spn] == '\t')
    +                               {       c[spn] = 0;
    +                                       spn++;
    +                               }
    +                               fields.emplace_back(c);
    +                       }
                            if (fields.size() != 4)
                              // OK if 5th field exists and starts with #
                              if (fields.size() < 5 || fields[4][0] != '#')
  5. yakra commented on May 18, 2020

    @yakra
    ContributorAuthor

    spn assignment followed by 4-line while loop can consolidated into a 1-line for loop.

    • TravelerList.cpp
    • Waypoint.cpp
    • read_wpt.cpp
  6. yakra commented on May 18, 2020

    @yakra
    ContributorAuthor

    Ooh yes. This does help now; lab2 scales all the way to 16 threads, taking 0.94s vice 1.24s before.
    Improvements for lab3 may be more dramatic.

  7. yakra commented on May 20, 2020

    @yakra
    ContributorAuthor

    The obligatory C++ speed graph:
    List4
    Compare to:
    | graph from #278 | graph from #320 | graph from #313 |
    Takes a while to take effect, but the effects are decent, better than I was anticipating.

    The difference for BiggaTomato looks like a bigger improvement than it really should have been. Particularly at 1 thread, as by definition removing the mutex will (other than the negligible overhead of the lock & unlock operations themselves) only help when multi-threaded.
    It so happened that a few of the passes for the _tl4 test took 16.3, 16.7 & 17.0 s, driving up the average. Leave those data out, and we're at 12.59s, more in line with the 12.43s in C70534.

  8. added a commit that references this issue on May 20, 2020
    11d0ca7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions