Function image generator for GPU parallel computing

Source: Internet
Author: User
Tags amd radeon

A number of days ago, the technical master Vczh developed a function image rendering program that can draw an image with the equation f (x, y) = 0. The principle is to bring the coordinates of each point in the image into function f to obtain two equations for x and y, and then use the Newton Iteration Method to obtain a set of points and draw them to the image. His program can be used to draw a variety of amazing equations. However, his program is very slow, because it is a very time-consuming task to use Newton Iteration Method to Solve every point coordinate. Even if Parallel. For is adopted, the CPU is very difficult to calculate. After studying his program, I thought I could use a graphics card that is good at parallel computing to accelerate the iteration process. It is no longer appropriate to use OpenCL to complete this task.

 

The entire process was quite smooth, and it was completely changed based on the original Vczh program. Only slightly changed the policy. The procedure is as follows:

  1. After the input function f is parsed, the partial derivatives of f/∂ x and f/∂ y are generated respectively, and then the three binary functions are converted into valid OpenCL expressions.
  2. Use OpenCL to implement the Newton Iteration Method.
  3. Assign each point on the image to an OpenCL thread, and then compute its points by countless parallel OpenCL threads.

 

The OpenCL code is as follows:

Fp_t func (fp_t x, fp_t y) {return {dynamic generation};} fp_t df_dx (fp_t x, fp_t y) {return {dynamic generation};} fp_t df_dy (fp_t x, fp_t y) {return {dynamic generation};} fp_t solvex (fp_t start, const fp_t consty) {for (int I = 0; I <MAX_ITER; ++ I) {fp_t result = func (start, consty); if (result <= EPSILON & result> =-EPSILON) {return start;} fp_t d = df_dx (start, consty ); if (d <= EPSILON & d >=- EPSILON) {return NAN;} else {start-= result/d ;}} return NAN ;} kernel void ComputeX (global write_only fp_t * points, int unit, int width, int cx, int cy, float origin_x, float origin_y) {int gx = get_global_id (0 ); int gy = get_global_id (1); uint write_loc = gx + gy * width; fp_t py = origin_y + (fp_t) (gy + 1-cy)/unit; fp_t px = origin_x + (fp_t) (cx-gx-1)/unit; points [write_loc] = solvex (px, py );}

This is the code for solving f (x, a) = 0. It is basically the same for solving f (B, y) = 0. Fp_t is a typedef defined based on the situation, which may be float or double. Because not all OpenCL devices support double-precision floating points, we need to write common types of algorithms and use macros to control them.

 

The easiest way to use OpenCL in. NET is to use the Cloo library. The Coo library fully encapsulates all functions of OpenCL (1.1) and is an object model that is very easy to use by. NET. I only used Cloo and OpenCL once, and I don't want to endure the troublesome library of DirectX ComputeShader ..

 

The source code of my program has been completely uploaded to github. Https://github.com/Ninputer/opencl-plot click Download to package all the code. You can click here to download the Binary Package.

 

To run this program, you must install the OpenCL implementation platform. Currently, OpenCL on Windows mainly provides implementation platforms from NVidia, AMD, and Intel. If you have a relatively new NVidia or AMD graphics card, you only need to install the latest driver package with OpenCL. The following video cards support Double Precision Floating Point: NVidia GeForce 200 series, 400 series, 500 series graphics cards; AMD Radeon HD 5800, 5900, 6900 series. The Radeon 6900 series does not yet support the official dual-precision floating point number (cl_khr_fp64) extension, so this program also supports cl_amd_fp64 dual-precision floating point extension, which has the same functionality. Graphics cards in the G80 and RV770 architectures, as well as AMD low-end graphics cards, can only support single-precision floating-point numbers, and may be slightly less accurate during painting.

 

Users who do not support OpenCL graphics cards can use multi-core CPUs for OpenCL computing, which is still faster than the original C # version. If you use Intel Core i3, i5, i7 series CPU, you can use Intel OpenCL SDK,: http://software.intel.com/en-us/articles/opencl-sdk/ other multi-Core CPU can use amd app sdk,: http://developer.amd.com/sdks/AMDAPPSDK/downloads/Pages/default.aspx

 

After the program is started, you can select the OpenCL computing platform and device. If multiple OpenCL platforms are installed, you can choose any one. Currently, this program does not support multi-video parallel technology (SLI and Crossfire ). NVidia CUDA platform interface Example:

 

Amd app platform interface Example:

 

Intel OpenCL platform interface Example:

 

Enter the equation to make full use of your imagination!

Note: When using graphics card computing, it is best not to play games. When using IE9 to browse webpages, or so on, the GPU may be reset when the load is too heavy. If some formulas require too much computation or the graphics card is low-end and may cause GPU reset, use Windows 7 and Vista for experiments, the WDDM driver model is more stable (it is easy to use blue screen XP ).

 

Welcome to my meager get the latest dynamic http://weibo.com/ninputer

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.