MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
occa.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 
22 namespace mfem
23 {
24 
25 /// Return the default occa::device used by MFEM.
26 occa::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. */
31 occa::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. */
36 template <typename T>
37 const 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. */
47 template <typename T>
48 occa::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. */
58 template <typename T>
59 occa::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. */
69 inline bool DeviceCanUseOcca()
70 {
76 }
77 
78 typedef std::pair<int,int> occa_id_t;
79 typedef 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
[device] OCCA CUDA backend. Enabled when MFEM_USE_OCCA = YES and MFEM_USE_CUDA = YES.
Definition: device.hpp:56
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
[host] OCCA OpenMP backend. Enabled when MFEM_USE_OCCA = YES.
Definition: device.hpp:53
occa::device & OccaDev()
Return the default occa::device used by MFEM.
Definition: occa.cpp:27
T * Write(MemoryClass mc, int size)
Get write-only access to the memory with the given MemoryClass.
std::map< occa_id_t, occa::kernel > occa_kernel_t
Definition: occa.hpp:79
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. The returned occa::memory is associated with the default occa::device used by MFEM.
Definition: occa.hpp:59
[host] OCCA CPU backend: sequential execution on each MPI rank. Enabled when MFEM_USE_OCCA = YES...
Definition: device.hpp:51
static MemoryClass GetDeviceMemoryClass()
Get the current Device MemoryClass. This is the MemoryClass used by most MFEM device kernels to acces...
Definition: device.hpp:269
bool DeviceCanUseOcca()
Function that determines if an OCCA kernel should be used, based on the current mfem::Device configur...
Definition: occa.hpp:69
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. The returned occa::memory is associated with the default occa::device used by MFEM.
Definition: occa.hpp:37
Biwise-OR of all OpenMP backends.
Definition: device.hpp:89
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. The returned occa::memory is associated with the default occa::device used by MFEM.
Definition: occa.hpp:48
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:246
T * ReadWrite(MemoryClass mc, int size)
Get read-write access to the memory with the given MemoryClass.
Class used by MFEM to store pointers to host and/or device memory.
std::pair< int, int > occa_id_t
Definition: occa.hpp:78
bool UseDevice() const
Read the internal device flag.
Biwise-OR of all device backends.
Definition: device.hpp:93
const T * Read(MemoryClass mc, int size) const
Get read-only access to the memory with the given MemoryClass.