Skip to content

Commit 8a481c0

Browse files
Merge pull request #8 from DoctorLai/parallel-transform
Add C++ std::transform_reduce example
2 parents 2b77d2c + 4a8b3b4 commit 8a481c0

File tree

8 files changed

+68
-5
lines changed

8 files changed

+68
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ Examples include (and will expand to):
1515
* [thread-safe-queue](./thread-safe-queue/)
1616
* Smart pointers
1717
* [unique-ptr-basics](./unique-ptr-basics/)
18+
* [smart-ptr](./smart-ptr/)
1819
* Lock‑free / wait‑free data structures
1920
* Views
2021
* [views-zip-enumerate](./views-zip-enumerate/)
2122
* Atomics and memory ordering
2223
* Folding
2324
* [fold-left-fold-right](./fold-left-fold-right/)
2425
* RAII and ownership patterns
26+
* Parallelism
27+
* [parallel-transform](./parallel-transform/)
2528
* Performance‑oriented C++ idioms
2629

2730
---

fold-left-fold-right/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include ../common.mk
44
# per-example flags
55
CXXFLAGS += -pthread
66

7-
TARGET := fold_left_fold_right
7+
TARGET := $(notdir $(CURDIR))
88
SRCS := main.cpp
99
OBJS := $(SRCS:.cpp=.o)
1010

parallel-transform/Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# pull in shared compiler settings
2+
include ../common.mk
3+
4+
# per-example flags
5+
CXXFLAGS += -pthread
6+
7+
## get it from the folder name
8+
TARGET := $(notdir $(CURDIR))
9+
SRCS := main.cpp
10+
OBJS := $(SRCS:.cpp=.o)
11+
12+
all: $(TARGET)
13+
14+
$(TARGET): $(OBJS)
15+
$(CXX) $(CXXFLAGS) -o $@ $^
16+
17+
%.o: %.cpp
18+
$(CXX) $(CXXFLAGS) -c $< -o $@
19+
20+
run: $(TARGET)
21+
./$(TARGET) $(ARGS)
22+
23+
clean:
24+
rm -f $(OBJS) $(TARGET)
25+
26+
# Delegates to top-level Makefile
27+
check-format:
28+
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)
29+
30+
.PHONY: all clean run check-format

parallel-transform/main.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
This shows using parallel transform_reduce from C++17
3+
to perform a fold (reduce) operation on a range of values. e.g. sum up 100 values in parallel
4+
*/
5+
#include <vector>
6+
#include <numeric> // std::transform_reduce
7+
#include <functional> // std::plus
8+
#include <iostream>
9+
#include <execution> // std::execution::par
10+
11+
int
12+
main()
13+
{
14+
std::vector<int> numbers(100);
15+
// Initialize the vector with values 1 to 100
16+
std::iota(numbers.begin(), numbers.end(), 1);
17+
18+
// Use parallel transform_reduce to sum the values in parallel
19+
int sum = std::transform_reduce(
20+
std::execution::par, // parallel execution policy parallel or std::execution::seq for sequential
21+
numbers.begin(),
22+
numbers.end(),
23+
0, // initial value
24+
std::plus<int>{}, // binary operation to combine results
25+
[](int v) { return v; } // unary operation to transform each element
26+
);
27+
std::cout << "Sum of numbers from 1 to 100: " << sum << '\n';
28+
29+
return 0;
30+
}

smart-ptr/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CXX = g++
22
CXXFLAGS = -std=c++23 -Wall -Wextra -g -I.
33

4-
TARGET = main
4+
TARGET := $(notdir $(CURDIR))
55
SRCS = main.cpp
66

77
all: $(TARGET)

thread-safe-queue/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include ../common.mk
44
# per-example flags
55
CXXFLAGS += -pthread
66

7-
TARGET := thread_safe_queue
7+
TARGET := $(notdir $(CURDIR))
88
SRCS := main.cpp
99
OBJS := $(SRCS:.cpp=.o)
1010

unique-ptr-basics/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# shared compiler configuration
22
include ../common.mk
33

4-
TARGET := unique_ptr_basics
4+
TARGET := $(notdir $(CURDIR))
55
SRCS := main.cpp
66
OBJS := $(SRCS:.cpp=.o)
77

views-zip-enumerate/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include ../common.mk
44
# per-example flags
55
CXXFLAGS += -pthread
66

7-
TARGET := views-zip-enumerate
7+
TARGET := $(notdir $(CURDIR))
88
SRCS := main.cpp
99
OBJS := $(SRCS:.cpp=.o)
1010

0 commit comments

Comments
 (0)