Skip to content

Commit

Permalink
More performance testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Jan 8, 2024
1 parent d218105 commit 1ef381e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
97 changes: 96 additions & 1 deletion js/webgpu/PerformanceTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* @author Jonathan Olson <[email protected]>
*/

import { alpenglow, OldBindingType, BlitShader, OldComputeShader, DeviceContext, wgsl_copy_storage_operation, wgsl_expensive_operation, wgsl_fake_combine_to_texture } from '../imports.js';
import { alpenglow, OldBindingType, BlitShader, OldComputeShader, DeviceContext, wgsl_copy_storage_operation, wgsl_expensive_operation, wgsl_fake_combine_to_texture, U32Order, BufferArraySlot, getArrayType, RadixSortModule, Routine, Procedure, u32 } from '../imports.js';
import Random from '../../../dot/js/Random.js';
import Utils from '../../../dot/js/Utils.js';

// eslint-disable-next-line bad-sim-text
const random = new Random();
Expand Down Expand Up @@ -155,6 +156,100 @@ export default class PerformanceTesting {
buffersToDestroy.forEach( buffer => buffer.destroy() );
} )();
}

public static async loopRadixSortTest(
combineStrategy: boolean,
separateComputePasses: boolean
): Promise<void> {
const countPerFrame = 100;

const inputSize = 4000;
// const inputSize = workgroupSize * workgroupSize * ( 6 ) - 27 * 301;
// const inputSize = workgroupSize * workgroupSize * ( workgroupSize - 3 ) - 27 * 301;
// eslint-disable-next-line bad-sim-text
const uintNumbers = new Uint32Array( _.range( 0, inputSize ).map( () => Math.floor( Math.random() * 1000000 ) ) );

const device = ( await DeviceContext.getDevice() )!;
const deviceContext = new DeviceContext( device );

const order = U32Order;
const size = inputSize;
const maximumSize = inputSize + 100;

const inputSlot = new BufferArraySlot( getArrayType( order.type, maximumSize ) );
const outputSlot = new BufferArraySlot( getArrayType( order.type, maximumSize ) );

const radixSortModule = new RadixSortModule( {
input: inputSlot,
output: outputSlot,
name: 'performance test',

order: order,
totalBits: 32,

radixWorkgroupSize: 64,
radixGrainSize: 4,
scanWorkgroupSize: 64,
scanGrainSize: 4,

lengthExpression: u32( size ),

bitsPerPass: 2, // TODO: try 8 once we are doing more
bitsPerInnerPass: 2,
earlyLoad: false,
scanModuleOptions: {
areScannedReductionsExclusive: false
}
} );

const routine = await Routine.create(
deviceContext,
radixSortModule,
[ inputSlot, outputSlot ],
combineStrategy ? Routine.COMBINE_ALL_LAYOUT_STRATEGY : Routine.INDIVIDUAL_LAYOUT_STRATEGY,
async ( context, execute, input: number[] ) => {
context.setTypedBufferValue( inputSlot, input );

for ( let i = 0; i < countPerFrame; i++ ) {
execute( context, input.length );
}
}
);

const procedure = new Procedure( routine ).bindRemainingBuffers();

let startTime: DOMHighResTimeStamp | null = null;
let countElapsed = 0;
const elapsedTimes: number[] = [];

const step = async () => {
requestAnimationFrame( step );

if ( startTime === null ) {
startTime = performance.now();
}

countElapsed++;

if ( countElapsed % 500 === 0 ) {
const now = performance.now();
const elapsed = now - startTime;
startTime = now;
elapsedTimes.push( elapsed );
console.log( Utils.toFixed( elapsed, 0 ), elapsedTimes.length > 1 ? Utils.toFixed( _.sum( elapsedTimes.slice( 1 ) ) / elapsedTimes.slice( 1 ).length, 0 ) : 0 );
}

// TODO: maybe avoid the await on the first frame?

// TODO: accept typed arrays and get things working more efficiently!
await procedure.standaloneExecute( deviceContext, [ ...uintNumbers ], {
procedureExecuteOptions: {
separateComputePasses: separateComputePasses
}
} );
};
await step();
}
}

alpenglow.register( 'PerformanceTesting', PerformanceTesting );
6 changes: 6 additions & 0 deletions tests/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@
} );
};

window.radixPerformanceTest = function() {
setTimeout( async () => {
phet.alpenglow.PerformanceTesting.loopRadixSortTest();
} );
};

window.centroidTest = () => {
setTimeout( async () => {

Expand Down

0 comments on commit 1ef381e

Please sign in to comment.