-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh(#30): Add ability for ARGoS to pin threads to cores
- Loading branch information
Showing
11 changed files
with
255 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @file <argos3/core/simulator/space/space_multi_thread.cpp> | ||
* | ||
* @author John Harwell - <john.r.harwell@gmail.com> | ||
*/ | ||
|
||
#include <argos3/core/simulator/space/space_multi_thread.h> | ||
|
||
namespace argos { | ||
|
||
/****************************************/ | ||
/****************************************/ | ||
|
||
CSpaceMultiThread::CSpaceMultiThread(UInt32 un_n_threads, | ||
bool b_pin_threads_to_cores) : | ||
m_bPinThreadsToCores(b_pin_threads_to_cores), | ||
m_vecThreads(un_n_threads) { | ||
LOG << "[INFO] Using " << GetNumThreads() << " parallel threads" << std::endl; | ||
if (m_bPinThreadsToCores) { | ||
LOG << "[INFO] Pinning threads to cores by thread ID" << std::endl; | ||
} | ||
} | ||
|
||
/****************************************/ | ||
/****************************************/ | ||
void CSpaceMultiThread::DestroyAllThreads(void) { | ||
int nErrors; | ||
if (!m_vecThreads.empty()) { | ||
for(UInt32 i = 0; i < GetNumThreads(); ++i) { | ||
if((nErrors = pthread_cancel(m_vecThreads[i]))) { | ||
THROW_ARGOSEXCEPTION("Error canceling threads " << ::strerror(nErrors)); | ||
} | ||
} | ||
auto** ppJoinResult = new void*[GetNumThreads()]; | ||
for(UInt32 i = 0; i < GetNumThreads(); ++i) { | ||
if((nErrors = pthread_join(m_vecThreads[i], ppJoinResult + i))) { | ||
THROW_ARGOSEXCEPTION("Error joining threads " << ::strerror(nErrors)); | ||
} | ||
if(ppJoinResult[i] != PTHREAD_CANCELED) { | ||
LOGERR << "[WARNING] Thread #" << i<< " not canceled" << std::endl; | ||
} | ||
} | ||
delete[] ppJoinResult; | ||
} | ||
} | ||
|
||
/****************************************/ | ||
/****************************************/ | ||
|
||
void CSpaceMultiThread::CreateSingleThread(UInt32 unThreadId, | ||
void *(*start_routine) (void *), | ||
void *arg) { | ||
if (unThreadId > GetNumThreads()) { | ||
THROW_ARGOSEXCEPTION("Cannot create thread " << unThreadId << ": Id out of range"); | ||
} | ||
/* Create the thread */ | ||
int nErrors = pthread_create(&m_vecThreads[unThreadId], | ||
nullptr, | ||
start_routine, | ||
arg); | ||
if(nErrors != 0) { | ||
THROW_ARGOSEXCEPTION("Error creating thread: " << ::strerror(nErrors)); | ||
} | ||
/* pin thread i to core i */ | ||
if (m_bPinThreadsToCores) { | ||
cpu_set_t cpuset; | ||
CPU_ZERO(&cpuset); | ||
CPU_SET(unThreadId, &cpuset); | ||
int rc = pthread_setaffinity_np(m_vecThreads[unThreadId], | ||
sizeof(cpu_set_t), | ||
&cpuset); | ||
if (rc != 0) { | ||
THROW_ARGOSEXCEPTION("Error setting thread " << unThreadId << " affinity: " << ::strerror(rc)); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @file <argos3/core/simulator/space/space_multi_thread.h> | ||
* | ||
* @brief This file provides the definition of the Swarmanoid 3D Space with | ||
* threads. | ||
* | ||
* @author John Harwell - <john.r.harwell@gmail.com> | ||
*/ | ||
|
||
#ifndef INCLUDE_SPACE_MULTI_THREAD_H_ | ||
#define INCLUDE_SPACE_MULTI_THREAD_H_ | ||
|
||
#include <vector> | ||
|
||
namespace argos { | ||
class CSpace; | ||
} | ||
|
||
#include <argos3/core/simulator/space/space.h> | ||
|
||
namespace argos { | ||
|
||
/** | ||
* @brief Base class for common threading functionality used by @ref | ||
* CSpaceMultithreadBalanceLength, @ref CSpaceMultithreadBalanceQuantity. | ||
*/ | ||
class CSpaceMultiThread : public CSpace { | ||
public: | ||
|
||
CSpaceMultiThread(UInt32 un_n_threads, bool b_pin_threads); | ||
|
||
/** | ||
* @brief Return the # of configured threads. If threads were not | ||
* specified, returns 0. | ||
*/ | ||
inline UInt32 GetNumThreads() const { return m_vecThreads.size(); } | ||
|
||
protected: | ||
|
||
/** | ||
* @brief After ARGoS finishes running the experiment, dispose of all | ||
* threads, which are no longer needed. | ||
*/ | ||
void DestroyAllThreads(); | ||
|
||
/** | ||
* @brief During initialization, create a single thread. | ||
* | ||
* @param unThreadId The index/ID of the thread. | ||
* @param start_routine Pointer to the routine that the thread should start | ||
* execution in. | ||
* @param arg The single argument which will be passed to @p | ||
* start_routine. Can be NULL. | ||
*/ | ||
|
||
void CreateSingleThread(UInt32 unThreadId, | ||
void *(*start_routine) (void *), | ||
void *arg); | ||
|
||
private: | ||
|
||
/** Should threads be pinned to cores ? */ | ||
bool m_bPinThreadsToCores; | ||
|
||
/** The slave thread array */ | ||
std::vector<pthread_t> m_vecThreads; | ||
}; | ||
|
||
} | ||
|
||
#endif /* INCLUDE_SPACE_MULTI_THREAD_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.