Neko
1.99.3
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
bc_utils.h
Go to the documentation of this file.
1
/*
2
Copyright (c) 2025, The Neko Authors
3
All rights reserved.
4
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions
7
are met:
8
9
* Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
11
12
* Redistributions in binary form must reproduce the above
13
copyright notice, this list of conditions and the following
14
disclaimer in the documentation and/or other materials provided
15
with the distribution.
16
17
* Neither the name of the authors nor the names of its
18
contributors may be used to endorse or promote products derived
19
from this software without specific prior written permission.
20
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33
*/
34
35
#ifndef __BC_METAL_UTILS_H__
36
#define __BC_METAL_UTILS_H__
37
38
#ifdef __APPLE__
39
40
#import <Metal/Metal.h>
41
42
extern
id<MTLDevice>
neko_metal_device
(
void
);
43
extern
id<MTLLibrary>
neko_metal_library
(
void
);
44
extern
void
*
glb_cmd_queue
;
45
46
/* ------------------------------------------------------------------ */
47
/* Pipeline state cache (shared across all BC files) */
48
/* ------------------------------------------------------------------ */
49
50
static
NSMutableDictionary<NSString *, id<MTLComputePipelineState>
> *
bc_pipelines
=
nil
;
51
52
static
inline
id<MTLComputePipelineState>
bc_get_pipeline
(
NSString
*name) {
53
if
(!
bc_pipelines
) {
54
bc_pipelines
= [
NSMutableDictionary
new
];
55
}
56
id<MTLComputePipelineState>
pso
=
bc_pipelines
[name];
57
if
(!
pso
) {
58
id<MTLDevice>
dev
=
neko_metal_device
();
59
NSError
*
err
=
nil
;
60
id<MTLLibrary>
lib
=
neko_metal_library
();
61
id<MTLFunction>
fn
= [
lib
newFunctionWithName
:name];
62
if
(!
fn
) {
63
NSLog
(
@"Metal BC: kernel '%@' not found in library"
, name);
64
abort
();
65
}
66
pso
= [
dev
newComputePipelineStateWithFunction
:
fn
error:&
err
];
67
if
(
err
) {
68
NSLog
(
@"Metal BC: pipeline error for '%@': %@"
, name,
err
);
69
abort
();
70
}
71
bc_pipelines
[name] =
pso
;
72
}
73
return
pso
;
74
}
75
76
/* ------------------------------------------------------------------ */
77
/* Dispatch helper */
78
/* ------------------------------------------------------------------ */
79
80
#define METAL_BC_NTHREADS 1024
81
82
static
inline
void
bc_dispatch_1d
(
id<MTLCommandQueue>
queue
,
83
id<MTLComputePipelineState>
pso
,
84
void
(^
encode
)(
id<MTLComputeCommandEncoder>
),
85
NSUInteger
n) {
86
if
(n == 0)
return
;
87
@
autoreleasepool
{
88
id<MTLCommandBuffer>
cb
= [
queue
commandBuffer
];
89
id<MTLComputeCommandEncoder>
enc
= [
cb
computeCommandEncoder
];
90
[
enc
setComputePipelineState
:
pso
];
91
encode
(
enc
);
92
MTLSize
threads
=
MTLSizeMake
(n, 1, 1);
93
NSUInteger
tpg
= n <
METAL_BC_NTHREADS
? n :
METAL_BC_NTHREADS
;
94
MTLSize
tg
=
MTLSizeMake
(
tpg
, 1, 1);
95
[
enc
dispatchThreads
:
threads
threadsPerThreadgroup
:
tg
];
96
[
enc
endEncoding
];
97
[
cb
commit
];
98
[
cb
waitUntilCompleted
];
99
}
100
}
101
102
#endif
/* __APPLE__ */
103
#endif
/* __BC_METAL_UTILS_H__ */
dirichlet_apply_scalar_kernel
__global__ void dirichlet_apply_scalar_kernel(const int *__restrict__ msk, T *__restrict__ x, const T g, const int m)
Definition
dirichlet_kernel.h:42
device::glb_cmd_queue
type(c_ptr), bind(C), public glb_cmd_queue
Global command queue.
Definition
device.F90:52
src
bc
bcknd
device
metal
bc_utils.h
Generated on Tue Jun 16 2026 05:17:17 for Neko by
1.9.8