[ 
https://issues.apache.org/jira/browse/RNG-104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16868055#comment-16868055
 ] 

Alex D Herbert commented on RNG-104:
------------------------------------

The array can be filled in blocks and the last block is incomplete. Here's some 
example code I used:
{code:java}
private static final ReentrantLock UNFAIR_LOCK = new ReentrantLock(false);

private static void nextInt(Lock lock, UniformRandomProvider rng, int[] array, 
int start, int end) {
    lock.lock();
    try {
        for (int i = start; i < end; i++) {
            array[i] = rng.nextInt();
        }
    } finally {
        lock.unlock();
    }
}

@Benchmark
@Threads(4)
public int[] Threads4_createIntArraySeedBlocks_UnfairLock(SeedRandomSources 
sources, TestSizes sizes) {
    final UniformRandomProvider rng = sources.getGenerator();
    final int[] seed = new int[sizes.getSize()];
    for (int i = 0; i < seed.length; i += sizes.getBlockSize()) {
        nextInt(UNFAIR_LOCK, rng, seed, i, Math.min(i + sizes.getBlockSize(), 
seed.length));
    }
    return seed;
}
{code}
Similar code was used for the synchronized version. I will commit the tests 
used for this ticket to the JMH examples module.

The code is sub-optimal as the Math.min is only required on the final 
iteration. It won't effect the benchmark as they all used this method but if 
the block size is a constant it will allow the JVM to unroll the main fill 
loop. So I may modify it to do the loop without the min and then a final fill 
at the end as is done in {{IntProvider.nextBytesFill}} for example.
{quote}How does it impact the time before wrapping around?
{quote}
Given the smily face do you mean how long before the 2^1024 period repeats?

Well given that the calculation was based on outputting a deviate every 0.1 
nanoseconds and the data for single {{long}} generation shows an output takes 
about 15 nanoseconds then the 969 years will become 145,350 years. I think this 
generator is fine for the intended usage.

 

> SeedFactory seed creation performance analysis
> ----------------------------------------------
>
>                 Key: RNG-104
>                 URL: https://issues.apache.org/jira/browse/RNG-104
>             Project: Commons RNG
>          Issue Type: Task
>          Components: simple
>    Affects Versions: 1.3
>            Reporter: Alex D Herbert
>            Assignee: Alex D Herbert
>            Priority: Minor
>         Attachments: t1.jpg, t4.jpg, well_lock_vs_sync1.png, 
> well_lock_vs_sync4.jpg, well_sync_performance.jpg, 
> well_unfair_performance.jpg, well_unfair_vs_fair.jpg, xor_unfair_int1.png, 
> xor_unfair_long1.png
>
>
> The SeedFactory is used to create seeds for the random generators. To ensure 
> thread safety this uses synchronized blocks around a single generator. The 
> current method only generates a single int or long per synchronisation. 
> Analyze the performance of this approach. The analysis will investigate 
> generating multiple values inside each synchronisation around the generator.
> This analysis will also investigate methods to supplement the SeedFactory 
> with fast methods to create seeds. This will use a fast seeding method to 
> generate a single long value. This can be a seed for a SplitMix generator 
> used to create a seed of any length.
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to