-
Notifications
You must be signed in to change notification settings - Fork 11
/
saxpy_metal_objc.mm
89 lines (67 loc) · 2.5 KB
/
saxpy_metal_objc.mm
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
#import "saxpy_metal_objc.h"
struct saxpy_constants
{
uint num_elements;
};
@implementation SaxpyMetalObjC
{
id<MTLComputePipelineState> _mPSO;
id<MTLBuffer> _mX;
id<MTLBuffer> _mY;
id<MTLBuffer> _ma;
id<MTLBuffer> _mConst;
uint _mNumElements;
uint _mNumThreadsPerGroup;
uint _mNumGroupsPerGrid;
}
@synthesize scalar_a ;
- (instancetype) initWithNumElements:(size_t) numElements
NumThreadsPerGroup:(size_t) numThreadsPerGroup
NumGroupsPerGrid:(size_t) numGroupsPerGrid
{
self = [super init];
if (self)
{
_mNumElements = numElements;
_mNumThreadsPerGroup = numThreadsPerGroup;
_mNumGroupsPerGrid = numGroupsPerGrid;
[ self loadLibraryWithName:@"./saxpy.metallib" ];
_mPSO = [ self getPipelineStateForFunction:@"saxpy" ];
_mX = [ self getSharedMTLBufferForBytes: _mNumElements * sizeof(float) for:@"_mX" ];
_mY = [ self getSharedMTLBufferForBytes: _mNumElements * sizeof(float) for:@"_mY" ];
_ma = [ self getSharedMTLBufferForBytes: sizeof(float) for:@"_ma" ];
_mConst = [ self getSharedMTLBufferForBytes: sizeof(struct saxpy_constants) for:@"_mConst" ];
struct saxpy_constants c;
memset( &c, (int)0, sizeof(struct saxpy_constants) );
c.num_elements = _mNumElements;
memcpy( _mConst.contents, &c, sizeof(struct saxpy_constants) );
}
return self;
}
-(float*) getRawPointerX
{
return (float*)_mX.contents;
}
-(float*) getRawPointerY
{
return (float*)_mY.contents;
}
-(void) performComputation
{
id<MTLCommandBuffer> commandBuffer = [ self.commandQueue commandBuffer ];
assert( commandBuffer != nil );
id<MTLComputeCommandEncoder> computeEncoder = [ commandBuffer computeCommandEncoder ];
assert( computeEncoder != nil );
[ computeEncoder setComputePipelineState:_mPSO ];
[ computeEncoder setBuffer:_mX offset:0 atIndex:0 ];
[ computeEncoder setBuffer:_mY offset:0 atIndex:1 ];
((float*)_ma.contents)[0] = self.scalar_a;
[ computeEncoder setBuffer:_ma offset:0 atIndex:2 ];
[ computeEncoder setBuffer:_mConst offset:0 atIndex:3 ];
[computeEncoder dispatchThreadgroups:MTLSizeMake( _mNumGroupsPerGrid, 1, 1 )
threadsPerThreadgroup:MTLSizeMake( _mNumThreadsPerGroup, 1, 1 ) ];
[computeEncoder endEncoding];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];
}
@end