diff --git a/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp b/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp index cc6f94acaf..cb57f9babe 100644 --- a/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp +++ b/Modules/OpenCL/Testing/mitkOclBinaryThresholdImageFilterTest.cpp @@ -1,111 +1,111 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include // itk filter for reference computation #include #include #include using namespace mitk; /** This function is testing the class mitk::OclContextManager. */ -int mitkOclBinaryThresholdImageFilterTest( int /*argc*/, char**/* argv[]*/ ) +int mitkOclBinaryThresholdImageFilterTest( int argc, char* argv[] ) { MITK_TEST_BEGIN("mitkOclBinaryThresholdImageFilterTest"); ServiceReference ref = GetModuleContext()->GetServiceReference(); MITK_TEST_CONDITION_REQUIRED( ref != 0, "Got valid ServiceReference" ); OclResourceService* resources = GetModuleContext()->GetService(ref); MITK_TEST_CONDITION_REQUIRED( resources != NULL, "OpenCL Resource service available." ); cl_context gpuContext = resources->GetContext(); MITK_TEST_CONDITION_REQUIRED( gpuContext != NULL, "Got not-null OpenCL context."); cl_device_id gpuDevice = resources->GetCurrentDevice(); MITK_TEST_CONDITION_REQUIRED( gpuDevice != NULL, "Got not-null OpenCL device."); //Create a random reference image mitk::Image::Pointer inputImage = mitk::ImageGenerator::GenerateRandomImage(119, 204, 52, 1, // dimension 1.0f, 1.0f, 1.0f, // spacing 255, 0); // max, min MITK_TEST_CONDITION_REQUIRED( inputImage.IsNotNull(), "Input (random) mitk::Image object instantiated."); // FIXME: could also be random values int upperThr = 255; int lowerThr = 60; int outsideVal = 0; int insideVal = 100; mitk::OclBinaryThresholdImageFilter* oclFilter = new mitk::OclBinaryThresholdImageFilter; MITK_TEST_CONDITION_REQUIRED( oclFilter != NULL, "Filter was created. "); oclFilter->SetInput( inputImage ); oclFilter->SetUpperThreshold( upperThr ); oclFilter->SetLowerThreshold( lowerThr ); oclFilter->SetOutsideValue( outsideVal ); oclFilter->SetInsideValue( insideVal ); oclFilter->Update(); mitk::Image::Pointer outputImage = mitk::Image::New(); outputImage = oclFilter->GetOutput(); MITK_TEST_CONDITION_REQUIRED( outputImage.IsNotNull(), "Filter returned an not-NULL image. "); // reference computation typedef itk::Image< unsigned char, 3> ImageType; typedef itk::BinaryThresholdImageFilter< ImageType, ImageType > ThresholdFilterType; ImageType::Pointer itkInputImage = ImageType::New(); CastToItkImage( inputImage, itkInputImage ); ThresholdFilterType::Pointer refThrFilter = ThresholdFilterType::New(); refThrFilter->SetInput( itkInputImage ); refThrFilter->SetLowerThreshold( lowerThr ); refThrFilter->SetUpperThreshold( upperThr ); refThrFilter->SetOutsideValue( outsideVal ); refThrFilter->SetInsideValue( insideVal ); typedef itk::SubtractImageFilter< ImageType, ImageType > SubtractFilterType; SubtractFilterType::Pointer subFilt = SubtractFilterType::New(); ImageType::Pointer gpuReferenceImage = ImageType::New(); CastToItkImage( oclFilter->GetOutput() ,gpuReferenceImage ); subFilt->SetInput1( refThrFilter->GetOutput() ); subFilt->SetInput2( gpuReferenceImage ); typedef itk::StatisticsImageFilter< ImageType > StatFilterType; StatFilterType::Pointer stats = StatFilterType::New(); stats->SetInput( subFilt->GetOutput() ); stats->Update(); MITK_TEST_CONDITION( stats->GetMaximum() == 0, "Maximal value in the difference image is 0."); MITK_TEST_CONDITION( stats->GetMinimum() == 0, "Minimal value in the difference image is 0.") MITK_TEST_END(); } diff --git a/Modules/OpenCL/Testing/mitkOclResourceServiceTest.cpp b/Modules/OpenCL/Testing/mitkOclResourceServiceTest.cpp index d42942b7f2..b20f4bea4f 100644 --- a/Modules/OpenCL/Testing/mitkOclResourceServiceTest.cpp +++ b/Modules/OpenCL/Testing/mitkOclResourceServiceTest.cpp @@ -1,115 +1,115 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTestingMacros.h" #include "mitkOclUtils.h" #include #include #include #include "mitkOclResourceService.h" #include "mitkException.h" #include #include using namespace mitk; /** This function is testing the class mitk::OclContextManager. */ -int mitkOclResourceServiceTest( int /*argc*/, char** /*argv[]*/ ) +int mitkOclResourceServiceTest( int argc, char* argv[] ) { MITK_TEST_BEGIN("mitkOclResourceServiceTest"); ServiceReference ref = GetModuleContext()->GetServiceReference(); MITK_TEST_CONDITION_REQUIRED( ref != NULL, "Resource service available." ); OclResourceService* resources = GetModuleContext()->GetService(ref); MITK_TEST_CONDITION_REQUIRED( resources != NULL, "Resource service available." ); cl_context first = resources->GetContext(); MITK_TEST_CONDITION_REQUIRED(first != NULL, "Got not-null OpenCL context."); OclResourceService* resources_2 = GetModuleContext()->GetService(ref); MITK_TEST_CONDITION_REQUIRED( resources == resources_2, "Same resource reference the second time." ); cl_context second = resources_2->GetContext(); MITK_TEST_CONDITION_REQUIRED( first == second, "Both return same context"); // further tests requires for valid context if( first ) { cl_image_format testFmt; testFmt.image_channel_data_type = CL_FLOAT; testFmt.image_channel_order = CL_RGBA; MITK_TEST_CONDITION( resources->GetIsFormatSupported( &testFmt ), "Checking if format CL_FLOAT / CL_RGBA supported." ); } // create test program const std::string testProgramSource = "__kernel void testKernel( __global uchar* buffer ){ \ const unsigned int globalPosX = get_global_id(0); \ buffer[globalPosX] = buffer[globalPosX] + 1;}"; cl_int err = 0; size_t progSize = testProgramSource.length(); const char* progSource = testProgramSource.c_str(); cl_program testProgram = clCreateProgramWithSource( first, 1, &progSource, &progSize, &err ); MITK_TEST_CONDITION_REQUIRED( err == CL_SUCCESS, "Test program loaded succesfully."); err = clBuildProgram(testProgram, 0, NULL, NULL, NULL, NULL); MITK_TEST_CONDITION_REQUIRED( err == CL_SUCCESS, "Test program built succesfully."); resources->InsertProgram( testProgram, "test_program", true); MITK_TEST_CONDITION( resources->GetProgram("test_program") == testProgram, "Program correctly stored by ResourceService"); // the manger throws exception when accessing non-existant programs MITK_TEST_FOR_EXCEPTION( mitk::Exception, resources->GetProgram("blah"); ); // another test source, this one does not compile const std::string testProgramSource_notCompiling = "__kernel void testKernel( __global uchar* buffer ){ \ const unsigned intt globalPosX = get_global_id(0); }"; progSize = testProgramSource_notCompiling.length(); const char* progSource2 = testProgramSource_notCompiling.c_str(); cl_program notComp_testProgram = clCreateProgramWithSource( first, 1, &progSource2, &progSize, &err ); // the error in the source code has no influence on loading the program MITK_TEST_CONDITION_REQUIRED( err == CL_SUCCESS, "Test program 2 loaded succesfully."); err = clBuildProgram(notComp_testProgram, 0, NULL, NULL, NULL, NULL); MITK_TEST_CONDITION_REQUIRED( err == CL_BUILD_PROGRAM_FAILURE, "Test program 2 failed to build."); std::cout << " --> The (expected) OpenCL Build Error occured : ";// << GetOclErrorString(err); resources->InsertProgram( notComp_testProgram, "test_program_failed", true); MITK_TEST_CONDITION( resources->GetProgram("test_program_failed") == notComp_testProgram, "Program correctly stored by ResourceService"); // calling the InvalidateStorage() will result in removing the _failed test program inserted above resources->InvalidateStorage(); // the second test program should no more exist in the storage, hence we await an exception MITK_TEST_FOR_EXCEPTION( mitk::Exception, resources->GetProgram("test_program_failed"); ); MITK_TEST_END(); } US_INITIALIZE_MODULE("OpenCLTestDriver", "", "", "" ) diff --git a/Modules/OpenCL/mitkOclResourceServiceImpl_Private.cpp b/Modules/OpenCL/mitkOclResourceServiceImpl_Private.cpp index 5a64b375ec..9d0e999741 100644 --- a/Modules/OpenCL/mitkOclResourceServiceImpl_Private.cpp +++ b/Modules/OpenCL/mitkOclResourceServiceImpl_Private.cpp @@ -1,254 +1,254 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkOclResourceServiceImpl_p.h" OclResourceService::~OclResourceService() { } OclResourceServiceImpl::OclResourceServiceImpl() : m_Context(NULL), m_Devices(NULL), m_ProgramStorage() { this->CreateContext(); } OclResourceServiceImpl::~OclResourceServiceImpl() { // if map non-empty, release all remaining if( m_ProgramStorage.size() ) { ProgramMapType::iterator it = m_ProgramStorage.begin(); while(it != m_ProgramStorage.end() ) { clReleaseProgram( it->second ); m_ProgramStorage.erase( it++ ); } } // if devices were allocated, delete if(m_Devices) { // TODO: Available first in OpenCL 1.2 : query the device for CL_PLATFORM_VERSION // through clGetPlatformInfo // clReleaseDevice(m_Devices[0]); delete [] m_Devices; } // if context was created release it if(m_Context) clReleaseContext( this->m_Context ); } cl_context OclResourceServiceImpl::GetContext() const { return m_Context; } cl_command_queue OclResourceServiceImpl::GetCommandQueue() const { // check if queue valid cl_int clErr = clGetCommandQueueInfo( m_CommandQueue, CL_QUEUE_CONTEXT, NULL, NULL, NULL ); if( clErr != CL_SUCCESS ) { MITK_WARN << "Have no valid command queue. Query returned : " << GetOclErrorAsString( clErr ); return NULL; } return this->m_CommandQueue; } cl_device_id OclResourceServiceImpl::GetCurrentDevice() const { return m_Devices[0]; } bool OclResourceServiceImpl::GetIsFormatSupported(cl_image_format *fmt) { cl_image_format temp; temp.image_channel_data_type = fmt->image_channel_data_type; temp.image_channel_order = fmt->image_channel_order; return (this->m_ImageFormats->GetNearestSupported(&temp, fmt)); } void OclResourceServiceImpl::PrintContextInfo() { if( m_Context == NULL){ MITK_ERROR("OpenCL.ResourceService") << "No OpenCL Context available "; } else { oclPrintDeviceInfo( m_Devices[0] ); } } void OclResourceServiceImpl::InsertProgram(cl_program _program_in, std::string name, bool forceOverride) { std::pair< ProgramMapType::iterator, bool> retValue; typedef std::pair< std::string, cl_program > MapElemPair; retValue = m_ProgramStorage.insert( MapElemPair(name, _program_in) ); // insertion failed, i.e. a program with same name exists if( !retValue.second ) { std::string overrideMsg(""); if( forceOverride ) { // overwrite old instance - m_ProgramStorage.at(name) = _program_in; + m_ProgramStorage[name] = _program_in; overrideMsg +=" The old program was overwritten!"; } MITK_WARN("OpenCL.ResourceService") << "The program " << name << " already exists." << overrideMsg; } } cl_program OclResourceServiceImpl::GetProgram(const std::string &name) const { ProgramMapType::const_iterator it; it = m_ProgramStorage.find(name); if( it != m_ProgramStorage.end() ) { return it->second; } mitkThrow() << "Requested OpenCL Program (" << name <<") not found."; } void OclResourceServiceImpl::InvalidateStorage() { // do nothing if no context present if(m_Context == NULL) return; // query the map ProgramMapType::iterator it = m_ProgramStorage.begin(); while(it != m_ProgramStorage.end() ) { // query the program build status cl_build_status status; unsigned int query = clGetProgramBuildInfo( it->second, m_Devices[0], CL_PROGRAM_BUILD_STATUS, sizeof(cl_int), &status, NULL ); MITK_DEBUG << "Quering status for " << it->first << std::endl; // remove program if no succesfull build // we need to pay attention to the increment of the iterator when erasing current element if( status != CL_BUILD_SUCCESS ) { MITK_DEBUG << " +-- Build failed " << std::endl; m_ProgramStorage.erase( it++ ); } else { ++it; } } } void OclResourceServiceImpl::RemoveProgram(const std::string& name) { ProgramMapType::iterator it = m_ProgramStorage.find(name); if( it != m_ProgramStorage.end() ) { m_ProgramStorage.erase(it); } else { MITK_WARN("OpenCL.ResourceService") << "Program name [" <m_Context = clCreateContext( 0, 1, &m_cdDevice, NULL, NULL, &clErr); CHECK_OCL_ERR( clErr ); // get the info size clErr = clGetContextInfo(m_Context, CL_CONTEXT_DEVICES, 0,NULL, &szParmDataBytes ); this->m_Devices = (cl_device_id*) malloc(szParmDataBytes); // get device info clErr = clGetContextInfo(m_Context, CL_CONTEXT_DEVICES, szParmDataBytes, m_Devices, NULL); CHECK_OCL_ERR( clErr ); // create command queue m_CommandQueue = clCreateCommandQueue(m_Context, m_Devices[0], 0, &clErr); CHECK_OCL_ERR( clErr ); this->PrintContextInfo( ); // collect available image formats for current context this->m_ImageFormats = mitk::OclImageFormats::New(); this->m_ImageFormats->SetGPUContext(m_Context); } catch( std::exception& e) { MITK_ERROR("OpenCL.ResourceService") << "Exception while creating context: \n" << e.what(); } }