Skip to content

safeguard memory-aligned allocation #57

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

Merged
merged 16 commits into from
Jan 27, 2022
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
1 change: 0 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ revdep/
.vscode/
new-benchmarks.R
bench*
revdep/
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: RcppThread
Title: R-Friendly Threading in C++
Version: 2.0.0
Version: 2.0.1
Authors@R: c(
person("Thomas", "Nagler",, "mail@tnagler.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-1855-0046"))
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# # RcppThread 2.0.1
#
# BUG FIX
#
# * safeguard construction of memory aligned objects.


# RcppThread 2.0.0

NEW FEATURES

* Add R function `detectCores()` (#48).

* Add classes `ProgressCounter` and `ProgressBar` for tracking progress in long-
Expand Down
10 changes: 10 additions & 0 deletions R/inline.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
inlineCxxPlugin <- function(...) {
settings <- Rcpp::Rcpp.plugin.maker(
include.before = "#include <RcppThread.h>",
package = "RcppThread",
libs = if (Sys.info()["sysname"] == "Linux") "-latomic -lpthread" else ""
)()
settings$env$USE_CXX11 <- "yes"
settings
}

28 changes: 11 additions & 17 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
Fixed URL/DOI formatting required by Uwe Ligges via mail.

The DOI in the CITATION, DESCRIPTION, and documentation is for a new JSS publication that will be registered after publication on CRAN.

Fixes build warning on windows about misaligned address/allocation. Hopefully
also the problems on Fedora, which I could not reproduce.

## Test environments
* ubuntu 16.04 (devel, release, old-rel)
* ubuntu 20.04 (devel, release, old-rel)
* macOS X (release)
* Windows Server 2019 (release)
* CRAN win builder (devel)
* rhub::check_for_cran()
* rhub::check_with_sanitizers()
* rhub::check_with_valgrind()

## R CMD check results

0 errors | 0 warnings | 1 note

* Note caused by invalid DOI.

## Reverse dependencies

We checked 11 reverse dependencies (11 from CRAN), comparing R CMD check results across CRAN and dev versions of this package.

* We saw 0 new problems
* We failed to check 0 packages
## Check status summary
WARN NOTE OK
Source packages 0 0 1
Reverse depends 1 17 1

The warning in package CDSeq is known and expected.
22 changes: 11 additions & 11 deletions docs/xml/classRcppThread_1_1ThreadPool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<compoundname>RcppThread::ThreadPool</compoundname>
<includes local="no">ThreadPool.hpp</includes>
<sectiondef kind="private-attrib">
<memberdef kind="variable" id="classRcppThread_1_1ThreadPool_1ae01e3405af79ea4d86d1b63b2c822311" prot="private" static="no" mutable="no">
<type>quickpool::ThreadPool</type>
<definition>quickpool::ThreadPool RcppThread::ThreadPool::pool_</definition>
<memberdef kind="variable" id="classRcppThread_1_1ThreadPool_1ad5e5077aa6d1b23d5344cba2fef23761" prot="private" static="no" mutable="no">
<type>std::unique_ptr&lt; quickpool::ThreadPool &gt;</type>
<definition>std::unique_ptr&lt;quickpool::ThreadPool&gt; RcppThread::ThreadPool::pool_</definition>
<argsstring></argsstring>
<name>pool_</name>
<briefdescription>
Expand All @@ -15,7 +15,7 @@
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="65" column="27" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="65" bodyend="-1"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="65" column="21" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="65" bodyend="-1"/>
</memberdef>
<memberdef kind="variable" id="classRcppThread_1_1ThreadPool_1a884ba54cc9f225392007a0736b80977d" prot="private" static="no" mutable="no">
<type>std::thread::id</type>
Expand Down Expand Up @@ -393,7 +393,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="53" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="167" bodyend="197"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="53" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="167" bodyend="192"/>
</memberdef>
<memberdef kind="function" id="classRcppThread_1_1ThreadPool_1a5209d746d78202faf17524ce6bf2e94a" prot="public" static="no" const="no" explicit="no" inline="yes" virt="non-virtual">
<templateparamlist>
Expand Down Expand Up @@ -463,7 +463,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="56" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="223" bodyend="226"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="56" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="218" bodyend="221"/>
</memberdef>
<memberdef kind="function" id="classRcppThread_1_1ThreadPool_1a6c93f9e84fadfdc1a3357770d47a738a" prot="public" static="no" const="no" explicit="no" inline="yes" virt="non-virtual">
<type>void</type>
Expand All @@ -477,7 +477,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="58" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="231" bodyend="244"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="58" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="226" bodyend="239"/>
</memberdef>
<memberdef kind="function" id="classRcppThread_1_1ThreadPool_1a1ba95e4fe30c81e19c7980c865a8ce86" prot="public" static="no" const="no" explicit="no" inline="yes" virt="non-virtual">
<type>void</type>
Expand All @@ -491,7 +491,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="59" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="248" bodyend="251"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="59" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="243" bodyend="246"/>
</memberdef>
<memberdef kind="function" id="classRcppThread_1_1ThreadPool_1a332b4f9a74d1c4e206e3957a299e0629" prot="public" static="no" const="no" explicit="no" inline="yes" virt="non-virtual">
<type>void</type>
Expand All @@ -518,7 +518,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="61" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="256" bodyend="259"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="61" column="10" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="251" bodyend="254"/>
</memberdef>
<memberdef kind="function" id="classRcppThread_1_1ThreadPool_1aa73eb6889492403da12d60a1a92de519" prot="public" static="no" const="yes" explicit="no" inline="yes" virt="non-virtual">
<type>size_t</type>
Expand All @@ -532,7 +532,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
</detaileddescription>
<inbodydescription>
</inbodydescription>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="62" column="12" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="263" bodyend="266"/>
<location file="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" line="62" column="12" bodyfile="/data/dev/r/RcppThread/inst/include/RcppThread/ThreadPool.hpp" bodystart="258" bodyend="261"/>
</memberdef>
</sectiondef>
<sectiondef kind="public-static-func">
Expand Down Expand Up @@ -567,7 +567,7 @@ Consider the following code: <programlisting><codeline><highlight class="normal"
<member refid="classRcppThread_1_1ThreadPool_1a884ba54cc9f225392007a0736b80977d" prot="private" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>owner_thread_</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a86f352d702e3a8dc211838c9518a83d7" prot="public" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>parallelFor</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a5209d746d78202faf17524ce6bf2e94a" prot="public" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>parallelForEach</name></member>
<member refid="classRcppThread_1_1ThreadPool_1ae01e3405af79ea4d86d1b63b2c822311" prot="private" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>pool_</name></member>
<member refid="classRcppThread_1_1ThreadPool_1ad5e5077aa6d1b23d5344cba2fef23761" prot="private" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>pool_</name></member>
<member refid="classRcppThread_1_1ThreadPool_1af1c8048954f42b41d19bd8fa9133490a" prot="public" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>push</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a14e16052783d48a17791b3f29d5149f0" prot="public" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>pushReturn</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a0967f38d786196019a06f0e65642cfb3" prot="public" virt="non-virtual"><scope>RcppThread::ThreadPool</scope><name>resize</name></member>
Expand Down
2 changes: 1 addition & 1 deletion docs/xml/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<member refid="classRcppThread_1_1Thread_1a1ae92afcef21111e10a96cf5f4c8c8f0" kind="function"><name>hardware_concurrency</name></member>
</compound>
<compound refid="classRcppThread_1_1ThreadPool" kind="class"><name>RcppThread::ThreadPool</name>
<member refid="classRcppThread_1_1ThreadPool_1ae01e3405af79ea4d86d1b63b2c822311" kind="variable"><name>pool_</name></member>
<member refid="classRcppThread_1_1ThreadPool_1ad5e5077aa6d1b23d5344cba2fef23761" kind="variable"><name>pool_</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a884ba54cc9f225392007a0736b80977d" kind="variable"><name>owner_thread_</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a83ffe33d4d776b62baf312a1c4965c08" kind="function"><name>ThreadPool</name></member>
<member refid="classRcppThread_1_1ThreadPool_1a524ee4a9d217cba6cbdfd3405515376b" kind="function"><name>ThreadPool</name></member>
Expand Down
14 changes: 10 additions & 4 deletions docs/xml/quickpool_8hpp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<includes local="no">exception</includes>
<includes local="no">functional</includes>
<includes local="no">future</includes>
<includes local="no">memory</includes>
<includes local="no">mutex</includes>
<includes local="no">numeric</includes>
<includes local="no">thread</includes>
Expand All @@ -20,7 +21,7 @@
<node id="6">
<label>functional</label>
</node>
<node id="9">
<node id="10">
<label>numeric</label>
</node>
<node id="5">
Expand Down Expand Up @@ -52,22 +53,27 @@
</childnode>
<childnode refid="11" relation="include">
</childnode>
<childnode refid="12" relation="include">
</childnode>
</node>
<node id="7">
<label>future</label>
</node>
<node id="11">
<node id="12">
<label>vector</label>
</node>
<node id="8">
<node id="9">
<label>mutex</label>
</node>
<node id="10">
<node id="11">
<label>thread</label>
</node>
<node id="2">
<label>algorithm</label>
</node>
<node id="8">
<label>memory</label>
</node>
</incdepgraph>
<briefdescription>
</briefdescription>
Expand Down
35 changes: 15 additions & 20 deletions inst/include/RcppThread/ThreadPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ThreadPool
size_t getNumThreads() const;

private:
quickpool::ThreadPool pool_;
std::unique_ptr<quickpool::ThreadPool> pool_;
std::thread::id owner_thread_;
};

Expand All @@ -75,7 +75,7 @@ inline ThreadPool::ThreadPool()
//! @param nWorkers number of worker threads to create; if `nWorkers = 0`, all
//! work pushed to the pool will be done in the main thread.
inline ThreadPool::ThreadPool(size_t nWorkers)
: pool_{ nWorkers }
: pool_{ new quickpool::ThreadPool(nWorkers) }
, owner_thread_{ std::this_thread::get_id() }
{}

Expand Down Expand Up @@ -108,7 +108,7 @@ template<class F, class... Args>
void
ThreadPool::push(F&& f, Args&&... args)
{
pool_.push(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
pool_->push(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
}

//! pushes jobs returning a value to the thread pool.
Expand All @@ -122,7 +122,7 @@ auto
ThreadPool::pushReturn(F&& f, Args&&... args)
-> std::future<decltype(f(args...))>
{
return pool_.async(
return pool_->async(
std::bind(std::forward<F>(f), std::forward<Args>(args)...));
}

Expand Down Expand Up @@ -169,7 +169,7 @@ ThreadPool::parallelFor(int begin, int end, F f, size_t nBatches)
if (nBatches == 0) {
// each worker has its dedicated range, but can steal part of another
// worker's ranges when done with own
auto thr = std::max(pool_.get_active_threads(), static_cast<size_t>(1));
auto thr = std::max(pool_->get_active_threads(), static_cast<size_t>(1));
auto workers = quickpool::loop::create_workers<F>(f, begin, end, thr);
for (size_t k = 0; k < thr; k++) {
this->push([=] { workers->at(k).run(workers); });
Expand All @@ -181,17 +181,12 @@ ThreadPool::parallelFor(int begin, int end, F f, size_t nBatches)
return;
nBatches = std::min(nBatches, nTasks);

size_t minSize = nTasks / nBatches;
int remSize = nTasks % nBatches;

size_t sz = nTasks / nBatches;
int rem = nTasks % nBatches;
for (size_t b = 0; b < nBatches; b++) {
int bSize = minSize + (remSize-- > 0);
this->push([=] {
for (int i = begin; i < begin + bSize; ++i) {
f(i);
}
});
begin += bSize;
int bs = sz + (rem-- > 0);
this->push([=] { for (int i = begin; i < begin + bs; ++i) f(i); });
begin += bs;
}
}
}
Expand Down Expand Up @@ -235,34 +230,34 @@ ThreadPool::wait()
return;
}
do {
pool_.wait(100);
pool_->wait(100);
Rcout << "";
checkUserInterrupt();

} while (!pool_.done());
} while (!pool_->done());
Rcout << "";
}

//! waits for all jobs to finish.
inline void
ThreadPool::join()
{
pool_.wait();
pool_->wait();
}

//! sets the number of active threads in the pool.
//! @param threads the desired number of threads.
inline void
ThreadPool::setNumThreads(size_t threads)
{
pool_.set_active_threads(threads);
pool_->set_active_threads(threads);
}

//! gets the number of active threads in the pool.
inline size_t
ThreadPool::getNumThreads() const
{
return pool_.get_active_threads();
return pool_->get_active_threads();
}

} // end namespace RcppThread
Loading