mt19937 | C mt19937 random generator wrapper for Node.js | Runtime Evironment library
kandi X-RAY | mt19937 Summary
kandi X-RAY | mt19937 Summary
C++ mt19937 random generator wrapper for Node.js.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of mt19937
mt19937 Key Features
mt19937 Examples and Code Snippets
Community Discussions
Trending Discussions on mt19937
QUESTION
I'm trying to write a simple struct around an std random number generator. According to the compiler I can initialize this class but when I try to use the function it gives the following error:
Error C2280 'RandNormGen::RandNormGen(const RandNormGen &)': attempting to reference a deleted function
I don't fully understand what this means or what to do about it, but I think I narrowed it down to having something to do with the std::random_device rd;
here is my struct:
...ANSWER
Answered 2022-Mar-25 at 23:44std::random_device
is not copyable or movable. So your class also cannot be copied or moved.
However, there isn't really any point to keeping the std::random_device
instance around. You (hopefully) use it only to seed the actual random number generator. So remove it from the struct and instead in the constructor:
QUESTION
This is the following code I have for plotting a series of random points in gnuplot. I have no errors in gnuplot-iostream header.
...ANSWER
Answered 2022-Mar-16 at 17:15For debugging I used 5 points instead of 1000, so it was easier to see the first error:
QUESTION
I am using the GPU radix sort algorithm of the CUB library to sort N 32-bit unsigned integers whose values all utilize only k of their 32 bits, starting from the least significant bit.
Thus, I specify the bit subrange [begin_bit, end_bit) when calling cub::DeviceRadixSort::SortKeys in hopes of improving the sorting performance. I am using the latest release of CUB (1.16.0).
However, SortKeys crashes (not deterministically, but almost always) and reports an illegal memory access error when trying to sort 1 billion keys with certain specified bit ranges of [begin_bit=0, end_bit=k), and k = {20,19,18}, e.g. ./cub_sort_test 1000000000 0 20
I tested this on a Volta and an Ampere NVIDIA GPU with CUDA versions 11.4 and 11.2 respectively. Has anyone encountered this previously, and/or know a fix? Here is the minimal, reproducable example code:
...ANSWER
Answered 2022-Feb-27 at 15:17The problem with your code is that you do not use SortKeys
correctly. SortKeys
does not work in-place. You need to provide a separate output buffer for the sorted data.
QUESTION
Consider an Euler integrator that solves a stochastic differential equation:
...ANSWER
Answered 2022-Feb-22 at 10:14Seeding a PRNG if often costly and should usually only be done once during the whole program run so, no, this is not efficient.
I suggest that you break the creation of the PRNG out into a separate function that has a static
PRNG (only initialized once).
QUESTION
I have defined two classes : (1) class Point_CCS_xy
, and (2) random_Point_CCS_xy_generator
. I would like to define a function in Point_CCS_xy
class which makes use of random_Point_CCS_xy_generator
class to generate random points on a cartesian coordinate system within a range Min
and Max
, both of which have been defined as long double
. I have already forward declared
class random_Point_CCS_xy_generator
, however I get an Variable has incomplete type random_Point_CCS_xy_generator
error on this line random_Point_CCS_xy_generator rpg(Min, Max)
; and also this error Member reference base type 'vector (size_t)' (aka 'vector (unsigned long long)') is not a structure or union
, on this line Point_CCS_xy_vec.push_back(p);
.
MWE
...ANSWER
Answered 2022-Feb-21 at 02:14You have to complete the definition of that class random_Point_CCS_xy_generator before you can use it in the actual function definition. Probably you will have to move the methods out of a .h file and into an implementation file.
QUESTION
I like the idea of the lazy ranges you can make with std::views::iota
but was surprised to see that iota
is currently the only thing like it in the standard; it is the only "range factory" besides views::single
and views::empty
. There is not currently, for example, the equivalent of std::generate
as a range factory.
I note however it is trivial to implement the semantics of generate
by using a transform view on iota and just ignoring the value iota passes to transform i.e.
ANSWER
Answered 2022-Feb-17 at 17:10The reason why generate2
cannot work is that it does not model the range
concept, that is, the type returned by its begin()
does not model input_iterator
, because input_iterator
requires difference_type
and value_type
to exist and i++
is a valid expression.
In addition, your iterator does not satisfy sentinel_for
, which means that it cannot serve as its own sentinel, because sentinel_for
requires semiregular
which requires default_initializable
, so you also need to add default constructors for it.
You also need to rewrite bool operator!=(...)
to bool operator==(...) const
since operator!=
does not reverse synthesize operator==
. But it's easier to just use default_sentinel_t
as sentinel in your case.
if you add them to iterator
you will find the code will be well-formed:
QUESTION
Implementing a reset()
method, which uses some of the members from this
to construct a new object and then move-assign that object to *this
.
Questions:
- Does this cause any problems? UB or otherwise? (seems fine to me?)
- Is there a more idiomatic way?
Destructor and move-assignment operator only implemented to prove what is happening/ The real class has neither. "Rule of 5" not followed, as not needed here.
...ANSWER
Answered 2022-Feb-13 at 09:53
- Does this cause any problems? UB or otherwise? (seems fine to me?)
As long as the move assignment and the constructor are implemented in such a way that it does what you want, then I don't see a problem.
- Is there a more idiomatic way?
I would invert the direction of re-use.
QUESTION
I have made a random maze generator which takes in 3 commandline arguments (Height, Width, SeedValue). The maze in itself is finished, it works most of the time, however my parameters width and height are getting switched somewhere and I cant find it. e.g. if I give the parameters 10 and 10 the width should be 10 * 5 + 1 = 51 and the Height should be 10 * 3 + 1 = 31. I now have a height of 51 and a width of 31.
Does anyone see/have any tips where these variables might get switched up?
...ANSWER
Answered 2022-Jan-06 at 23:50The most obvious thing is that you've swapped height and width in the 2d vector you create in main
. Just correcting it, from
QUESTION
I have two kernels that process some data sequentially (launched with only one thread). I want to combine the two so that I can have one kernel to launch with two threads. After doing so, I was expecting to get an exec time of max(kernel1, kernel2) but what I got was the sum of the two exec times. I narrowed down the problem to something like the code below.
...ANSWER
Answered 2022-Jan-02 at 11:19Each Cuda multiprocessor has execution units (several each for int, float, special functions, ...). Those work as pipelines, which take several cycles to complete a calculation, but in each cycle a new calculation can be inserted (=scheduled) and several calculations are processed at the same time at different stages of the pipeline.
Groups of 32 threads (warps) within a block are scheduled the same instruction at the same time (same cycle or often two cycles depending on how many execution and datapath resources are available on the architecture and needed for this instruction), together with a bitfield, stating, for which threads this instruction should be actively executed. If some threads of a warp evaluated an if clause as false, they are temporarily deactivated. Or some threads may have already exited the kernel.
The effect is that if the 32 warps diverge (branch differently), each execution path has to be run through for each of the 32 threads (with some threads deactivated for each path). That should be avoided for performance reasons, as the computation resources are reserved nevertheless. Threads from different warps don't have this interdependency. The algorithm should be structured in a way to consider this.
With Volta, Independent Thread Scheduling was introduced. Each thread has its own instruction counter (and manages a separate function callstack). But the scheduler still will schedule groups of 32 threads (warps) with bitfields for active threads. What changed is that the scheduler can interleave the diverging paths. Instead of executing CCCIIIEEECCC pre-Volta (instructions: C=common, I=if branch, e=else branch), it could execute CCCIEEIIECCC, if the available execution units or the memory latency better fits. As programmer, one has to be careful, as it can be no longer assumed that the threads have not diverged, even when executing the same instruction. That is why __syncwarp was introduced and all kind of cooperation functions (e.g. the shuffle instructions) got a sync variant. Nevertheless (although we cannot know for sure, if the threads diverged) one still has to program in a way that all 32 threads can work together, if executed synchronously, especially for coalesced memory accesses. Putting __syncwarp after each possibly diverging instruction can help to ensure convergence. (But do performance profiling).
The Independent Thread Scheduling is also the reason, why __syncthreads must definitely be called correctly on the RTX 3080 - with each thread participating. A typical correcting solution for the deadlock case you mentioned in the comment is to close the if clause, sync all the threads and open a new if clause with the same condition as the previous one.
QUESTION
The usual pattern of using std::shared_timed_mutex is to let the 'reader' thread acquire it in shared mode and the 'writer' thread acquires it in exclusive mode. In this manner, the reads and writes cannot happen at the same time and thus the program is free from data-race/undefined behavior.
I wanted to understand if at all there's any problem if I change the mode among the threads i.e. the reader thread reads the shared variable after acquiring the lock in exclusive mode and the writer thread writes in the shared variable after taking the mutex in shared mode.
...ANSWER
Answered 2021-Dec-13 at 17:31unlock_shared
explicitly synchronizes with subsequent lock
calls on the same mutex. This would allow the reader to read data written by any of the writers. Similarly, lock_shared
synchronizes with prior calls to unlock
. So it is possible to use a shared_mutex
backwards without a data race (note: rand
is not required to be thread-safe).
But... should you?
The purpose of a mutex is to ensure data integrity, not just at the level of bytes (ie: data races), but at a higher level. You have 5 threads writing to 5 different locations. But... what is the meaning of the data? Are these data completely distinct from one another, or does the collection of data have some meaning that needs to be preserved? That is, if one thread writes to one value, does the reader get malformed information if another thread hasn't written its value yet?
If these data values are all completely, fundamentally separate, then a mutex is unnecessary (at least for basic types). What you're really doing is just atomic writes. The writers can write to an atomic
, and the reader will read these. Since the values are all disparate and lack any question of ordering between them, you don't need to block any thread from writing. All you need to do is ensure data integrity at the level of an individual T
. Lock-free atomics will be much faster than any mutex-based solution.
But if the data has some notion of integrity to it, if the group of threads is collectively creating a single value that the reader thread should read in its entirety, then what you're looking for is a barrier
, not a mutex. This object allows you to see if a group of execution agents have collectively reached a certain point. And if they have, it is safe for you to read the data. And once you're finished reading it, it is safe to release the agents to write to them once again.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install mt19937
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page