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