Skip to content

Commit bf83be6

Browse files
committed
fix: introduce no_init template class for uninitialized types and update vector usage in ranx.cpp
1 parent d019873 commit bf83be6

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/ranx.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#if defined(__CUDACC__) || defined(__HIP_PLATFORM_AMD__)
3636
#include <thrust/universal_vector.h>
3737
#include <thrust/device_vector.h>
38+
#include <thrust/host_vector.h>
3839
#endif
3940

4041
#include <ranx/random>
@@ -43,6 +44,25 @@
4344
const std::string VERSION = "1.0.0";
4445
const std::string PROGRAM_NAME = "ranx";
4546

47+
template<typename T>
48+
class no_init
49+
{ static_assert(
50+
std::is_fundamental<T>::value,
51+
"should be a fundamental type");
52+
public:
53+
// constructor without initialization
54+
no_init () noexcept {}
55+
// implicit conversion T → no_init<T>
56+
RANX_DEVICE_CODE
57+
constexpr no_init (T value) noexcept: v_{value} {}
58+
// implicit conversion no_init<T> → T
59+
RANX_DEVICE_CODE
60+
constexpr operator T () const noexcept { return v_; }
61+
private:
62+
T v_;
63+
};
64+
65+
4666
void print_version()
4767
{
4868
std::cout << PROGRAM_NAME << " version " << VERSION << "\n"
@@ -115,6 +135,9 @@ int main(int argc, char* argv[])
115135
std::string eof_string = "\n";
116136
std::string bof_string = "";
117137

138+
std::ios_base::sync_with_stdio(false); // Disable sync with C stdio
139+
std::cin.tie(nullptr); // Untie cin from cout
140+
118141
// Parse command line arguments
119142
for (int i = 1; i < argc; ++i)
120143
{
@@ -171,21 +194,26 @@ int main(int argc, char* argv[])
171194
if (generate_float)
172195
{ // Generate floating point numbers
173196
#if defined(__CUDACC__)
174-
// thrust::universal_vector<double> numbers(count);
175-
thrust::device_vector<double> numbers(count);
197+
// thrust::universal_vector<float> numbers(count);
198+
thrust::device_vector<no_init<float>> d_numbers(count);
176199
ranx::cuda::generate_n
200+
( d_numbers.begin()
177201
#elif defined(__HIP_PLATFORM_AMD__)
178-
thrust::device_vector<int> numbers(count);
202+
thrust::device_vector<no_init<float>> d_numbers(count);
179203
ranx::rocm::generate_n
204+
( d_numbers.begin()
180205
#else
181-
std::vector<double> numbers(count);
206+
std::vector<no_init<float>> numbers(count);
182207
ranx::generate_n
183-
#endif
184208
( std::begin(numbers)
209+
#endif
185210
, count
186-
, ranx::bind(trng::uniform01_dist<double>(), pcg32(seed))
211+
, ranx::bind(trng::uniform01_dist<float>(), pcg32(seed))
187212
);
188-
213+
#if defined(__CUDACC__) || defined(__HIP_PLATFORM_AMD__)
214+
thrust::host_vector<no_init<float>> numbers(count);
215+
thrust::copy(d_numbers.begin(), d_numbers.end(), numbers.begin());
216+
#endif
189217
std::cout << std::fixed << std::setprecision(precision);
190218
for (size_t i = 0; i < count; ++i)
191219
{ if (i > 0) std::cout << delimiter;
@@ -200,7 +228,7 @@ int main(int argc, char* argv[])
200228
}
201229

202230
// Create a vector with all possible values
203-
std::vector<int> all_numbers(max_value - min_value + 1);
231+
std::vector<no_init<int>> all_numbers(max_value - min_value + 1);
204232
std::iota(all_numbers.begin(), all_numbers.end(), min_value);
205233

206234
// Shuffle using standard algorithm
@@ -219,21 +247,25 @@ int main(int argc, char* argv[])
219247
else
220248
{ // Generate regular integers
221249
#if defined(__CUDACC__)
222-
// thrust::universal_vector<int> numbers(count);
223-
thrust::device_vector<int> numbers(count);
250+
thrust::device_vector<no_init<int>> d_numbers(count);
224251
ranx::cuda::generate_n
252+
( d_numbers.begin()
225253
#elif defined(__HIP_PLATFORM_AMD__)
226-
thrust::device_vector<int> numbers(count);
254+
thrust::device_vector<no_init<int>> d_numbers(count);
227255
ranx::rocm::generate_n
256+
( d_numbers.begin()
228257
#else
229-
std::vector<int> numbers(count);
258+
std::vector<no_init<int>> numbers(count);
230259
ranx::generate_n
231-
#endif
232260
( std::begin(numbers)
261+
#endif
233262
, count
234263
, ranx::bind(trng::uniform_int_dist(min_value, max_value), pcg32(seed))
235264
);
236-
265+
#if defined(__CUDACC__) || defined(__HIP_PLATFORM_AMD__)
266+
thrust::host_vector<no_init<int>> numbers(count);
267+
thrust::copy(d_numbers.begin(), d_numbers.end(), numbers.begin());
268+
#endif
237269
for (size_t i = 0; i < count; ++i)
238270
{ if (i > 0) std::cout << delimiter;
239271
std::cout << numbers[i];

0 commit comments

Comments
 (0)