6
6
#include < thrust/random.h>
7
7
8
8
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
12
12
// three separate float arrays. The zip_iterator "zips" these arrays
13
13
// into a single virtual Float3 array.
14
14
@@ -54,17 +54,17 @@ int main(void)
54
54
// We'll store the components of the 3d vectors in separate arrays. One set of
55
55
// arrays will store the 'A' vectors and another set will store the 'B' vectors.
56
56
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
58
58
// 'array of structures' (AoS) approach. The primary reason is that structures,
59
59
// like Float3, don't always obey the memory coalescing rules, so they are not
60
60
// efficiently transferred to and from memory. Another reason to prefer SoA to
61
61
// 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
63
63
// is wasteful to load the entire structure from memory. With the SoA approach,
64
64
// we can chose which elements of the structure we wish to read.
65
65
66
66
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
68
68
thrust::device_vector<float > A2 = random_vector (N); // z components of the 'A' vectors
69
69
70
70
thrust::device_vector<float > B0 = random_vector (N); // x components of the 'B' vectors
@@ -78,7 +78,7 @@ int main(void)
78
78
// We'll now illustrate two ways to use zip_iterator to compute the dot
79
79
// products. The first method is verbose but shows how the parts fit together.
80
80
// The second method hides these details and is more concise.
81
-
81
+
82
82
83
83
// METHOD #1
84
84
// Defining a zip_iterator type can be a little cumbersome ...
@@ -87,24 +87,24 @@ int main(void)
87
87
typedef thrust::zip_iterator<FloatIteratorTuple> Float3Iterator;
88
88
89
89
// 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
+
94
94
// Finally, we pass the zip_iterators into transform() as if they
95
95
// were 'normal' iterators for a device_vector<Float3>.
96
96
thrust::transform (A_first, A_last, B_first, result.begin (), DotProduct ());
97
97
98
98
99
99
// 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,
101
101
// 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 ())),
105
105
result.begin (),
106
106
DotProduct () );
107
-
107
+
108
108
109
109
110
110
// Finally, we'll print a few results
@@ -126,8 +126,8 @@ int main(void)
126
126
std::cout << " (" << thrust::get<0 >(b) << " ," << thrust::get<1 >(b) << " ," << thrust::get<2 >(b) << " )" ;
127
127
std::cout << " = " ;
128
128
std::cout << dot << std::endl;
129
- }
129
+ }
130
130
131
131
return 0 ;
132
132
}
133
-
133
+
0 commit comments