Skip to content

Instantly share code, notes, and snippets.

@flomnes
Created July 25, 2024 08:20
Show Gist options
  • Save flomnes/282329d13b10b2bd8b2c7d0f9faf19a9 to your computer and use it in GitHub Desktop.
Save flomnes/282329d13b10b2bd8b2c7d0f9faf19a9 to your computer and use it in GitHub Desktop.
Legacy vs. New number of cores
#include <string>
#include <map>
#include <cmath>
using uint = unsigned int;
std::map<std::string, uint> getRawNumberCoresPerLevel(uint nbLogicalCores)
{
std::map<std::string, uint> table;
switch (nbLogicalCores)
{
case 1:
table["min"] = 1;
table["low"] = 1;
table["med"] = 1;
table["high"] = 1;
table["max"] = 1;
break;
case 2:
table["min"] = 1;
table["low"] = 1;
table["med"] = 1;
table["high"] = 2;
table["max"] = 2;
break;
case 3:
table["min"] = 1;
table["low"] = 2;
table["med"] = 2;
table["high"] = 2;
table["max"] = 3;
break;
case 4:
table["min"] = 1;
table["low"] = 2;
table["med"] = 2;
table["high"] = 3;
table["max"] = 4;
break;
case 5:
table["min"] = 1;
table["low"] = 2;
table["med"] = 3;
table["high"] = 4;
table["max"] = 5;
break;
case 6:
table["min"] = 1;
table["low"] = 2;
table["med"] = 3;
table["high"] = 4;
table["max"] = 6;
break;
case 7:
table["min"] = 1;
table["low"] = 2;
table["med"] = 3;
table["high"] = 5;
table["max"] = 7;
break;
case 8:
table["min"] = 1;
table["low"] = 2;
table["med"] = 4;
table["high"] = 6;
table["max"] = 8;
break;
case 9:
table["min"] = 1;
table["low"] = 3;
table["med"] = 5;
table["high"] = 7;
table["max"] = 8;
break;
case 10:
table["min"] = 1;
table["low"] = 3;
table["med"] = 5;
table["high"] = 8;
table["max"] = 9;
break;
case 11:
table["min"] = 1;
table["low"] = 3;
table["med"] = 6;
table["high"] = 8;
table["max"] = 10;
break;
case 12:
table["min"] = 1;
table["low"] = 3;
table["med"] = 6;
table["high"] = 9;
table["max"] = 11;
break;
default:
table["min"] = 1;
table["low"] = (uint)std::ceil(nbLogicalCores / 4.);
table["med"] = (uint)std::ceil(nbLogicalCores / 2.);
table["high"] = (uint)std::ceil(3 * nbLogicalCores / 4.);
table["max"] = nbLogicalCores - 1;
break;
}
return table;
}
enum NumberOfCoresMode
{
ncMin = 0,
ncLow,
ncAvg,
ncHigh,
ncMax,
ncUnknown
};
uint getNumberOfCores_Map(NumberOfCoresMode ncMode, const std::map<std::string, uint>& table)
{
// Getting the number of parallel years based on the number of cores level.
switch (ncMode)
{
case ncMin:
return table.at("min");
break;
case ncLow:
return table.at("low");
break;
case ncAvg:
return table.at("med");
break;
case ncHigh:
return table.at("high");
break;
case ncMax:
return table.at("max");
break;
default:
return -1;
}
}
uint getNumberOfCoresLegacy(NumberOfCoresMode ncMode, uint nbLogicalCores)
{
auto table = getRawNumberCoresPerLevel(nbLogicalCores);
return getNumberOfCores_Map(ncMode, table);
}
#include <iostream>
#include <functional>
void print(std::function<uint(NumberOfCoresMode, uint)> get)
{
std::cout << "[nbCores] ncMin ncLow ncAvg ncHigh ncMax" << std::endl;
for (uint nbLogicalCores = 1; nbLogicalCores <= 12; nbLogicalCores++)
{
std::cout << "[" << nbLogicalCores << "] "
<< get(ncMin, nbLogicalCores) << ' '
<< get(ncLow, nbLogicalCores) << ' '
<< get(ncAvg, nbLogicalCores) << ' '
<< get(ncHigh, nbLogicalCores) << ' '
<< get(ncMax, nbLogicalCores) << std::endl;
}
}
uint getNumberOfCoresNew(NumberOfCoresMode ncMode, uint nbLogicalCores)
{
switch(ncMode)
{
case ncMin: return 1;
case ncLow: return (uint)std::ceil(nbLogicalCores / 4.);
case ncAvg: return (uint)std::ceil(nbLogicalCores / 2.);
case ncHigh: return (uint)std::ceil(3 * nbLogicalCores / 4.);
case ncMax: return nbLogicalCores;
default: return -1;
}
}
int main()
{
std::cout << "Existant" << std::endl;
print(getNumberOfCoresLegacy);
std::cout << "Nouveau" << std::endl;
print(getNumberOfCoresNew);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment