Skip to content

Commit

Permalink
Move CardSampler to separate files and use it in evaluation tests (#91
Browse files Browse the repository at this point in the history
)

* Refactor card_sampler include paths in benchmark files

* Refactor to use `card_sampler::CardSampler` in tests

* Refactor card_sampler implementation to separate header and source files
  • Loading branch information
azriel1rf committed May 2, 2024
1 parent 44213bf commit a86c76b
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 322 deletions.
8 changes: 8 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option(BUILD_TESTS "Build test ON/OFF" ON)
option(BUILD_EXAMPLES "Build examples ON/OFF" ON)

add_library(pheval STATIC
src/card_sampler.cc
src/dptables.c
src/evaluator5.cc
src/evaluator5.c
Expand All @@ -41,6 +42,7 @@ target_include_directories(pheval PUBLIC
target_compile_options(pheval PUBLIC -O3)
set(PUB_HEADERS include/phevaluator/phevaluator.h
include/phevaluator/card.h
include/phevaluator/card_sampler.h
include/phevaluator/rank.h)
set_target_properties(pheval PROPERTIES
VERSION ${PROJECT_VERSION}
Expand Down Expand Up @@ -114,6 +116,7 @@ endif()

if (BUILD_PLO4)
add_library(phevalplo4 STATIC
src/card_sampler.cc
src/dptables.c
src/evaluator_plo4.c
src/evaluator_plo4.cc
Expand All @@ -130,6 +133,7 @@ if (BUILD_PLO4)
target_compile_options(phevalplo4 PUBLIC -O3)
set(PUB_HEADERS include/phevaluator/phevaluator.h
include/phevaluator/card.h
include/phevaluator/card_sampler.h
include/phevaluator/rank.h)
set_target_properties(phevalplo4 PROPERTIES
VERSION ${PROJECT_VERSION}
Expand All @@ -148,6 +152,7 @@ if (BUILD_PLO5)
)

add_library(phevalplo5 STATIC
src/card_sampler.cc
src/dptables.c
src/evaluator_plo5.c
src/evaluator_plo5.cc
Expand All @@ -164,6 +169,7 @@ if (BUILD_PLO5)
target_compile_options(phevalplo5 PUBLIC -O3)
set(PUB_HEADERS include/phevaluator/phevaluator.h
include/phevaluator/card.h
include/phevaluator/card_sampler.h
include/phevaluator/rank.h)
set_target_properties(phevalplo5 PROPERTIES
VERSION ${PROJECT_VERSION}
Expand All @@ -181,6 +187,7 @@ if (BUILD_PLO6)
)

add_library(phevalplo6 STATIC
src/card_sampler.cc
src/dptables.c
src/evaluator_plo6.c
src/evaluator_plo6.cc
Expand All @@ -197,6 +204,7 @@ if (BUILD_PLO6)
target_compile_options(phevalplo6 PUBLIC -O3)
set(PUB_HEADERS include/phevaluator/phevaluator.h
include/phevaluator/card.h
include/phevaluator/card_sampler.h
include/phevaluator/rank.h)
set_target_properties(phevalplo6 PROPERTIES
VERSION ${PROJECT_VERSION}
Expand Down
32 changes: 10 additions & 22 deletions cpp/benchmark/benchmark.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "phevaluator/phevaluator.h"
#include "benchmark/benchmark.h"
#include "card_sampler.h"
#include "phevaluator/card_sampler.h"
#include "phevaluator/phevaluator.h"

using namespace phevaluator;

Expand Down Expand Up @@ -65,16 +65,13 @@ const int SIZE = 100;

static void EvaluateRandomFiveCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};
card_sampler::CardSampler cs{};
for (int i = 0; i < SIZE; i++) {
hands.push_back(cs.sample(5));
}
for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
EvaluateCards(hands[i][0],
hands[i][1],
hands[i][2],
hands[i][3],
EvaluateCards(hands[i][0], hands[i][1], hands[i][2], hands[i][3],
hands[i][4]);
}
}
Expand All @@ -83,42 +80,33 @@ BENCHMARK(EvaluateRandomFiveCards);

static void EvaluateRandomSixCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};
card_sampler::CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(cs.sample(6));
}

for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
EvaluateCards(hands[i][0],
hands[i][1],
hands[i][2],
hands[i][3],
hands[i][4],
hands[i][5]);
EvaluateCards(hands[i][0], hands[i][1], hands[i][2], hands[i][3],
hands[i][4], hands[i][5]);
}
}
}
BENCHMARK(EvaluateRandomSixCards);

static void EvaluateRandomSevenCards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};
card_sampler::CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(cs.sample(7));
}

for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
EvaluateCards(hands[i][0],
hands[i][1],
hands[i][2],
hands[i][3],
hands[i][4],
hands[i][5],
hands[i][6]);
EvaluateCards(hands[i][0], hands[i][1], hands[i][2], hands[i][3],
hands[i][4], hands[i][5], hands[i][6]);
}
}
}
Expand Down
16 changes: 5 additions & 11 deletions cpp/benchmark/benchmark_plo4.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
#include "phevaluator/phevaluator.h"
#include "benchmark/benchmark.h"
#include "card_sampler.h"
#include "phevaluator/card_sampler.h"
#include "phevaluator/phevaluator.h"

using namespace phevaluator;

const int SIZE = 100;

static void EvaluateRandomPlo4Cards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};
card_sampler::CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(cs.sample(9));
}

for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
EvaluatePlo4Cards(hands[i][0],
hands[i][1],
hands[i][2],
hands[i][3],
hands[i][4],
hands[i][5],
hands[i][6],
hands[i][7],
EvaluatePlo4Cards(hands[i][0], hands[i][1], hands[i][2], hands[i][3],
hands[i][4], hands[i][5], hands[i][6], hands[i][7],
hands[i][8]);
}
}
Expand Down
20 changes: 6 additions & 14 deletions cpp/benchmark/benchmark_plo5.cc
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
#include "phevaluator/phevaluator.h"
#include "benchmark/benchmark.h"
#include "card_sampler.h"
#include "phevaluator/card_sampler.h"
#include "phevaluator/phevaluator.h"

using namespace phevaluator;


const int SIZE = 100;

static void EvaluateRandomPlo5Cards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};
card_sampler::CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(cs.sample(10));
}

for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
EvaluatePlo5Cards(hands[i][0],
hands[i][1],
hands[i][2],
hands[i][3],
hands[i][4],
hands[i][5],
hands[i][6],
hands[i][7],
hands[i][8],
hands[i][9]);
EvaluatePlo5Cards(hands[i][0], hands[i][1], hands[i][2], hands[i][3],
hands[i][4], hands[i][5], hands[i][6], hands[i][7],
hands[i][8], hands[i][9]);
}
}
}
Expand Down
20 changes: 6 additions & 14 deletions cpp/benchmark/benchmark_plo6.cc
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
#include "phevaluator/phevaluator.h"
#include "benchmark/benchmark.h"
#include "card_sampler.h"
#include "phevaluator/card_sampler.h"
#include "phevaluator/phevaluator.h"

using namespace phevaluator;

const int SIZE = 100;

static void EvaluateRandomPlo6Cards(benchmark::State& state) {
std::vector<std::vector<int>> hands;
CardSampler cs{};
card_sampler::CardSampler cs{};

for (int i = 0; i < SIZE; i++) {
hands.push_back(cs.sample(11));
}

for (auto _ : state) {
for (int i = 0; i < SIZE; i++) {
EvaluatePlo6Cards(hands[i][0],
hands[i][1],
hands[i][2],
hands[i][3],
hands[i][4],
hands[i][5],
hands[i][6],
hands[i][7],
hands[i][8],
hands[i][9],
hands[i][10]);
EvaluatePlo6Cards(hands[i][0], hands[i][1], hands[i][2], hands[i][3],
hands[i][4], hands[i][5], hands[i][6], hands[i][7],
hands[i][8], hands[i][9], hands[i][10]);
}
}
}
Expand Down
30 changes: 0 additions & 30 deletions cpp/benchmark/card_sampler.h

This file was deleted.

21 changes: 21 additions & 0 deletions cpp/include/phevaluator/card_sampler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <array>
#include <vector>

#ifdef __cplusplus
extern "C" {
#endif

namespace card_sampler {
class CardSampler {
std::array<int, 52> deck;

public:
CardSampler(void);
std::vector<int> sample(int size);
};
} // namespace card_sampler

#ifdef __cplusplus
} // closing brace for extern "C"
#endif
30 changes: 30 additions & 0 deletions cpp/src/card_sampler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <phevaluator/card_sampler.h>
#include <array>
#include <chrono>
#include <numeric>
#include <random>
#include <utility>
#include <vector>

namespace card_sampler {
static unsigned seed =
std::chrono::system_clock::now().time_since_epoch().count();
static std::default_random_engine generator(seed);

CardSampler::CardSampler(void) {
std::iota(deck.begin(), deck.end(), 0);
}

std::vector<int> CardSampler::sample(int size) {
std::vector<int> ret;
int residual_cards = deck.size();
for (int i = 0; i < size; i++) {
int target_index = generator() % residual_cards;
int tail_index = residual_cards - 1;
std::swap(deck[target_index], deck[tail_index]);
ret.push_back(deck[tail_index]);
residual_cards--;
}
return ret;
}
} // namespace card_sampler
Loading

0 comments on commit a86c76b

Please sign in to comment.