Skip to content

Commit

Permalink
make kernels depend each other, use original variables and access order
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetyusufoglu committed Nov 11, 2024
1 parent 8fefd70 commit 07496fa
Show file tree
Hide file tree
Showing 2 changed files with 361 additions and 145 deletions.
117 changes: 91 additions & 26 deletions benchmarks/babelstream/src/babelStreamCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,105 @@ namespace
[[maybe_unused]] constexpr auto minArrSize = 1024 * 128;

// Scalar value for Mul and Triad kernel parameters.
[[maybe_unused]] constexpr auto scalarVal = 2.0f;
[[maybe_unused]] constexpr double scalarVal = 0.4;

// Block thread extent for DotKernel test work division parameters.
[[maybe_unused]] constexpr auto blockThreadExtentMain = 1024;
[[maybe_unused]] constexpr auto dotGridBlockExtent = 256;

// Number of runs for each kernel, can be changed by command line arguments.
// At least 100 runs are recommended for good benchmarking.
// To prevent timeouts in CI, a small value is used.
[[maybe_unused]] auto numberOfRuns = 2;

// Data input value for babelstream.
[[maybe_unused]] constexpr auto valA = 1.0f;
// Data input values for babelstream.
[[maybe_unused]] constexpr double valA = 0.1;
[[maybe_unused]] constexpr double valB = 0.2;
// Change this if triad kernel is going to be run alone
[[maybe_unused]] constexpr double valC = 0.0;

//! Values corresponding to the command line argument run-kernels
enum class KernelsToRun
{
All, // init, add, copy, mul, triad
Triad, // only init and triad
NStream // only init and nstream
};

[[maybe_unused]] KernelsToRun kernelsToBeExecuted{KernelsToRun::All};

//! handleCustomArguments Gets custom cmd line arguments from the all arguments.
//! Namely gets --array-size=1234 and --number-runs=1234 and keeps the others which are
//! command line args for Catch2 session.
[[maybe_unused]] static void handleCustomArguments(int& argc, char* argv[])
{
std::vector<char*> newArgv;
newArgv.push_back(argv[0]); // Keep the program name
std::vector<char*> newArgv({argv[0]}); // keep program name

for(int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if(arg.rfind("--array-size=", 0) == 0)
{
auto const arrSize = std::stoi(arg.substr(13)); // Convert to integer
if(arrSize > minArrSize)
try
{
arraySizeMain = arrSize;
std::cout << "Array size provided(items): " << arraySizeMain << std::endl;
// Convert argument to integer
auto arrSize = std::stoi(arg.substr(13));
if(arrSize > minArrSize)
{
arraySizeMain = arrSize;
std::cout << "Array size set to: " << arraySizeMain << std::endl;
}
else
{
std::cout << "Array size too small. Must be at least " << minArrSize
<< ", using default: " << arraySizeMain << std::endl;
}
}
else
catch(std::invalid_argument const&)
{
std::cout << "Too small array size given. Must be at least " << minArrSize << std::endl;
std::cout << "Using default array size(number of items): " << arraySizeMain << std::endl;
std::cerr << "Invalid array size argument: " << arg << ". Default value used." << std::endl;
}
}
else if(arg.rfind("--number-runs=", 0) == 0)
{
auto const numRuns = std::stoi(arg.substr(14)); // Convert to integer
if(numRuns > 0)
try
{
numberOfRuns = numRuns;
std::cout << "Number of runs provided: " << numberOfRuns << std::endl;
// Convert argument to integer
auto const numRuns = std::stoi(arg.substr(14));
if(numRuns > 0)
{
numberOfRuns = numRuns;
std::cout << "Number of runs provided: " << numberOfRuns << std::endl;
}
else
{
std::cout << "Using default number of runs: " << numberOfRuns << std::endl;
}
}
else
catch(std::invalid_argument const&)
{
std::cout << "Using default number of runs: " << numberOfRuns << std::endl;
std::cerr << "Invalid number of runs argument: " << arg << " . Default value used." << std::endl;
}
}
else if(arg.rfind("--run-kernels=", 0) == 0)
{
// Get argument to determine which kernels will be run
auto const kernelsString = arg.substr(14);
if(kernelsString == "nstream")
{
std::cout << "Only nstream kernel will be executed." << std::endl;
kernelsToBeExecuted = KernelsToRun::NStream;
}
else if(kernelsString == "triad")
{
kernelsToBeExecuted = KernelsToRun::Triad;
std::cout << "Only triad kernel will be executed." << std::endl;
}
else if(kernelsString == "all")
{
// The variable kernelsToBeExecuted default value is "all";
kernelsToBeExecuted = KernelsToRun::All;
std::cout << "All 5 babelstream kernels are going to be executed." << std::endl;
}
}
else
Expand All @@ -87,8 +137,13 @@ namespace
if(arg.rfind("-?", 0) == 0 || arg.rfind("--help", 0) == 0 || arg.rfind("-h", 0) == 0)
{
std::cout << "Usage of custom arguments (arguments which are not Catch2): --array-size=33554432 and "
"--number-runs=100"
"--number-runs=100\n"
<< std::endl;
std::cout
<< "If you want to run only nstream kernel or triad kernel use --run-kernels=nstream or "
"--run-kernels=triad. Otherwise all 5 standard kernels will be executed. Init, Copy, Mul, Add, "
"Triad. (and Dot kernel, if multi-threaded acc is used.)"
<< std::endl;
}
}

Expand All @@ -98,6 +153,12 @@ namespace
{
argv[i] = newArgv[static_cast<size_t>(i)];
}

// Array size must a multiple of
if(arraySizeMain % blockThreadExtentMain != 0)
throw std::runtime_error(
"Array size is " + std::to_string(arraySizeMain) + ". It must be a multiple of block-size "
+ std::to_string(blockThreadExtentMain));
}

//! FuzzyEqual compares two floating-point or integral type values.
Expand All @@ -111,7 +172,7 @@ namespace
{
if constexpr(std::is_floating_point_v<T>)
{
return std::fabs(a - b) < std::numeric_limits<T>::epsilon() * static_cast<T>(100.0);
return std::fabs(a - b) < (std::numeric_limits<T>::epsilon() * static_cast<T>(100.0));
}
else if constexpr(std::is_integral_v<T>)
{
Expand Down Expand Up @@ -219,6 +280,7 @@ namespace
WorkDivTriad,
WorkDivMult,
WorkDivDot,
WorkDivNStream,
DeviceName,
TimeUnit,
KernelNames,
Expand Down Expand Up @@ -279,6 +341,8 @@ namespace
return "WorkDivMult ";
case BMInfoDataType::WorkDivDot:
return "WorkDivDot ";
case BMInfoDataType::WorkDivNStream:
return "WorkDivNStream";
default:
return "";
}
Expand Down Expand Up @@ -353,11 +417,13 @@ namespace
{
std::stringstream ss;
// define lambda to add values to a string stream created already
auto addItemValue = [&, this](BMInfoDataType item) {
ss << "\n" << typeToTypeStr(item) << ":" << metaDataMap.at(item);
auto addItemValue = [&, this](BMInfoDataType item)
{
if(metaDataMap.count(item) != 0)
ss << "\n" << typeToTypeStr(item) << ":" << metaDataMap.at(item);
};

// Initially chose some data to serialize
// Initially chose some data to serialize from the meta-data map to add to string stream
ss << "\n";
addItemValue(BMInfoDataType::AcceleratorType);
addItemValue(BMInfoDataType::NumRuns);
Expand All @@ -369,9 +435,8 @@ namespace
addItemValue(BMInfoDataType::WorkDivMult);
addItemValue(BMInfoDataType::WorkDivAdd);
addItemValue(BMInfoDataType::WorkDivTriad);
if(metaDataMap.count(BMInfoDataType::WorkDivDot) != 0)
addItemValue(BMInfoDataType::WorkDivDot);

addItemValue(BMInfoDataType::WorkDivDot);
addItemValue(BMInfoDataType::WorkDivNStream);
auto getItemFromStrList = [this](BMInfoDataType item, int index) -> std::string
{
std::string const str = metaDataMap.at(item);
Expand Down
Loading

0 comments on commit 07496fa

Please sign in to comment.