项目作者: pfac-lib

项目描述 :
PFAC is an open library for exact string matching performed on NVIDIA GPUs
高级语言: C++
项目地址: git://github.com/pfac-lib/PFAC.git
创建时间: 2015-05-01T06:13:38Z
项目社区:https://github.com/pfac-lib/PFAC

开源协议:

下载


What is PFAC?

PFAC is an open library for exact string matching performed on GPUs. PFAC runs on processors that support CUDA. Supporting OS includes ubuntu, Fedora and MAC OS.

PFAC library provides C-style API and users need not have background on GPU computing or parallel computing. PFAC has APIs hiding CUDA stuff.

News

  • PFAC r1.0 updated 2011/02/23
  • PFAC r1.0 released 2011/02/21
  • PFAC r1.1 released 2011/04/27
  • PFAC r1.2 released 2012/04/29

Simple Example

Example 1: Using PFAC_matchFromHost function

The file “example_pattern” in the directory “../test/pattern/“ contains 4 patterns.

  1. AB
  2. ABG
  3. BEDE
  4. ED

The file “example_input” in the directory “../test/data/“contains a string.

  1. ABEDEDABG

The following example shows the basic steps to use PFAC library for string matching.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <PFAC.h>
  6. int main(int argc, char **argv)
  7. {
  8. char dumpTableFile[] = "table.txt" ;
  9. char inputFile[] = "../test/data/example_input" ;
  10. char patternFile[] = "../test/pattern/example_pattern" ;
  11. PFAC_handle_t handle ;
  12. PFAC_status_t PFAC_status ;
  13. int input_size ;
  14. char *h_inputString = NULL ;
  15. int *h_matched_result = NULL ;
  16. // step 1: create PFAC handle
  17. PFAC_status = PFAC_create( &handle ) ;
  18. assert( PFAC_STATUS_SUCCESS == PFAC_status );
  19. // step 2: read patterns and dump transition table
  20. PFAC_status = PFAC_readPatternFromFile( handle, patternFile) ;
  21. if ( PFAC_STATUS_SUCCESS != PFAC_status ){
  22. printf("Error: fails to read pattern from file, %s\n", PFAC_getErrorString(PFAC_status) );
  23. exit(1) ;
  24. }
  25. // dump transition table
  26. FILE *table_fp = fopen( dumpTableFile, "w") ;
  27. assert( NULL != table_fp ) ;
  28. PFAC_status = PFAC_dumpTransitionTable( handle, table_fp );
  29. fclose( table_fp ) ;
  30. if ( PFAC_STATUS_SUCCESS != PFAC_status ){
  31. printf("Error: fails to dump transition table, %s\n", PFAC_getErrorString(PFAC_status) );
  32. exit(1) ;
  33. }
  34. //step 3: prepare input stream
  35. FILE* fpin = fopen( inputFile, "rb");
  36. assert ( NULL != fpin ) ;
  37. // obtain file size
  38. fseek (fpin , 0 , SEEK_END);
  39. input_size = ftell (fpin);
  40. rewind (fpin);
  41. // allocate memory to contain the whole file
  42. h_inputString = (char *) malloc (sizeof(char)*input_size);
  43. assert( NULL != h_inputString );
  44. h_matched_result = (int *) malloc (sizeof(int)*input_size);
  45. assert( NULL != h_matched_result );
  46. memset( h_matched_result, 0, sizeof(int)*input_size ) ;
  47. // copy the file into the buffer
  48. input_size = fread (h_inputString, 1, input_size, fpin);
  49. fclose(fpin);
  50. // step 4: run PFAC on GPU
  51. PFAC_status = PFAC_matchFromHost( handle, h_inputString, input_size, h_matched_result ) ;
  52. if ( PFAC_STATUS_SUCCESS != PFAC_status ){
  53. printf("Error: fails to PFAC_matchFromHost, %s\n", PFAC_getErrorString(PFAC_status) );
  54. exit(1) ;
  55. }
  56. // step 5: output matched result
  57. for (int i = 0; i < input_size; i++) {
  58. if (h_matched_result[i] != 0) {
  59. printf("At position %4d, match pattern %d\n", i, h_matched_result[i]);
  60. }
  61. }
  62. PFAC_status = PFAC_destroy( handle ) ;
  63. assert( PFAC_STATUS_SUCCESS == PFAC_status );
  64. free(h_inputString);
  65. free(h_matched_result);
  66. return 0;
  67. }

The screen shows the following matched results.

  1. At position 0, match pattern 1
  2. At position 1, match pattern 3
  3. At position 2, match pattern 4
  4. At position 4, match pattern 4
  5. At position 6, match pattern 2

Example 2: Using PFAC_matchFromDevice function

Prepare the same input file and pattern file. The screen shows the same matched results as example 1.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <cuda_runtime.h>
  6. #include <PFAC.h>
  7. int main(int argc, char **argv)
  8. {
  9. char dumpTableFile[] = "table.txt" ;
  10. char inputFile[] = "../test/data/example_input" ;
  11. char patternFile[] = "../test/pattern/example_pattern" ;
  12. PFAC_handle_t handle ;
  13. PFAC_status_t PFAC_status ;
  14. int input_size ;
  15. char *h_input_string = NULL ;
  16. int *h_matched_result = NULL ;
  17. char *d_input_string;
  18. int *d_matched_result;
  19. cudaError_t status;
  20. // step 1: create PFAC handle
  21. PFAC_status = PFAC_create( &handle ) ;
  22. assert( PFAC_STATUS_SUCCESS == PFAC_status );
  23. // step 2: read patterns and dump transition table
  24. PFAC_status = PFAC_readPatternFromFile( handle, patternFile) ;
  25. if ( PFAC_STATUS_SUCCESS != PFAC_status ){
  26. printf("Error: fails to read pattern from file, %s\n", PFAC_getErrorString(PFAC_status) );
  27. exit(1) ;
  28. }
  29. // dump transition table
  30. FILE *table_fp = fopen( dumpTableFile, "w") ;
  31. assert( NULL != table_fp ) ;
  32. PFAC_status = PFAC_dumpTransitionTable( handle, table_fp );
  33. fclose( table_fp ) ;
  34. if ( PFAC_STATUS_SUCCESS != PFAC_status ){
  35. printf("Error: fails to dump transition table, %s\n", PFAC_getErrorString(PFAC_status) );
  36. exit(1) ;
  37. }
  38. //step 3: prepare input stream
  39. FILE* fpin = fopen( inputFile, "rb");
  40. assert ( NULL != fpin ) ;
  41. // obtain file size
  42. fseek (fpin , 0 , SEEK_END);
  43. input_size = ftell (fpin);
  44. rewind (fpin);
  45. // allocate memory to contain the whole file
  46. h_input_string = (char *) malloc (sizeof(char)*input_size);
  47. assert( NULL != h_input_string );
  48. // allocate memory to contain the matched results
  49. h_matched_result = (int *) malloc (sizeof(int)*input_size);
  50. assert( NULL != h_matched_result );
  51. memset( h_matched_result, 0, sizeof(int)*input_size ) ;
  52. // copy the file into the buffer
  53. input_size = fread (h_input_string, 1, input_size, fpin);
  54. fclose(fpin);
  55. // compute the size of for stroing the matched results
  56. int data_size = sizeof(int) * input_size;
  57. // allocate GPU memory for input string
  58. status = cudaMalloc((void **) &d_input_string, input_size);
  59. if ( cudaSuccess != status ){
  60. fprintf(stderr, "Error: %s\n", cudaGetErrorString(status));
  61. exit(1) ;
  62. }
  63. // allocate GPU memory for matched result
  64. status = cudaMalloc((void **) &d_matched_result, data_size);
  65. if ( cudaSuccess != status ){
  66. fprintf(stderr, "Error: %s\n", cudaGetErrorString(status));
  67. exit(1) ;
  68. }
  69. // copy input string from host to device
  70. cudaMemcpy(d_input_string, h_input_string, input_size, cudaMemcpyHostToDevice);
  71. // step 4: run PFAC on GPU by calling PFAC_matchFromDevice
  72. PFAC_status = PFAC_matchFromDevice( handle, d_input_string, input_size, d_matched_result ) ;
  73. if ( PFAC_STATUS_SUCCESS != PFAC_status ){
  74. printf("Error: fails to PFAC_matchFromHost, %s\n", PFAC_getErrorString(PFAC_status) );
  75. exit(1) ;
  76. }
  77. // copy the result data from device to host
  78. cudaMemcpy(h_matched_result, d_matched_result, data_size, cudaMemcpyDeviceToHost);
  79. // step 5: output matched result
  80. for (int i = 0; i < input_size; i++) {
  81. if (h_matched_result[i] != 0) {
  82. printf("At position %4d, match pattern %d\n", i, h_matched_result[i]);
  83. }
  84. }
  85. PFAC_status = PFAC_destroy( handle ) ;
  86. assert( PFAC_STATUS_SUCCESS == PFAC_status );
  87. free(h_input_string);
  88. free(h_matched_result);
  89. cudaFree(d_input_string);
  90. cudaFree(d_matched_result);
  91. return 0;
  92. }

Refer to the user guide page for further information and examples.

Publications

  1. Cheng-Hung Lin, Chen-Hsiung Liu, Lung-Sheng Chien, Shih-Chieh Chang, “Accelerating Pattern Matching Using a Novel Parallel Algorithm on GPUs,” IEEE Transactions on Computers, vol. 62, no. 10, pp. 1906-1916, Oct. 2013, doi:10.1109/TC.2012.254
  2. Cheng-Hung Lin, Sheng-Yu Tsai, Chen-Hsiung Liu, Shih-Chieh Chang, and Jyuo-Min Shyu, “Accelerating String Matching Using Multi-threaded Algorithm on GPU,” in Proc. of IEEE GLOBAL COMMUNICATIONS CONFERENCE (GLOBECOM), Miami, Florida, USA, December 6-10, pp.1-5, 2010.

Contributors

The primary developers of PFAC are Lung-Sheng Chien, Chen-Hsiung Liu, Cheng-Hung Lin, and Shih-Chieh Chang.
Website maintainer Chun-Cheng Huang