std::vector<DeviceEntry> PlatformList::getDevices (cl_device_type type) const { std::vector<DeviceEntry> result; for (size_t i = 0; i < platforms ().size (); i++) { std::vector<DeviceEntry> d = platforms ()[i]->getDevices (type); result.insert (result.end (), d.begin (), d.end ()); } return result; }
//--------------------------------------------------------------------------- int OpenCLData::initDeviceList() { cl_uint numPlatforms; cl_uint numDevices; cl_int status; char sTemp[128]; // Get platform count status = clGetPlatformIDs(0, NULL, &numPlatforms); CL_STATUS(status, "clGetPlatformIDs"); if(numPlatforms <= 0) return FAILURE; // Get platform IDs std::unique_ptr<cl_platform_id[]> platforms(new cl_platform_id[numPlatforms]); status = clGetPlatformIDs(numPlatforms, platforms.get(), NULL); CL_STATUS(status, "clGetPlatformIDs"); for(cl_uint i = 0; i < numPlatforms; i++) { // Get device count status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices); CL_STATUS(status, "clGetDeviceIDs"); if(numDevices <= 0) continue; // Get device IDs std::unique_ptr<cl_device_id[]> devices(new cl_device_id[numDevices]); status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices, devices.get(), NULL); CL_STATUS(status, "clGetDeviceIDs"); for(cl_uint j = 0; j < numDevices; j++) { SDeviceInfo info; status = clGetDeviceInfo(devices[j], CL_DEVICE_TYPE, sizeof(cl_device_type), &info.deviceType, NULL); status |= clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, 128, sTemp, NULL); info.vendor = QString(sTemp).trimmed();; status |= clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 128, sTemp, NULL); info.name = QString(sTemp).trimmed(); status |= clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &info.computeUnits, NULL); status |= clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void*)&info.maxWorkGroupSize, NULL); status |= clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), (void*)&info.maxDims, NULL); status |= clGetDeviceInfo(devices[j], CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(size_t)*3, (void*)info.maxWorkItemSizes, NULL); status |= clGetDeviceInfo(devices[j], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(cl_uint), &info.maxClockFreq, NULL); CL_STATUS(status, "clGetDeviceInfo"); m_deviceInfo.append(info); m_deviceList.append(info.name); } } return SUCCESS; }
error* get_platforms(clobj_t **_platforms, uint32_t *num_platforms) { return c_handle_error([&] { *num_platforms = 0; pyopencl_call_guarded(clGetPlatformIDs, 0, nullptr, buf_arg(*num_platforms)); pyopencl_buf<cl_platform_id> platforms(*num_platforms); pyopencl_call_guarded(clGetPlatformIDs, platforms, buf_arg(*num_platforms)); *_platforms = buf_to_base<platform>(platforms).release(); }); }
/// Main function int main() { clxx::platforms platforms(clxx::get_platforms()); clxx::context context(clxx::make_context_properties(platforms[0]), clxx::device_type_t::all); float array[ARRAY_SIZE]; clxx::mem buffer(context, clxx::mem_flags_t::read_write, sizeof(array), array); std::cout << "buffer.get_type(): " << buffer.get_type() << std::endl; std::cout << "buffer.get_flags(): " << buffer.get_flags() << std::endl; std::cout << "buffer.get_size(): " << buffer.get_size() << std::endl; std::cout << "buffer.get_map_count(): " << buffer.get_map_count() << std::endl; std::cout << "buffer.get_reference_count(): " << buffer.get_reference_count() << std::endl; return 0; }
SEXP getPlatformIDs(){ //returns a list of platform ids cl_uint num_platforms = 0; clGetPlatformIDs(0, 0, &num_platforms); std::vector<cl_platform_id> platforms(num_platforms); clGetPlatformIDs(num_platforms, platforms.empty() ? NULL : &platforms.front(), &num_platforms); //for each platform in platforms add its pointer to the return list Rcpp::List result(platforms.size()); for (int i=0; i<platforms.size(); i++){ cl_platform_id *tempPlatformID = new cl_platform_id; *tempPlatformID = platforms[i]; Rcpp::XPtr<cl_platform_id> tempXPtr(tempPlatformID); result[i] = tempXPtr; } return result; }
void Platform::queryTotalPlatforms() { cl_int errorCode; cl_uint numberOfPlatforms; errorCode = clGetPlatformIDs( 0, 0, &numberOfPlatforms ); errorCheck( errorCode ); std::cout << "Found " << numberOfPlatforms << " platform(s)" << std::endl; std::vector<cl_platform_id> platforms(numberOfPlatforms); errorCheck( clGetPlatformIDs( numberOfPlatforms, platforms.data(), 0 ) ); for( unsigned int i = 0; i < platforms.size(); ++i ) { std::cout << "Platform " << i << std::endl; getPlatformInfo( platforms.at(0) ); std::cout << std::endl; } }
//------------------------------------------------------------------------------ void print_platforms() { cl_uint numPlatforms = 0; cl_platform_id platform = 0; cl_int status = clGetPlatformIDs(0, 0, &numPlatforms); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformIDs()" << std::endl; exit(EXIT_FAILURE); } if(numPlatforms < 1) { std::cout << "No OpenCL platform detected" << std::endl; exit(EXIT_SUCCESS); } typedef std::vector< cl_platform_id > PlatformIds; PlatformIds platforms(numPlatforms); status = clGetPlatformIDs(platforms.size(), &platforms[0], 0); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformIDs()" << std::endl; exit(EXIT_FAILURE); } std::vector< char > buf(0x10000, char(0)); int p = 0; std::cout << "\n***************************************************\n"; std::cout << "Number of platforms: " << platforms.size() << std::endl; for(PlatformIds::const_iterator i = platforms.begin(); i != platforms.end(); ++i, ++p) { std::cout << "\n-----------\n"; std::cout << "Platform " << p << std::endl; std::cout << "-----------\n"; status = ::clGetPlatformInfo(*i, CL_PLATFORM_VENDOR, buf.size(), &buf[ 0 ], 0 ); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformInfo(): " << std::endl; exit(EXIT_FAILURE); } std::cout << "Vendor: " << &buf[ 0 ] << '\n'; status = ::clGetPlatformInfo(*i, CL_PLATFORM_PROFILE, buf.size(), &buf[ 0 ], 0 ); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformInfo(): " << std::endl; exit(EXIT_FAILURE); } std::cout << "Profile: " << &buf[ 0 ] << '\n'; status = ::clGetPlatformInfo(*i, CL_PLATFORM_VERSION, buf.size(), &buf[ 0 ], 0 ); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformInfo(): " << std::endl; exit(EXIT_FAILURE); } std::cout << "Version: " << &buf[ 0 ] << '\n'; status = ::clGetPlatformInfo(*i, CL_PLATFORM_NAME, buf.size(), &buf[ 0 ], 0 ); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformInfo(): " << std::endl; exit(EXIT_FAILURE); } std::cout << "Name: " << &buf[ 0 ] << '\n'; status = ::clGetPlatformInfo(*i, CL_PLATFORM_EXTENSIONS, buf.size(), &buf[ 0 ], 0 ); if(status != CL_SUCCESS) { std::cerr << "ERROR - clGetPlatformInfo(): " << std::endl; exit(EXIT_FAILURE); } std::cout << "Extensions: " << &buf[ 0 ] << '\n'; print_devices(*i); std::cout << "\n===================================================\n"; } }
Context& initializeContextFromVA(VADisplay display) { (void)display; #if !defined(HAVE_VAAPI) NO_VAAPI_SUPPORT_ERROR; #elif !defined(HAVE_OPENCL) NO_OPENCL_SUPPORT_ERROR; #else contextInitialized = false; cl_uint numPlatforms; cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); if (status != CL_SUCCESS) CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get number of platforms"); if (numPlatforms == 0) CV_Error(cv::Error::OpenCLInitError, "OpenCL: No available platforms"); std::vector<cl_platform_id> platforms(numPlatforms); status = clGetPlatformIDs(numPlatforms, &platforms[0], NULL); if (status != CL_SUCCESS) CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get platform Id list"); // For CL-VA interop, we must find platform/device with "cl_intel_va_api_media_sharing" extension. // With standard initialization procedure, we should examine platform extension string for that. // But in practice, the platform ext string doesn't contain it, while device ext string does. // Follow Intel procedure (see tutorial), we should obtain device IDs by extension call. // Note that we must obtain function pointers using specific platform ID, and can't provide pointers in advance. // So, we iterate and select the first platform, for which we got non-NULL pointers, device, and CL context. int found = -1; cl_context context = 0; cl_device_id device = 0; for (int i = 0; i < (int)numPlatforms; ++i) { // Get extension function pointers clGetDeviceIDsFromVA_APIMediaAdapterINTEL = (clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn) clGetExtensionFunctionAddressForPlatform(platforms[i], "clGetDeviceIDsFromVA_APIMediaAdapterINTEL"); clCreateFromVA_APIMediaSurfaceINTEL = (clCreateFromVA_APIMediaSurfaceINTEL_fn) clGetExtensionFunctionAddressForPlatform(platforms[i], "clCreateFromVA_APIMediaSurfaceINTEL"); clEnqueueAcquireVA_APIMediaSurfacesINTEL = (clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn) clGetExtensionFunctionAddressForPlatform(platforms[i], "clEnqueueAcquireVA_APIMediaSurfacesINTEL"); clEnqueueReleaseVA_APIMediaSurfacesINTEL = (clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn) clGetExtensionFunctionAddressForPlatform(platforms[i], "clEnqueueReleaseVA_APIMediaSurfacesINTEL"); if (((void*)clGetDeviceIDsFromVA_APIMediaAdapterINTEL == NULL) || ((void*)clCreateFromVA_APIMediaSurfaceINTEL == NULL) || ((void*)clEnqueueAcquireVA_APIMediaSurfacesINTEL == NULL) || ((void*)clEnqueueReleaseVA_APIMediaSurfacesINTEL == NULL)) { continue; } // Query device list cl_uint numDevices = 0; status = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(platforms[i], CL_VA_API_DISPLAY_INTEL, display, CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0, NULL, &numDevices); if ((status != CL_SUCCESS) || !(numDevices > 0)) continue; numDevices = 1; // initializeContextFromHandle() expects only 1 device status = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(platforms[i], CL_VA_API_DISPLAY_INTEL, display, CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, numDevices, &device, NULL); if (status != CL_SUCCESS) continue; // Creating CL-VA media sharing OpenCL context cl_context_properties props[] = { CL_CONTEXT_VA_API_DISPLAY_INTEL, (cl_context_properties) display, CL_CONTEXT_INTEROP_USER_SYNC, CL_FALSE, // no explicit sync required 0 }; context = clCreateContext(props, numDevices, &device, NULL, NULL, &status); if (status != CL_SUCCESS) { clReleaseDevice(device); } else { found = i; break; } } if (found < 0) CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't create context for VA-API interop"); Context& ctx = Context::getDefault(false); initializeContextFromHandle(ctx, platforms[found], context, device); contextInitialized = true; return ctx; #endif }
void opencl_metainfo::initialize() { // get number of available platforms auto num_platforms = v1get<cl_uint>(CAF_CLF(clGetPlatformIDs)); // get platform ids std::vector<cl_platform_id> platforms(num_platforms); v2callcl(CAF_CLF(clGetPlatformIDs), num_platforms, platforms.data()); if (platforms.empty()) { throw std::runtime_error("no OpenCL platform found"); } // support multiple platforms -> "for (auto platform : platforms)"? auto platform = platforms.front(); // detect how many devices we got cl_uint num_devs = 0; cl_device_type dev_type = CL_DEVICE_TYPE_GPU; // try get some GPU devices and try falling back to CPU devices on error try { num_devs = v1get<cl_uint>(CAF_CLF(clGetDeviceIDs), platform, dev_type); } catch (std::runtime_error&) { dev_type = CL_DEVICE_TYPE_CPU; num_devs = v1get<cl_uint>(CAF_CLF(clGetDeviceIDs), platform, dev_type); } // get available devices std::vector<cl_device_id> ds(num_devs); v2callcl(CAF_CLF(clGetDeviceIDs), platform, dev_type, num_devs, ds.data()); std::vector<device_ptr> devices(num_devs); // lift raw pointer as returned by OpenCL to C++ smart pointers auto lift = [](cl_device_id ptr) { return device_ptr{ptr, false}; }; std::transform(ds.begin(), ds.end(), devices.begin(), lift); // create a context context_.reset(v2get(CAF_CLF(clCreateContext), nullptr, num_devs, ds.data(), pfn_notify, nullptr), false); for (auto& device : devices) { CAF_LOG_DEBUG("creating command queue for device(s)"); command_queue_ptr cmd_queue; try { cmd_queue.reset(v2get(CAF_CLF(clCreateCommandQueue), context_.get(), device.get(), unsigned{CL_QUEUE_PROFILING_ENABLE}), false); } catch (std::runtime_error&) { CAF_LOG_DEBUG("unable to create command queue for device"); } if (cmd_queue) { auto max_wgs = v3get<size_t>(CAF_CLF(clGetDeviceInfo), device.get(), unsigned{CL_DEVICE_MAX_WORK_GROUP_SIZE}); auto max_wid = v3get<cl_uint>(CAF_CLF(clGetDeviceInfo), device.get(), unsigned{CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS}); dim_vec max_wi_per_dim(max_wid); v2callcl(CAF_CLF(clGetDeviceInfo), device.get(), unsigned{CL_DEVICE_MAX_WORK_ITEM_SIZES}, sizeof(size_t) * max_wid, max_wi_per_dim.data()); devices_.push_back(device_info{std::move(device), std::move(cmd_queue), max_wgs, max_wid, max_wi_per_dim}); } } if (devices_.empty()) { std::string errstr = "could not create a command queue for any device"; CAF_LOG_ERROR(errstr); throw std::runtime_error(std::move(errstr)); } }
/* * Find and return the preferred OpenCL platform * In case that preferredPlatform is NULL, the ID of the first discovered platform will be returned */ cl_platform_id FindOpenCLPlatform(const char* preferredPlatform, cl_device_type deviceType) { cl_uint numPlatforms = 0; cl_int err = CL_SUCCESS; // Get (in numPlatforms) the number of OpenCL platforms available // No platform ID will be return, since platforms is NULL err = clGetPlatformIDs(0, NULL, &numPlatforms); if (CL_SUCCESS != err) { LogError("Error: clGetplatform_ids() to get num platforms returned %s.\n", TranslateOpenCLError(err)); return NULL; } LogInfo("Number of available platforms: %u\n", numPlatforms); if (0 == numPlatforms) { LogError("Error: No platforms found!\n"); return NULL; } std::vector<cl_platform_id> platforms(numPlatforms); // Now, obtains a list of numPlatforms OpenCL platforms available // The list of platforms available will be returned in platforms err = clGetPlatformIDs(numPlatforms, &platforms[0], NULL); if (CL_SUCCESS != err) { LogError("Error: clGetplatform_ids() to get platforms returned %s.\n", TranslateOpenCLError(err)); return NULL; } // Check if one of the available platform matches the preferred requirements for (cl_uint i = 0; i < numPlatforms; i++) { bool match = true; cl_uint numDevices = 0; // If the preferredPlatform is not NULL then check if platforms[i] is the required one // Otherwise, continue the check with platforms[i] if ((NULL != preferredPlatform) && (strlen(preferredPlatform) > 0)) { // In case we're looking for a specific platform match = CheckPreferredPlatformMatch(platforms[i], preferredPlatform); } // match is true if the platform's name is the required one or don't care (NULL) if (match) { // Obtains the number of deviceType devices available on platform // When the function failed we expect numDevices to be zero. // We ignore the function return value since a non-zero error code // could happen if this platform doesn't support the specified device type. err = clGetDeviceIDs(platforms[i], deviceType, 0, NULL, &numDevices); if (CL_SUCCESS != err) { LogError("clGetDeviceIDs() returned %s.\n", TranslateOpenCLError(err)); } if (0 != numDevices) { // There is at list one device that answer the requirements return platforms[i]; } } } return NULL; }
int main() try { sge::opencl::system opencl_system; fcppt::io::cout() << FCPPT_TEXT("Querying the number of available platforms...\n"); sge::opencl::platform::object_sequence &platforms( opencl_system.platforms()); if(platforms.empty()) { fcppt::io::cerr() << FCPPT_TEXT("Couldn't find any OpenCL platforms on your system.\n"); return EXIT_FAILURE; } fcppt::io::cout() << FCPPT_TEXT("Number of OpenCL platforms: ") << platforms.size() << FCPPT_TEXT("\n") << FCPPT_TEXT("Platform listing begin:\n") << FCPPT_TEXT("-----------------------\n"); sge::opencl::platform::object_sequence::size_type platform_index = 0; for( sge::opencl::platform::object const ¤t_platform : platforms ) { fcppt::io::cout() << FCPPT_TEXT("\tPlatform ") << platform_index++ << FCPPT_TEXT(":\n") << FCPPT_TEXT("\tName: ") << fcppt::from_std_string( current_platform.name() ) << FCPPT_TEXT("\n") << FCPPT_TEXT("\tVendor: ") << fcppt::from_std_string( current_platform.vendor() ) << FCPPT_TEXT("\n") << FCPPT_TEXT("Profile type: ") << (current_platform.profile() == sge::opencl::platform::profile_type::full ? fcppt::string(FCPPT_TEXT("full")) : fcppt::string(FCPPT_TEXT("embedded"))) << FCPPT_TEXT("\n") << FCPPT_TEXT("\tVersion: ") << current_platform.version().major_part() << FCPPT_TEXT(".") << current_platform.version().minor_part() << FCPPT_TEXT("\n"); if(!current_platform.version().platform_specific().empty()) fcppt::io::cout() << FCPPT_TEXT("\tPlatform specific version info: ") << fcppt::from_std_string(current_platform.version().platform_specific()) << FCPPT_TEXT("\n"); fcppt::io::cout() << FCPPT_TEXT("\tExtension list begin:\n") << FCPPT_TEXT("\t*********************\n") << FCPPT_TEXT("\t\t") << fcppt::from_std_string( boost::algorithm::join( current_platform.extensions(), std::string("\n\t\t"))) << FCPPT_TEXT("\r\t*********************\n") << FCPPT_TEXT("-----------------------\n"); } fcppt::io::cout() << FCPPT_TEXT("Platform listing end\n"); sge::opencl::platform::object_sequence::size_type chosen_platform_index; if(platforms.size() == 1) { chosen_platform_index = 0; } else { fcppt::io::cout() << FCPPT_TEXT("Your choice: "); do chosen_platform_index = query_value_from_user<sge::opencl::platform::object_sequence::size_type>( fcppt::io::cin()); while(chosen_platform_index >= platforms.size()); } fcppt::io::cout() << FCPPT_TEXT("List devices with properties? [y/n] "); fcppt::char_type list_devices; do list_devices = query_value_from_user<fcppt::char_type>( fcppt::io::cin()); while(list_devices != FCPPT_TEXT('y') && list_devices != FCPPT_TEXT('n')); sge::opencl::platform::object &chosen_platform = platforms[chosen_platform_index]; if(list_devices == FCPPT_TEXT('y')) { fcppt::io::cout() << FCPPT_TEXT("Number of devices on this platform: ") << chosen_platform.devices().size() << FCPPT_TEXT('\n') << FCPPT_TEXT("Device listing begin:\n") << FCPPT_TEXT("-----------------------\n"); for( sge::opencl::device::object const ¤t_device : chosen_platform.devices() ) { current_device.output_info( std::cout); fcppt::io::cout() << FCPPT_TEXT("-----------------------\n"); } fcppt::io::cout() << FCPPT_TEXT("-----------------------\n") << FCPPT_TEXT("Device listing end\n"); } fcppt::io::cout() << FCPPT_TEXT("Creating sge::systems object...\n"); sge::window::dim const window_dim{ 1024u, 768u }; sge::systems::instance< brigand::list< sge::systems::with_window, sge::systems::with_renderer< sge::systems::renderer_caps::core > > > const sys( sge::systems::make_list ( // FIXME: Move this higher? sge::systems::config() .log_settings( sge::systems::log_settings{ sge::log::option_container{ sge::log::option{ sge::opencl::log_location(), fcppt::log::level::verbose } } } ) ) ( sge::systems::window( sge::systems::window_source( sge::systems::original_window( sge::window::title( FCPPT_TEXT("Simple OpenCL example") ) ) ) ) .dont_show() ) ( sge::systems::renderer( sge::renderer::pixel_format::object( sge::renderer::pixel_format::color::depth32, sge::renderer::pixel_format::depth_stencil::off, sge::renderer::pixel_format::optional_multi_samples(), sge::renderer::pixel_format::srgb::no ), sge::renderer::display_mode::parameters( sge::renderer::display_mode::vsync::on, sge::renderer::display_mode::optional_object() ), sge::viewport::optional_resize_callback{ sge::viewport::center_on_resize( window_dim ) } ) ) ); fcppt::io::cout() << FCPPT_TEXT("Done. Creating a context with all devices on this platform...\n"); sge::opencl::device::object_ref_sequence const device_refs( fcppt::algorithm::map< sge::opencl::device::object_ref_sequence >( chosen_platform.devices(), []( sge::opencl::device::object &_device ) { return fcppt::make_ref( _device ); } ) ); sge::opencl::context::object main_context( sge::opencl::context::parameters( chosen_platform, device_refs ) .share_with( sys.renderer_device_core()) .error_callback( sge::opencl::context::error_callback{ &opencl_error_callback } ) ); fcppt::io::cout() << FCPPT_TEXT("Context created, listing available planar image formats (read/write)\n"); sge::opencl::memory_object::image::format_sequence const planar_image_formats = main_context.supported_planar_image_formats( CL_MEM_READ_WRITE); for( auto const format : planar_image_formats ) { sge::opencl::memory_object::image::format_output( std::cout, format ); std::cout << '\n'; } fcppt::io::cout() << FCPPT_TEXT("Listing available volume image formats (read/write)...\n"); sge::opencl::memory_object::image::format_sequence const volume_image_formats = main_context.supported_volume_image_formats( CL_MEM_READ_WRITE); for( auto const format : volume_image_formats ) { sge::opencl::memory_object::image::format_output( std::cout, format ); std::cout << '\n'; } fcppt::io::cout() << FCPPT_TEXT("Done, now creating a program..."); sge::opencl::program::object main_program( sys.log_context(), main_context, sge::opencl::program::source_string_sequence{ std::string( "__kernel void hello_kernel(" "float const multiplier," "__global float *input)" "{" "int gid = get_global_id(0);" "int lid = get_local_id(0);" "input[gid] = lid * multiplier;" "}" ) }, sge::opencl::program::optional_build_parameters()); fcppt::io::cout() << FCPPT_TEXT("Program created, building the program...\n"); volatile bool build_finished = false; main_program.build( sge::opencl::program::build_parameters() .notification_callback( sge::opencl::program::notification_callback{ std::bind( &program_build_finished, std::ref( build_finished ) ) } ) ); std::cout << "Waiting for build completion\n"; while(!build_finished) std::cout << "Build not finished yet\n"; fcppt::io::cout() << FCPPT_TEXT("Program built, now creating a kernel...\n"); sge::opencl::kernel::object main_kernel( main_program, sge::opencl::kernel::name( "hello_kernel")); fcppt::io::cout() << FCPPT_TEXT("Kernel created, now creating a vertex buffer...\n"); sge::renderer::vertex::declaration_unique_ptr const vertex_declaration( sys.renderer_device_core().create_vertex_declaration( sge::renderer::vertex::declaration_parameters( sge::renderer::vf::dynamic::make_format<vf::format>()))); sge::renderer::vertex::buffer_unique_ptr const vb( sys.renderer_device_core().create_vertex_buffer( sge::renderer::vertex::buffer_parameters( *vertex_declaration, sge::renderer::vf::dynamic::make_part_index< vf::format, vf::part >(), sge::renderer::vertex::count( 6u), sge::renderer::resource_flags_field{ sge::renderer::resource_flags::readable}))); fcppt::io::cout() << FCPPT_TEXT("Done, now creating OpenCL buffer from it\n"); sge::opencl::memory_object::buffer cl_vb( main_context, *vb, sge::opencl::memory_object::renderer_buffer_lock_mode::write_only); main_kernel.argument( sge::opencl::kernel::argument_index( 1u), cl_vb); main_kernel.argument( sge::opencl::kernel::argument_index( 0u), sge::opencl::kernel::numeric_type( static_cast<cl_float>( 2.0 ) ) ); fcppt::io::cout() << FCPPT_TEXT("Done, now creating a command queue\n"); sge::opencl::command_queue::object main_queue( device_refs[0].get(), main_context, sge::opencl::command_queue::execution_mode::out_of_order, sge::opencl::command_queue::profiling_mode::disabled); fcppt::io::cout() << FCPPT_TEXT("Done, now enqueueing kernel and running it\n"); { sge::opencl::memory_object::scoped_objects scoped_vb( main_queue, sge::opencl::memory_object::base_ref_sequence{ fcppt::reference_to_base< sge::opencl::memory_object::base >( fcppt::make_ref( cl_vb ) ) } ); sge::opencl::command_queue::enqueue_kernel( main_queue, main_kernel, sge::opencl::command_queue::global_dim1( sge::opencl::dim1( vb->linear_size() * 2u ) ), sge::opencl::command_queue::local_dim1( sge::opencl::dim1( 2u ) ), sge::opencl::event::sequence() ); } fcppt::io::cout() << FCPPT_TEXT("Now locking the vb for reading and printing the values\n"); { sge::renderer::vertex::scoped_lock const scoped_vb( *vb, sge::renderer::lock_mode::readwrite); typedef sge::renderer::vf::view<vf::part> vertex_view; vertex_view const vertices( scoped_vb.value()); for( auto const vertex : vertices ) { fcppt::io::cout() << vertex.get<vf::scalar_quantity>() << FCPPT_TEXT('\n'); } } fcppt::io::cout() << FCPPT_TEXT("Done\n"); } catch( fcppt::exception const &_error ) { fcppt::io::cerr() << _error.string() << FCPPT_TEXT('\n'); return EXIT_FAILURE; } catch( std::exception const &_error ) { std::cerr << _error.what() << '\n'; return EXIT_FAILURE; }
int ocl_t::init() { std::cout << "Query available compute devices ...\n"; cl_int err; cl_uint num; err = clGetPlatformIDs(0, 0, &num); if (err != CL_SUCCESS) { std::cerr << "Unable to get platforms\n"; return 0; } std::vector<cl_platform_id> platforms(num); err = clGetPlatformIDs(num, &platforms[0], &num); if (err != CL_SUCCESS) { std::cerr << "Unable to get platform ID\n"; return 0; } int device_counter = 0; for (size_t platform_id = 0; platform_id < num; platform_id++){ size_t dev_c, info_c; clGetPlatformInfo(platforms[platform_id], CL_PLATFORM_NAME, 0, NULL, &info_c); std::string platname; platname.resize(info_c); clGetPlatformInfo(platforms[platform_id], CL_PLATFORM_NAME, info_c, &platname[0], 0); std::cout << "Platform :" << platname << "\n"; cl_context_properties prop[] = { CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platforms[platform_id]), 0 }; context = clCreateContextFromType(prop, CL_DEVICE_TYPE_ALL, NULL, NULL, NULL); if (context == 0) { std::cerr << "Can't create OpenCL context\n"; return 0; } clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &dev_c); std::vector<cl_device_id> devices(dev_c / sizeof(cl_device_id)); clGetContextInfo(context, CL_CONTEXT_DEVICES, dev_c, &devices[0], 0); for (auto i = devices.begin(); i != devices.end(); i++){ clGetDeviceInfo(*i, CL_DEVICE_NAME, 0, NULL, &info_c); std::string devname; devname.resize(info_c); clGetDeviceInfo(*i, CL_DEVICE_NAME, info_c, &devname[0], 0); std::cout << "\tDevice " << device_counter++ << ": " << devname.c_str() << "\n"; pdpair_t pd; pd.device_id = i - devices.begin(); pd.platform_id = platform_id; ocl_device_list.push_back(pd); } clReleaseContext(context); } if (list_available_devices) return 0; cl_context_properties prop[] = { CL_CONTEXT_PLATFORM, reinterpret_cast<cl_context_properties>(platforms[ocl_device_list[opencl_device_id].platform_id]), 0 }; context = clCreateContextFromType(prop, CL_DEVICE_TYPE_ALL, NULL, NULL, NULL); if (context == 0) { std::cerr << "Can't create OpenCL context\n"; return 0; } size_t dev_c, info_c; clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &dev_c); std::vector<cl_device_id> devices(dev_c / sizeof(cl_device_id)); clGetContextInfo(context, CL_CONTEXT_DEVICES, dev_c, &devices[0], 0); device_used = devices[ocl_device_list[opencl_device_id].device_id]; clGetDeviceInfo(device_used, CL_DEVICE_NAME, 0, NULL, &info_c); std::string devname; devname.resize(info_c); clGetDeviceInfo(device_used, CL_DEVICE_NAME, info_c, &devname[0], 0); std::cout << "Execute on Device " << opencl_device_id << ": " << devname << std::endl; std::cout << "OK!\n"; queue = clCreateCommandQueue(context, device_used, 0, 0); if (queue == 0) { std::cerr << "Can't create command queue\n"; return 0; } cl_res = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_float)* iterations, NULL, NULL); if (cl_res == 0) { std::cerr << "Can't create OpenCL buffer\n"; return 0; } FILE* f = fopen("kernel.c", "rb"); fseek(f, 0, SEEK_END); size_t tell = ftell(f); rewind(f); ocl_src_char = (char*)calloc(tell + 1, 1); fread(ocl_src_char, tell, 1, f); initialized = 1; return 0; }
std::string PlatformList::list () const { std::stringstream str; for (size_t i = 0; i < platforms ().size (); i++) str << platforms ()[i]->list (); return str.str (); }
int Parallel::setup() { /** * OpenCL initialization. */ cl_int status = Simulator::setup(); CheckStatus(status, "Simulator::setup() failed."); cl_uint numPlatforms; status = clGetPlatformIDs(0, NULL, &numPlatforms); CheckStatus(status, "clGetPlatformIDs, fetching number"); DEBUG_STDOUT("Number of platforms: " << numPlatforms); cl_platform_id platform = NULL; if (numPlatforms > 0) { std::unique_ptr<cl_platform_id[]> platforms (new cl_platform_id[numPlatforms]); status = clGetPlatformIDs(numPlatforms, platforms.get(), NULL); CheckStatus(status, "clGetPlatformIDs, fetching platforms"); for (unsigned i = 0; i < numPlatforms; ++i) { char pbuf[100]; status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL); CheckStatus(status, "clGetPlatformInfo"); } // Just grab the first platform. platform = platforms[0]; } CheckConditional(platform != NULL, "platform == NULL"); cl_uint numDevices; status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices); CheckStatus(status, "clGetDeviceIDs: fetching number"); DEBUG_STDOUT("Number of devices: " << numDevices); cl_device_id *devices = new cl_device_id[numDevices]; status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, numDevices, devices, NULL); CheckStatus(status, "clGetDeviceIDs: fetching devices"); int deviceIndex = 0; for (unsigned i = 0; i < numDevices; ++i) { char pbuf[100]; status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuf), pbuf, NULL); if (!strncmp(pbuf, "ATI", 3)) { deviceIndex = i; } } /* Create the context. */ context = clCreateContext(0, numDevices, devices, NULL, NULL, &status); CheckConditional(context != NULL, "clCreateContextFromType"); /* Create command queue */ cl_command_queue_properties prop = CL_QUEUE_PROFILING_ENABLE; commandQueue = clCreateCommandQueue(context, devices[deviceIndex], prop, &status); CheckStatus(status, "clCreateCommandQueue"); /* Create a CL program using the kernel source */ SDKFile kernelFile; std::string kernelPath = getenv("HOME") + std::string("/md-simulator/src/TestKernel.cl"); if(!kernelFile.open(kernelPath.c_str())) { DEBUG_STDERR("Failed to load kernel file : " << kernelPath); return MD_FAILURE; } const char *source = kernelFile.source().c_str(); size_t sourceSize[] = {strlen(source)}; program = clCreateProgramWithSource(context, 1, &source, sourceSize, &status); CheckStatus(status, "clCreateProgramWithSource"); /* Create a cl program executable for all the devices specified */ status = clBuildProgram(program, numDevices, devices, NULL, NULL, NULL); if (status != CL_SUCCESS) { if (status == CL_BUILD_PROGRAM_FAILURE) { cl_int logStatus; std::unique_ptr<char[]> buildLog (nullptr); //char *buildLog = NULL; size_t buildLogSize = 0; logStatus = clGetProgramBuildInfo(program, devices[deviceIndex], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog.get(), &buildLogSize); CheckStatus(logStatus, "clGetProgramBuildInfo"); buildLog = std::unique_ptr<char[]>(new char[buildLogSize]); if(!buildLog) { return MD_FAILURE; } std::fill_n(buildLog.get(), buildLogSize, 0); logStatus = clGetProgramBuildInfo(program, devices[deviceIndex], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog.get(), NULL); CheckStatus(logStatus, "clGetProgramBuildInfo (2)"); DEBUG_STDERR("\n\t\t\tBUILD LOG\n"); DEBUG_STDERR("************************************************\n"); DEBUG_STDERR(buildLog.get()); DEBUG_STDERR("************************************************\n"); } } CheckStatus(status, "clBuildProgram"); /* Get a kernel object handle for a kernel with the given name */ kernel = clCreateKernel(program, "computeAccelerations", &status); CheckStatus(status, "clCreateKernel"); /* Check group size against group size returned by kernel */ status = clGetKernelWorkGroupInfo(kernel, devices[deviceIndex], CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &kernelWorkGroupSize, 0); CheckStatus(status, "clGetKernelWorkGroupInfo"); DEBUG_STDOUT("kernelWorkGroupSize: " << kernelWorkGroupSize); /** * Initialize some simulator data structures. */ global = particleCount * particleCount; local = particleCount; if (global * local > kernelWorkGroupSize) { DEBUG_STDERR("WARNING - global * local > kernelWorkGroupSize; global: " << global << ", local: " << local << ", kernelWorkGroupSize: " << kernelWorkGroupSize); return MD_FAILURE; } // Data holds the molecule positions. data = std::unique_ptr<float[]> (new float[particleCount * 3]); // Constants holds simulator constants. constants = std::unique_ptr<float[]> (new float[NUM_CONSTANTS]); // Copy constants to buffer; constants[0] = epsilon; constants[1] = sigma; constants[2] = negForceCutoffMinusHalf; constants[3] = forceCutoffMinusHalf; constants[4] = wallStiffness; // Results holds pairwise forces. results = std::unique_ptr<float[]> (new float[particleCount * particleCount * 3]); return MD_SUCCESS; }