Skip to content

Commit b749bd9

Browse files
lursziwoplazaaleksanderkatanAleksander Katanreczkok
authored
feat: concurrent sum (for huge numbers) (#1495)
* concurrent sum module * compute shader blueprint * endeavour to run compute shader * introduced std log and std log2 * log now supports vectors * removed redundant files * log in vectorops * added tests * complete tests * updated to main * Fixes - compute shader runs * alg fixes * working presum * introduced concurrent-sum example * concurrent sum in math examples * finally somewhat parallel * Fixed exceeding workgroup size * One function to wrap the compute shader * packed whole concurrent-sum * Added quicker concurrentSum algorithm - shared memory * each thread copies two values into shared memory, instead of one * Found workGroupSize buffer * Introduced sums scan * Compute shader used for sums * export fixes * incrementSHader * Found copy bug * alg fix * Dynamic buffer length support * Now supporting optional buffer as output * no more consts * Divided into upPass and downPass * Refmt * work in progress * Fixed * reintroduced down pass * warning fixes * Fully working impl * example for testing * clean layouts * fully working recursive step * Dispatching multiple workgroups * supporting optional buffer * stash the cache * buffer cache * nits * performance inprovements * alternative cache * gpu time * refactor of types * 🦕 * basic support for any operation * concurrent sum supports all operators now * example tidy up * removed unnecessary flush * example improvements * types * Support for single element sum * example sanity changes' * filenames * 🦕 * fixes * 🦕 * 🦕 * flex example * lines * speed up isntead of time * some improvements * tailwind baby * 3 values * working example * pr fixes * html simplifiaction * other operators fix * typo * length is now chooseable * fixed on mobile * tgpu callable * profile pic mod * copilot * buttons cleanup * introduced calculate index * got rid of ugly casts * Major rename * Removed old concurrent sum example * 🦕 * typo * better cache * oppsies * html changes * height control * looking goood on mobile * 🦕 * dynamic y axis * rename * 🦕 * PR fixes 1 * fix * single element scan fix * 🦕 * proper private values * docs and tests * farewell buffer cache * 🦕 * Merge fixes * docs: Tests for prefix scan (#1804) * jsdocs example * implicit single scan conv * fix timestamp queries * 🦕🦕🦕 * tgpu bump 0.6 -> 0.8 * API option 4 * 🦕🦕🦕 * 🦕🦕🦕🦕 * tgpu version bump * PR fixes 1 * Code duplication with scan (only difference is true vs false), this body can be extracted to a helper function. * outputBuffer does not work in scan (there is always a new one-element buffer returned) * fix faulty test * ordering of lines * removed number cast * simplified array init * bars opacity in loop * readme change * .with update * better pipeline if * removed tgpu callable * uniformAdd -> uniformOp * fix, simplify, modernize (#2164) * simplify (#2169) * COMBINE (#2182) * add -> op * export computer for *advanced* usage I guess :P * two level cache * fixes * better wording imp --------- Co-authored-by: Iwo Plaza <iwoplaza@gmail.com> Co-authored-by: Aleksander Katan <56294622+aleksanderkatan@users.noreply.github.com> Co-authored-by: Aleksander Katan <aleksander.katan@swmansion.com> Co-authored-by: Konrad Reczko <reczkok@gmail.com> Co-authored-by: Konrad Reczko <66403540+reczkok@users.noreply.github.com>
1 parent 397a4fc commit b749bd9

24 files changed

Lines changed: 1491 additions & 1 deletion

File tree

apps/typegpu-docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@typegpu/color": "workspace:*",
3030
"@typegpu/geometry": "workspace:*",
3131
"@typegpu/noise": "workspace:*",
32+
"@typegpu/concurrent-scan": "workspace:*",
3233
"@typegpu/three": "workspace:*",
3334
"@typegpu/sdf": "workspace:*",
3435
"three": "catalog:example",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { initCache, prefixScan } from '@typegpu/concurrent-scan';
2+
import * as d from 'typegpu/data';
3+
import type { TgpuRoot } from 'typegpu';
4+
import * as std from 'typegpu/std';
5+
6+
type SumResult = {
7+
success: boolean;
8+
jsTime: number;
9+
gpuTime: number;
10+
gpuShaderTime: number;
11+
};
12+
13+
function prefixSumOnJS(arr: number[]) {
14+
for (let i = 1; i < arr.length; i++) {
15+
arr[i] += arr[i - 1];
16+
}
17+
// In Blelloch scan, the result starts with identity element
18+
arr.unshift(0);
19+
arr.pop();
20+
return arr;
21+
}
22+
23+
function arraysEqual(a: number[], b: number[]): boolean {
24+
if (a.length !== b.length) {
25+
return false;
26+
}
27+
for (let i = 0; i < a.length; i++) {
28+
if (a[i] !== b[i]) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
}
34+
35+
export async function performCalculationsWithTime(
36+
root: TgpuRoot,
37+
inputArray: number[],
38+
): Promise<SumResult> {
39+
const arraySize = inputArray.length;
40+
const inputBuffer = root
41+
.createBuffer(d.arrayOf(d.f32, arraySize))
42+
.$usage('storage');
43+
inputBuffer.write(inputArray);
44+
45+
// JS version
46+
const jsStartTime = performance.now();
47+
const jsResult = prefixSumOnJS(inputArray);
48+
const jsTime = performance.now() - jsStartTime;
49+
50+
// GPU version
51+
initCache(root, { operation: std.add, identityElement: 0 });
52+
const querySet = root.createQuerySet('timestamp', 2);
53+
const gpuStartTime = performance.now();
54+
const calcResult = prefixScan(
55+
root,
56+
{
57+
inputBuffer: inputBuffer,
58+
outputBuffer: inputBuffer,
59+
operation: std.add,
60+
identityElement: 0,
61+
},
62+
querySet,
63+
);
64+
querySet.resolve();
65+
await root.device.queue.onSubmittedWorkDone();
66+
const gpuTime = performance.now() - gpuStartTime;
67+
68+
const gpuResult = await calcResult.read();
69+
const timestamps = await querySet.read();
70+
const gpuShaderTime = Number(timestamps[1] - timestamps[0]) / 1_000_000;
71+
72+
return {
73+
success: arraysEqual(jsResult, gpuResult),
74+
jsTime,
75+
gpuTime,
76+
gpuShaderTime,
77+
};
78+
}

0 commit comments

Comments
 (0)