Speed up 3D rand
Show older comments
I was wondering if I could so anything to speed up the process of generating random matrices.
For instance,
tic
rand(1000, 1000, 1000)
toc
gives 7-9 sec as a result.
Using this line in a monte carlo simulation of 100000 repetitions means that the program will need at least 277 hours.. I read that CUDA could be handy but my GPU doesn't support it.
8 Comments
Bora Eryilmaz
on 12 Dec 2022
Edited: Bora Eryilmaz
on 12 Dec 2022
rand(1000, 1000, 1000) creates a 3-D array of random numbers. Is that what you need? If you just need a random matrix, you can do rand(1000, 1000). If you don't need the whole random matrix at once, you can create the random values (vectors, scalars, etc.) as needed.
Basil Tsekenis
on 12 Dec 2022
Bora Eryilmaz
on 12 Dec 2022
Edited: Bora Eryilmaz
on 12 Dec 2022
Generating 1 billion random numbers will take a long time and a lot of memory no matter what you do. You might have incremental speedups using GPUs, etc., but I wouldn't expect a lot more speed improvements.
Basil Tsekenis
on 12 Dec 2022
Bruno Luong
on 12 Dec 2022
Edited: Bruno Luong
on 12 Dec 2022
Then you need to call RANDN not RAND
"Calling external C++ code could make things faster?"
I don't think so. MATLAB randn is low-level coded function (probably in C) and you won't be able to beat it, unless you are a genious of C++. But a genius of C won't ask such question.
John D'Errico
on 12 Dec 2022
Edited: John D'Errico
on 12 Dec 2022
Is your computer infinitely fast? Of course not. Everything takes some time. Here, you are asking to generate 1 BILLION random numbers. That is 10^9 numbers. (Worse, then you want to do that 10000 times.) It requires that your computer must allocate and fill approximately 8 gigabytes of RAM. And, of course, you would not be happy if MATLAB used some crapola random number generater. Yes, a simple linear congruential random number generator from the old days would be faster. But it would be complete crap in terms of appearing to be truly random. And then you would be complaining for other very valid reasons.
Whether you really want normally distributed or uniformly distributed numbers is not clear, since you have said both. But no, hoping to write c-code to do this will not be faster. Certainly not much faster, unless you are willing to accept crap for randomness. Anyway, anything you do will not give you orders of magnitude speedups, as I think you would want. The MATLAB code to do what you want is already highly optimized C code.
We get spoiled, thinking our computers are infinitely fast. They can do anything. And if a small problem is easy and seems to run almost instantaneously, then just make it bigger. But eventually you run into hard limits on the capabilities of your computer. This is when mathematics becomes important, where you need to find creative ways of solving a problem better, instead an application of brute force - not that I am suggesting there is anything you could obviously do to improve what you are doing.
If the problem is too big to solve in a finite time, then accept that, and make it smaller. Or get a bigger, faster yet computer. You could always rent time on a big compute cluster, but nothing will be free.
Finally, I might point out that I've invested something like 2500+ hours of compute time on one project of my own. (That is 8 cores running for at least part of the time, cranking on the system fans.) I just let my computer run in the background, and over night, sometimes every night for weeks on end. I have it set up to save the intermediate results periodically so a restart is always available. And my computer is moderately fast and big. Big problems take big time.
Bora Eryilmaz
on 12 Dec 2022
You must be accessing the elements of the random matrix in a for-loop or something. I can't imagine you would be doing direct array operations on the whole random array since that would quickly grind your computer to a halt. So assuming you are using some kind of loop to access "parts" of the random array, you could just generate random numbers for that part as needed, and not in advance.
Bruno Luong
on 12 Dec 2022
OP wants to speed up, not necessary to reduce memory.
Answers (1)
tic
A = rand(1000, 1000, 1000);
t = toc
So on average rand generated 1 number every:
format longg
s = seconds(t/numel(A))
about 10 nanoseconds. What's your requirement for how fast generating a random number needs to be for your application? That will tell us if what you want to do is theoretically feasible.
The SIMD-oriented Fast Mersenne Twister may be a bit faster (see the rng function for how to enable this version) but if you're looking for this to take a millisecond to generate that whole array I think that's likely not achievable.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!