From 2ed0804cafc17e70a296570901c1ac085666074b Mon Sep 17 00:00:00 2001 From: CatherineMoore Date: Wed, 12 Nov 2025 18:11:07 -0500 Subject: [PATCH] Amd/dev/catmoore/pal7 (#1670) * Remove SimpleTimer.h due to invalid license. Recode use of timer routines using in tsp.cpp. * 1. Remove counting_iterator.h due to licensing issues 2. Update Makefile to use c++-20 3. Remove counting_iterator references in call to transform_reduce --- examples/stdpar/tsp/Makefile | 3 +- examples/stdpar/tsp/SimpleTimer.h | 51 --------------- examples/stdpar/tsp/counting_iterator.h | 82 ------------------------- examples/stdpar/tsp/tsp.cpp | 38 +++++------- 4 files changed, 18 insertions(+), 156 deletions(-) delete mode 100644 examples/stdpar/tsp/SimpleTimer.h delete mode 100644 examples/stdpar/tsp/counting_iterator.h diff --git a/examples/stdpar/tsp/Makefile b/examples/stdpar/tsp/Makefile index da8a9b191..72dfac71b 100644 --- a/examples/stdpar/tsp/Makefile +++ b/examples/stdpar/tsp/Makefile @@ -23,7 +23,8 @@ ifeq ("$(wildcard $(HIPSTDPARPRIMPATH)/rocprim)","") endif CC = $(LLVM_INSTALL_DIR)/bin/clang++ -opts=-I. -DNDEBUG -O3 --hipstdpar --hipstdpar-path=$(HIPSTDPARPATH) --hipstdpar-thrust-path=$(HIPSTDPARTHRUSTPATH) --hipstdpar-prim-path=$(HIPSTDPARPRIMPATH) --offload-arch=$(LLVM_GPU_ARCH) -std=c++17 +#opts=-I. -DNDEBUG -O3 --hipstdpar --hipstdpar-path=$(HIPSTDPARPATH) --hipstdpar-thrust-path=$(HIPSTDPARTHRUSTPATH) --hipstdpar-prim-path=$(HIPSTDPARPRIMPATH) --offload-arch=$(LLVM_GPU_ARCH) -std=c++17 +opts=-I. -g -O0 --hipstdpar --hipstdpar-path=$(HIPSTDPARPATH) --hipstdpar-thrust-path=$(HIPSTDPARTHRUSTPATH) --hipstdpar-prim-path=$(HIPSTDPARPRIMPATH) --offload-arch=$(LLVM_GPU_ARCH) -std=c++20 # Build both babelstream stdpar binaries tsp:tsp.o diff --git a/examples/stdpar/tsp/SimpleTimer.h b/examples/stdpar/tsp/SimpleTimer.h deleted file mode 100644 index 0babcff67..000000000 --- a/examples/stdpar/tsp/SimpleTimer.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * borrowed from https://gist.github.com/mcleary/b0bf4fa88830ff7c882d - */ - -#include -#include -#include -#include - -class SimpleTimer -{ -public: - void start() - { - m_StartTime = std::chrono::system_clock::now(); - m_bRunning = true; - } - - void stop() - { - m_EndTime = std::chrono::system_clock::now(); - m_bRunning = false; - } - - double elapsedMilliseconds() - { - std::chrono::time_point endTime; - - if(m_bRunning) - { - endTime = std::chrono::system_clock::now(); - } - else - { - endTime = m_EndTime; - } - - return std::chrono::duration_cast(endTime - m_StartTime).count(); - } - - double elapsedSeconds() - { - return elapsedMilliseconds() / 1000.0; - } - -private: - std::chrono::time_point m_StartTime; - std::chrono::time_point m_EndTime; - bool m_bRunning = false; - -}; // class Simpletimer diff --git a/examples/stdpar/tsp/counting_iterator.h b/examples/stdpar/tsp/counting_iterator.h deleted file mode 100644 index 477aa11c6..000000000 --- a/examples/stdpar/tsp/counting_iterator.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef COUNTING_ITERATOR_H_ -#define COUNTING_ITERATOR_H_ - -#include -#include // for std::make_signed -#include - -/* - * This class is borrowed from Lulesh: - * https://github.com/LLNL/LULESH - */ -struct counting_iterator -{ - -private: - using self = counting_iterator; - -public: - using Index_t = int64_t; - using value_type = Index_t; - using difference_type = typename std::make_signed::type; - using pointer = Index_t*; - using reference = Index_t&; - using iterator_category = std::random_access_iterator_tag; - - counting_iterator() : value(0) { } - explicit counting_iterator(value_type v) : value(v) { } - - value_type operator*() const { return value; } - value_type operator[](difference_type n) const { return value + n; } - - self& operator++() { ++value; return *this; } - self operator++(int) { - self result{value}; - ++value; - return result; - } - self& operator--() { --value; return *this; } - self operator--(int) { - self result{value}; - --value; - return result; - } - self& operator+=(difference_type n) { value += n; return *this; } - self& operator-=(difference_type n) { value -= n; return *this; } - - friend self operator+(self const& i, difference_type n) { - return self(i.value + n); - } - friend self operator+(difference_type n, self const& i) { - return self(i.value + n); - } - friend difference_type operator-(self const& x, self const& y) { - return x.value - y.value; - } - friend self operator-(self const& i, difference_type n) { - return self(i.value - n); - } - - friend bool operator==(self const& x, self const& y) { - return x.value == y.value; - } - friend bool operator!=(self const& x, self const& y) { - return x.value != y.value; - } - friend bool operator<(self const& x, self const& y) { - return x.value < y.value; - } - friend bool operator<=(self const& x, self const& y) { - return x.value <= y.value; - } - friend bool operator>(self const& x, self const& y) { - return x.value > y.value; - } - friend bool operator>=(self const& x, self const& y) { - return x.value >= y.value; - } -private: - value_type value; -}; - -#endif // COUNTING_ITERATOR_H_ diff --git a/examples/stdpar/tsp/tsp.cpp b/examples/stdpar/tsp/tsp.cpp index fa52ce33c..48fef359a 100644 --- a/examples/stdpar/tsp/tsp.cpp +++ b/examples/stdpar/tsp/tsp.cpp @@ -1,8 +1,6 @@ #include "route_iterator.h" #include "route_cost.h" #include "tsp_utils.h" -#include "counting_iterator.h" -#include "SimpleTimer.h" // #include // #include @@ -15,8 +13,13 @@ #include #include +#include #include #include +#include + +using namespace std; +using namespace std::chrono; // ============================================ // ============================================ @@ -79,23 +82,20 @@ void test_city_distance() } // test_city_distance -// ============================================ -// ============================================ + template route_cost find_best_route(int const* distances) { - + int X = factorial(N); return std::transform_reduce(std::execution::par_unseq, - counting_iterator(0), - counting_iterator(factorial(N)), + std::views::iota(0, X).begin(), + std::views::iota(0, X).end(), route_cost(), - //route_cost::minf, [](route_cost x, route_cost y) { return x.cost < y.cost ? x : y; }, [=](int64_t i) { int cost = 0; - route_iterator it(i); // first city visited @@ -106,40 +106,34 @@ route_cost find_best_route(int const* distances) while (!it.done()) { int to = it.next(); - cost += distances[to + N*from]; + cost += distances[to + N * from]; from = to; } - - // debug - // printf("route #%d cost=%d\n",i,cost); - // update best_route -> reduction return route_cost(i, cost); }); -} // find_best_route +} // ============================================================ // ============================================================ //! \param[in] nbRepeat number of repeat (for accurate time measurement) + template void solve_traveling_salesman(int nbRepeat = 1) { - // find best route auto distances_small = init_distance_matrix_small(N); - route_cost best_route; - SimpleTimer timer; - timer.start(); + auto start = high_resolution_clock::now(); for (int i = 0; i(distances_small.data()); - timer.stop(); - double time = timer.elapsedSeconds()/nbRepeat; + auto end = high_resolution_clock::now(); + duration elapsed = (end - start) / nbRepeat; // print best route - printf("Trav Salesman Prob N=%d, best route cost is: %d, average time is %f seconds\n",N, best_route.cost, time); + printf("Trav Salesman Prob N=%d, best route cost is: %d, average time is %f seconds\n",N, best_route.cost, elapsed.count()); printf("Solution route is "); route_iterator rit(best_route.route);