Compadre 1.5.5
Loading...
Searching...
No Matches
UtilityTest.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <string>
3#include <vector>
4#include <map>
5#include <stdlib.h>
6#include <cstdio>
7#include <random>
8
9#include <Compadre_Config.h>
10#include <Compadre_GMLS.hpp>
13
14#include "GMLS_Tutorial.hpp"
15
16#ifdef COMPADRE_USE_MPI
17#include <mpi.h>
18#endif
19
20#include <Kokkos_Timer.hpp>
21#include <Kokkos_Core.hpp>
22
23using namespace Compadre;
24
25//! [Parse Command Line Arguments]
26
27// called from command line
28int main (int argc, char* args[]) {
29
30#ifdef COMPADRE_USE_MPI
31// initialized MPI (if avaialble) with command line arguments given
32MPI_Init(&argc, &args);
33#endif
34
35// initializes Kokkos with command line arguments given
36Kokkos::initialize(argc, args);
37
38// becomes false if there is unwanted index in the filtered flags
39bool all_passed = true;
40
41// code block to reduce scope for all Kokkos View allocations
42// otherwise, Views may be deallocating when we call Kokkos finalize() later
43{
44 // set the number of columns
45 int num_cols = 50; // default 50 columns
46 if (argc >= 3) {
47 if (args[2] != NULL) {
48 auto arg3toi = atoi(args[2]);
49 if (isdigit(arg3toi)) {
50 if (arg3toi > 0) {
51 num_cols = arg3toi;
52 }
53 }
54 }
55 }
56
57 // set the number of flags
58 int num_flags = 200; // default 200 flags
59 if (argc >= 2) {
60 if (args[1] != NULL) {
61 auto arg2toi = atoi(args[1]);
62 if (isdigit(arg2toi)) {
63 if (arg2toi > 0) {
64 num_flags = arg2toi;
65 }
66 }
67 }
68 }
69 //! [Parse Command Line Arguments]
70
71 //! [Setting Up Data]
72 Kokkos::Timer timer;
73 Kokkos::Profiling::pushRegion("Setup Data");
74
75 // create a 2D view of inputs
76 Kokkos::View<int**, Kokkos::DefaultExecutionSpace> data_device("data", num_flags, num_cols);
77 Kokkos::View<int**>::HostMirror data = Kokkos::create_mirror_view(data_device);
78
79 // create a view of flags
80 Kokkos::View<int*, Kokkos::DefaultExecutionSpace> flags_device("flags", num_flags);
81 Kokkos::View<int*>::HostMirror flags = Kokkos::create_mirror_view(flags_device);
82
83 //! [Setting Up Data]
84
85 Kokkos::Profiling::popRegion();
86 Kokkos::Profiling::pushRegion("Filter And Extract Data");
87
88 //! [Filtering And Extracting Data]
89 // create arbitrary data
90 for (int i=0; i<num_flags; i++) {
91 for (int j=0; j<num_cols; j++) {
92 if ((i % 2) == 0) {
93 data(i, j) = 1;
94 } else {
95 data(i, j) = 0;
96 }
97 }
98 }
99 // copy the data from host to device
100 Kokkos::deep_copy(data_device, data);
101
102 // create arbitrary flags
103 int num_filtered_flags = 0; // number of filtered flags
104 for (int i=0; i<num_flags; i++) {
105 if ((i % 2) == 0) {
106 flags(i) = 1;
107 num_filtered_flags++;
108 } else {
109 flags(i) = 0;
110 }
111 }
112 // copy the flags from host to device
113 Kokkos::deep_copy(flags_device, flags);
114
115 // Then call out the function to create view
116 auto filtered_flags = filterViewByID<Kokkos::HostSpace>(flags_device, 1);
117 auto extracted_data = Extract::extractViewByIndex<Kokkos::HostSpace>(data_device, filtered_flags);
118
119 //! [Filtering Data]
120
121 Kokkos::Profiling::popRegion();
122 Kokkos::Profiling::pushRegion("Check Filtered And Extracted Data");
123
124 //! [Checking Filtered And Extracted Data]
125
126 if (filtered_flags.extent(0) != (size_t)num_filtered_flags) {
127 all_passed = false;
128 std::cout << "Failed - number of filtered flags not matched!" << filtered_flags.extent(0) << " " << num_filtered_flags << std::endl;
129 }
130 for (size_t i=0; i<filtered_flags.extent(0); i++) {
131 if (filtered_flags(i) % 2 != 0) {
132 all_passed = false;
133 std::cout << "Failed - incorrect filtered flags " << filtered_flags(i) << std::endl;
134 }
135 }
136 // All values inside extracted data should now be 1
137 for (size_t i=0; i<extracted_data.extent(0); i++) {
138 for (size_t j=0; j<extracted_data.extent(1); j++) {
139 if (extracted_data(i, j) != 1) {
140 all_passed = false;
141 std::cout << "Failed - incorrect values in extracted view at index " << i << " " << j << " " << extracted_data(i, j) << std::endl;
142 }
143 }
144 }
145
146 //! [Checking Filtered And Extracted Data]
147 // stop timing comparison loop
148 Kokkos::Profiling::popRegion();
149 //! [Finalize Program]
150
151} // end of code block to reduce scope, causing Kokkos View de-allocations
152// otherwise, Views may be deallocating when we call Kokkos finalize() later
153
154// finalize Kokkos and MPI (if available)
155Kokkos::finalize();
156#ifdef COMPADRE_USE_MPI
157MPI_Finalize();
158#endif
159
160// output to user that test passed or failed
161if (all_passed) {
162 fprintf(stdout, "Passed test \n");
163 return 0;
164} else {
165 fprintf(stdout, "Failed test \n");
166 return -1;
167}
168
169} // main
170
171//! [Finalize Program]
int main(int argc, char *args[])
[Parse Command Line Arguments]
Definition: UtilityTest.cpp:28