You are not logged in.

#1 2020-04-09 18:56:43

qwe8013
Member
Registered: 2020-04-08
Posts: 30

[SOLVED] OpenCL on AMD Ryzen 5

I have a laptop with AMD Ryzen 5 processor with integrated graphics. Now, there is installed opencl-mesa driver.
clinfo shows that there is an OpenCL device with OpenCL 1.1 support

I have made a test program for OpenCL:

#include <iostream>
#include <CL/cl.h>
#include <cstring>

const char* src1 = "__kernel void test(__global int* message)\n{\nint gid = get_global_id(0);\nmessage[gid] += gid;\n}\n";

int main()
{
    cl_uint qwe;
    cl_platform_id plat;
    cl_int ret = clGetPlatformIDs(1, &plat, &qwe);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 1" << std::endl;
        return 0;
    }

    cl_device_id dev;
    ret = clGetDeviceIDs(plat, CL_DEVICE_TYPE_ALL, 1, &dev, &qwe);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 2" << std::endl;
        return 0;
    }

    cl_context cont = clCreateContext(0, 1, &dev, 0, 0, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 3" << std::endl;
        return 0;
    }

    size_t l = strlen(src1);
    cl_program prog = clCreateProgramWithSource(cont, 1, &src1, &l, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 4" << std::endl;
        return 0;
    }

    ret = clBuildProgram(prog, 1, &dev, 0, 0, 0);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 5" << std::endl;
        return 0;
    }

    cl_mem buf = clCreateBuffer(cont, CL_MEM_READ_WRITE, 10000*sizeof(cl_int), 0, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 6" << std::endl;
        return 0;
    }

    cl_command_queue cmd = clCreateCommandQueue(cont, dev, 0, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 7" << std::endl;
        return 0;
    }

    cl_kernel krn = clCreateKernel(prog, "test", &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 8" << std::endl;
        return 0;
    }

    cl_int data[10000] = {0};
    ret = clEnqueueWriteBuffer(cmd, buf, CL_TRUE, 0, 10000*sizeof(cl_int), data, 0, 0, 0);
    
    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 9" << std::endl;
        return 0;
    }

    ret = clSetKernelArg(krn, 0, sizeof(cl_mem), &buf);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 10 " << ret << std::endl;
        return 0;
    }

    size_t q = 10000;
    ret = clEnqueueNDRangeKernel(cmd, krn, 1, 0, &q, 0, 0, 0, 0);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 11 " << ret << std::endl;
        return 0;
    }

    ret = clEnqueueReadBuffer(cmd, buf, CL_TRUE, 0, 10000*sizeof(cl_int), data, 0, 0, 0);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 12" << std::endl;
        return 0;
    }

    std::cout << data[0] << "   " << data[1] << "   " << data[2] << "   " << data[3] << std::endl;

    std::cout << "Success!" << std::endl;
    return 0;
}

But it hangs after kernels started to execute, how can I fix that?

Last edited by qwe8013 (2020-05-02 20:56:12)

Offline

#2 2020-04-10 12:21:18

Lone_Wolf
Member
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 10,428

Re: [SOLVED] OpenCL on AMD Ryzen 5

Does it show any output ?

How (exact command please) are you running the progam ?


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#3 2020-04-10 12:54:38

qwe8013
Member
Registered: 2020-04-08
Posts: 30

Re: [SOLVED] OpenCL on AMD Ryzen 5

I added some output to my program:

#include <iostream>
#include <CL/cl.h>
#include <cstring>

const char* src1 = "__kernel void test(__global int* message)\n{\nint gid = get_global_id(0);\nmessage[gid] += gid;\n}\n";

int main()
{
    cl_uint qwe;
    cl_platform_id plat;
    cl_int ret = clGetPlatformIDs(1, &plat, &qwe);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 1" << std::endl;
        return 0;
    }

	std::cout << "clGetPlatformIDs" << std::endl;

    cl_device_id dev;
    ret = clGetDeviceIDs(plat, CL_DEVICE_TYPE_ALL, 1, &dev, &qwe);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 2" << std::endl;
        return 0;
    }

	std::cout << "clGetDeviceIDs" << std::endl;

    cl_context cont = clCreateContext(0, 1, &dev, 0, 0, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 3" << std::endl;
        return 0;
    }

	std::cout << "clCreateContext" << std::endl;

    size_t l = strlen(src1);
    cl_program prog = clCreateProgramWithSource(cont, 1, &src1, &l, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 4" << std::endl;
        return 0;
    }

	std::cout << "clCreateProgramWithSource" << std::endl;

    ret = clBuildProgram(prog, 1, &dev, 0, 0, 0);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 5" << std::endl;
        return 0;
    }

	std::cout << "clBuildProgram" << std::endl;

    cl_mem buf = clCreateBuffer(cont, CL_MEM_READ_WRITE, 10000*sizeof(cl_int), 0, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 6" << std::endl;
        return 0;
    }

	std::cout << "clCreateBuffer" << std::endl;

    cl_command_queue cmd = clCreateCommandQueue(cont, dev, 0, &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 7" << std::endl;
        return 0;
    }

	std::cout << "clCreateCommandQueue" << std::endl;

    cl_kernel krn = clCreateKernel(prog, "test", &ret);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 8" << std::endl;
        return 0;
    }

	std::cout << "clCreateKernel" << std::endl;

    cl_int data[10000] = {0};
    ret = clEnqueueWriteBuffer(cmd, buf, CL_TRUE, 0, 10000*sizeof(cl_int), data, 0, 0, 0);
    
    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 9" << std::endl;
        return 0;
    }

	std::cout << "clEnqueueWriteBuffer" << std::endl;

    ret = clSetKernelArg(krn, 0, sizeof(cl_mem), &buf);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 10 " << ret << std::endl;
        return 0;
    }

	std::cout << "clSetKernelArg" << std::endl;

    size_t q = 10000;
    ret = clEnqueueNDRangeKernel(cmd, krn, 1, 0, &q, 0, 0, 0, 0);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 11 " << ret << std::endl;
        return 0;
    }

	std::cout << "clEnqueueNDRangeKernel" << std::endl;

    ret = clEnqueueReadBuffer(cmd, buf, CL_TRUE, 0, 10000*sizeof(cl_int), data, 0, 0, 0);

    if (ret != CL_SUCCESS)
    {
        std::cout << "Error 12" << std::endl;
        return 0;
    }

	std::cout << "clEnqueueReadBuffer" << std::endl;

    std::cout << data[0] << "   " << data[1] << "   " << data[2] << "   " << data[3] << std::endl;

    std::cout << "Success!" << std::endl;
    return 0;
}

Does it show any output ?

How (exact command please) are you running the progam ?

I compile program with command:

g++ main.cpp -lOpenCL

and run it:

./a.out

output:

qwe8013@qwe8013-notebook ~/VSCodeProjects/ocl_test $ ./a.out
clGetPlatformIDs
clGetDeviceIDs
clCreateContext
clCreateProgramWithSource
clBuildProgram
clCreateBuffer
clCreateCommandQueue
clCreateKernel
clEnqueueWriteBuffer
clSetKernelArg
clEnqueueNDRangeKernel

PS
clinfo output:

qwe8013@qwe8013-notebook ~ $ clinfo
Number of platforms                               1
  Platform Name                                   Clover
  Platform Vendor                                 Mesa
  Platform Version                                OpenCL 1.1 Mesa 20.0.4
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_icd
  Platform Extensions function suffix             MESA

  Platform Name                                   Clover
Number of devices                                 1
  Device Name                                     AMD RAVEN (DRM 3.36.0, 5.6.3-arch1-1, LLVM 9.0.1)
  Device Vendor                                   AMD
  Device Vendor ID                                0x1002
  Device Version                                  OpenCL 1.1 Mesa 20.0.4
  Driver Version                                  20.0.4
  Device OpenCL C Version                         OpenCL C 1.1 
  Device Type                                     GPU
  Device Profile                                  FULL_PROFILE
  Device Available                                Yes
  Compiler Available                              Yes
  Max compute units                               8
  Max clock frequency                             1200MHz
  Max work item dimensions                        3
  Max work item sizes                             256x256x256
  Max work group size                             256
  Preferred work group size multiple              64
  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                              3221225472 (3GiB)
  Error Correction support                        No
  Max memory allocation                           2254857830 (2.1GiB)
  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 RAVEN (DRM 3.36.0, 5.6.3-arch1-1, 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 RAVEN (DRM 3.36.0, 5.6.3-arch1-1, 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 RAVEN (DRM 3.36.0, 5.6.3-arch1-1, 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.12
  ICD loader Profile                              OpenCL 2.2

Last edited by qwe8013 (2020-04-10 13:08:09)

Offline

#4 2020-04-10 13:33:03

Lone_Wolf
Member
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 10,428

Re: [SOLVED] OpenCL on AMD Ryzen 5

It does work here with my  RX 580 videocard, so the code is valid.

$ ./a.out 
clGetPlatformIDs
clGetDeviceIDs
clCreateContext
clCreateProgramWithSource
clBuildProgram
clCreateBuffer
clCreateCommandQueue
clCreateKernel
clEnqueueWriteBuffer
clSetKernelArg
clEnqueueNDRangeKernel
clEnqueueReadBuffer
0   1   2   3
Success!
$ 



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

  Platform Name                                   Clover
Number of devices                                 1
  Device Name                                     Radeon RX 580 Series (POLARIS10, DRM 3.36.0, 5.6.3-arch1-1, LLVM 9.0.1)
  Device Vendor                                   AMD
  Device Vendor ID                                0x1002
  Device Version                                  OpenCL 1.1 Mesa 20.0.4
  Driver Version                                  20.0.4
  Device OpenCL C Version                         OpenCL C 1.1 
  Device Type                                     GPU
  Device Profile                                  FULL_PROFILE
  Device Available                                Yes
  Compiler Available                              Yes
  Max compute units                               36
  Max clock frequency                             1366MHz
  Max work item dimensions                        3
  Max work item sizes                             256x256x256
  Max work group size                             256
  Preferred work group size multiple              64
  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                                   Radeon RX 580 Series (POLARIS10, DRM 3.36.0, 5.6.3-arch1-1, 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                                   Radeon RX 580 Series (POLARIS10, DRM 3.36.0, 5.6.3-arch1-1, 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                                   Radeon RX 580 Series (POLARIS10, DRM 3.36.0, 5.6.3-arch1-1, 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.12
  ICD loader Profile                              OpenCL 2.2
$

Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#5 2020-04-10 13:59:22

qwe8013
Member
Registered: 2020-04-08
Posts: 30

Re: [SOLVED] OpenCL on AMD Ryzen 5

It does work here with my  RX 580 videocard, so the code is valid.

Thanks for testing.

But how to make OpenCL to work with AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx × 8 ?

Offline

#6 2020-04-15 10:49:55

Lone_Wolf
Member
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 10,428

Re: [SOLVED] OpenCL on AMD Ryzen 5

https://wiki.archlinux.org/index.php/GPGPU gives an overview .

I'd try opencl-amd first. While it's proprietary it cooperates well with the mesa stack .

If you prefer an opensource solution you can try rocm. Be warned though building rocm is a lot of work and its stability is not very good on archlinux.


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#7 2020-05-02 20:54:42

qwe8013
Member
Registered: 2020-04-08
Posts: 30

Re: [SOLVED] OpenCL on AMD Ryzen 5

OK, thanks, installing an opencl-amd helped.

Offline

Board footer

Powered by FluxBB