

<https://asukiaaa.blogspot.com/2018/02/ubuntuintelcpugpuopencl.html>  
こちらの記事そのままコピペして実行したいのですが、出力結果のHelloWorldが文字化けしてしまいます。  
おそらくメモリ破壊が起きていると思うのですが、なぜでしょうか？

OS:Ubuntu20.04  
CPU:J4125  
RAM:8GB

[code]

    $ gcc hello.c -lOpenCL
    In file included from /usr/include/CL/cl.h:32,
                     from hello.c:7:
    /usr/include/CL/cl_version.h:34:9: note: #pragma message: cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 220 (OpenCL 2.2)
       34 | #pragma message("cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 220 (OpenCL 2.2)")
    
[/code]

[code]

     ./a.out 
     lSd
    
[/code]

[code]

    clinfo 
    Number of platforms                               1
      Platform Name                                   Intel(R) CPU Runtime for OpenCL(TM) Applications
      Platform Vendor                                 Intel(R) Corporation
      Platform Version                                OpenCL 2.1 LINUX
      Platform Profile                                FULL_PROFILE
      Platform Extensions                             cl_khr_icd 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_byte_addressable_store cl_khr_depth_images cl_khr_3d_image_writes cl_intel_exec_by_local_thread cl_khr_spir cl_khr_fp64 cl_khr_image2d_from_buffer cl_intel_vec_len_hint 
      Platform Host timer resolution                  1ns
      Platform Extensions function suffix             INTEL
    
      Platform Name                                   Intel(R) CPU Runtime for OpenCL(TM) Applications
    Number of devices                                 1
      Device Name                                     Intel(R) Celeron(R) J4125 CPU @ 2.00GHz
      Device Vendor                                   Intel(R) Corporation
      Device Vendor ID                                0x8086
      Device Version                                  OpenCL 2.1 (Build 0)
      Driver Version                                  18.1.0.0920
      Device OpenCL C Version                         OpenCL C 2.0 
      Device Type                                     CPU
      Device Profile                                  FULL_PROFILE
      Device Available                                Yes
      Compiler Available                              Yes
      Linker Available                                Yes
      Max compute units                               4
      Max clock frequency                             2000MHz
      Device Partition                                (core)
        Max number of sub-devices                     4
        Supported partition types                     by counts, equally, by names (Intel)
        Supported affinity domains                    (n/a)
      Max work item dimensions                        3
      Max work item sizes                             8192x8192x8192
      Max work group size                             8192
      Preferred work group size multiple              <getWGsizes:1171: create context : error -2>
      Max sub-groups per work group                   1
      Preferred / native vector sizes                 
        char                                                 1 / 16      
        short                                                1 / 8       
        int                                                  1 / 4       
        long                                                 1 / 2       
        half                                                 0 / 0        (n/a)
        float                                                1 / 4       
        double                                               1 / 2        (cl_khr_fp64)
      Half-precision Floating-point support           (n/a)
      Single-precision Floating-point support         (core)
        Denormals                                     Yes
        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                              8169574400 (7.609GiB)
      Error Correction support                        No
      Max memory allocation                           2042393600 (1.902GiB)
      Unified memory for Host and Device              Yes
      Shared Virtual Memory (SVM) capabilities        (core)
        Coarse-grained buffer sharing                 Yes
        Fine-grained buffer sharing                   Yes
        Fine-grained system sharing                   Yes
        Atomics                                       Yes
      Minimum alignment for any data type             128 bytes
      Alignment of base address                       1024 bits (128 bytes)
      Preferred alignment for atomics                 
        SVM                                           64 bytes
        Global                                        64 bytes
        Local                                         0 bytes
      Max size for global variable                    65536 (64KiB)
      Preferred total size of global vars             65536 (64KiB)
      Global Memory cache type                        Read/Write
      Global Memory cache size                        4194304 (4MiB)
      Global Memory cache line size                   64 bytes
      Image support                                   Yes
        Max number of samplers per kernel             480
        Max size for 1D images from buffer            127649600 pixels
        Max 1D or 2D image array size                 2048 images
        Base address alignment for 2D image buffers   64 bytes
        Pitch alignment for 2D image buffers          64 pixels
        Max 2D image size                             16384x16384 pixels
        Max 3D image size                             2048x2048x2048 pixels
        Max number of read image args                 480
        Max number of write image args                480
        Max number of read/write image args           480
      Max number of pipe args                         16
      Max active pipe reservations                    65535
      Max pipe packet size                            1024
      Local memory type                               Global
      Local memory size                               32768 (32KiB)
      Max number of constant args                     480
      Max constant buffer size                        131072 (128KiB)
      Max size of kernel argument                     3840 (3.75KiB)
      Queue properties (on host)                      
        Out-of-order execution                        Yes
        Profiling                                     Yes
        Local thread execution (Intel)                Yes
      Queue properties (on device)                    
        Out-of-order execution                        Yes
        Profiling                                     Yes
        Preferred size                                4294967295 (4GiB)
        Max size                                      4294967295 (4GiB)
      Max queues on device                            4294967295
      Max events on device                            4294967295
      Prefer user sync for interop                    No
      Profiling timer resolution                      1ns
      Execution capabilities                          
        Run OpenCL kernels                            Yes
        Run native kernels                            Yes
        Sub-group independent forward progress        No
        IL version                                    SPIR-V_1.0
        SPIR versions                                 1.2
      printf() buffer size                            1048576 (1024KiB)
      Built-in kernels                                (n/a)
      Device Extensions                               cl_khr_icd 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_byte_addressable_store cl_khr_depth_images cl_khr_3d_image_writes cl_intel_exec_by_local_thread cl_khr_spir cl_khr_fp64 cl_khr_image2d_from_buffer cl_intel_vec_len_hint 
    
    NULL platform behavior
      clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...)  No platform
      clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...)   No platform
      clCreateContext(NULL, ...) [default]            No platform
      clCreateContext(NULL, ...) [other]              <checkNullCtx:2758: create context with device from non-default platform : error -2>
      clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT)  No platform
      clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU)  No platform
      clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU)  No platform
      clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR)  No platform
      clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM)  No platform
      clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL)  No platform
    
[/code]


CC BY-SA 4.0
2023-04-18T02:54:31.190
94557
2023-11-12T05:07:05.300
18802
question
c++ linux ubuntu
OpenCLでHello Worldを実行するとメモリ破壊が起こる


リンク先のサンプルコードは極めて雑でエラーチェックを一切やっておらず、もし途中でエラーが発生しても無視して先に進んでしまう危険なコードになってしまっています（Web上に転がっているコードは玉石混交であり、無条件に信用してはいけません）。また、C/C++では、未初期化のローカル変数の内容は不定であり、未初期化の変数に読み取りアクセスすると
**未定義動作**
を引き起こします。おそらく、メモリ破壊が起きているわけではなく、OpenCLのカーネルコードがまったく実行されておらず、またOpenCLのバッファからデータを読み出すこともできていないせいで、未初期化のローカル変数に最初から入っていたでたらめなデータを`puts()`で出力してしまっただけでは？

まずAPIの戻り値（`cl_int`型）をすべてチェックして、どこでエラーが発生しているのかを明らかにしてください。成功した場合は`CL_SUCCESS`が、失敗した場合はそれ以外のエラーコードが返ってきます。


CC BY-SA 4.0
2023-11-12T05:07:05.300
96762
2023-11-12T05:07:05.300
15413
94557
answer