Skip to content

Commit

Permalink
benchmarking distribution #6 Adds real uniform distribution benchmark…
Browse files Browse the repository at this point in the history
…s, removes error when plotting
  • Loading branch information
EarltShirt committed Jun 23, 2023
1 parent e1de326 commit e89327f
Show file tree
Hide file tree
Showing 18 changed files with 796 additions and 42 deletions.
2 changes: 1 addition & 1 deletion benchmark/distributions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
cmake_minimum_required(VERSION 3.10) # Adjust this to your actual CMake version

# Add an executable target called 'uniform' to be built from 'uniform.cpp'
add_executable(uniform uniform.cpp)
add_executable(uniform uniform_int.cpp uniform_real.cpp)

# Include directories for the 'uniform' target
target_include_directories(uniform PRIVATE
Expand Down
3 changes: 2 additions & 1 deletion benchmark/distributions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ cd distributions
chmod +x runbenchmark.sh
./runbenchmark.sh
```
And then either type `uniform_int` or `uniform_real` when asked to choose the distribution to benchmark.

After running this, the benchmarks can be found in the benchmark/distributions directory next to the scripts. The benchmarks are composed of two files, comparison.csv containing the results of the benchmarks and comparison.png containing the plots of the benchmarks in ascending order of execution time.
After running this, the benchmarks can be found in the benchmark/distributions directory next to the scripts. The benchmarks are composed of two files, `uniform_int.csv` (or `uniform_real.csv`) containing the results of the benchmarks and `uniform_int.png` (or `uniform_real.png`) containing the plots of the benchmarks in ascending order of execution time.


After each execution, the results are saved in these two files, but if you want to rerun the benchmark, you will have to respond 'Y' to the question 'Do you want to erase the previous results ?' in the script.
Expand Down
1 change: 0 additions & 1 deletion benchmark/distributions/comparison.csv

This file was deleted.

16 changes: 13 additions & 3 deletions benchmark/distributions/plotresults.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import argparse
import pandas as pd
import matplotlib.pyplot as plt
import os

# Parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument('csv_file', help='Name of the CSV file')
args = parser.parse_args()

# Read the CSV file
data = pd.read_csv(args.csv_file)

file_name = os.path.splitext(args.csv_file)[0]

# read the csv file
data = pd.read_csv('/home/u4/csmi/2022/devora/solar-shading/benchmark/distributions/comparison.csv')

# plot the method
plt.figure(figsize=(20,12))
Expand Down Expand Up @@ -42,5 +52,5 @@
plt.xticks(rotation=45, ha='right', fontsize=8)

plt.tight_layout()
plt.savefig('comparison.png')
plt.savefig(file_name + '.png')
plt.show()
20 changes: 14 additions & 6 deletions benchmark/distributions/runbenchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,39 @@

set -e

# Ask for the .cpp file name
echo "Enter the name of the .cpp file (without the .cpp extension):"
read cpp_file_name

# Compiler flags array
declare -a flagsArr=("-O1" "-O2" "-O3" "-Ofast" "-O1 -march=native" "-O2 -march=native" "-O3 -march=native" "-Ofast -march=native" "-O1 -march=native -funroll-loops" "-O2 -march=native -funroll-loops" "-O3 -march=native -funroll-loops" "-Ofast -march=native -funroll-loops" "-O1 -march=native -fdisable-tree-cunrolli" "-O2 -march=native -fdisable-tree-cunrolli" "-O3 -march=native -fdisable-tree-cunrolli" "-Ofast -march=native -fdisable-tree-cunrolli")

# Loop through the array of flags
for flag in "${flagsArr[@]}"
do
# Build the C++ code with the current flag
g++ -Wno-enum-compare $flag -o uniform uniform.cpp # -I/home/u4/csmi/2022/devora/intel/oneapi/mkl/2023.1.0/include -L/home/u4/csmi/2022/devora/intel/oneapi/mkl/2023.1.0/lib/intel64 -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl
g++ -Wno-enum-compare $flag -o $cpp_file_name $cpp_file_name.cpp

# Execute the C++ code with the current flag as a command-line argument
./uniform $flag
./$cpp_file_name $flag

# Remove the executable
rm uniform
rm $cpp_file_name
done

# Execute the Python script for plotting
python3 plotresults.py
# Determine the CSV file name based on the CPP file name
csv_file_name="${cpp_file_name}.csv"

# Execute the Python script for plotting, passing the CSV file name
python3 plotresults.py "$csv_file_name"


# Ask user for confirmation to delete all lines except the first one from the CSV file
read -p "Do you want to delete all lines except the first one from the CSV file? [Y/n] " answer

case $answer in
[Yy]* )
sed -i '2,$d' comparison.csv
sed -i '2,$d' $csv_file_name
echo "All lines except the first one from the CSV file have been deleted."
;;
* )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) {


// Open the CSV file for writing
std::ofstream outputFile("comparison.csv", std::ofstream::app);
std::ofstream outputFile("uniform_int.csv", std::ofstream::app);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Parameters Initialization ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions benchmark/distributions/uniform_int.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flag,Method,Duration
Binary file added benchmark/distributions/uniform_int.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions benchmark/distributions/uniform_real.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <fstream>
#include "../extlibs/EigenRand/EigenRand/EigenRand"
#include "../extlibs/Xoshiro-cpp/XoshiroCpp.hpp"
#include "../extlibs/pcg-cpp/include/pcg_random.hpp"
#include "../extlibs/eigen/Eigen/Dense"
#include "../extlibs/eigen/Eigen/Core"

using namespace XoshiroCpp;

int main(int argc, char *argv[]) {
std::string flag;
std::stringstream ss;

if (argc > 1) {
for(int i = 1; i < argc; ++i) {
if(i != 1) {
ss << " ";
}
ss << argv[i];
}

flag = ss.str();
}
else {
flag = "default";
}

std::ofstream outputFile("uniform_real.csv", std::ofstream::app);

const int numVectors = 100000000;

std::vector<double> stdVector(numVectors);
std::vector<double> mersenneVector(numVectors);
Eigen::VectorXd EigenVector(numVectors);
std::vector<double> pcgCppVector(numVectors);
std::vector<double> xoshiroCppVector(numVectors);
Eigen::VectorXd EigenVector2(numVectors);

auto startEigen = std::chrono::steady_clock::now();
Eigen::VectorXd eigenVector = Eigen::VectorXd::Random(numVectors).array() / 2.0 + 0.5; // Remap the Eigen random from [-1,1] to [0,1]
auto endEigen = std::chrono::steady_clock::now();
auto durationEigen = std::chrono::duration_cast<std::chrono::milliseconds>(endEigen - startEigen);
outputFile<< "\n" << flag << ",Eigen::VectorXd::Random,"<< durationEigen.count();

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dist(0.0, 1.0);

auto startStdUniform = std::chrono::steady_clock::now();
for (int i = 0; i < numVectors; ++i) {
stdVector[i] = dist(gen);
}
auto endStdUniform = std::chrono::steady_clock::now();
auto durationStdUniform = std::chrono::duration_cast<std::chrono::milliseconds>(endStdUniform - startStdUniform);
outputFile<< "\n" << flag << ",std::uniform_real_distribution," << durationStdUniform.count();

std::mt19937 mersenne(gen());
std::uniform_real_distribution<double> mersenneDist(0.0, 1.0);

auto startMersenne = std::chrono::steady_clock::now();
for (int i = 0; i < numVectors; ++i) {
mersenneVector[i] = mersenneDist(mersenne);
}
auto endMersenne = std::chrono::steady_clock::now();
auto durationMersenne = std::chrono::duration_cast<std::chrono::milliseconds>(endMersenne - startMersenne);
outputFile<< "\n" << flag << ",Mersenne Twister," << durationMersenne.count();

Eigen::Rand::Vmt19937_64 urng{ 42 };
auto startEigenRand = std::chrono::steady_clock::now();
EigenVector = Eigen::Rand::uniformRealLike(EigenVector, urng);
auto endEigenRand = std::chrono::steady_clock::now();
auto durationEigenRand = std::chrono::duration_cast<std::chrono::milliseconds>(endEigenRand - startEigenRand);
outputFile << "\n" << flag << ",EigenRand,"<< durationEigenRand.count();

pcg64_fast rng;
rng.seed(42);
std::uniform_real_distribution<double> pcgDist(0.0, 1.0);

auto startPcgCpp = std::chrono::steady_clock::now();
for (int i = 0; i < numVectors; ++i) {
pcgCppVector[i] = pcgDist(rng);
}
auto endPcgCpp = std::chrono::steady_clock::now();
auto durationPcgCpp = std::chrono::duration_cast<std::chrono::milliseconds>(endPcgCpp - startPcgCpp);
outputFile << "\n" << flag << ",pcg-cpp," << durationPcgCpp.count();

const std::uint64_t seed = 12345;
Xoshiro256PlusPlus rg(seed);
std::uniform_real_distribution<double> DIST(0.0,1.0);

auto startXoshiroCpp = std::chrono::steady_clock::now();
for (int i = 0; i < numVectors; i++) {
xoshiroCppVector[i] = DIST(rg);
}
auto endXoshiroCpp = std::chrono::steady_clock::now();
auto durationXoshiroCpp = std::chrono::duration_cast<std::chrono::milliseconds>(endXoshiroCpp - startXoshiroCpp);
outputFile << "\n" << flag << ",XoshiroCpp,"<< durationXoshiroCpp.count();
outputFile.close();

return 0;
}
1 change: 1 addition & 0 deletions benchmark/distributions/uniform_real.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flag,Method,Duration
Binary file added benchmark/distributions/uniform_real.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 25 additions & 1 deletion results/csv/results.csv
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
RNG TYPE,EXECUTION TIME
METHOD,TIME
Shading mask STD RNG -Ofast -march=native,9.99015
Shading mask PCG RNG -Ofast -march=native,8.34877
Shading mask XOSHIRO RNG -Ofast -march=native,7.88745
Shading mask MIX RNG -Ofast -march=native,9.741
Shading mask STD RNG -Ofast -march=native,9.88132
Shading mask PCG RNG -Ofast -march=native,9.80248
Shading mask XOSHIRO RNG -Ofast -march=native,9.92391
Shading mask MIX RNG -Ofast -march=native,10.1443
Shading mask STD RNG -Ofast -march=native,10.9991
Shading mask PCG RNG -Ofast -march=native,9.99749
Shading mask XOSHIRO RNG -Ofast -march=native,9.81854
Shading mask MIX RNG -Ofast -march=native,9.63068
Shading mask STD RNG -Ofast -march=native,11.0136
Shading mask PCG RNG -Ofast -march=native,10.4724
Shading mask XOSHIRO RNG -Ofast -march=native,10.855
Shading mask MIX RNG -Ofast -march=native,10.9804
Shading mask STD RNG -Ofast -march=native -funroll-loops -funsafe-math-optimizations,11.7976
Shading mask PCG RNG -Ofast -march=native -funroll-loops -funsafe-math-optimizations,10.2469
Shading mask XOSHIRO RNG -Ofast -march=native -funroll-loops -funsafe-math-optimizations,9.38577
Shading mask MIX RNG -Ofast -march=native -funroll-loops -funsafe-math-optimizations,10.0054
Shading mask STD RNG -O3 -march=native,10.7028
Shading mask PCG RNG -O3 -march=native,10.8316
Shading mask XOSHIRO RNG -O3 -march=native,10.3154
Shading mask MIX RNG -O3 -march=native,11.3336
Loading

0 comments on commit e89327f

Please sign in to comment.