Core dumped OpenCL

0

Witam. Piszę program w C++ i OpenCL. Mam pewien problem. Opierając się na przykładowym kodzie z internetu, zrobiłem takie coś. W skrócie chcę, aby kernel nadał każdemu pikselowi 2D kolor (x, x, x, x). Następnie program ma przekazać dane z urządzenia do pamięci hosta, a potem wyświetlić. Oto kod:

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<cmath>
#ifdef __APPLE__
    #include<OpenCL/opencl.h>
#else
    #include<CL/cl.h>
#endif
 
#define IMG_SIZE 100
#define MAX_SRC_SIZE (0x100000)
 
using namespace std;
 
void err_check( int err, string err_code ) {
        if ( err != CL_SUCCESS ) {
                cout << "Error: " << err_code << "(" << err << ")" << endl;
                exit(-1);
        }
}
 
 
int main()
{
    cl_platform_id platform_id = NULL;
    cl_uint ret_num_platform;
 
    cl_device_id device_id = NULL;
    cl_uint ret_num_device;
 
    cl_context context = NULL;
 
    cl_command_queue command_queue = NULL;
 
    cl_program program = NULL;
 
    cl_kernel kernel = NULL;
 
    cl_int err;
 
    float input[IMG_SIZE * 3], output[IMG_SIZE * 3];

 
    // step 1 : getting platform ID
    err = clGetPlatformIDs(1, &platform_id, &ret_num_platform);
    err_check(err,"clGetPlatformIDs");
 
    // step 2 : Get Device ID
    err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_device );
    err_check(err,"clGetDeviceIDs");
 
    // step 3 : Create Context
    context = clCreateContext(NULL,1,&device_id,NULL,NULL,&err);
    err_check(err, "clCreateContext");
 
    cl_bool sup;
    size_t rsize;
    clGetDeviceInfo(device_id, CL_DEVICE_IMAGE_SUPPORT, sizeof(sup), &sup, &rsize);
    if (sup != CL_TRUE){
        cout<<"Image not Supported"<<endl;
    }
    // Step 4 : Create Command Queue
    command_queue =  clCreateCommandQueue(context, device_id, 0, &err);
    err_check(err, "clCreateCommandQueue");
 
    // Step 5 : Reading Kernel Program
 
    FILE *fp;
    size_t kernel_src_size;
    char *kernel_src_std;
 
    fp = fopen("kernel6.cl","r");
 
    kernel_src_std = (char *)malloc(MAX_SRC_SIZE);
    kernel_src_size = fread(kernel_src_std, 1, MAX_SRC_SIZE,fp);
 
    fclose(fp);
 
    //  Create Image data formate
    cl_image_format img_fmt;
 
    img_fmt.image_channel_order = CL_RGBA;
    img_fmt.image_channel_data_type = CL_FLOAT;
 
    // Step 6 : Create Image Memory Object
    cl_mem image2;
 
    size_t width, height;
    width = height = sqrt(IMG_SIZE);
 
 
    image2 = clCreateImage2D(context, CL_MEM_READ_WRITE, &img_fmt, width, height, 0,0,&err);
    err_check(err, "image2: clCreateImage2D");
 
    // Copy Data from Host to Device
    cl_event event[5];
 
    size_t origin[] = {0,0,0}; // Defines the offset in pixels in the image from where to write.
    size_t region[] = {width, height, 1}; // Size of object to be transferred
    err = clEnqueueWriteImage(command_queue, image2, CL_TRUE, origin, region,0,0, input, 0, NULL,&event[0] );
    err_check(err,"clEnqueueWriteImage");
    //cout<<kernel_src_std;
    // Step 7 : Create and Build Program
    program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src_std, 0, &err);
    err_check(err, "clCreateProgramWithSource");
 
    err = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
 
    if (err == CL_BUILD_PROGRAM_FAILURE)
        cout<<"clBulidProgram Fail...."<<endl;
    err_check(err, "clBuildProgram");
 
    // Step 8 : Create Kernel
    kernel = clCreateKernel(program,"image_copy",&err );
 
    // Step 9 : Set Kernel Arguments
 
     
    err_check(err, "Arg 1 : clSetKernelArg");
 
    err = clSetKernelArg(kernel, 0,sizeof(cl_mem), (void *)&image2);
    err_check(err, "Arg 2 : clSetKernelArg");

    // Step 10 : Execute Kernel 
    size_t GWSize[]={width, height,1};
    
    //std::cout<<"SYLWiA!"<<std::endl;
    err = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, GWSize, NULL, 1, event,&event[1]);
    //std::cout<<"Wszytstko wporządku?"<<std::endl;
    // Step 11 : Read output Data, from Device to Host
    err = clEnqueueReadImage(command_queue, image2, CL_TRUE, origin, region, 0, 0, output, 2, event, &event[2] );
 
    // Print Output
 
    for(int i=0;i<4;i++){
        for(int j = 0; j<IMG_SIZE; ++j){
 
            cout<<output[(i*IMG_SIZE)+j]<<"  ";
 
        }
 
    }
    cout<<endl;
 


    clReleaseMemObject(image2);
    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseCommandQueue(command_queue);
    clReleaseContext(context);
 
    free(kernel_src_std);
 
    return(0);
} 

kernel6.cl:

 // copy image1 to image2 using image read function

__kernel void image_copy( __write_only image2d_t image2)
{

    const int x = get_global_id(0);
    const int y = get_global_id(1);
float4 pixel;
    
    pixel = (float4)(x, x,x, x);
    //pixel.z *=2;
    write_imagef(image2, (int2)(x,y), pixel);
}

Wyjście wykonania:

 0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  2.93264e-38  0  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5  5  5  6  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9  
Naruszenie ochrony pamięci (core dumped)

Macie może pomysł co może być nie tak?

0

Odpal to pod valgrindem albo pod debugerem i pokaze ci gdzie źle się odnosisz do pamięci.

0

Czy ma ktoś może u siebie zainstalowane biblioteki OpenCL? Czy byłby ktoś w stanie powiedzieć czy ten przykład zadziała: https://iws44.iiita.ac.in/wiki/opencl/doku.php?id=clcreateimage2d ?

Mój output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 6Naruszenie ochrony pamięci (core dumped)


Jeżeli by u Was działało, a u mnie nie, to oznaczało by że mam jakieś błędy instalacji. 

Co dziwne, valgrind nie wskazuje jednoznacznie gdzie dokładnie mam błąd. Bo jeżeli bym usunął  jedną linijkę kodu, to on wskaże potem kolejną. Poniżej zamieszczam wynik dla kodu z przykładu z podanego linku, po usunięciu fragmetnów o image3(bo to nie jest potrzebne).

$ valgrind --leak-check=full ./main6
==8892== Memcheck, a memory error detector
==8892== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==8892== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==8892== Command: ./main6
==8892==
==8892== Warning: set address range perms: large range [0x800000000, 0xc00000000) (noaccess)
==8892== Conditional jump or move depends on uninitialised value(s)
==8892== at 0x4C2BFB8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x65ADBB4: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x4018FA: main (in /home/reddy/Pulpit/cl/main6)
==8892==
==8892== Conditional jump or move depends on uninitialised value(s)
==8892== at 0x4C2C007: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x65ADB86: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x4018FA: main (in /home/reddy/Pulpit/cl/main6)
==8892==
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 6==8892== Invalid read of size 8
==8892== at 0x4E34108: clReleaseMemObject (in /usr/lib/nvidia-319/libOpenCL.so.1.0.0)
==8892== by 0x401C62: main (in /home/reddy/Pulpit/cl/main6)
==8892== Address 0x2b636474736269fc is not stack'd, malloc'd or (recently) free'd
==8892==
==8892==
==8892== Process terminating with default action of signal 11 (SIGSEGV)
==8892== General Protection Fault
==8892== at 0x4E34108: clReleaseMemObject (in /usr/lib/nvidia-319/libOpenCL.so.1.0.0)
==8892== by 0x401C62: main (in /home/reddy/Pulpit/cl/main6)
3 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ==8892==
==8892== HEAP SUMMARY:
==8892== in use at exit: 2,667,391 bytes in 2,674 blocks
==8892== total heap usage: 3,326 allocs, 652 frees, 3,886,518 bytes allocated
==8892==
==8892== 8 bytes in 1 blocks are definitely lost in loss record 19 of 1,218
==8892== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x4E33CB6: ??? (in /usr/lib/nvidia-319/libOpenCL.so.1.0.0)
==8892== by 0x4E354E1: ??? (in /usr/lib/nvidia-319/libOpenCL.so.1.0.0)
==8892== by 0x4E34E9F: clGetPlatformIDs (in /usr/lib/nvidia-319/libOpenCL.so.1.0.0)
==8892== by 0x4013FE: main (in /home/reddy/Pulpit/cl/main6)
==8892==
==8892== 288 bytes in 1 blocks are possibly lost in loss record 1,008 of 1,218
==8892== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x4012074: _dl_allocate_tls (dl-tls.c:297)
==8892== by 0x5915ABC: pthread_create@@GLIBC_2.2.5 (allocatestack.c:571)
==8892== by 0x6A48E91: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x6524FF6: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x64E8877: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x64E913C: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65AC5E4: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65AC151: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x4014FF: main (in /home/reddy/Pulpit/cl/main6)
==8892==
==8892== 288 bytes in 1 blocks are possibly lost in loss record 1,009 of 1,218
==8892== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x4012074: _dl_allocate_tls (dl-tls.c:297)
==8892== by 0x5915ABC: pthread_create@@GLIBC_2.2.5 (allocatestack.c:571)
==8892== by 0x6A48E91: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65BADBA: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65BA1B4: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65AC8D7: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65AC151: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x4014FF: main (in /home/reddy/Pulpit/cl/main6)
==8892==
==8892== 1,152 bytes in 4 blocks are possibly lost in loss record 1,133 of 1,218
==8892== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x4012074: _dl_allocate_tls (dl-tls.c:297)
==8892== by 0x5915ABC: pthread_create@@GLIBC_2.2.5 (allocatestack.c:571)
==8892== by 0x6A48E91: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65BADBA: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65BA5E6: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65AC8BE: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x65AC151: ??? (in /usr/lib/nvidia-319/libnvidia-opencl.so.319.32)
==8892== by 0x4014FF: main (in /home/reddy/Pulpit/cl/main6)
==8892==
==8892== 1,048,576 bytes in 1 blocks are definitely lost in loss record 1,218 of 1,218
==8892== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8892== by 0x401637: main (in /home/reddy/Pulpit/cl/main6)
==8892==
==8892== LEAK SUMMARY:
==8892== definitely lost: 1,048,584 bytes in 2 blocks
==8892== indirectly lost: 0 bytes in 0 blocks
==8892== possibly lost: 1,728 bytes in 6 blocks
==8892== still reachable: 1,617,079 bytes in 2,666 blocks
==8892== suppressed: 0 bytes in 0 blocks
==8892== Reachable blocks (those to which a pointer was found) are not shown.
==8892== To see them, rerun with: --leak-check=full --show-reachable=yes
==8892==
==8892== For counts of detected and suppressed errors, rerun with: -v
==8892== Use --track-origins=yes to see where uninitialised values come from
==8892== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 2 from 2)
Unicestwiony

0

skompiluj z flagami debugowymi, a pokaże.

tak samo możesz poprzez gdb wczytać plik core i zobaczysz gdzie jest błąd

0

To samo mi pokazuje. W zależności od tego jaka będzie następna funkcja po err = clEnqueueReadImage(command_queue, image2, CL_TRUE, origin, region, 0, 0, output, 2, event, &event[2] ); to na nią mi pokże.

(gdb) where
#0  0x00007ffff7bd7105 in clReleaseMemObject ()
   from /usr/lib/nvidia-319/libOpenCL.so.1
#1  0x0000000000401c63 in main ()

1 użytkowników online, w tym zalogowanych: 0, gości: 1