Skip to content

Commit bea9a3b

Browse files
Add profiler/metal to profile gpu on macos
1 parent 01cbcf9 commit bea9a3b

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

examples/matmul/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ FLAGS=-std=c++17 $(STDLIB) -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCP
1515
run: ./build/$(TARGET)
1616
$(LIBSPEC) && ./build/$(TARGET)
1717

18+
run_with_profile: ./build/$(TARGET)_with_profile
19+
$(LIBSPEC) && export METAL_CAPTURE_ENABLED=1 && ./build/$(TARGET)_with_profile
20+
1821
# Use clang -v to see the include paths
1922
# Note in this example optimization is turned on
2023
build/$(TARGET): run.cpp
2124
mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET)
2225

26+
build/$(TARGET)_with_profile: run.cpp
27+
mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET)_with_profile $(GPUCPP)/experimental/profiler/metal.mm -framework metal -framework Foundation -DMETAL_PROFILER
28+
2329
watch:
2430
@command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; }
2531
mkdir -p build && $(CODEPATH) | entr -s "$(LIBSPEC) && rm -f ./build/$(TARGET) && make -j$(NUM_JOBS) ./build/$(TARGET) && ./build/$(TARGET)"

examples/matmul/run.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "experimental/wgsl.h" // loopUnrolling
1414
#include "numeric_types/half.hpp"
1515

16+
#ifdef METAL_PROFILER
17+
#include "experimental/profiler/metal.hpp"
18+
#endif
19+
1620
using namespace gpu;
1721

1822
const std::string versionToStr(int version);
@@ -799,7 +803,11 @@ void runTest(int version, size_t M, size_t K, size_t N,
799803
Tensor input = createTensor(ctx, Shape{M, K}, numtype, inputPtr.get());
800804
Tensor weights = createTensor(ctx, Shape{N, K}, numtype, weightsPtr.get()); // column-major
801805

806+
#ifdef METAL_PROFILER
807+
constexpr size_t nIter = 1;
808+
#else
802809
constexpr size_t nIter = 30;
810+
#endif
803811

804812
// Initialize Kernel and bind GPU buffers
805813

@@ -815,8 +823,10 @@ void runTest(int version, size_t M, size_t K, size_t N,
815823
kernels[i] = selectMatmul(ctx, version, {input, weights, outputs[i]}, M, K, N, numtype);
816824
}
817825

826+
#ifndef METAL_PROFILER
818827
printf("[ Press enter to start tests ... ]\n");
819828
getchar();
829+
#endif
820830
LOG(kDefLog, kInfo, "Dispatching Kernel version %d: %s, %d iterations ...",
821831
version, versionToStr(version).c_str(), nIter);
822832

@@ -930,11 +940,17 @@ int main() {
930940
N = 2 * 4096;
931941
}
932942

943+
#ifdef METAL_PROFILER
944+
startCapture();
945+
#endif
933946
if (enableF16) {
934947
runTestWithCheck<half>(version, M, K, N, transposedInput, kTestSize, numtype);
935948
} else {
936949
runTestWithCheck<float>(version, M, K, N, transposedInput, kTestSize, numtype);
937950
}
951+
#ifdef METAL_PROFILER
952+
stopCapture();
953+
#endif
938954

939955
LOG(kDefLog, kInfo, "Done.");
940956
return 0;

experimental/profiler/metal.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifdef __APPLE__
2+
extern "C" {
3+
void startCapture();
4+
void stopCapture();
5+
}
6+
#endif

experimental/profiler/metal.mm

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#import <Foundation/Foundation.h>
2+
#import <Metal/Metal.h>
3+
#import <QuartzCore/CAMetalLayer.h>
4+
5+
6+
extern "C" {
7+
void startCapture() {
8+
if (![[NSProcessInfo processInfo].environment[@"METAL_CAPTURE_ENABLED"] boolValue]) {
9+
NSLog(@"METAL_CAPTURE_ENABLED is not set. Please set it to 1 to enable Metal capture.");
10+
return;
11+
}
12+
13+
MTLCaptureDescriptor *descriptor = [[MTLCaptureDescriptor alloc] init];
14+
descriptor.destination = MTLCaptureDestinationGPUTraceDocument;
15+
descriptor.outputURL = [NSURL fileURLWithPath:@"gpu.cpp.gputrace"];
16+
17+
NSFileManager *fileManager = [NSFileManager defaultManager];
18+
if ([fileManager fileExistsAtPath:@"gpu.cpp.gputrace"]) {
19+
NSError *error = nil;
20+
[fileManager removeItemAtPath:@"gpu.cpp.gputrace" error:&error];
21+
if (error) {
22+
NSLog(@"Error deleting existing gpu.cpp.gputrace directory: %@", error);
23+
return;
24+
} else {
25+
NSLog(@"Deleted existing gpu.cpp.gputrace directory.");
26+
}
27+
}
28+
29+
NSError *error = nil;
30+
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
31+
if (!device) {
32+
NSLog(@"MTLCreateSystemDefaultDevice returned nil. Metal may not be supported on this system.");
33+
return;
34+
}
35+
descriptor.captureObject = device;
36+
37+
BOOL success = [MTLCaptureManager.sharedCaptureManager startCaptureWithDescriptor:descriptor error:&error];
38+
if (!success) {
39+
NSLog(@" error capturing mtl => %@ ", [error localizedDescription] );
40+
}
41+
}
42+
43+
void stopCapture() {
44+
[MTLCaptureManager.sharedCaptureManager stopCapture];
45+
}
46+
}

0 commit comments

Comments
 (0)