Skip to content

Commit

Permalink
Added par_partial_sum to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmoein committed Sep 13, 2023
1 parent 9f8059d commit 00c933e
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/CommonMakefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ PROJECT_INCLUDE_DIR = ../../include
SRCS = ../test/thrpool_tester.cc \
../test/par_sort_tester.cc \
../test/par_accumulate_tester.cc \
../test/par_map_reduce.cc
../test/par_map_reduce.cc \
../test/par_partial_sum.cc

HEADERS = $(LOCAL_INCLUDE_DIR)/ThreadPool/SharedQueue.h \
$(LOCAL_INCLUDE_DIR)/ThreadPool/SharedQueue.tcc \
Expand All @@ -26,7 +27,8 @@ TARGET_LIB =
TARGETS += $(LOCAL_BIN_DIR)/thrpool_tester \
$(LOCAL_BIN_DIR)/par_sort_tester \
$(LOCAL_BIN_DIR)/par_accumulate_tester \
$(LOCAL_BIN_DIR)/par_map_reduce
$(LOCAL_BIN_DIR)/par_map_reduce \
$(LOCAL_BIN_DIR)/par_partial_sum

# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -90,18 +92,24 @@ PAR_MAP_REDUCE_OBJ = $(LOCAL_OBJ_DIR)/par_map_reduce.o
$(LOCAL_BIN_DIR)/par_map_reduce: $(TARGET_LIB) $(PAR_MAP_REDUCE_OBJ)
$(CXX) -o $@ $(PAR_MAP_REDUCE_OBJ) $(LIBS)

PAR_PARTIAL_SUM_OBJ = $(LOCAL_OBJ_DIR)/par_partial_sum.o
$(LOCAL_BIN_DIR)/par_partial_sum: $(TARGET_LIB) $(PAR_PARTIAL_SUM_OBJ)
$(CXX) -o $@ $(PAR_PARTIAL_SUM_OBJ) $(LIBS)

# -----------------------------------------------------------------------------

depend:
makedepend $(CXXFLAGS) -Y $(SRCS)

clean:
rm -f $(LIB_OBJS) $(TARGETS) $(THRPOOL_TESTER_OBJ) $(PAR_SORT_TESTER_OBJ) \
$(PAR_ACCUMULATE_TESTER_OBJ) $(PAR_MAP_REDUCE_OBJ)
$(PAR_ACCUMULATE_TESTER_OBJ) $(PAR_MAP_REDUCE_OBJ) \
$(PAR_PARTIAL_SUM_OBJ)

clobber:
rm -f $(LIB_OBJS) $(TARGETS) $(THRPOOL_TESTER_OBJ) $(PAR_SORT_TESTER_OBJ) \
$(PAR_ACCUMULATE_TESTER_OBJ) $(PAR_MAP_REDUCE_OBJ)
$(PAR_ACCUMULATE_TESTER_OBJ) $(PAR_MAP_REDUCE_OBJ) \
$(PAR_PARTIAL_SUM_OBJ)

install_lib:
cp -pf $(TARGET_LIB) $(PROJECT_LIB_DIR)/.
Expand Down
7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ target_compile_options(par_map_reduce
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/bigobj>
)
add_test(NAME par_map_reduce COMMAND par_map_reduce)

add_executable(par_partial_sum par_partial_sum.cc)
target_link_libraries(par_partial_sum PRIVATE Threads::Threads)
target_compile_options(par_partial_sum
PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/bigobj>
)
add_test(NAME par_partial_sum COMMAND par_partial_sum)
2 changes: 1 addition & 1 deletion test/par_map_reduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static void par_map_reduce() {
const auto m_chunk_size =
data_size / THREAD_COUNT;

fut_maps.reserve(m_chunk_size);
fut_maps.reserve(THREAD_COUNT + 1);
for (std::size_t i = 0; i < data_size; i += m_chunk_size) {
fut_maps.push_back(
thr_pool.dispatch(false,
Expand Down
140 changes: 140 additions & 0 deletions test/par_partial_sum.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Hossein Moein
// September 13, 2023
/*
Copyright (c) 2023-2028, Hossein Moein
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Hossein Moein and/or the ThreadPool nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Hossein Moein BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <ThreadPool/ThreadPool.h>

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>

using namespace hmthrp;

// ----------------------------------------------------------------------------

static constexpr std::size_t THREAD_COUNT = 5;

// ----------------------------------------------------------------------------

struct PartialCalc {

using itr_t = typename std::vector<std::size_t>::iterator;
using fut_t = std::future<std::size_t>;

std::size_t
operator() (const itr_t src_begin, const itr_t src_end,
itr_t dst_begin,
fut_t *prev_fut) {

std::partial_sum(src_begin, src_end, dst_begin);

const std::size_t len = std::distance(src_begin, src_end);
std::size_t ret_val { *(dst_begin + (len - 1)) };

// If we were doing this in place (src == dst), we only needed to
// add the prev value to the first element of the new block
//
if (prev_fut) {
const std::size_t prev_value { prev_fut->get() };

for (std::size_t i = 0; i < len; ++i)
*(dst_begin + i) += prev_value;
ret_val = *(dst_begin + (len - 1));
}
return (ret_val);
}
};

// ----------------------------------------------------------------------------

static void parallel_partial_sum() {

std::cout << "Running parallel_partial_sum() ..." << std::endl;

constexpr std::size_t n { 1000003 };
std::vector<std::size_t> data (n);

std::iota(data.begin(), data.end(), 1);

std::vector<std::size_t> result (n, 0);
constexpr std::size_t block_size { n / THREAD_COUNT };
std::vector<std::future<std::size_t>> futs;
ThreadPool thr_pool { THREAD_COUNT };

futs.reserve(THREAD_COUNT + 1);
futs.push_back(thr_pool.dispatch(false,
PartialCalc{ },
data.begin(),
data.begin() + block_size,
result.begin(),
nullptr));
for (std::size_t i = block_size; i < n; i += block_size) {
const std::size_t block_end {
((i + block_size) > n) ? n : i + block_size
};
// This depends on reserving the right size
//
PartialCalc::fut_t *prev_fut = &(futs.back());

futs.push_back(thr_pool.dispatch(false,
PartialCalc{ },
data.begin() + i,
data.begin() + block_end,
result.begin() + i,
prev_fut));
}
futs.back().get(); // wait for the last thread

constexpr std::size_t last_sum { (n * (n + 1)) / 2 };

assert(result.back() == last_sum);
return;
}

// ----------------------------------------------------------------------------

int main (int, char *[]) {

parallel_partial_sum();

return (EXIT_SUCCESS);
}

// ----------------------------------------------------------------------------

// Local Variables:
// mode:C++
// tab-width:4
// c-basic-offset:4
// End:

0 comments on commit 00c933e

Please sign in to comment.