summaryrefslogtreecommitdiff
path: root/default.nix
blob: fc8cc982f401fd7b3f063b249ce137b9b273ece4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
{ pkgs ? (import <nixpkgs> {})
, llvmPackages ? pkgs.llvmPackages_20
, lld ? pkgs.lld_20
, stdenv ? llvmPackages.stdenv
, clang-tools ? llvmPackages.clang-tools
# Build AdaptiveCpp with the ROCm backend. Off by default so the usual
# host/OpenMP path stays cheap to build; turn on with
#   nix-build -A <attr> --arg rocm true
, rocm ? false
# Same for the CUDA backend:
#   nix-build -A <attr> --arg cuda true
# Note that nixpkgs ships the CUDA toolkit as unfree, so this additionally
# needs allowUnfree, e.g.
#   nix-build -A kel.lbm.sycl-cuda --arg cuda true \\
#     --arg pkgs '(import <nixpkgs> { config.allowUnfree = true; })'
, cuda ? false
}:

let
	glad_opengl_33_core = stdenv.mkDerivation {
		pname = "glad_opengl_33_core";
		version = "0.0.1";

		dontUnpack = true;

		nativeBuildInputs = [
			pkgs.python312Packages.glad2
		];

		buildPhase = ''
			mkdir -p glad_out
			glad \
			--api gl:core=3.3 \
			--out-path glad_out \
			--reproducible \
			c
			mkdir -p glad_out/lib
			cc -Iglad_out/include -fPIC -shared -o glad_out/lib/libgl.so glad_out/src/gl.c
		'';

		installPhase = ''
			mv glad_out $out
		'';
	};

	# One builder, three instances, so the GPU toolchains differ from the base
	# one in exactly one input and nothing else.
	mkAdaptiveCpp = backend: pkgs.callPackage .nix/adaptive-cpp.nix {
		inherit stdenv llvmPackages lld;

		config = {
			rocm = backend == "rocm";
			cuda = backend == "cuda";
		};
	};

	adaptive-cpp-base = mkAdaptiveCpp "host";
	adaptive-cpp-rocm = mkAdaptiveCpp "rocm";
	adaptive-cpp-cuda = mkAdaptiveCpp "cuda";

	# What the examples and the default build use. Flip with --arg rocm/cuda true.
	adaptive-cpp =
		if rocm then adaptive-cpp-rocm
		else if cuda then adaptive-cpp-cuda
		else adaptive-cpp-base;

	# forstio master as of 2026-11-09. Bump together with the sha256 below.
	forstioRev = "97ae1a8f23074fea3784a008921c5d9fc3c1e8fb";

	forstio = let
		forstioSrc = stdenv.mkDerivation {
			name = "forstio-src";

		 # Pinned to a commit, not to master. A branch snapshot is a moving
		 # target: every push to forstio regenerates the tarball and the sha256
		 # below stops matching, so the build breaks with a hash mismatch for
		 # reasons that have nothing to do with this repo.
		 #
		 # To bump: set forstioRev, then run
		 #   nix-prefetch-url --type sha256 \
		 #     https://git.keldu.de/forstio-forstio/snapshot/<rev>.tar.gz
		 src = builtins.fetchurl {
			url = "https://git.keldu.de/forstio-forstio/snapshot/${forstioRev}.tar.gz";
			sha256 = "sha256:0rmss3d83ryb24s8nirjrnxz32irws3hzwqkkxhk4xcf3rn8p8ls";
		 };

		 phases = [ "unpackPhase" "installPhase" ];

		 unpackPhase = ''
			mkdir source
			tar -xzf "$src" -C source --strip-components=1
		 '';

		 installPhase = ''
			cp -r source $out
		 '';
	  };
	in
	(import "${forstioSrc}/default.nix" {
		inherit stdenv clang-tools adaptive-cpp;
	}).forstio;

	pname = "kel-lbm";
	version = "0.0.5";
in rec {
	kel = {
		lbm = {
			core = pkgs.callPackage ./modules/core/.nix/derivation.nix {
				inherit forstio stdenv clang-tools pname version;
			};
			# Both toolchains, same sources, same everything else -- so a failure
			# in one and not the other points at the backend rather than the code.
			sycl = pkgs.callPackage ./modules/sycl/.nix/derivation.nix {
				inherit forstio stdenv clang-tools pname version kel;
				adaptive-cpp = adaptive-cpp-base;
			};
			sycl-rocm = pkgs.callPackage ./modules/sycl/.nix/derivation.nix {
				inherit forstio stdenv clang-tools pname version kel;
				adaptive-cpp = adaptive-cpp-rocm;
				variant = "-rocm";
			};
			sycl-cuda = pkgs.callPackage ./modules/sycl/.nix/derivation.nix {
				inherit forstio stdenv clang-tools pname version kel;
				adaptive-cpp = adaptive-cpp-cuda;
				variant = "-cuda";
			};

			# Control protocol shared by the orchestrator and the workers.
			# Schemas and arithmetic only -- no transport, no GL -- so it stays
			# testable natively on both sides of the browser boundary.
			swarm = pkgs.callPackage ./modules/swarm/.nix/derivation.nix {
				inherit forstio stdenv clang-tools pname version kel;
			};

			# Browser backend. Built by em++ rather than the nix cc-wrapper, which
			# is why it takes its include roots through sconsFlags instead of
			# buildInputs.
			webgl = pkgs.callPackage ./modules/webgl/.nix/derivation.nix {
				inherit stdenv clang-tools pname version kel;
			};
		};
	};

	# Proof of concept work. Reachable from the top level so it actually gets
	# built; it was previously orphaned and had drifted into not evaluating.
	poc = import ./poc/default.nix {
		inherit pkgs stdenv pname version kel;
	};

	util = {
		lbm_ogl_renderer = pkgs.callPackage ./util/ogl_renderer/.nix/derivation.nix {
			inherit forstio stdenv clang-tools pname version kel;
			kel-glad = glad_opengl_33_core;
		};
	};

	ex = {
		cavity_2d_gpu = pkgs.callPackage ./examples/cavity_2d_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_2d_gpu = pkgs.callPackage ./examples/poiseulle_2d_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_3d_gpu = pkgs.callPackage ./examples/poiseulle_3d_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		# Same case on a 32x16x16 domain, so it runs end to end in seconds.
		poiseulle_3d_gpu_minimal = pkgs.callPackage ./examples/poiseulle_3d_gpu_minimal/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		# 32^3 channel carrying one HLBM-coupled sphere. The first 3D case with
		# a particle at all, and the stepping stone to the fixed-axis rotor.
		poiseulle_particle_3d_hlbm_gpu = pkgs.callPackage ./examples/poiseulle_particle_3d_hlbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		# Fixed-position rotor, free to spin about one arbitrary fixed axis and
		# driven by the fluid. Angular velocity is an output.
		vawt_3d_hlbm_gpu = pkgs.callPackage ./examples/vawt_3d_hlbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		moving_poiseulle_particles_2d_hlbm_gpu = pkgs.callPackage ./examples/moving_poiseulle_particles_2d_hlbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		moving_poiseulle_particles_2d_fplbm_gpu = pkgs.callPackage ./examples/moving_poiseulle_particles_2d_fplbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		moving_poiseulle_particles_2d_psm_gpu = pkgs.callPackage ./examples/moving_poiseulle_particles_2d_psm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_particles_2d_psm_gpu = pkgs.callPackage ./examples/poiseulle_particles_2d_psm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_particles_2d_hlbm_gpu = pkgs.callPackage ./examples/poiseulle_particles_2d_hlbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_particles_2d_bgk_gpu = pkgs.callPackage ./examples/poiseulle_particles_2d_bgk_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_particles_2d_ibm_gpu = pkgs.callPackage ./examples/poiseulle_particles_2d_ibm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		poiseulle_particles_2d_fplbm_gpu = pkgs.callPackage ./examples/poiseulle_particles_2d_fplbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		poiseulle_particles_2d_all_gpu = pkgs.symlinkJoin {
			name = "poiseulle_particles_2d_all_gpu";
			paths = [
				ex.poiseulle_particles_2d_bgk_gpu
				ex.poiseulle_particles_2d_psm_gpu
				ex.poiseulle_particles_2d_hlbm_gpu
				ex.poiseulle_particles_2d_fplbm_gpu
				ex.poiseulle_particles_2d_ibm_gpu
			];
		};
		
		poiseulle_particles_2d_gpu = pkgs.callPackage ./examples/poiseulle_particles_2d_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		stokes_drag_particle_2d_bgk_gpu = pkgs.callPackage ./examples/stokes_drag_particle_2d_bgk_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		stokes_drag_particle_2d_psm_gpu = pkgs.callPackage ./examples/stokes_drag_particle_2d_psm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		stokes_drag_particle_2d_fplbm_gpu = pkgs.callPackage ./examples/stokes_drag_particle_2d_fplbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		stokes_drag_particle_2d_hlbm_gpu = pkgs.callPackage ./examples/stokes_drag_particle_2d_hlbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		poiseulle_3d = pkgs.callPackage ./examples/poiseulle_3d/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		settling_cubes_2d_ibm_gpu = pkgs.callPackage ./examples/settling_cubes_2d_ibm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		settling_cubes_2d_hlbm_gpu = pkgs.callPackage ./examples/settling_spheres_2d_hlbm_gpu/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		stdkr_2d = pkgs.callPackage ./examples/schaefer_turek_durst_krause_rannbacher/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		stdkr_2d_bgk = pkgs.callPackage ./examples/stdkr_2d_bgk/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		ops_2d = pkgs.callPackage ./examples/one_particle_sedimentation_2d/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};
		
		throat_2d = pkgs.callPackage ./examples/throat_clog_2d/.nix/derivation.nix {
			inherit pname version stdenv forstio adaptive-cpp;
			inherit kel;
		};

		settling_particles = pkgs.symlinkJoin {
			name = "kel-lbm-settling_particles-${version}";
			paths = [
				ex.settling_cubes_2d_ibm_gpu
			];
		};
	};

	debug = {
		inherit adaptive-cpp;
	};

	release = {
		dev = {
			inherit kel;
		};

		ex = pkgs.symlinkJoin {
			name = "kel-lbm-sims-${version}";
			paths = [
				kel.lbm.core
				kel.lbm.sycl
				ex.poiseulle_particles_2d_bgk_gpu
				ex.poiseulle_particles_2d_psm_gpu
				ex.poiseulle_particles_2d_hlbm_gpu
				ex.poiseulle_particles_2d_fplbm_gpu
				ex.poiseulle_particles_2d_ibm_gpu
			];
		};

		poiseulle_particles_2d = pkgs.symlinkJoin {
			name = "kel-lbm-sims-poiseulle_particles_2d-${version}";
			paths = [
				ex.poiseulle_particles_2d_bgk_gpu
				ex.poiseulle_particles_2d_psm_gpu
				ex.poiseulle_particles_2d_hlbm_gpu
				ex.poiseulle_particles_2d_fplbm_gpu
			];
		};
		stokes_drag_2d = pkgs.symlinkJoin {
			name = "kel-lbm-sims-stokes_drag_2d-${version}";
			paths = [
				ex.stokes_drag_particle_2d_bgk_gpu
				ex.stokes_drag_particle_2d_psm_gpu
				ex.stokes_drag_particle_2d_hlbm_gpu
				ex.stokes_drag_particle_2d_fplbm_gpu
			];
		};

		ecmor = pkgs.symlinkJoin {
			name = "kel_ecmor";
			paths = [
				release.stokes_drag_2d
				release.poiseulle_particles_2d
			];
		};
	};

	default = release.ex;
}