66#include < thrust/random.h>
77
88
9- // This example shows how thrust::zip_iterator can be used to create a
10- // 'virtual' array of structures. In this case the structure is a 3d
11- // vector type (Float3) whose (x,y,z) components will be stored in
9+ // This example shows how thrust::zip_iterator can be used to create a
10+ // 'virtual' array of structures. In this case the structure is a 3d
11+ // vector type (Float3) whose (x,y,z) components will be stored in
1212// three separate float arrays. The zip_iterator "zips" these arrays
1313// into a single virtual Float3 array.
1414
@@ -54,17 +54,17 @@ int main(void)
5454 // We'll store the components of the 3d vectors in separate arrays. One set of
5555 // arrays will store the 'A' vectors and another set will store the 'B' vectors.
5656
57- // This 'structure of arrays' (SoA) approach is usually more efficient than the
57+ // This 'structure of arrays' (SoA) approach is usually more efficient than the
5858 // 'array of structures' (AoS) approach. The primary reason is that structures,
5959 // like Float3, don't always obey the memory coalescing rules, so they are not
6060 // efficiently transferred to and from memory. Another reason to prefer SoA to
6161 // AoS is that we don't aways want to process all members of the structure. For
62- // example, if we only need to look at first element of the structure then it
62+ // example, if we only need to look at first element of the structure then it
6363 // is wasteful to load the entire structure from memory. With the SoA approach,
6464 // we can chose which elements of the structure we wish to read.
6565
6666 thrust::device_vector<float > A0 = random_vector (N); // x components of the 'A' vectors
67- thrust::device_vector<float > A1 = random_vector (N); // y components of the 'A' vectors
67+ thrust::device_vector<float > A1 = random_vector (N); // y components of the 'A' vectors
6868 thrust::device_vector<float > A2 = random_vector (N); // z components of the 'A' vectors
6969
7070 thrust::device_vector<float > B0 = random_vector (N); // x components of the 'B' vectors
@@ -78,7 +78,7 @@ int main(void)
7878 // We'll now illustrate two ways to use zip_iterator to compute the dot
7979 // products. The first method is verbose but shows how the parts fit together.
8080 // The second method hides these details and is more concise.
81-
81+
8282
8383 // METHOD #1
8484 // Defining a zip_iterator type can be a little cumbersome ...
@@ -87,24 +87,24 @@ int main(void)
8787 typedef thrust::zip_iterator<FloatIteratorTuple> Float3Iterator;
8888
8989 // Now we'll create some zip_iterators for A and B
90- Float3Iterator A_first = thrust::make_zip_iterator (make_tuple (A0.begin (), A1.begin (), A2.begin ()));
91- Float3Iterator A_last = thrust::make_zip_iterator (make_tuple (A0.end (), A1.end (), A2.end ()));
92- Float3Iterator B_first = thrust::make_zip_iterator (make_tuple (B0.begin (), B1.begin (), B2.begin ()));
93-
90+ Float3Iterator A_first = thrust::make_zip_iterator (thrust:: make_tuple (A0.begin (), A1.begin (), A2.begin ()));
91+ Float3Iterator A_last = thrust::make_zip_iterator (thrust:: make_tuple (A0.end (), A1.end (), A2.end ()));
92+ Float3Iterator B_first = thrust::make_zip_iterator (thrust:: make_tuple (B0.begin (), B1.begin (), B2.begin ()));
93+
9494 // Finally, we pass the zip_iterators into transform() as if they
9595 // were 'normal' iterators for a device_vector<Float3>.
9696 thrust::transform (A_first, A_last, B_first, result.begin (), DotProduct ());
9797
9898
9999 // METHOD #2
100- // Alternatively, we can avoid creating variables for X_first, X_last,
100+ // Alternatively, we can avoid creating variables for X_first, X_last,
101101 // and Y_first and invoke transform() directly.
102- thrust::transform ( thrust::make_zip_iterator (make_tuple (A0.begin (), A1.begin (), A2.begin ())),
103- thrust::make_zip_iterator (make_tuple (A0.end (), A1.end (), A2.end ())),
104- thrust::make_zip_iterator (make_tuple (B0.begin (), B1.begin (), B2.begin ())),
102+ thrust::transform ( thrust::make_zip_iterator (thrust:: make_tuple (A0.begin (), A1.begin (), A2.begin ())),
103+ thrust::make_zip_iterator (thrust:: make_tuple (A0.end (), A1.end (), A2.end ())),
104+ thrust::make_zip_iterator (thrust:: make_tuple (B0.begin (), B1.begin (), B2.begin ())),
105105 result.begin (),
106106 DotProduct () );
107-
107+
108108
109109
110110 // Finally, we'll print a few results
@@ -126,8 +126,8 @@ int main(void)
126126 std::cout << " (" << thrust::get<0 >(b) << " ," << thrust::get<1 >(b) << " ," << thrust::get<2 >(b) << " )" ;
127127 std::cout << " = " ;
128128 std::cout << dot << std::endl;
129- }
129+ }
130130
131131 return 0 ;
132132}
133-
133+
0 commit comments