You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are a few potential bottlenecks affecting scaling to more threads:
disk access
more competition for the strtok_mtx mutex
execution units, because hyperthreading
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 bandwidthNo, wait...
less cacheI'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.
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.
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. ;)
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.
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] != '#')
The obligatory C++ speed graph:
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.
Activity
yakra commentedon Dec 18, 2019
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. ;)
yakra commentedon Apr 4, 2020
yakra commentedon May 8, 2020
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.
yakra commentedon May 18, 2020
spnassignment followed by 4-linewhileloop can consolidated into a 1-lineforloop.yakra commentedon May 18, 2020
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.
yakra commentedon May 20, 2020
The obligatory C++ speed graph:

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
_tl4test 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 inC70534..list processing enhancements