// // 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 "harness/compat.h" #include #include #include #include #include "procs.h" #include "harness/testHarness.h" #include "harness/errorHelpers.h" #include "harness/conversions.h" //--- the code for the kernel executables static const char *write_kernel_code = "\n" "__kernel void test_write(__global unsigned char *src, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(dstimg) + tid_x;\n" " float4 color;\n" "\n" " indx *= 4;\n" " color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);\n" " color /= (float4)(255.0f, 255.0f, 255.0f, 255.0f);\n" " write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n"; //--- the verify functions static int verify_subimage( unsigned char *src, unsigned char *dst, size_t srcx, size_t srcy, size_t dstx, size_t dsty, size_t subw, size_t subh, size_t pitch, size_t element_pitch ) { size_t i, j, k; size_t srcj, dstj; size_t srcLoc, dstLoc; for( j = 0; j < subh; j++ ){ srcj = ( j + srcy ) * pitch * element_pitch; dstj = ( j + dsty ) * pitch * element_pitch; for( i = 0; i < subw; i++ ){ srcLoc = srcj + ( i + srcx ) * element_pitch; dstLoc = dstj + ( i + dstx ) * element_pitch; for( k = 0; k < element_pitch; k++ ){ // test each channel if( src[srcLoc+k] != dst[dstLoc+k] ){ return -1; } } } } return 0; } static int verify_copy_array( int *inptr, int *outptr, int n ) { int i; for( i = 0; i < n; i++ ) { if( outptr[i] != inptr[i] ) return -1; } return 0; } //----- helper functions static cl_uchar *generate_image( int n, MTdata d ) { cl_uchar *ptr = (cl_uchar *)malloc( n ); int i; for( i = 0; i < n; i++ ) ptr[i] = (cl_uchar)genrand_int32(d); return ptr; } static int copy_size( cl_device_id device, cl_context context, cl_command_queue queue, int num_elements, MTdata d ) { cl_mem streams[2]; cl_event copyEvent; cl_ulong queueStart, submitStart, writeStart, writeEnd; cl_int *int_input_ptr, *int_output_ptr; int err = 0; int i; int_input_ptr = (cl_int*)malloc(sizeof(cl_int) * num_elements); int_output_ptr = (cl_int*)malloc(sizeof(cl_int) * num_elements); streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int) * num_elements, NULL, &err); if( !streams[0] ){ log_error("clCreateBuffer failed\n"); return -1; } streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int) * num_elements, NULL, &err); if( !streams[1] ){ log_error("clCreateBuffer failed\n"); return -1; } for (i=0; i> 30; // seed with incorrect data } err = clEnqueueWriteBuffer( queue, streams[0], true, 0, sizeof(cl_int)*num_elements, (void *)int_input_ptr, 0, NULL, NULL ); if( err != CL_SUCCESS ){ print_error( err, "clWriteArray failed" ); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } err = clEnqueueCopyBuffer( queue, streams[0], streams[1], 0, 0, sizeof(cl_int)*num_elements, 0, NULL, ©Event ); if( err != CL_SUCCESS ){ print_error( err, "clCopyArray failed" ); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } // This synchronization point is needed in order to assume the data is valid. // Getting profiling information is not a synchronization point. err = clWaitForEvents( 1, ©Event ); if( err != CL_SUCCESS ) { clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } // test profiling while( ( err = clGetEventProfilingInfo( copyEvent, CL_PROFILING_COMMAND_QUEUED, sizeof( cl_ulong ), &queueStart, NULL ) ) == CL_PROFILING_INFO_NOT_AVAILABLE ); if( err != CL_SUCCESS ){ print_error( err, "clGetEventProfilingInfo failed" ); clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } while( ( err = clGetEventProfilingInfo( copyEvent, CL_PROFILING_COMMAND_SUBMIT, sizeof( cl_ulong ), &submitStart, NULL ) ) == CL_PROFILING_INFO_NOT_AVAILABLE ); if( err != CL_SUCCESS ){ print_error( err, "clGetEventProfilingInfo failed" ); clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } err = clGetEventProfilingInfo( copyEvent, CL_PROFILING_COMMAND_START, sizeof( cl_ulong ), &writeStart, NULL ); if( err != CL_SUCCESS ){ print_error( err, "clGetEventProfilingInfo failed" ); clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } err = clGetEventProfilingInfo( copyEvent, CL_PROFILING_COMMAND_END, sizeof( cl_ulong ), &writeEnd, NULL ); if( err != CL_SUCCESS ){ print_error( err, "clGetEventProfilingInfo failed" ); clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } err = clEnqueueReadBuffer( queue, streams[1], true, 0, sizeof(cl_int)*num_elements, (void *)int_output_ptr, 0, NULL, NULL ); if( err != CL_SUCCESS ){ print_error( err, "clEnqueueReadBuffer failed" ); clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); return -1; } if( verify_copy_array(int_input_ptr, int_output_ptr, num_elements) ){ log_error( "test failed\n" ); err = -1; } else{ log_info( "test passed\n" ); err = 0; } // cleanup clReleaseEvent(copyEvent); clReleaseMemObject( streams[0] ); clReleaseMemObject( streams[1] ); free( (void *)int_output_ptr ); free( (void *)int_input_ptr ); if (check_times(queueStart, submitStart, writeStart, writeEnd, device)) err = -1; return err; } // end copy_size() static int copy_partial_size( cl_device_id device, cl_context context, cl_command_queue queue, int num_elements, cl_uint srcStart, cl_uint dstStart, int size, MTdata d ) { cl_mem streams[2]; cl_event copyEvent; cl_ulong queueStart, submitStart, writeStart, writeEnd; cl_int *inptr, *outptr; int err = 0; int i; inptr = (cl_int *)malloc(sizeof(cl_int) * num_elements); outptr = (cl_int *)malloc(sizeof(cl_int) * num_elements); streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int) * num_elements, NULL, &err); if (!streams[0]) { log_error("clCreateBuffer failed\n"); return -1; } streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(cl_int) * num_elements, NULL, &err); if (!streams[1]) { log_error("clCreateBuffer failed\n"); return -1; } for (i=0; i