From 4a8b3b480348a6479d0c42045e476ead2677436d Mon Sep 17 00:00:00 2001 From: Zhihua Lai Date: Thu, 18 Dec 2025 12:16:14 +0000 Subject: [PATCH] Make target the folder name --- README.md | 3 +++ fold-left-fold-right/Makefile | 2 +- parallel-transform/Makefile | 30 ++++++++++++++++++++++++++++++ parallel-transform/main.cpp | 30 ++++++++++++++++++++++++++++++ smart-ptr/Makefile | 2 +- thread-safe-queue/Makefile | 2 +- unique-ptr-basics/Makefile | 2 +- views-zip-enumerate/Makefile | 2 +- 8 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 parallel-transform/Makefile create mode 100644 parallel-transform/main.cpp diff --git a/README.md b/README.md index 1532cac..2862aa9 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Examples include (and will expand to): * [thread-safe-queue](./thread-safe-queue/) * Smart pointers * [unique-ptr-basics](./unique-ptr-basics/) + * [smart-ptr](./smart-ptr/) * Lock‑free / wait‑free data structures * Views * [views-zip-enumerate](./views-zip-enumerate/) @@ -22,6 +23,8 @@ Examples include (and will expand to): * Folding * [fold-left-fold-right](./fold-left-fold-right/) * RAII and ownership patterns +* Parallelism + * [parallel-transform](./parallel-transform/) * Performance‑oriented C++ idioms --- diff --git a/fold-left-fold-right/Makefile b/fold-left-fold-right/Makefile index bd77cf7..941ebb4 100644 --- a/fold-left-fold-right/Makefile +++ b/fold-left-fold-right/Makefile @@ -4,7 +4,7 @@ include ../common.mk # per-example flags CXXFLAGS += -pthread -TARGET := fold_left_fold_right +TARGET := $(notdir $(CURDIR)) SRCS := main.cpp OBJS := $(SRCS:.cpp=.o) diff --git a/parallel-transform/Makefile b/parallel-transform/Makefile new file mode 100644 index 0000000..8921317 --- /dev/null +++ b/parallel-transform/Makefile @@ -0,0 +1,30 @@ +# pull in shared compiler settings +include ../common.mk + +# per-example flags +CXXFLAGS += -pthread + +## get it from the folder name +TARGET := $(notdir $(CURDIR)) +SRCS := main.cpp +OBJS := $(SRCS:.cpp=.o) + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +run: $(TARGET) + ./$(TARGET) $(ARGS) + +clean: + rm -f $(OBJS) $(TARGET) + +# Delegates to top-level Makefile +check-format: + $(MAKE) -f ../Makefile check-format DIR=$(CURDIR) + +.PHONY: all clean run check-format diff --git a/parallel-transform/main.cpp b/parallel-transform/main.cpp new file mode 100644 index 0000000..5b4f76d --- /dev/null +++ b/parallel-transform/main.cpp @@ -0,0 +1,30 @@ +/** + This shows using parallel transform_reduce from C++17 + to perform a fold (reduce) operation on a range of values. e.g. sum up 100 values in parallel +*/ +#include +#include // std::transform_reduce +#include // std::plus +#include +#include // std::execution::par + +int +main() +{ + std::vector numbers(100); + // Initialize the vector with values 1 to 100 + std::iota(numbers.begin(), numbers.end(), 1); + + // Use parallel transform_reduce to sum the values in parallel + int sum = std::transform_reduce( + std::execution::par, // parallel execution policy parallel or std::execution::seq for sequential + numbers.begin(), + numbers.end(), + 0, // initial value + std::plus{}, // binary operation to combine results + [](int v) { return v; } // unary operation to transform each element + ); + std::cout << "Sum of numbers from 1 to 100: " << sum << '\n'; + + return 0; +} diff --git a/smart-ptr/Makefile b/smart-ptr/Makefile index cadca4f..e7cce5b 100644 --- a/smart-ptr/Makefile +++ b/smart-ptr/Makefile @@ -1,7 +1,7 @@ CXX = g++ CXXFLAGS = -std=c++23 -Wall -Wextra -g -I. -TARGET = main +TARGET := $(notdir $(CURDIR)) SRCS = main.cpp all: $(TARGET) diff --git a/thread-safe-queue/Makefile b/thread-safe-queue/Makefile index a6aa96b..941ebb4 100644 --- a/thread-safe-queue/Makefile +++ b/thread-safe-queue/Makefile @@ -4,7 +4,7 @@ include ../common.mk # per-example flags CXXFLAGS += -pthread -TARGET := thread_safe_queue +TARGET := $(notdir $(CURDIR)) SRCS := main.cpp OBJS := $(SRCS:.cpp=.o) diff --git a/unique-ptr-basics/Makefile b/unique-ptr-basics/Makefile index 337df4f..adb9514 100644 --- a/unique-ptr-basics/Makefile +++ b/unique-ptr-basics/Makefile @@ -1,7 +1,7 @@ # shared compiler configuration include ../common.mk -TARGET := unique_ptr_basics +TARGET := $(notdir $(CURDIR)) SRCS := main.cpp OBJS := $(SRCS:.cpp=.o) diff --git a/views-zip-enumerate/Makefile b/views-zip-enumerate/Makefile index a5b354c..941ebb4 100644 --- a/views-zip-enumerate/Makefile +++ b/views-zip-enumerate/Makefile @@ -4,7 +4,7 @@ include ../common.mk # per-example flags CXXFLAGS += -pthread -TARGET := views-zip-enumerate +TARGET := $(notdir $(CURDIR)) SRCS := main.cpp OBJS := $(SRCS:.cpp=.o)