Compiling gdal supports OpenCL using GPU acceleration

Source: Internet
Author: User


Objective


Gdal Library provides gdalwarp support a variety of high-performance image resampling algorithm, image resampling algorithm is widely used in image correction, re-projection, cutting, mosaic and other algorithms, and for these algorithms, the computation of coordinate transformation is quite small, most of the computation is in the image of the resampling algorithm, In particular, three convolution sampling and more advanced resampling algorithms, the computational volume will multiply, so improving the processing efficiency of these algorithms first is to improve the efficiency of resampling. Due to the multi-core of GPU, the parallel processing of GPU is very hot, and it can greatly improve processing speed. For these reasons, the GDALWARP also provides OpenCL-based GPU acceleration, which was previously tested in Gdal's mailing list and found that with OpenCL acceleration, the speed on Telsa's graphics card can be up to 20~60 times the CPU.



Gdal library is generally compiled without opening, so the default gdal is not supported GPU parallel processing, this article is to guide you on the Windows platform using the Visual Studio series compiled Gdal, how to support opencl parallel processing.


Required Software


-visual Studio Series (VS2003 or later)
-OPENCL Library (either AMD or NVIDIA)
-gdal Source


Modify the Nmake.opt file


There are a total of changes in the Nmake.opt file, two of which are added, and two are modifications.


First place (Increase)


First, refer to the other reference libraries in the Nmake.opt file to add the OpenCL Library directory. First find the following line in Namke.opt:


########### END OF STUFF THAT NORMALLY NEEDS TO BE UPDATED ##################


Add the following code in front of this line of code (using the OpenCL library under the NVIDIA CUDA installation package):


 
#include opencl lib
INCLUDE_OPENCL = YES

# Uncomment for OPENCL_AMD support
!IFDEF INCLUDE_OPENCL
OPENCL_DIR="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.0"
OPENCL_CFLAGS = -I$(OPENCL_DIR)/include -DHAVE_OPENCL
OPENCL_LIB    = $(OPENCL_DIR)/lib/Win32/OpenCL.lib
!ELSE
OPENCL_DIR=
OPENCL_CFLAGS = -I
OPENCL_LIB     = 
!ENDIF
Second place (Increase)


Then add the following code below the line of code above (END of STUFF that normally NEEDS to be UPDATED):


# liml
!IFDEF INCLUDE_OPENCL
OPENCL_FLAG = -DHAVE_OPENCL
!ENDIF
Third place (revision)


Next find the code CFLAGS = This code, add $ (opencl_flag) after this code.


Around the place (modify)


Find code External_libs =, add $ (opencl_lib)at the back.


Modify the Makefile.vc file under the ALG directory


The Makefile.vc file needs to be modified in two places, one at a place.


First place (Increase)


Add the following code to the file in front of OBJ = To set the macro definition using OpenCL.


!IFDEF INCLUDE_OPENCL
EXTRAFLAGS = $(EXTRAFLAGS) $(OPENCL_CFLAGS) -DHAVE_OPENCL
!ENDIF
Second place (Amendment)


Add the OpenCL algorithm file gdalwarpkernel_opencl.objafter OBJ = in the file. Note that when added, it is separated from the other obj files by a space.


Compile


After modifying the above files, using vs or Command-line compilation, normal should be compiled directly. If the compilation is not prompt. h file does not open anything, please check the OpenCL library path setting is not a problem, if there are spaces in the OpenCL path, then in the nmake.opt file, the path is enclosed in double quotation marks, if the connection error is finally prompted, or if the Lib file is not found, the same first check the OpenCL library The path is set correctly, and if there are spaces in the path, enclose them in double quotation marks.



Note
If you still do not compile, modify the gdalwarpkernel_opencl.h and file gdalwarpkernel_opencl.c files According to the prompt content. I have encountered this problem, the modification of the H file, the changes before and after the code as follows (the above is the original code, below is the modified code):


 
#if defined(DEBUG_OPENCL) && DEBUG_OPENCL == 1
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
#endif
#if defined(DEBUG_OPENCL) && DEBUG_OPENCL == 1
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
#else
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#endif


At this point, if there is no problem with the compilation, then you can use the software such as depends to view the DLL's dependencies to view the Gdal's dynamic library. If the dynamic library of Gdal relies on opencl.dll, then it should be supported, and you can test whether OPENCL is supported using Gdal's own gdalwarp.exe.
Note : The resampling algorithm in the Gdal library supports both bilinear interpolation and three cubic convolution of the OpenCL library, which is not supported for OpenCL acceleration, so specify the resampling algorithm for testing when testing.


Two questions


The following two questions were found when using the Gdal GPU test:
1. When resampling using OpenCL, only the first device can be used for processing (for example, my notebook gets two GPU devices, the first one is Intel's integrated graphics card, the second is Nvidia's discrete graphics card)
2, the processing failure when using the-WM parameter setting is larger than the graphics card memory



The first question, I roughly modified the next, refer to the following code, the second one is not.
The first issue of the modification, the main is the default use of the first setting, replaced with the last device, and so on, can be set by the user designated to use the device for processing.
Pre-Modified code:


 
    // Find the GPU CL device, this is what we really want
    // If there is no GPU device is CL capable, fall back to CPU
    err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    if (err != CL_SUCCESS)
    {
        // Find the CPU CL device, as a fallback
        err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_CPU, 1, &device, NULL);
        if( err != CL_SUCCESS || device == 0 )
            return NULL;
    }


The modified code


 
 
  // Find the GPU CL device, this is what we really want
    // If there is no GPU device is CL capable, fall back to CPU
    err = clGetDeviceIDs(platforms[num_platforms-1], CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    if (err != CL_SUCCESS)
    {
        // Find the CPU CL device, as a fallback
        err = clGetDeviceIDs(platforms[num_platforms-1], CL_DEVICE_TYPE_CPU, 1, &device, NULL);
        if( err != CL_SUCCESS || device == 0 )
            return NULL;
    }


Compile gdal support OpenCL using GPU acceleration


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.