Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC-30110 Use a really simple queue to optimize parallel join #17683

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions system/jlib/jqueue.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ template <class BASE, bool ALLOWNULLS>
class SafeQueueOf : private QueueOf<BASE, ALLOWNULLS>
{
typedef SafeQueueOf<BASE, ALLOWNULLS> SELF;
typedef QueueOf<BASE, ALLOWNULLS> PARENT;
protected:
mutable CriticalSection crit;
inline void unsafeenqueue(BASE *e) { QueueOf<BASE, ALLOWNULLS>::enqueue(e); }
Expand All @@ -267,6 +268,7 @@ public:
void dequeue(BASE *e) { CriticalBlock b(crit); return QueueOf<BASE, ALLOWNULLS>::dequeue(e); }
inline unsigned ordinality() const { return QueueOf<BASE, ALLOWNULLS>::ordinality(); }
void set(unsigned idx, BASE *e) { CriticalBlock b(crit); return QueueOf<BASE, ALLOWNULLS>::set(idx, e); }
using PARENT::ensure;
};


Expand Down Expand Up @@ -535,6 +537,79 @@ public:
}
};

//A lighter-weight limited thread queue which does not allow timeouts.
//Linux futexes mean that semaphores now perform very well...
template <class BASE, bool ALLOWNULLS>
class ReallySimpleInterThreadQueueOf : protected SafeQueueOf<BASE, ALLOWNULLS>
{
typedef ReallySimpleInterThreadQueueOf<BASE, ALLOWNULLS> SELF;
typedef SafeQueueOf<BASE, ALLOWNULLS> PARENT;
protected:
Semaphore space;
Semaphore avail;
unsigned limit = 0;
std::atomic<bool> stopped{false};

public:
ReallySimpleInterThreadQueueOf<BASE, ALLOWNULLS>()
{
}

~ReallySimpleInterThreadQueueOf<BASE, ALLOWNULLS>()
{
stop();
}

void reset()
{
space.reinit(limit);
avail.reinit(0);
stopped = false;
}

bool enqueue(BASE *e)
{
space.wait();
if (stopped)
return false;
PARENT::enqueue(e);
avail.signal();
return true;
}

BASE *dequeue()
{
avail.wait();
if (stopped)
return nullptr;
BASE * result = PARENT::dequeue();
space.signal();
return result;
}

void setLimit(unsigned num)
{
limit = num;
space.reinit(limit);
PARENT::ensure(limit);
}

void stop(unsigned maxReaders = 0, unsigned maxWriters = 0) // stops all waiting operations
{
//Assume maxreaders < limit, maxwriters < limit if not provided
stopped = true;
avail.signal(maxReaders ? maxReaders : limit);
space.signal(maxWriters ? maxWriters : limit);
}

BASE *dequeueNow()
{
return PARENT::dequeue();
}

using PARENT::ordinality;
};

#define ForEachQueueItemIn(x,y) unsigned numItems##x = (y).ordinality(); \
for (unsigned x = 0; x< numItems##x; ++x)

Expand Down
2 changes: 1 addition & 1 deletion thorlcr/activities/msort/thsortu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ class CMultiCoreUnorderedJoinHelper: public CMultiCoreJoinHelperBase
multiWriter->abort();
}

SimpleInterThreadQueueOf<cWorkItem,false> workqueue;
ReallySimpleInterThreadQueueOf<cWorkItem,false> workqueue;
Owned<IRowMultiWriterReader> multiWriter;
Owned<IRowWriter> rowWriter;

Expand Down
Loading