aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/basic/test_rw_image_access_qualifier.cpp
blob: b06c82a5c0d1ce6c0694e10751e1225deaa13bf3 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

#include "procs.h"
#include "harness/clImageHelper.h"

static const char* rw_kernel_code =
"kernel void test_rw_images(read_write image2d_t src_image) {\n"
"  int tid_x = get_global_id(0);\n"
"  int tid_y = get_global_id(1);\n"
"\n"
"  int2 coords = (int2)(tid_x, tid_y);\n"
"\n"
"  uint4 src_val = read_imageui(src_image, coords);\n"
"  src_val += 3;\n"
"\n"
"  // required to ensure that following read from image at\n"
"  // location coord returns the latest color value.\n"
"  atomic_work_item_fence(CLK_IMAGE_MEM_FENCE,\n"
"                         memory_order_acq_rel,\n"
"                         memory_scope_work_item);\n"
"\n"
"  write_imageui(src_image, coords, src_val);\n"
"}\n";


int test_rw_image_access_qualifier(cl_device_id device_id, cl_context context, cl_command_queue commands, int num_elements)
{
    // This test should be skipped if images are not supported.
    if (checkForImageSupport(device_id))
    {
        return TEST_SKIPPED_ITSELF;
    }

    // Support for read-write image arguments is required for an
    // or 2.X device if the device supports images. In OpenCL-3.0
    // read-write images are optional. This test is already being skipped
    // for 1.X devices.
    if (get_device_cl_version(device_id) >= Version(3, 0))
    {
        cl_uint are_rw_images_supported{};
        test_error(
            clGetDeviceInfo(device_id, CL_DEVICE_MAX_READ_WRITE_IMAGE_ARGS,
                            sizeof(are_rw_images_supported),
                            &are_rw_images_supported, nullptr),
            "clGetDeviceInfo failed for CL_DEVICE_MAX_READ_IMAGE_ARGS\n");
        if (0 == are_rw_images_supported)
        {
            return TEST_SKIPPED_ITSELF;
        }
    }

    unsigned int i;

    unsigned int size_x;
    unsigned int size_y;
    unsigned int size;

    cl_int err;

    cl_program program;
    cl_kernel kernel;

    cl_mem_flags flags;
    cl_image_format format;
    cl_mem src_image;

    unsigned int *input;
    unsigned int *output;

    /* Create test input */
    size_x = 4;
    size_y = 4;
    size = size_x * size_y * 4;

    input = (unsigned int *)malloc(size*sizeof(unsigned int));
    output = (unsigned int *)malloc(size*sizeof(unsigned int));

    if (!input && !output) {
        log_error("Error: memory allocation failed\n");
    return -1;
    }

    MTdata mtData = init_genrand(gRandomSeed);
    /* Fill input array with random values */
    for (i = 0; i < size; i++) {
        input[i] = genrand_int32(mtData);
    }
    free_mtdata(mtData);
    mtData = NULL;

    /* Zero out output array */
    for (i = 0; i < size; i++) {
        output[i] = 0.0f;
    }

    /* Build the program executable */
    err = create_single_kernel_helper(context, &program, &kernel, 1,
                                      &rw_kernel_code, "test_rw_images");
    if (err != CL_SUCCESS || !program) {
        log_error("Error: clCreateProgramWithSource failed\n");
    return err;
    }

    /* Create arrays for input and output data */
    format.image_channel_order = CL_RGBA;
    format.image_channel_data_type = CL_UNSIGNED_INT32;

    /* Create input image */
    flags = CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR;
    src_image = create_image_2d(context, flags, &format,
                                size_x, size_y, 0,
                                (void *)input, &err);
    if (err != CL_SUCCESS || !src_image) {
        log_error("Error: clCreateImage2D failed\n");
        return err;
    }

    /* Set kernel arguments */
  err = clSetKernelArg(kernel, 0, sizeof(src_image), &src_image);
  if (err != CL_SUCCESS) {
    log_error("Error: clSetKernelArg failed\n");
    return err;
  }

    /* Set kernel execution parameters */
    int dim_count = 2;
    size_t global_dim[2];
    size_t local_dim[2];

    global_dim[0] = size_x;
    global_dim[1] = size_y;

    local_dim[0] = 1;
    local_dim[1] = 1;

    /* Execute kernel */
    err = CL_SUCCESS;
    unsigned int num_iter = 1;
    for(i = 0; i < num_iter; i++) {
        err |= clEnqueueNDRangeKernel(commands, kernel, dim_count,
                                      NULL, global_dim, local_dim,
                                      0, NULL, NULL);
    }

    /* Read back the results from the device to verify the output */
    const size_t origin[3] = {0, 0, 0};
    const size_t region[3] = {size_x, size_y, 1};
    err |= clEnqueueReadImage(commands, src_image, CL_TRUE, origin, region, 0, 0,
                              output, 0, NULL, NULL);
    if (err != CL_SUCCESS) {
        log_error("Error: clEnqueueReadBuffer failed\n");
    return err;
    }

    /* Verify the correctness of kernel result */
  err = 0;
    for (i = 0; i < size; i++) {
        if (output[i] != (input[i] + 3)) {
      log_error("Error: mismatch at index %d\n", i);
            err++;
            break;
        }
    }

  /* Release programs, kernel, contect, and memory objects */
    clReleaseMemObject(src_image);
    clReleaseProgram(program);
    clReleaseKernel(kernel);

  /* Deallocate arrays */
    free(input);
    free(output);

    return err;
}