forked from RL-S/Kokkidio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewMap.cpp
67 lines (50 loc) · 2.03 KB
/
ViewMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <Kokkidio.hpp>
#include <iostream>
#include <magic_enum.hpp>
int main(int argc, char** argv){
Kokkos::ScopeGuard guard(argc, argv);
using namespace Kokkidio;
std::cout << "DefaultTarget: " << magic_enum::enum_name(DefaultTarget) <<'\n';
int nRows {10}, nCols {20};
/* existing Eigen object */
Eigen::ArrayXXd eigenArray {nRows, nCols};
/*********************************************************/
/* Construction */
/*********************************************************/
/* Create ViewMap using factory function for specific target,
* while deducing Eigen type */
auto mv1 = viewMap<Target::host>(eigenArray);
/* Create ViewMap using a constructor or factory function.
* Deduces Eigen type, and uses default target */
ViewMap mv2 {eigenArray};
auto mv3 = viewMap(eigenArray);
/* Note:
* Do not actually pass the same Eigen object to multiple ViewMaps!
* If you do, then resize() will only apply to the Eigen object
* and the ViewMap it was called on, but not the other ViewMaps. */
/* Create ViewMap using size parameters.
* ArrayXXd is dynamically sized in both dimensions,
* so two parameters are required */
ViewMap<Eigen::ArrayXXd> mv4 {nRows, nCols};
/* ArrayXd is a column vector, so only rows are required */
ViewMap<Eigen::ArrayXd> mv5 {nRows};
/* Array3d is a fixed size type, so no parameters are required */
ViewMap<Eigen::Array3d> mv6;
/*********************************************************/
/* Using ViewMap */
/*********************************************************/
/* set values on host, using Eigen's assignment operator on ViewMap::map() */
mv1.map() = 1;
/* set values on target, using Kokkos::deep_copy with ViewMap::view() */
Kokkos::deep_copy(mv2.view(), 2);
/* set values on target with parallel dispatch: */
/* with Kokkidio::ParallelRange */
parallel_for( mv3.cols(), KOKKOS_LAMBDA(ParallelRange<> rng){
rng(mv3) = 3;
});
/* or just an integer, using the standard Kokkos-style */
parallel_for( mv4.size(), KOKKOS_LAMBDA(int i){
mv4.data()[i] = 4;
});
return 0;
}