def test_cw_gen_declaration(): '''Check the CWriter class gen_declaration method produces the expected declarations. ''' cwriter = CWriter() # Basic entries symbol = DataSymbol("dummy1", INTEGER_TYPE) result = cwriter.gen_declaration(symbol) assert result == "int dummy1" symbol = DataSymbol("dummy1", CHARACTER_TYPE) result = cwriter.gen_declaration(symbol) assert result == "char dummy1" symbol = DataSymbol("dummy1", BOOLEAN_TYPE) result = cwriter.gen_declaration(symbol) assert result == "bool dummy1" # Array argument array_type = ArrayType(REAL_TYPE, [2, ArrayType.Extent.ATTRIBUTE, 2]) symbol = DataSymbol("dummy2", array_type, interface=ArgumentInterface( ArgumentInterface.Access.READ)) result = cwriter.gen_declaration(symbol) assert result == "double * restrict dummy2" # Array with unknown access array_type = ArrayType(INTEGER_TYPE, [2, ArrayType.Extent.ATTRIBUTE, 2]) symbol = DataSymbol("dummy2", array_type, interface=ArgumentInterface( ArgumentInterface.Access.UNKNOWN)) result = cwriter.gen_declaration(symbol) assert result == "int * restrict dummy2" # Check invalid datatype produces and error symbol._datatype = "invalid" with pytest.raises(NotImplementedError) as error: _ = cwriter.gen_declaration(symbol) assert "Could not generate the C definition for the variable 'dummy2', " \ "type 'invalid' is currently not supported." in str(error.value)
def test_symtab_implementation_for_opencl(): ''' Tests that the GOcean specialised Symbol Table implements the abstract properties needed to generate OpenCL. ''' kschedule = GOKernelSchedule('test') # Test symbol table without any kernel argument with pytest.raises(GenerationError) as err: _ = kschedule.symbol_table.iteration_indices assert ("GOcean 1.0 API kernels should always have at least two " "arguments representing the iteration indices but the Symbol " "Table for kernel 'test' has only 0 argument(s)." in str(err.value)) # Test symbol table with 1 kernel argument arg1 = DataSymbol("arg1", INTEGER_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READ)) kschedule.symbol_table.add(arg1) kschedule.symbol_table.specify_argument_list([arg1]) with pytest.raises(GenerationError) as err: _ = kschedule.symbol_table.iteration_indices assert ("GOcean 1.0 API kernels should always have at least two " "arguments representing the iteration indices but the Symbol " "Table for kernel 'test' has only 1 argument(s)." in str(err.value)) # Test symbol table with 2 kernel argument arg2 = DataSymbol("arg2", INTEGER_TYPE, interface=ArgumentInterface( ArgumentInterface.Access.READ)) kschedule.symbol_table.add(arg2) kschedule.symbol_table.specify_argument_list([arg1, arg2]) iteration_indices = kschedule.symbol_table.iteration_indices assert iteration_indices[0] is arg1 assert iteration_indices[1] is arg2 # Test symbol table with 3 kernel argument array_type = ArrayType(REAL_TYPE, [10, 10]) arg3 = DataSymbol("buffer1", array_type, interface=ArgumentInterface( ArgumentInterface.Access.READ)) kschedule.symbol_table.add(arg3) kschedule.symbol_table.specify_argument_list([arg1, arg2, arg3]) iteration_indices = kschedule.symbol_table.iteration_indices data_args = kschedule.symbol_table.data_arguments assert iteration_indices[0] is arg1 assert iteration_indices[1] is arg2 assert data_args[0] is arg3 # Test gen_ocl with wrong iteration indices types and shapes. arg1._datatype._intrinsic = ScalarType.Intrinsic.REAL with pytest.raises(GenerationError) as err: _ = kschedule.symbol_table.iteration_indices assert ("GOcean 1.0 API kernels first argument should be a scalar " "integer but got 'Scalar<REAL, UNDEFINED>' for kernel 'test'." in str(err.value)) arg1._datatype._intrinsic = ScalarType.Intrinsic.INTEGER # restore arg2._datatype = ArrayType(INTEGER_TYPE, [10]) with pytest.raises(GenerationError) as err: _ = kschedule.symbol_table.iteration_indices assert ("GOcean 1.0 API kernels second argument should be a scalar integer" " but got 'Array<Scalar<INTEGER, UNDEFINED>, shape=[10]>' for " "kernel 'test'." in str(err.value))