[Metal] GPUStartTime/GPUEndTime availability

Hi,


i'm trying to build a program that use Metal on MacOS 12.1 and 10.13 but I have an issue on 10.13.


GPUStartTime and GPUEndTime are defined available only with iOS, as defined in MTLCommandBuffer.h:


@property (readonly) CFTimeInterval GPUStartTime NS_AVAILABLE_IOS(10_3);

@property (readonly) CFTimeInterval GPUEndTime NS_AVAILABLE_IOS(10_3);


On MacOS 12.1 these defines was changed to:


@property (readonly) CFTimeInterval GPUStartTime API_AVAILABLE(macos(10.15), macCatalyst(13.0), ios(10.3));

@property (readonly) CFTimeInterval GPUEndTime API_AVAILABLE(macos(10.15), macCatalyst(13.0), ios(10.3));


The goal is not to use those two features on MacOS 10.13, but to find a solution to avoid not having the program compile without having to change the standard Metal headers that the default operating system provides.


I already tried with the following clang diagnostic pragmas, but nothing works


  #pragma clang diagnostic ignored "-Wunsupported-availability-guard"

  #pragma clang diagnostic ignored "-Wunguarded-availability"

  #pragma clang diagnostic ignored "-Wunguarded-availability-new"

  #pragma clang diagnostic ignored "-Wunavailable-declarations"

  #pragma clang diagnostic ignored "-Wavailability"

  #pragma clang diagnostic ignored "-Wpartial-availability"


I got the same error:


error: 'GPUStartTime' is unavailable: not available on macOS


How to workaround this limitation?


Thanks!

Gabriele

MacBook Pro 13″, macOS 10.13

Posted on Jan 9, 2022 11:55 AM

Reply

Similar questions

14 replies

Jan 13, 2022 9:31 PM in response to _matrix

got it!


      NSMethodSignature *signature = [object_getClass(commandBuffer) instanceMethodSignatureForSelector:@selector(GPUStartTime)];

     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

      [invocation setTarget:commandBuffer];

      [invocation setSelector:@selector(GPUStartTime)];

      [invocation invoke];

      [invocation getReturnValue:&myGPUStartTime];

Jan 13, 2022 11:53 AM in response to _matrix

It sounds like you may be doing something unusual with the SDK version or something. The normal way to handle this is to target the current SDK version and then just change the deployment version to whatever old version you want to support. In your code, you will have to avoid any references to newer symbols at runtime.


Here is documentation from Apple describing how to do this.


It can be tricky because new Xcode versions often have templates and sample code that will only work on the latest version and is not backwards compatible at all. Many of those availability macros are designed for building code that could potentially run on different OS versions but isn't actually helpful for writing your own backwards-compatible code.

Jan 13, 2022 2:23 PM in response to _matrix

You compile using the SDK version that you have. If your development machine is running 10.15, then you should be using the latest version of Xcode that runs on 10.15. According to this site, you should be running Xcode 12.4.


Then, you set the deployment target to 10.13. Your app will now run on anything from 10.13-12.1. It is your responsibility to make sure that any symbols you reference actually exist on 10.13. At this point, there is little risk of anything breaking on any OS versions in the foreseeable future.

Jan 13, 2022 8:54 AM in response to _matrix

Don't use those functions if you need 10.13 support.


However, there is very little value to maintaining 10.13 support in the first place. The only people still using 10.13 are people who have 10 year-old computers that 1) can no longer be updated and 2) are still running.


If you aren't directly calling those functions, then you may be using some 3rd party framework that does.

Jan 13, 2022 1:38 PM in response to etresoft

In my opinion the problem lies in the headers.


With OpenCL it is possible to use external headers, but with Metal the only thing Apple releases is metal-cpp.

https://developer.apple.com/metal/cpp/


I should do some tests to understand if it is actually possible to use these headers instead of those present by default in the operating system.


Beyond that it would be useful to understand if there are license restrictions, so if for example it is possible to use all this in an open-source software with an MIT license.


Jan 13, 2022 2:02 PM in response to _matrix

_matrix wrote:

In my opinion the problem lies in the headers.

The headers are fine. Make sure you aren't doing anything unusual. Download one of the Metal sample apps from Apple and build it. You don't have to do anything fancy. Just accept all defaults.


Then, if you want the sample app to run on 10.13, just change the deployment and test it on 10.13. I don't know if the compiler is smart enough to check backwards compatibility with the deployment target. This might be something that Swift can do, but not Objective-C.

With OpenCL it is possible to use external headers, but with Metal the only thing Apple releases is metal-cpp.
https://developer.apple.com/metal/cpp/

No. That's something different. Your problem here is with the Metal framework itself. I'm still not sure what you are doing wrong. But those GPUStartTime/GPUEndTime properties are just regular Objective-C/Swift properties. While they are part of the Metal framework, they aren't themselves "Metal".


What you linked to with metal-cpp is the Metal shader language. That is the language that your MTLCommandBuffer is going to ultimately execute on the GPU.

I should do some tests to understand if it is actually possible to use these headers instead of those present by default in the operating system.

You are going in the wrong direction here. You don't want to try additional funky things that aren't going to work. Just use the defaults. They are guaranteed to work. The only reason you are having trouble now is because you are trying to do something funky.

Beyond that it would be useful to understand if there are license restrictions, so if for example it is possible to use all this in an open-source software with an MIT license.

There are no license restrictions on your own code. Anything you release that uses Metal would, or course, require the appropriate Metal support. An MIT license would be fine. However, the "free software" folks might not like it if you have dependencies on non-"free" APIs like Metal. But that is strictly a social thing. They will say mean things about you on Twitter and some IRC protocol from the 1980s. There are no legal restrictions.

Jan 13, 2022 9:23 PM in response to _matrix

I think i'm missing something here


with NSInvocation:

invocation myGPUStartTime: 0.000000


    if ([commandBuffer respondsToSelector:@selector(GPUStartTime)])

    {

     CFTimeInterval myGPUStartTime = 0;

     NSMethodSignature *signature = [object_getClass(commandBuffer) instanceMethodSignatureForSelector:@selector(GPUStartTime)];

     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

      [invocation invoke];

      [invocation getReturnValue:&myGPUStartTime];

     printf("invocation myGPUStartTime: %f\n", myGPUStartTime);

    }



without:

myGPUStartTime: 424140.214153


    CFTimeInterval myGPUStartTime = [commandBuffer GPUStartTime];

    printf("myGPUStartTime: %f\n", myGPUStartTime);



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

[Metal] GPUStartTime/GPUEndTime availability

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.