.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "MCE::Grep 3pm" .TH MCE::Grep 3pm "2014-10-24" "perl v5.20.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" MCE::Grep \- Parallel grep model similar to the native grep function .SH "VERSION" .IX Header "VERSION" This document describes MCE::Grep version 1.517 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& ## Exports mce_grep, mce_grep_f, and mce_grep_s \& use MCE::Grep; \& \& ## Array or array_ref \& my @a = mce_grep { $_ % 5 == 0 } 1..10000; \& my @b = mce_grep { $_ % 5 == 0 } [ 1..10000 ]; \& \& ## File_path, glob_ref, or scalar_ref \& my @c = mce_grep_f { /phrase/ } "/path/to/file"; \& my @d = mce_grep_f { /phrase/ } $file_handle; \& my @e = mce_grep_f { /phrase/ } \e$scalar; \& \& ## Sequence of numbers (begin, end [, step, format]) \& my @f = mce_grep_s { %_ * 3 == 0 } 1, 10000, 5; \& my @g = mce_grep_s { %_ * 3 == 0 } [ 1, 10000, 5 ]; \& \& my @h = mce_grep_s { %_ * 3 == 0 } { \& begin => 1, end => 10000, step => 5, format => undef \& }; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides a parallel grep implementation via Many-core Engine. \s-1MCE\s0 incurs a small overhead due to passing of data. Therefore, a fast code block will likely run faster using the native grep function in Perl. The overhead quickly diminishes as the complexity of the code block increases. .PP .Vb 2 \& my @m1 = grep { $_ % 5 == 0 } 1..1000000; ## 0.137 secs \& my @m2 = mce_grep { $_ % 5 == 0 } 1..1000000; ## 0.295 secs .Ve .PP Chunking, enabled by default, greatly reduces the overhead behind the scene. The time for mce_grep below also includes the time for data exchanges between the manager and worker processes. More parallelization will be seen when the code block requires additional \s-1CPU\s0 time code-wise. .PP .Vb 2 \& my @m1 = grep { /[2357][1468][9]/ } 1..1000000; ## 0.653 secs \& my @m2 = mce_grep { /[2357][1468][9]/ } 1..1000000; ## 0.347 secs .Ve .PP The mce_grep_s function will provide better times, useful when the input data is simply a range of numbers. Workers generate sequences mathematically among themselves without any interaction from the manager process. Two arguments are required for mce_grep_s (begin, end). Step defaults to 1 if begin is smaller than end, otherwise \-1. .PP .Vb 1 \& my @m3 = mce_grep_s { /[2357][1468][9]/ } 1, 1000000; ## 0.271 secs .Ve .PP Although this document is about MCE::Grep, the MCE::Stream module can write results immediately without waiting for all chunks to complete. This is made possible by passing the reference of the array (in this case \f(CW@m4\fR and \f(CW@m5\fR). .PP .Vb 1 \& use MCE::Stream default_mode => \*(Aqgrep\*(Aq; \& \& my @m4; mce_stream \e@m4, sub { /[2357][1468][9]/ }, 1..1000000; \& \& ## Completed in 0.304 secs. That is amazing considering the \& ## overhead for passing data between the manager and worker. \& \& my @m5; mce_stream_s \e@m5, sub { /[2357][1468][9]/ }, 1, 1000000; \& \& ## Completed in 0.227 secs. Like with mce_grep_s, specifying a \& ## sequence specification turns out to be faster due to lesser \& ## overhead for the manager process. .Ve .PP A good use-case for MCE::Grep is for searching through a large log file much like one might do using the native grep function. Lets assume the file contains a hundred thousand records separated by a string \*(L"::\en\en\*(R" between each record. The imaginary pattern used also returns less than 50 records. .PP The native implementation is what one might do actually. What's not clearly visible here is the initial memory consumption, due to Perl reading the entire content into memory, prior to grep actually starting. A 300 \s-1MB\s0 file will consume roughly 640 \s-1MB.\s0 The time to run is 1.217 seconds for the file residing in the OS-level file-system cache. .PP .Vb 1 \& $/ = "::\en\en"; \& \& open my $LOG, "<", "/path/to/log/file"; \& my @match = grep { $_ =~ /pattern/ } <$LOG>; \& close $LOG; .Ve .PP The memory utilization is much better with \s-1MCE\s0; 8 workers * 23 \s-1MB\s0 = 184 \s-1MB. MCE\s0 caps at some point, therefore allowing one to process a file much larger than available memory. The time to run is 0.416 seconds (2.93x faster) which includes the overhead for chunking and serializing data back to the main process as if processing serially. .PP .Vb 1 \& use MCE::Grep; \& \& MCE::Grep::init { RS => "::\en\en" }; \& \& my @match = mce_grep_f { $_ =~ /pattern/ } "/path/to/file"; .Ve .PP It gets even better for counting though. The native grep function takes 1.136 seconds to run whereas \s-1MCE\s0 takes just 0.155 seconds (7.33x faster). .PP .Vb 2 \& ## Native Grep \& my $count = grep { $_ =~ /pattern/ } <$LOG>; \& \& ## MCE Grep \& my $count = mce_grep_f { $_ =~ /pattern/ } "/path/to/file"; .Ve .SH "OVERRIDING DEFAULTS" .IX Header "OVERRIDING DEFAULTS" The following list 5 options which may be overridden when loading the module. .PP .Vb 1 \& use Sereal qw(encode_sereal decode_sereal); \& \& use MCE::Grep \& max_workers => 4, ## Default \*(Aqauto\*(Aq \& chunk_size => 100, ## Default \*(Aqauto\*(Aq \& tmp_dir => "/path/to/app/tmp", ## $MCE::Signal::tmp_dir \& freeze => \e&encode_sereal, ## \e&Storable::freeze \& thaw => \e&decode_sereal ## \e&Storable::thaw \& ; .Ve .PP There is a simpler way to enable Sereal with \s-1MCE 1.5.\s0 The following will attempt to use Sereal if available, otherwise will default back to using Storable for serialization. .PP .Vb 1 \& use MCE::Grep Sereal => 1; \& \& ## Serialization is through Sereal if available. \& my @m2 = mce_grep { $_ % 5 == 0 } 1..10000; .Ve .SH "CUSTOMIZING MCE" .IX Header "CUSTOMIZING MCE" .IP "init" 3 .IX Item "init" The init function accepts a hash of \s-1MCE\s0 options. The gather option, if specified, will be set to undef due to being used internally by the module. .Sp .Vb 1 \& use MCE::Grep; \& \& MCE::Grep::init { \& chunk_size => 1, max_workers => 4, \& \& user_begin => sub { \& print "## ", MCE\->wid, " started\en"; \& }, \& \& user_end => sub { \& print "## ", MCE\->wid, " completed\en"; \& } \& }; \& \& my @a = mce_grep { $_ % 5 == 0 } 1..100; \& \& print "\en", "@a", "\en"; \& \& \-\- Output \& \& ## 2 started \& ## 3 started \& ## 1 started \& ## 4 started \& ## 3 completed \& ## 4 completed \& ## 1 completed \& ## 2 completed \& \& 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 .Ve .SH "API DOCUMENTATION" .IX Header "API DOCUMENTATION" .IP "mce_grep { code } list" 3 .IX Item "mce_grep { code } list" Input data can be defined using a list or passing a reference to an array. .Sp .Vb 2 \& my @a = mce_grep { /[2357]/ } 1..1000; \& my @b = mce_grep { /[2357]/ } [ 1..1000 ]; .Ve .IP "mce_grep_f { code } file" 3 .IX Item "mce_grep_f { code } file" The fastest of these is the /path/to/file. Workers communicate the next offset position among themselves without any interaction from the manager process. .Sp .Vb 3 \& my @c = mce_grep_f { /phrase/ } "/path/to/file"; \& my @d = mce_grep_f { /phrase/ } $file_handle; \& my @e = mce_grep_f { /phrase/ } \e$scalar; .Ve .IP "mce_grep_s { code } sequence" 3 .IX Item "mce_grep_s { code } sequence" Sequence can be defined as a list, an array reference, or a hash reference. The functions require both begin and end values to run. Step and format are optional. The format is passed to sprintf (% may be omitted below). .Sp .Vb 1 \& my ($beg, $end, $step, $fmt) = (10, 20, 0.1, "%4.1f"); \& \& my @f = mce_grep_s { /[1234]\e.[5678]/ } $beg, $end, $step, $fmt; \& my @g = mce_grep_s { /[1234]\e.[5678]/ } [ $beg, $end, $step, $fmt ]; \& \& my @h = mce_grep_s { /[1234]\e.[5678]/ } { \& begin => $beg, end => $end, step => $step, format => $fmt \& }; .Ve .IP "mce_grep { code } iterator" 3 .IX Item "mce_grep { code } iterator" An iterator reference can by specified for input data. Iterators are described under \*(L"\s-1SYNTAX\s0 for \s-1INPUT_DATA\*(R"\s0 at MCE::Core. .Sp .Vb 1 \& my @a = mce_grep { $_ % 3 == 0 } make_iterator(10, 30, 2); .Ve .SH "MANUAL SHUTDOWN" .IX Header "MANUAL SHUTDOWN" .IP "finish" 3 .IX Item "finish" \&\s-1MCE\s0 workers remain persistent as much as possible after running. Shutdown occurs when the script exits. One can manually shutdown \s-1MCE\s0 by simply calling finish after running. This resets the \s-1MCE\s0 instance. .Sp .Vb 1 \& use MCE::Grep; \& \& MCE::Grep::init { \& chunk_size => 20, max_workers => \*(Aqauto\*(Aq \& }; \& \& my @a = mce_grep { ... } 1..100; \& \& MCE::Grep::finish; .Ve .SH "INDEX" .IX Header "INDEX" \&\s-1MCE\s0 .SH "AUTHOR" .IX Header "AUTHOR" Mario E. Roy, .SH "LICENSE" .IX Header "LICENSE" This program is free software; you can redistribute it and/or modify it under the terms of either: the \s-1GNU\s0 General Public License as published by the Free Software Foundation; or the Artistic License. .PP See for more information.