Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds -rp parameter and option to run ML model on GPU and physics module on CPU #43

Draft
wants to merge 18 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions examples/app/eos_idealgas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class IdealGas : public EOS<FPType>
{
const FPType gamma_;
const FPType specific_heat_;
const int repeat;

public:
IdealGas(FPType gamma, FPType specific_heat)
: gamma_(gamma), specific_heat_(specific_heat)
IdealGas(FPType gamma, FPType specific_heat, int repeat)
: gamma_(gamma), specific_heat_(specific_heat), repeat(repeat)
{
}

Expand All @@ -39,14 +40,15 @@ class IdealGas : public EOS<FPType>
{
const FPType gamma = gamma_;
const FPType specific_heat = specific_heat_;

using mfem::ForallWrap;
MFEM_FORALL(i, length, {
pressure[i] = (gamma - 1) * density[i] * energy[i];
soundspeed2[i] = gamma * (gamma - 1) * energy[i];
bulkmod[i] = gamma * pressure[i];
temperature[i] = energy[i] / specific_heat;
for ( int r = 0; r < repeat; r++){
using mfem::ForallWrap;
MFEM_FORALL(i, length, {
pressure[i] = (gamma - 1) * density[i] * energy[i];
soundspeed2[i] = gamma * (gamma - 1) * energy[i];
bulkmod[i] = gamma * pressure[i];
temperature[i] = energy[i] / specific_heat;
});
}
}

#ifdef __ENABLE_PERFFLOWASPECT__
Expand Down
43 changes: 31 additions & 12 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ int run(const char *device_name,
const char *model_path,
const char *db_config,
bool lbalance,
int k_nearest)
int k_nearest,
int repeats)
{
// -------------------------------------------------------------------------
// setup
Expand All @@ -175,7 +176,10 @@ int run(const char *device_name,
CALIPER(mgr.start();)
CALIPER(CALI_MARK_BEGIN("Setup");)

const bool use_device = std::strcmp(device_name, "cpu") != 0;
const bool physics_use_device = ((std::strcmp(device_name, "gpu_cpu") == 0) || std::strcmp(device_name, "gpu_gpu") == 0);
const bool ml_use_device = ((std::strcmp(device_name, "gpu_cpu") == 0) || std::strcmp(device_name, "gpu_gpu") == 0);
koparasy marked this conversation as resolved.
Show resolved Hide resolved


AMSDBType dbType = AMSDBType::None;
if (std::strcmp(db_type, "csv") == 0) {
dbType = AMSDBType::CSV;
Expand Down Expand Up @@ -250,7 +254,7 @@ int run(const char *device_name,


mfem::MemoryManager::SetUmpireHostAllocatorName(physics_host_alloc.c_str());
if (use_device) {
if (physics_use_device) {
mfem::MemoryManager::SetUmpireDeviceAllocatorName(
physics_device_alloc.c_str());
}
Expand All @@ -261,7 +265,7 @@ int run(const char *device_name,
if (strcmp(pool, "default") != 0) {
AMSSetAllocator(AMSResourceType::HOST, ams_host_alloc.c_str());

if (use_device) {
if ( physics_use_device || ml_use_device ) {
AMSSetAllocator(AMSResourceType::DEVICE, ams_device_alloc.c_str());
AMSSetAllocator(AMSResourceType::PINNED, ams_pinned_alloc.c_str());
}
Expand All @@ -270,7 +274,13 @@ int run(const char *device_name,
mfem::Device::SetMemoryTypes(mfem::MemoryType::HOST_UMPIRE,
mfem::MemoryType::DEVICE_UMPIRE);

mfem::Device device(device_name);
mfem::Device device;

if ( physics_use_device )
device.Configure("cuda");
else
device.Configure("cpu");

std::cout << std::endl;
device.Print();
std::cout << std::endl;
Expand Down Expand Up @@ -332,8 +342,12 @@ int run(const char *device_name,

db_path = (strlen(db_config) > 0) ? db_config : nullptr;

AMSResourceType ams_device = AMSResourceType::HOST;
if (use_device) ams_device = AMSResourceType::DEVICE;
AMSResourceType ams_physics_device = AMSResourceType::HOST;
if (physics_use_device) ams_physics_device = AMSResourceType::DEVICE;

AMSResourceType ams_ml_device = AMSResourceType::HOST;
if (ml_use_device) ams_ml_device = AMSResourceType::DEVICE;

AMSExecPolicy ams_loadBalance = AMSExecPolicy::UBALANCED;
if (lbalance) ams_loadBalance = AMSExecPolicy::BALANCED;
#else
Expand All @@ -347,7 +361,7 @@ int run(const char *device_name,
for (int mat_idx = 0; mat_idx < num_mats; ++mat_idx) {
EOS<TypeValue> *base;
if (eos_name == std::string("ideal_gas")) {
base = new IdealGas<TypeValue>(1.6, 1.4);
base = new IdealGas<TypeValue>(1.6, 1.4, repeats);
} else if (eos_name == std::string("constant_host")) {
base = new ConstantEOSOnHost<TypeValue>(physics_host_alloc.c_str(), 1.0);
} else {
Expand Down Expand Up @@ -605,7 +619,7 @@ int main(int argc, char **argv)
std::cout.setstate(std::ios::failbit);
}

const char *device_name = "cpu";
const char *device_name = "cpu_cpu";
const char *eos_name = "ideal_gas";
const char *model_path = "";
const char *hdcache_path = "";
Expand Down Expand Up @@ -634,6 +648,7 @@ int main(int argc, char **argv)
double avg = 0.5;
double stdDev = 0.2;
bool reqDB = false;
int repeats = 1;
const char *pool = "default";

#ifdef __ENABLE_DB__
Expand All @@ -646,7 +661,7 @@ int main(int argc, char **argv)
// setup command line parser
// -------------------------------------------------------------------------
mfem::OptionsParser args(argc, argv);
args.AddOption(&device_name, "-d", "--device", "Device config string");
args.AddOption(&device_name, "-d", "--device", "device(physics)_device(model), for example: cpu_gpu means execute physics on the cpu and the model on gpu");

// set precision
args.AddOption(&precision_opt,
Expand Down Expand Up @@ -752,6 +767,8 @@ int main(int argc, char **argv)
args.AddOption(
&verbose, "-v", "--verbose", "-qu", "--quiet", "Print extra stuff");

args.AddOption(&repeats, "-rp", "--repeats", "Number of repeats to perform on physics eval");

args.AddOption(&pool,
"-ptype",
"--pool-type",
Expand Down Expand Up @@ -844,7 +861,8 @@ int main(int argc, char **argv)
model_path,
db_config,
lbalance,
k_nearest);
k_nearest,
repeats);
else if (precision == AMSDType::Double)
ret = run<double>(device_name,
db_type,
Expand All @@ -870,7 +888,8 @@ int main(int argc, char **argv)
model_path,
db_config,
lbalance,
k_nearest);
k_nearest,
repeats);
else {
std::cerr << "Invalid precision " << precision_opt << "\n";
return -1;
Expand Down
6 changes: 4 additions & 2 deletions src/AMSlib/AMS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ AMSExecutor AMSCreateExecutor(const AMSConfig config)
config.SPath,
config.DBPath,
config.dbType,
config.device,
config.physics_device,
config.ml_device,
config.threshold,
config.uqPolicy,
config.nClusters,
Expand All @@ -105,7 +106,8 @@ AMSExecutor AMSCreateExecutor(const AMSConfig config)
config.SPath,
config.DBPath,
config.dbType,
config.device,
config.physics_device,
koparasy marked this conversation as resolved.
Show resolved Hide resolved
config.ml_device,
static_cast<float>(config.threshold),
config.uqPolicy,
config.nClusters,
Expand Down
3 changes: 2 additions & 1 deletion src/AMSlib/include/AMS.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ enum struct AMSUQPolicy {
typedef struct ams_conf {
const AMSExecPolicy ePolicy;
const AMSDType dType;
const AMSResourceType device;
const AMSResourceType physics_device;
const AMSResourceType ml_device;
const AMSDBType dbType;
AMSPhysicFn cBack;
char *SPath;
Expand Down
4 changes: 2 additions & 2 deletions src/AMSlib/ml/surrogate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class SurrogateModel
inline void _load(const std::string& model_path,
const std::string& device_name)
{
DBG(Surrogate, "Using model at double precision");
DBG(Surrogate, "Using model(%s) at double precision at device %s", model_path.c_str(), device_name.c_str());
_load_torch(model_path, torch::Device(device_name), torch::kFloat64);
}

Expand All @@ -166,7 +166,7 @@ class SurrogateModel
inline void _load(const std::string& model_path,
const std::string& device_name)
{
DBG(Surrogate, "Using model at single precision");
DBG(Surrogate, "Using model (%s) at single precision at device %s", model_path.c_str(), device_name.c_str())
_load_torch(model_path, torch::Device(device_name), torch::kFloat32);
}

Expand Down
Loading