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

mesa: restore OpenCL support. #82729

Closed
wants to merge 3 commits into from
Closed

mesa: restore OpenCL support. #82729

wants to merge 3 commits into from

Conversation

athas
Copy link
Contributor

@athas athas commented Mar 16, 2020

Motivation for this change

This is a first shot at addressing #62933. In practice, OpenCL support should probably be hidden behind an option, because it takes up a significant chunk of space (requires a full LLVM at run-time).

Currently I put all the OpenCL stuff in the drivers output, because at least mesa.icd needs to be available via /run/opengl-driver.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

Copy link
Member

@killercup killercup left a comment

Choose a reason for hiding this comment

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

If someone can tell me how to best test this, I'll give it a go!

Edit: Also cmd+enter triggering the "approve" button is not the wisest choice, github

pkgs/development/libraries/mesa/default.nix Show resolved Hide resolved
@rnhmjoj
Copy link
Contributor

rnhmjoj commented Mar 17, 2020

@athas As you said, this should be behind a guard like enableOpenCL.

I would suggest to create a package mesa-opencl = mesa.override { enableOpenCL = true; } or maybe an output like mesa.opencl, if possible.
Then just adding it to opengl.extraPackages should install the ICD and the driver.

If someone can tell me how to best test this, I'll give it a go!

If you build mesa from you nixpkgs checkout, you can install it in you system by using the (internal) opengl.package option. Alternatively, add it to opengl.extraPackages if you only want to test openCL.

To test wheter the openCL driver can be loaded you can use opengl-info.
I have a small program that uses an OpenCL kernel to compute the Hurst exponent laying around, I could give you that but I'm sure there are better tests.

@athas
Copy link
Contributor Author

athas commented Mar 17, 2020

I had trouble using a mesa.opencl output, because I got circular dependencies between the drivers and opencl output. I think libOpenCL.so (which is big) has to live in drivers, because that is where mesa.icd must be in order to be accessible via /run/opengl-driver/etc/OpenCL/mesa.icd. At least, addressing this is beyond my current skill set.

For the same reason, it's also a little tricky to debug, since the ICD loader only seems to pick up stuff in /run/opengl-driver/etc/OpenCL.

@killercup
Copy link
Member

you can install it in you system by using the (internal) opengl.package option. Alternatively, add it to opengl.extraPackages if you only want to test openCL.

Thanks for the tips, @rnhmjoj! This works for me with a 5700XT:

{
hardware.opengl = let
    opencl_pr = import (builtins.fetchTarball {
        name = "opencl_pr";
        url = "https://github.com/athas/nixpkgs/archive/f92a2a9b69eba9909d25ffaab6ded4d6f0f4efad.tar.gz";
    }) { };
in {
    enable = true;
    driSupport32Bit = true;
    package = opencl_pr.mesa.drivers;
    extraPackages = [ opencl_pr.mesa ];
};
}

clinfo output looks good, but it also contains

=== CL_PROGRAM_BUILD_LOG ===
fatal error: cannot open file '/nix/store/l81x3yfwahahhxmfk4wbg2ki025v451p-libclc-2019-06-09/lib/clc/gfx1010-amdgcn-mesa-mesa3d.bc': No such file or directory
  Preferred work group size multiple              <getWGsizes:1200: create kernel : error -46>

Not sure if I should worry about that.

@athas
Copy link
Contributor Author

athas commented Mar 17, 2020

I think the libclc is just too old for that GPU. I'll make a PR about bumping libclc.

@athas
Copy link
Contributor Author

athas commented Mar 17, 2020

Oh, apparently Mesa OpenCL doesn't support Navi cards at all...

@killercup
Copy link
Member

Oh, that's too bad! This is what clinfo tell me now, but I don't really have a clue what everything means.

$ clinfo                                                                                                                                                                                                                                                   
Number of platforms                               1
  Platform Name                                   Clover
  Platform Vendor                                 Mesa
  Platform Version                                OpenCL 1.1 Mesa 19.3.3
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_icd
  Platform Extensions function suffix             MESA

  Platform Name                                   Clover
Number of devices                                 1
  Device Name                                     AMD NAVI10 (DRM 3.36.0, 5.5.9, LLVM 9.0.1)
  Device Vendor                                   AMD
  Device Vendor ID                                0x1002
  Device Version                                  OpenCL 1.1 Mesa 19.3.3
  Driver Version                                  19.3.3
  Device OpenCL C Version                         OpenCL C 1.1 
  Device Type                                     GPU
  Device Profile                                  FULL_PROFILE
  Device Available                                Yes
  Compiler Available                              Yes
  Max compute units                               40
  Max clock frequency                             2100MHz
  Max work item dimensions                        3
  Max work item sizes                             256x256x256
  Max work group size                             256
=== CL_PROGRAM_BUILD_LOG ===
fatal error: cannot open file '/nix/store/l81x3yfwahahhxmfk4wbg2ki025v451p-libclc-2019-06-09/lib/clc/gfx1010-amdgcn-mesa-mesa3d.bc': No such file or directory
  Preferred work group size multiple              <getWGsizes:1200: create kernel : error -46>
  Preferred / native vector sizes                 
    char                                                16 / 16      
    short                                                8 / 8       
    int                                                  4 / 4       
    long                                                 2 / 2       
    half                                                 8 / 8        (cl_khr_fp16)
    float                                                4 / 4       
    double                                               2 / 2        (cl_khr_fp64)
  Half-precision Floating-point support           (cl_khr_fp16)
    Denormals                                     No
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 No
    Round to infinity                             No
    IEEE754-2008 fused multiply-add               No
    Support is emulated in software               No
  Single-precision Floating-point support         (core)
    Denormals                                     No
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 No
    Round to infinity                             No
    IEEE754-2008 fused multiply-add               No
    Support is emulated in software               No
    Correctly-rounded divide and sqrt operations  No
  Double-precision Floating-point support         (cl_khr_fp64)
    Denormals                                     Yes
    Infinity and NANs                             Yes
    Round to nearest                              Yes
    Round to zero                                 Yes
    Round to infinity                             Yes
    IEEE754-2008 fused multiply-add               Yes
    Support is emulated in software               No
  Address bits                                    64, Little-Endian
  Global memory size                              8589934592 (8GiB)
  Error Correction support                        No
  Max memory allocation                           6871947673 (6.4GiB)
  Unified memory for Host and Device              No
  Minimum alignment for any data type             128 bytes
  Alignment of base address                       32768 bits (4096 bytes)
  Global Memory cache type                        None
  Image support                                   No
  Local memory type                               Local
  Local memory size                               32768 (32KiB)
  Max number of constant args                     16
  Max constant buffer size                        2147483647 (2GiB)
  Max size of kernel argument                     1024
  Queue properties                                
    Out-of-order execution                        No
    Profiling                                     Yes
  Profiling timer resolution                      0ns
  Execution capabilities                          
    Run OpenCL kernels                            Yes
    Run native kernels                            No
  Device Extensions                               cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_fp64 cl_khr_fp16

NULL platform behavior
  clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...)  Clover
  clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...)   Success [MESA]
  clCreateContext(NULL, ...) [default]            Success [MESA]
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT)  Success (1)
    Platform Name                                 Clover
    Device Name                                   AMD NAVI10 (DRM 3.36.0, 5.5.9, LLVM 9.0.1)
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU)  No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU)  Success (1)
    Platform Name                                 Clover
    Device Name                                   AMD NAVI10 (DRM 3.36.0, 5.5.9, LLVM 9.0.1)
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR)  No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM)  No devices found in platform
  clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL)  Success (1)
    Platform Name                                 Clover
    Device Name                                   AMD NAVI10 (DRM 3.36.0, 5.5.9, LLVM 9.0.1)

ICD loader properties
  ICD loader Name                                 OpenCL ICD Loader
  ICD loader Vendor                               OCL Icd free software
  ICD loader Version                              2.2.10
  ICD loader Profile                              OpenCL 2.1

@athas
Copy link
Contributor Author

athas commented Mar 17, 2020

I think that means that all the infrastructure works. The OpenCL ICD loader can find the Mesa ICD and talk to the hardware. It just can't compile any kernels, because some GPU-specific library files are missing.

Here is a small OpenCL test program that compiles and launches a kernel. Compile with

$ nix-shell -p ocl-icd -p opencl-headers --run 'gcc rot13.c -o rot13 -lOpenCL'

and then run with

$ ./rot13

I don't expect it'll work on your machine. It also doesn't work on my Vega 64, as the kernel launch hangs forever. That's an improvement, though: Mesa OpenCL used to reboot my computer, even on non-NixOS distributions.

The general buggyness of Mesa OpenCL is going to be an obstacle to testing this...

@rnhmjoj
Copy link
Contributor

rnhmjoj commented Mar 17, 2020

Oh, that's too bad! This is what clinfo tell me now, but I don't really have a clue what everything means.

That looks good. In case the driver is not found you would simply get:

ERROR: clGetPlatformIDs(-1001)

@killercup
Copy link
Member

@athas yep, doesn't start, "fatal error: cannot open file '/nix/store/l81x3yfwahahhxmfk4wbg2ki025v451p-libclc-2019-06-09/lib/clc/gfx1010-amdgcn-mesa-mesa3d.bc': No such file or directory".

@FRidh FRidh added this to Needs review in Staging Apr 3, 2020
@davidak
Copy link
Member

davidak commented May 16, 2020

I added the code from #82729 (comment) to my configuration.nix and now OpenCL works!

[davidak@gaming:~]$ nix run nixpkgs.clinfo -c clinfo
Number of platforms                               1
  Platform Name                                   Clover
  Platform Vendor                                 Mesa
  Platform Version                                OpenCL 1.1 Mesa 19.3.3
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_icd
  Platform Extensions function suffix             MESA

  Platform Name                                   Clover
Number of devices                                 1
  Device Name                                     AMD Radeon (TM) RX 480 Graphics (POLARIS10, DRM 3.35.0, 5.4.41, LLVM 9.0.1)

Even BOINC detects the GPU now:

Screenshot from 2020-05-17 00-05-40

Copy link
Member

@davidak davidak left a comment

Choose a reason for hiding this comment

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

works, changes looks OK as far as i can judge

@rnhmjoj
Copy link
Contributor

rnhmjoj commented May 17, 2020

@vcunat can you take a look at this PR?

Copy link
Contributor

@rnhmjoj rnhmjoj left a comment

Choose a reason for hiding this comment

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

The change looks good but openCL support should be optional to avoid a large closure size increase.
For convenience, a mesa-opencl package could be added so that users don't have to rebuild mesa.

@cab404
Copy link
Member

cab404 commented May 20, 2020

I tried it, but blender doesn't report my videocard (540X) as a usable device. Is that normal?

@davidak
Copy link
Member

davidak commented May 29, 2020

@cab404 i also don't see my AMD Radeon RX 480 as supported. so might be blender specific

@FRidh FRidh moved this from Needs review to WIP in Staging Jun 19, 2020
@FRidh FRidh added this to the 20.09 milestone Jun 21, 2020
@mohe2015
Copy link
Contributor

@athas May you rebase this please?

@athas
Copy link
Contributor Author

athas commented Nov 13, 2020

I'll try over the weekend, but I won't have the spare life cycles to actually test it.

@mohe2015
Copy link
Contributor

mohe2015 commented Nov 13, 2020

Cool @athas. https://github.com/mohe2015/nixpkgs/tree/mesa-opencl may or may not be a correct rebase (nonsquashed, only the first three commits as I did some further tests). Feel free to use it (without me as an author is fine as it's just a rebase)

I'm building now but it seems like it's almost a full rebuild...

@SuperSandro2000
Copy link
Member

Can you please fix the merge conflict and target staging with the mass rebuild?

@timhae
Copy link
Contributor

timhae commented Dec 4, 2020

I also added the code from the comment to my configuration.nix and was able to compile the program mentioned in the other comment, the output is (as expected) Hello, World!. Thanks a lot for your effort and please let me know if I can do any additional testing to help you.

@athas
Copy link
Contributor Author

athas commented Dec 5, 2020

I have rebased on top of master. Haven't tried running it, but by inspection of the files, it looks OK.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/openvx-opencl-setup-for-amd-cpu-and-gpu/10383/1

athas and others added 2 commits December 6, 2020 23:34
Co-authored-by: Sandro <sandro.jaeckel@gmail.com>
Co-authored-by: Sandro <sandro.jaeckel@gmail.com>
@SuperSandro2000
Copy link
Member

Please target staging and squash the commits.

@FRidh FRidh modified the milestones: 20.09, 21.03 Dec 20, 2020
@shpinog
Copy link

shpinog commented Mar 3, 2021

I also used the workaround above, but it doesn't work now. Unfortunately, rocm does not work on many platforms, as it requires hardware support for the processor.

fi

# We construct our own .icd file that contains an absolute path.
rm -rf $out/etc/OpenCL
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rm -rf $out/etc/OpenCL
rm -r $out/etc/OpenCL

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-get-opecl-working-on-amdgpu/11786/2

@davidak
Copy link
Member

davidak commented Apr 4, 2021

I also used this workaround, since may last year and noticed now that it does not work anymore.

My config.: https://codeberg.org/davidak/nixos-config/src/commit/3d15f77fbe04177c546c0a15026ec1f3240d50ac/modules/amd/default.nix

I don't see any OpenCL device:

[davidak@gaming:~/code/nixpkgs]$ nix run nixpkgs.clinfo -c clinfo
Number of platforms                               0
  • system: "x86_64-linux"
  • host os: Linux 5.11.2, NixOS, 21.05pre274338.102eb68ceec (Okapi)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.3.10
  • channels(root): "home-manager-20.09, nixos-21.05pre279456.04a2b269d89, nixos-hardware, nixos-unstable-21.05pre279456.04a2b269d89"
  • nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos

Maybe this needs an update?

@kvtb
Copy link
Contributor

kvtb commented Aug 31, 2021

Could we merge it under enableOpenCL conditional, so those who concern the closure size could opt-out?
It is the only showstopper, isn't it?

@sbruder
Copy link
Contributor

sbruder commented Sep 1, 2021

I rebased the PR (and the included patch against mesa) onto the latest master and added a switch for making OpenCL support optional (as @rnhmjoj suggested): https://github.com/sbruder/nixpkgs/commits/mesa-opencl and tested the PR cherry-picked onto 21.05 with the following config:

{ pkgs, ... }:
{
  hardware.opengl =
    let
      pkgsMesaClover = import
        (pkgs.fetchFromGitHub {
          owner = "sbruder";
          repo = "nixpkgs";
          rev = "41d33334ec2d07da8f5bd0f3749949241c42266b";
          sha256 = "sha256-sA/kwZ8kXuRC/sLjja2ISpyTnlEwZjifyYeiz9hFWRE=";
        })
        { inherit (pkgs) system; };
    in
    {
      package = pkgsMesaClover.mesa-opencl.drivers;
      extraPackages = [
        pkgsMesaClover.mesa-opencl
      ];
    };
}

After a rebuild clinfo detects my RX 480 (gist with the output of clinfo and comparison to its output with ROCm), though it only shows support for OpenCL 1.1, not 1.2 as it should support (though I don’t know if upstream does not support it on my hardware or if this is due to how nixpkgs packages it).

Some programs that work include @athas’ rot13 example, hashcat (with --force) and waifu2x-converter-cpp. Others that don’t work include Blender (which is a known problem and Blender will drop OpenCL support in the near future) and the NNEDI3CL VapourSynth plugin (fails with Boost.Compute: kernel compilation failed (-43)).

Besides that, some programs that do work show warnings like this, which probably are not problematic, but seems like it’s not fully working yet:

'+fp32-denormals' is not a recognized feature for this target (ignoring feature)
'+fp64-fp16-denormals' is not a recognized feature for this target (ignoring feature)
'+fp32-denormals' is not a recognized feature for this target (ignoring feature)
'+fp64-fp16-denormals' is not a recognized feature for this target (ignoring feature)
'+fp32-denormals' is not a recognized feature for this target (ignoring feature)
'+fp64-fp16-denormals' is not a recognized feature for this target (ignoring feature)
'+fp32-denormals' is not a recognized feature for this target (ignoring feature)
'+fp64-fp16-denormals' is not a recognized feature for this target (ignoring feature)

@rnhmjoj
Copy link
Contributor

rnhmjoj commented Sep 1, 2021

@sbruder can you make a new pull request (against staging) with your patch?

@sbruder sbruder mentioned this pull request Sep 1, 2021
12 tasks
@primeos
Copy link
Member

primeos commented Sep 18, 2021

I'll go ahead and close this in favor of #136402 (will be merged soon).

Thanks @athas et al.

@primeos primeos closed this Sep 18, 2021
Staging automation moved this from WIP to Done Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Staging
  
Done
Development

Successfully merging this pull request may close these issues.

None yet