summaryrefslogtreecommitdiff
path: root/utests/sub_buffer.cpp
blob: 04cfee7bfea9ea5a950ad66c29f34ca22c153c19 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "utest_helper.hpp"

void sub_buffer_check(void)
{
    cl_int error;
    cl_ulong max_alloc_size;
    cl_uint address_align;
    cl_mem main_buf;
    cl_mem sub_buf;
    char *main_buf_content;
    char sub_buf_content[32];

    error = clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(max_alloc_size), &max_alloc_size, NULL);
    OCL_ASSERT(error == CL_SUCCESS);
    error = clGetDeviceInfo(device, CL_DEVICE_MEM_BASE_ADDR_ALIGN, sizeof(address_align ), &address_align, NULL );
    OCL_ASSERT(error == CL_SUCCESS);

    max_alloc_size /= 8;
    main_buf_content = (char *)malloc(sizeof(char) * max_alloc_size);

    for (cl_ulong i = 0; i < max_alloc_size; i++) {
        main_buf_content[i] = rand() & 63;
    }

    main_buf = clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, max_alloc_size, main_buf_content, &error);
    OCL_ASSERT(error == CL_SUCCESS);

    /* Test read sub buffer. */
    for (cl_ulong sz = max_alloc_size / 4; sz <= max_alloc_size; sz += max_alloc_size / 4) {
        for (cl_ulong off = 0; off < max_alloc_size; off += 1234 + max_alloc_size / 3) {
            cl_buffer_region region;
            region.origin = off;
            region.size = sz;

            sub_buf = clCreateSubBuffer(main_buf, 0, CL_BUFFER_CREATE_TYPE_REGION, &region, &error );

            /* invalid size, should be failed. */
            if(off + sz > max_alloc_size) {
                OCL_ASSERT(error != CL_SUCCESS);
                continue;
            }
            /* invalid align, should be failed. */
            if(off & ((address_align/8)-1)) {
                OCL_ASSERT(error != CL_SUCCESS);
                continue;
            }

            OCL_ASSERT(error == CL_SUCCESS);

            error = clEnqueueReadBuffer(queue, sub_buf, CL_TRUE, 0, 32, (void *)sub_buf_content, 0, NULL, NULL);
            OCL_ASSERT(error == CL_SUCCESS);

#if 0
            printf("\nRead ########### Src buffer: \n");
            for (int i = 0; i < 32; ++i)
                printf(" %2.2u", main_buf_content[off + i]);

            printf("\nRead ########### dst buffer: \n");
            for (int i = 0; i < 32; ++i)
                printf(" %2.2u", sub_buf_content[i]);
            printf("\n");
#endif
            for (int i = 0; i < 32; ++i) {

                if (main_buf_content[off + i] != sub_buf_content[i]) {
                    printf ("different index is %d\n", i);
                    OCL_ASSERT(0);
                }
            }

        }
    }


    for (cl_ulong sz = max_alloc_size / 4; sz <= max_alloc_size; sz += max_alloc_size / 4) {
        for (cl_ulong off = 0; off < max_alloc_size; off += 1234 + max_alloc_size / 3) {
            cl_buffer_region region;
            region.origin = off;
            region.size = sz;

            sub_buf = clCreateSubBuffer(main_buf, 0, CL_BUFFER_CREATE_TYPE_REGION, &region, &error );

            /* invalid size, should be failed. */
            if(off + sz > max_alloc_size) {
                OCL_ASSERT(error != CL_SUCCESS);
                continue;
            }
            /* invalid align, should be failed. */
            if(off & (address_align/8-1)) {
                OCL_ASSERT(error != CL_SUCCESS);
                continue;
            }

            OCL_ASSERT(error == CL_SUCCESS);

            for (int i = 0; i < 32; i++) {
                sub_buf_content[i] = rand() & 63;
            }

            error = clEnqueueWriteBuffer(queue, main_buf, CL_TRUE, off, 32, sub_buf_content, 0, NULL, NULL);
            OCL_ASSERT(error == CL_SUCCESS);

            void * mapped_ptr = clEnqueueMapBuffer(queue, sub_buf, CL_TRUE, (cl_map_flags)( CL_MAP_READ | CL_MAP_WRITE ),
                    0, 32, 0, NULL, NULL, &error );
            OCL_ASSERT(error == CL_SUCCESS);

#if 0
            printf("\nMap ########### Src buffer: \n");
            for (int i = 0; i < 32; ++i)
                printf(" %2.2u", sub_buf_content[i]);

            printf("\nMap ########### dst buffer: \n");
            for (int i = 0; i < 32; ++i)
                printf(" %2.2u", ((char *)mapped_ptr)[i]);
            printf("\n");
#endif
            for (int i = 0; i < 32; i++) {

                if (mapped_ptr && ((char *)mapped_ptr)[i] != sub_buf_content[i]) {
                    printf ("different index is %d\n", i);
                    OCL_ASSERT(0);
                }
            }

            error = clEnqueueUnmapMemObject(queue, sub_buf, mapped_ptr, 0, NULL, NULL );
            OCL_ASSERT(error == CL_SUCCESS);

            clReleaseMemObject(sub_buf);
        }
    }

    clReleaseMemObject(main_buf);
    free(main_buf_content);
}

MAKE_UTEST_FROM_FUNCTION(sub_buffer_check);