From 93a1dc3a3a3e94c71bc6d90a0cf640d8c0ac05a8 Mon Sep 17 00:00:00 2001 From: Mats Petersson <6831237+Leporacanthicus@users.noreply.github.com> Date: Thu, 15 Aug 2019 10:18:57 +0100 Subject: [PATCH] Fix leaking memory and add enum for address spaces (#435) Test was not cleanig up all of the kernels and programs that it creates, which results in our driver finding that the common context still has references when exiting. The reason for not cleaning all kernels and programs was that it used a hard-coded constant 3 instead of the actual number of address spaces. This fixes the leak, and makes the code a little clearer by adding an enum describing each address space, rather than using magic numbers. Signed-off-by: Mats Petersson --- test_conformance/half/Test_vLoadHalf.c | 58 ++++++++++++------------- test_conformance/half/Test_vStoreHalf.c | 2 - test_conformance/half/main.c | 2 +- test_conformance/half/tests.h | 11 +++++ 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/test_conformance/half/Test_vLoadHalf.c b/test_conformance/half/Test_vLoadHalf.c index da7d16e7..7bc756d0 100644 --- a/test_conformance/half/Test_vLoadHalf.c +++ b/test_conformance/half/Test_vLoadHalf.c @@ -20,8 +20,6 @@ #include "cl_utils.h" #include "tests.h" -extern const char *addressSpaceNames[]; - static inline float half2float( cl_ushort us ) { uint32_t u = us; @@ -65,8 +63,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) { cl_int error; int vectorSize; - cl_program programs[kVectorSizeCount+kStrangeVectorSizeCount][4] = {{0}}; - cl_kernel kernels[kVectorSizeCount+kStrangeVectorSizeCount][4] = {{0}}; + cl_program programs[kVectorSizeCount+kStrangeVectorSizeCount][AS_NumAddressSpaces] = {{0}}; + cl_kernel kernels[kVectorSizeCount+kStrangeVectorSizeCount][AS_NumAddressSpaces] = {{0}}; uint64_t time[kVectorSizeCount+kStrangeVectorSizeCount] = {0}; uint64_t min_time[kVectorSizeCount+kStrangeVectorSizeCount] = {0}; size_t q; @@ -299,8 +297,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) if(g_arrVecSizes[vectorSize] != 3) { - programs[vectorSize][0] = MakeProgram( device, source, sizeof( source) / sizeof( source[0]) ); - if( NULL == programs[ vectorSize ][0] ) { + programs[vectorSize][AS_Global] = MakeProgram( device, source, sizeof( source) / sizeof( source[0]) ); + if( NULL == programs[ vectorSize ][AS_Global] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create program.\n" ); for ( q= 0; q < sizeof( source) / sizeof( source[0]); q++) @@ -309,8 +307,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) } else { } } else if(aligned) { - programs[vectorSize][0] = MakeProgram( device, sourceV3aligned, sizeof( sourceV3aligned) / sizeof( sourceV3aligned[0]) ); - if( NULL == programs[ vectorSize ][0] ) { + programs[vectorSize][AS_Global] = MakeProgram( device, sourceV3aligned, sizeof( sourceV3aligned) / sizeof( sourceV3aligned[0]) ); + if( NULL == programs[ vectorSize ][AS_Global] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create program.\n" ); for ( q= 0; q < sizeof( sourceV3aligned) / sizeof( sourceV3aligned[0]); q++) @@ -319,8 +317,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) } else { } } else { - programs[vectorSize][0] = MakeProgram( device, sourceV3, sizeof( sourceV3) / sizeof( sourceV3[0]) ); - if( NULL == programs[ vectorSize ][0] ) { + programs[vectorSize][AS_Global] = MakeProgram( device, sourceV3, sizeof( sourceV3) / sizeof( sourceV3[0]) ); + if( NULL == programs[ vectorSize ][AS_Global] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create program.\n" ); for ( q= 0; q < sizeof( sourceV3) / sizeof( sourceV3[0]); q++) @@ -329,8 +327,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) } } - kernels[ vectorSize ][0] = clCreateKernel( programs[ vectorSize ][0], "test", &error ); - if( NULL == kernels[vectorSize][0] ) + kernels[ vectorSize ][AS_Global] = clCreateKernel( programs[ vectorSize ][AS_Global], "test", &error ); + if( NULL == kernels[vectorSize][AS_Global] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create kernel. (%d)\n", error ); @@ -354,8 +352,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) source_ptr = source_private2; source_size = sizeof( source_private2) / sizeof( source_private2[0]); } - programs[vectorSize][1] = MakeProgram( device, source_ptr, source_size ); - if( NULL == programs[ vectorSize ][1] ) + programs[vectorSize][AS_Private] = MakeProgram( device, source_ptr, source_size ); + if( NULL == programs[ vectorSize ][AS_Private] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create private program.\n" ); @@ -364,8 +362,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) return -1; } - kernels[ vectorSize ][1] = clCreateKernel( programs[ vectorSize ][1], "test", &error ); - if( NULL == kernels[vectorSize][1] ) + kernels[ vectorSize ][AS_Private] = clCreateKernel( programs[ vectorSize ][AS_Private], "test", &error ); + if( NULL == kernels[vectorSize][AS_Private] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create private kernel. (%d)\n", error ); @@ -387,8 +385,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) source_ptr = source_local2; source_size = sizeof( source_local2) / sizeof( source_local2[0]); } - programs[vectorSize][2] = MakeProgram( device, source_ptr, source_size ); - if( NULL == programs[ vectorSize ][2] ) + programs[vectorSize][AS_Local] = MakeProgram( device, source_ptr, source_size ); + if( NULL == programs[ vectorSize ][AS_Local] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create local program.\n" ); @@ -397,8 +395,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) return -1; } - kernels[ vectorSize ][2] = clCreateKernel( programs[ vectorSize ][2], "test", &error ); - if( NULL == kernels[vectorSize][2] ) + kernels[ vectorSize ][AS_Local] = clCreateKernel( programs[ vectorSize ][AS_Local], "test", &error ); + if( NULL == kernels[vectorSize][AS_Local] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create local kernel. (%d)\n", error ); @@ -407,8 +405,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) if(g_arrVecSizes[vectorSize] == 3) { if(aligned) { - programs[vectorSize][3] = MakeProgram( device, source_constantV3aligned, sizeof(source_constantV3aligned) / sizeof( source_constantV3aligned[0]) ); - if( NULL == programs[ vectorSize ][3] ) + programs[vectorSize][AS_Constant] = MakeProgram( device, source_constantV3aligned, sizeof(source_constantV3aligned) / sizeof( source_constantV3aligned[0]) ); + if( NULL == programs[ vectorSize ][AS_Constant] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create constant program.\n" ); @@ -417,8 +415,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) return -1; } } else { - programs[vectorSize][3] = MakeProgram( device, source_constantV3, sizeof(source_constantV3) / sizeof( source_constantV3[0]) ); - if( NULL == programs[ vectorSize ][3] ) + programs[vectorSize][AS_Constant] = MakeProgram( device, source_constantV3, sizeof(source_constantV3) / sizeof( source_constantV3[0]) ); + if( NULL == programs[ vectorSize ][AS_Constant] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create constant program.\n" ); @@ -428,8 +426,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) } } } else { - programs[vectorSize][3] = MakeProgram( device, source_constant, sizeof(source_constant) / sizeof( source_constant[0]) ); - if( NULL == programs[ vectorSize ][3] ) + programs[vectorSize][AS_Constant] = MakeProgram( device, source_constant, sizeof(source_constant) / sizeof( source_constant[0]) ); + if( NULL == programs[ vectorSize ][AS_Constant] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create constant program.\n" ); @@ -439,8 +437,8 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) } } - kernels[ vectorSize ][3] = clCreateKernel( programs[ vectorSize ][3], "test", &error ); - if( NULL == kernels[vectorSize][3] ) + kernels[ vectorSize ][AS_Constant] = clCreateKernel( programs[ vectorSize ][AS_Constant], "test", &error ); + if( NULL == kernels[vectorSize][AS_Constant] ) { gFailCount++; vlog_error( "\t\tFAILED -- Failed to create constant kernel. (%d)\n", error ); @@ -491,7 +489,7 @@ int Test_vLoadHalf_private( cl_device_id device, bool aligned ) for( vectorSize = minVectorSize; vectorSize < kLastVectorSizeToTest; vectorSize++) { // here we loop through vector sizes, 3 is last - for ( addressSpace = 0; addressSpace < 4; addressSpace++) { + for ( addressSpace = 0; addressSpace < AS_NumAddressSpaces; addressSpace++) { uint32_t pattern = 0x7fffdead; /* @@ -604,7 +602,7 @@ exit: //clean up for( vectorSize = minVectorSize; vectorSize < kLastVectorSizeToTest; vectorSize++) { - for ( addressSpace = 0; addressSpace < 3; addressSpace++) { + for ( addressSpace = 0; addressSpace < AS_NumAddressSpaces; addressSpace++) { clReleaseKernel( kernels[ vectorSize ][addressSpace] ); clReleaseProgram( programs[ vectorSize ][addressSpace] ); } diff --git a/test_conformance/half/Test_vStoreHalf.c b/test_conformance/half/Test_vStoreHalf.c index 5fb9fff0..2b24a68c 100644 --- a/test_conformance/half/Test_vStoreHalf.c +++ b/test_conformance/half/Test_vStoreHalf.c @@ -21,8 +21,6 @@ #include "cl_utils.h" #include "tests.h" -extern const char *addressSpaceNames[]; - typedef struct ComputeReferenceInfoF_ { float *x; diff --git a/test_conformance/half/main.c b/test_conformance/half/main.c index 50a6bece..dde8e4f6 100644 --- a/test_conformance/half/main.c +++ b/test_conformance/half/main.c @@ -39,7 +39,7 @@ const char ** argList = NULL; size_t argCount = 0; char appName[64] = "ctest"; -const char *addressSpaceNames[] = {"global", "private", "local", "constant"}; +const char *addressSpaceNames[AS_NumAddressSpaces] = {"global", "private", "local", "constant"}; #pragma mark - #pragma mark Declarations diff --git a/test_conformance/half/tests.h b/test_conformance/half/tests.h index be1c28e4..2b14924c 100644 --- a/test_conformance/half/tests.h +++ b/test_conformance/half/tests.h @@ -16,6 +16,17 @@ #ifndef TESTS_H #define TESTS_H +typedef enum +{ + AS_Global, + AS_Private, + AS_Local, + AS_Constant, + AS_NumAddressSpaces +} AddressSpaceEnum; + +extern const char *addressSpaceNames[AS_NumAddressSpaces]; + int test_vload_half( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements ); int test_vloada_half( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements );