Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max threads checking for Metal #5588

Merged
merged 5 commits into from
Jan 8, 2021
Merged

Conversation

xndcn
Copy link
Contributor

@xndcn xndcn commented Dec 22, 2020

Originally, this checking will be asserted by Metal API Validation
in Xcode, otherwise the program will crash or output wrong results.

@xndcn
Copy link
Contributor Author

xndcn commented Dec 22, 2020

Xcode will do some checking like this:
metal.png

@steven-johnson
Copy link
Contributor

Please look at (and fix) the clang-tidy errors.

Originally, this checking will be asserted by Metal API Validation
in Xcode, otherwise the program will crash or output wrong results.
@xndcn
Copy link
Contributor Author

xndcn commented Dec 22, 2020

Please look at (and fix) the clang-tidy errors.

Thanks, fixed.

f.realize(output);
output.copy_to_host();

for (int32_t i = 0; i < output.width(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this loop necessary if we expect an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary indeed. Leave it just for code integrity...

@shoaibkamil
Copy link
Contributor

shoaibkamil commented Dec 22, 2020

Thanks for the PR! In principle, I'm not opposed to adding this check, though we don't do similar checks on any other backend. It might be better to only do the check in a debug target.

I'd like to move towards a standard set of checks we do in non-debug and debug targets across all the GPU runtimes. Curious to hear what @zvookin thinks.

Var x("x"), y("y");

f(x, y) = im(x, y) + 42;
f.gpu_blocks(y).gpu_threads(x, DeviceAPI::Metal);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the machine doesn't support Metal? I think this needs a check for whether the JIT target supports Metal, otherwise it should be skipped.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, even if the machine supports metal, we should skip if the target doesn't support it, eg

    if (!get_jit_target_from_environment().has_feature(Target::Metal)) {
        printf("[SKIP] error/metal_threads_too_large ignored for targets without Metal enabled.\n");
        _halide_user_assert(0);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, since now it only checks in debug runtime, so I add a specific target, like "metal_vector_too_large" did.

@xndcn
Copy link
Contributor Author

xndcn commented Dec 23, 2020

@shoaibkamil Thanks, it's reasonable to only do this check in debug runtime, so I have moved it.

For Cuda and OpenCL backends, they seem to do this checking in library. "cuLuanchKernel" and "clEnqueueNDRangeKernel" both have a return value to indicate this error: "cudaErrorLaunchOutOfResources" and "CL_INVALID_WORK_ITEM_SIZE".

For OpenGL Compute backend, after "glDispatchCompute" is called, "GL_INVALID_VALUE" is generated to indicate this error, too.

For D3D12Compute backend, since the threadgroups are encoded in the shader source, "D3DCompile" will failed when it exceeds the limit.

So it seems the Metal backend is an exception. Not sure what behavior the Vulkan is.

@alexreinking alexreinking added this to the v12.0.0 milestone Jan 2, 2021
Copy link
Contributor

@steven-johnson steven-johnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clone is green, LGTM

@steven-johnson
Copy link
Contributor

@shoaibkamil Do you also want to review before this lands?

@steven-johnson steven-johnson merged commit 2b3aaa8 into halide:master Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants