PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
6#pragma once
7
8#include <Kokkos_Core.hpp>
9#include <cmath>
10#include <filesystem>
11#include <fstream>
12#include <iomanip>
13#include <iostream>
14#include <sstream>
15
16#include "common/types.hpp"
17
18#if defined(Kokkos_ENABLE_CUDA)
19#include <cuda.h>
20#include <cuda_runtime_api.h>
21#endif
22
23#define SEND_TO_HOST(view) \
24 auto h_##view = Kokkos::create_mirror_view_and_copy( \
25 Kokkos::DefaultHostExecutionSpace{}, view)
26
27namespace PACMAN {
28
29namespace RbfPum {
30
31template <typename ExecSpace>
33 return Kokkos::SpaceAccessibility<
34 Kokkos::DefaultHostExecutionSpace,
35 typename ExecSpace::memory_space>::accessible;
36}
37
40template <typename T>
41using base_type =
42 typename std::remove_cv<typename std::remove_reference<T>::type>::type;
43
46static inline void PrintCudaMemoryUsage() {
47#if defined(Kokkos_ENABLE_CUDA)
48 size_t cuda_free, cuda_total;
50 std::cout << "used: " << (cuda_total - cuda_free) / 1'000'000.0 << "/"
51 << (cuda_total) / 1'000'000.0 << "MB" << std::endl;
52#else
53 return;
54#endif
55}
56
60template <typename ViewType> void PrintSizeOfView(ViewType &v) {
61 size_t size = 0;
62 if (v.rank() == 1) {
63 size = ViewType::required_allocation_size(v.extent(0));
64 } else if (v.rank() == 2) {
65 size = ViewType::required_allocation_size(v.extent(0), v.extent(1));
66 } else if (v.rank() == 3) {
67 size = ViewType::required_allocation_size(v.extent(0), v.extent(1),
68 v.extent(2));
69 } else {
70 size = ViewType::required_allocation_size(v.size());
71 }
72 std::cout << v.label() << " size: " << size << "B = " << size / 1'000'000.0
73 << "MB" << std::endl;
74}
75
76template <typename ViewType>
77void PrintView(ViewType &v, std::string sep = " ") {
78 std::cout << fp_consts::set_precision();
79 auto m = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, v);
80 for (index_t i = 0; i < m.extent(0); ++i) {
81 std::cout << m(i) << sep;
82 }
83 std::cout << std::endl;
84 std::cout << v.label() << ".extent(0): " << m.extent(0) << std::endl;
85}
86
87template <typename DataView>
89 std::fstream &file) {
90 using layout = typename DataView::array_layout;
91 Kokkos::View<typename DataView::const_value_type **, layout,
92 Kokkos::DefaultHostExecutionSpace>
93 M(data.data(), rows, cols);
94 for (index_t i = 0; i < rows; ++i) {
95 for (index_t j = 0; j < cols - 1; ++j) {
96 file << M(i, j) << ",";
97 }
98 file << M(i, cols - 1) << "\n";
99 }
100 file.flush();
101}
102
109template <typename ViewType, typename Comparator>
111 const Comparator &op) {
112 using T = typename ViewType::non_const_value_type;
113 using R = Kokkos::pair<T, T>;
114 const int_t n = v.extent_int(0);
115 KOKKOS_ASSERT(n > 0);
116 R ret = Kokkos::make_pair(v(0), v(0));
117 for (int_t i = 1; i < n; ++i) {
118 const auto elt = v(i);
119 if (op(elt, ret.first)) {
120 ret.first = elt;
121 }
122 if (op(ret.second, elt)) {
123 ret.second = elt;
124 }
125 }
126 return ret;
127}
128} // namespace RbfPum
129} // namespace PACMAN
typename std::remove_cv< typename std::remove_reference< T >::type >::type base_type
Returns a type without reference or const/volatile modifiers.
Definition utils.hpp:42
void ExportMatView(const DataView &data, const index_t rows, const index_t cols, std::fstream &file)
Definition utils.hpp:88
KOKKOS_INLINE_FUNCTION auto RbfPumMinMax(const ViewType &v, const Comparator &op)
A reimplementation of Kokkos::MinMax which works on any backend.
Definition utils.hpp:110
KOKKOS_FORCEINLINE_FUNCTION constexpr bool IsHostAccessible(void)
Definition utils.hpp:32
void PrintSizeOfView(ViewType &v)
Prints the memory usage of a Kokkos::View
Definition utils.hpp:60
void PrintView(ViewType &v, std::string sep=" ")
Definition utils.hpp:77
static void PrintCudaMemoryUsage()
If Cuda is activated, prints the used Nvidia device memory, else do nothing.
Definition utils.hpp:46
auto set_precision(void)
Definition types.hpp:101