How does GPGPU OpenCL implement exact string search?

Source: Internet
Author: User

How does GPGPU OpenCL implement exact string search?

1. Acceleration Method

(1) store a small amount of constant data, such as the mode string length and text length, in the private memory of the thread.

(2) Save the mode string in the local memory of the GPU, and accelerate the thread's access to the mode string.

(3) Save the text to be searched in global memory, use as many threads as possible to access global memory, and reduce the average thread access time.

(4) A section of the thread operation text in each work-group. Multiple work-groups process large texts in parallel.

  2. Synchronization

(1) CLK_LOCAL_MEM_FENCE and CLK_GLOBAL_MEM_FENCE are used in the work-group.

(2) The atomic operation on _ global int Is used globally to ensure that each thread writes the result to the correct position in the global memory. The operations supported by the device can be obtained by querying the extension of the device. For example, the kernel function supports atomic operations and printf operations:

  3. Code instance, large text exact mode string SEARCH

3.1 core function (string_search_kernel.cl ):

Int compare (_ global const uchar * text, _ local const uchar * pattern, uint length ){

For (uint l = 0; l

If (text [l]! = Pattern [l])

Return 0;

}

Return 1;

}

_ Kernel void

StringSearch (

_ Global uchar * text, // Input Text

Const uint textLength, // Length of the text

_ Global const uchar * pattern, // Pattern string

Const uint patternLength, // Pattern length

Const uint maxSearchLength, // Maximum search positions for each work-group

_ Global int * resultCount, // Result counts (global)

_ Global int * resultBuffer, // Save the match result

_ Local uchar * localPattern) // local buffer for the search pattern

{

Int localIdx = get_local_id (0 );

Int localSize = get_local_size (0 );

Int groupIdx = get_group_id (0 );

Uint lastSearchIdx = textLength-patternLength + 1;

Uint beginSearchIdx = groupIdx * maxSearchLength;

Uint endSearchIdx = beginSearchIdx + maxSearchLength;

If (beginSearchIdx> lastSearchIdx)

Return;

If (endSearchIdx> lastSearchIdx)

EndSearchIdx = lastSearchIdx;

For (int idx = localIdx; idx <patternLength; idx + = localSize)

LocalPattern [idx] = pattern [idx];

Barrier (CLK_LOCAL_MEM_FENCE );

For (uint stringPos = beginSearchIdx + localIdx; stringPos

If (compare (text + stringPos, localPattern, patternLength) = 1 ){

Int count = atomic_inc (resultCount );

ResultBuffer [count] = stringPos;

// Printf ("% d", stringPos );

}

Barrier (CLK_LOCAL_MEM_FENCE );

}

}

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.