MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
occa.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_OCCA_HPP
13#define MFEM_OCCA_HPP
14
15#include "../config/config.hpp"
16
17#ifdef MFEM_USE_OCCA
18#include "mem_manager.hpp"
19#include "device.hpp"
20#include <occa.hpp>
21
22namespace mfem
23{
24
25/// Return the default occa::device used by MFEM.
26occa::device &OccaDev();
27
28/// Wrap a pointer as occa::memory with the default occa::device used by MFEM.
29/** It is assumed that @a ptr is suitable for use with the current mfem::Device
30 configuration. */
31occa::memory OccaMemoryWrap(void *ptr, std::size_t bytes);
32
33/** @brief Wrap a Memory object as occa::memory for read only access with the
34 mfem::Device MemoryClass. The returned occa::memory is associated with the
35 default occa::device used by MFEM. */
36template <typename T>
37const occa::memory OccaMemoryRead(const Memory<T> &mem, size_t size)
38{
39 mem.UseDevice(true);
40 const void *ptr = mem.Read(Device::GetDeviceMemoryClass(), size);
41 return OccaMemoryWrap(const_cast<void *>(ptr), size*sizeof(T));
42}
43
44/** @brief Wrap a Memory object as occa::memory for write only access with the
45 mfem::Device MemoryClass. The returned occa::memory is associated with the
46 default occa::device used by MFEM. */
47template <typename T>
48occa::memory OccaMemoryWrite(Memory<T> &mem, size_t size)
49{
50 mem.UseDevice(true);
52 size*sizeof(T));
53}
54
55/** @brief Wrap a Memory object as occa::memory for read-write access with the
56 mfem::Device MemoryClass. The returned occa::memory is associated with the
57 default occa::device used by MFEM. */
58template <typename T>
59occa::memory OccaMemoryReadWrite(Memory<T> &mem, size_t size)
60{
61 mem.UseDevice(true);
63 size*sizeof(T));
64}
65
66
67/** @brief Function that determines if an OCCA kernel should be used, based on
68 the current mfem::Device configuration. */
77
78typedef std::pair<int,int> occa_id_t;
79typedef std::map<occa_id_t, occa::kernel> occa_kernel_t;
80
81} // namespace mfem
82
83#endif // MFEM_USE_OCCA
84
85#endif // MFEM_OCCA_HPP
static bool Allows(unsigned long b_mask)
Return true if any of the backends in the backend mask, b_mask, are allowed.
Definition device.hpp:259
static MemoryClass GetDeviceMemoryClass()
Get the current Device MemoryClass. This is the MemoryClass used by most MFEM device kernels to acces...
Definition device.hpp:282
Class used by MFEM to store pointers to host and/or device memory.
T * Write(MemoryClass mc, int size)
Get write-only access to the memory with the given MemoryClass.
T * ReadWrite(MemoryClass mc, int size)
Get read-write access to the memory with the given MemoryClass.
bool UseDevice() const
Read the internal device flag.
const T * Read(MemoryClass mc, int size) const
Get read-only access to the memory with the given MemoryClass.
occa::memory OccaMemoryReadWrite(Memory< T > &mem, size_t size)
Wrap a Memory object as occa::memory for read-write access with the mfem::Device MemoryClass....
Definition occa.hpp:59
occa::memory OccaMemoryWrite(Memory< T > &mem, size_t size)
Wrap a Memory object as occa::memory for write only access with the mfem::Device MemoryClass....
Definition occa.hpp:48
std::map< occa_id_t, occa::kernel > occa_kernel_t
Definition occa.hpp:79
bool DeviceCanUseOcca()
Function that determines if an OCCA kernel should be used, based on the current mfem::Device configur...
Definition occa.hpp:69
occa::memory OccaMemoryWrap(void *ptr, std::size_t bytes)
Wrap a pointer as occa::memory with the default occa::device used by MFEM.
Definition occa.cpp:29
const occa::memory OccaMemoryRead(const Memory< T > &mem, size_t size)
Wrap a Memory object as occa::memory for read only access with the mfem::Device MemoryClass....
Definition occa.hpp:37
occa::device & OccaDev()
Return the default occa::device used by MFEM.
Definition occa.cpp:27
std::pair< int, int > occa_id_t
Definition occa.hpp:78
@ OCCA_OMP
[host] OCCA OpenMP backend. Enabled when MFEM_USE_OCCA = YES.
Definition device.hpp:57
@ OCCA_CUDA
[device] OCCA CUDA backend. Enabled when MFEM_USE_OCCA = YES and MFEM_USE_CUDA = YES.
Definition device.hpp:60
@ OCCA_CPU
[host] OCCA CPU backend: sequential execution on each MPI rank. Enabled when MFEM_USE_OCCA = YES.
Definition device.hpp:55
@ DEVICE_MASK
Biwise-OR of all device backends.
Definition device.hpp:97
@ OMP_MASK
Biwise-OR of all OpenMP backends.
Definition device.hpp:93