29 occa::device occaDevice;
43 "occa-cuda",
"raja-cuda",
"cuda",
"occa-omp",
"raja-omp",
"omp",
44 "occa-cpu",
"raja-cpu",
"cpu"
51 Device Device::device_singleton;
61 std::map<std::string, Backend::Id> bmap;
64 bmap[internal::backend_name[i]] = internal::backend_list[i];
66 std::string::size_type beg = 0, end;
69 end = device.find(
',', beg);
70 end = (end != std::string::npos) ? end : device.size();
71 const std::string bname = device.substr(beg, end - beg);
72 std::map<std::string, Backend::Id>::iterator it = bmap.find(bname);
73 MFEM_VERIFY(it != bmap.end(),
"invalid backend name: '" << bname <<
'\'');
74 Get().MarkBackend(it->second);
75 if (end == device.size()) {
break; }
92 std::memcpy(
this, &Get(),
sizeof(
Device));
100 out <<
"Device configuration: ";
101 bool add_comma =
false;
104 if (backends & internal::backend_list[i])
106 if (add_comma) { out <<
','; }
108 out << internal::backend_name[i];
114 void Device::UpdateMemoryTypeAndClass()
128 void Device::Enable()
132 Get().mode = Device::ACCELERATED;
133 Get().UpdateMemoryTypeAndClass();
138 static void DeviceSetup(
const int dev,
int &ngpu)
140 MFEM_CUDA_CHECK(cudaGetDeviceCount(&ngpu));
141 MFEM_VERIFY(ngpu > 0,
"No CUDA device found!");
142 MFEM_CUDA_CHECK(cudaSetDevice(dev));
146 static void CudaDeviceSetup(
const int dev,
int &ngpu)
149 DeviceSetup(dev, ngpu);
153 static void RajaDeviceSetup(
const int dev,
int &ngpu)
156 if (ngpu <= 0) { DeviceSetup(dev, ngpu); }
160 static void OccaDeviceSetup(
const int dev)
166 if (cpu + omp + cuda > 1)
168 MFEM_ABORT(
"Only one OCCA backend can be configured at a time!");
172 #if OCCA_CUDA_ENABLED
173 std::string mode(
"mode: 'CUDA', device_id : ");
174 internal::occaDevice.setup(mode.append(1,
'0'+dev));
176 MFEM_ABORT(
"the OCCA CUDA backend requires OCCA built with CUDA!");
181 #if OCCA_OPENMP_ENABLED
182 internal::occaDevice.setup(
"mode: 'OpenMP'");
184 MFEM_ABORT(
"the OCCA OpenMP backend requires OCCA built with OpenMP!");
189 internal::occaDevice.setup(
"mode: 'Serial'");
193 if (occa::io::exists(MFEM_INSTALL_DIR
"/include/mfem/"))
195 mfemDir = MFEM_INSTALL_DIR
"/include/mfem/";
197 else if (occa::io::exists(MFEM_SOURCE_DIR))
199 mfemDir = MFEM_SOURCE_DIR;
203 MFEM_ABORT(
"Cannot find OCCA kernels in MFEM_INSTALL_DIR or MFEM_SOURCE_DIR");
206 occa::io::addLibraryPath(
"mfem", mfemDir);
207 occa::loadKernels(
"mfem");
209 MFEM_ABORT(
"the OCCA backends require MFEM built with MFEM_USE_OCCA=YES");
213 void Device::Setup(
const int device)
215 MFEM_VERIFY(ngpu == -1,
"the mfem::Device is already configured!");
220 #ifndef MFEM_USE_CUDA
222 "the CUDA backends require MFEM built with MFEM_USE_CUDA=YES");
224 #ifndef MFEM_USE_RAJA
226 "the RAJA backends require MFEM built with MFEM_USE_RAJA=YES");
228 #ifndef MFEM_USE_OPENMP
230 "the OpenMP and RAJA OpenMP backends require MFEM built with"
231 " MFEM_USE_OPENMP=YES");
[host] OpenMP backend. Enabled when MFEM_USE_OPENMP = YES.
Memory types: { CUDA, CUDA_UVM }.
void Print(std::ostream &out=mfem::out)
Print the configuration of the MFEM virtual device object.
void Configure(const std::string &device, const int dev=0)
Configure the Device backends.
[device] OCCA CUDA backend. Enabled when MFEM_USE_OCCA = YES and MFEM_USE_CUDA = YES.
[host] RAJA CPU backend: sequential execution on each MPI rank. Enabled when MFEM_USE_RAJA = YES...
[host] RAJA OpenMP backend. Enabled when MFEM_USE_RAJA = YES and MFEM_USE_OPENMP = YES...
Number of backends: from (1 << 0) to (1 << (NUM_BACKENDS-1)).
[host] OCCA OpenMP backend. Enabled when MFEM_USE_OCCA = YES.
[host] Default CPU backend: sequential execution on each MPI rank.
Biwise-OR of all CUDA backends.
static bool Allows(unsigned long b_mask)
Return true if any of the backends in the backend mask, b_mask, are allowed.
MemoryManager mm
The (single) global memory manager object.
Host memory; using new[] and delete[].
Biwise-OR of all OCCA backends.
Id
In the documentation below, we use square brackets to indicate the type of the backend: host or devic...
Biwise-OR of all RAJA backends.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
The MFEM Device class abstracts hardware devices such as GPUs, as well as programming models such as ...
[device] RAJA CUDA backend. Enabled when MFEM_USE_RAJA = YES and MFEM_USE_CUDA = YES.
[host] OCCA CPU backend: sequential execution on each MPI rank. Enabled when MFEM_USE_OCCA = YES...
[device] CUDA backend. Enabled when MFEM_USE_CUDA = YES.