Examples of Concurrent Solution Design

Session:
Examples of Concurrent Solution
Design
INTEL CONFIDENTIAL
Intel® Academic Community
Objectives
At the end of this session, you will be able to:
• Given an algorithm to a known problem, identify the
appropriate concurrent/parallel solutions for recognized
constructs as discussed during class.
• Given a series of partitioned modules, identify the
dependencies, communications, and synchronization
events between them.
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
2
Intel® Academic Community
Prerequisites
Basic concepts of concurrent/parallel programming
• Decomposition strategy
• Data decomposition
• Task decomposition
• Dependency identification
• Data dependency
• Control dependency
• Synchronizations mechanism for Race condition
• Mutual exclusion, Semaphore etc
At
•
•
•
•
•
least one implementation models
Win32 thread API or
P-thread library or
Java thread API or
OpenMP or
……
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
3
Intel® Academic Community
Agenda
Concurrent Program Design Review
Design Examples on Common Program Constructs
Summary
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
4
Intel® Academic Community
Agenda
Concurrent Program Design Review
Design Examples on Common Program Constructs
Summary
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
5
Intel® Academic Community
Concurrent Program Design in A Nutshell
Design Purpose
• For performance gain
Design Methodology Review
• Solution description
• Workloads partitioning
• Dependency analysis
• Communications and synchronizations analysis
• Implementation models
• Tuning for performance
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
6
Intel® Academic Community
Practices Review – Design Purpose
Multi/Many-Core platforms are available
What do we design a concurrent program for?
• Performance gain
• Tax paid for gain
• Overhead of parallelization
Check for workloads
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
7
Intel® Academic Community
Design Methodology Review (1)
Solution Description
• Effective solution to the problem
• Description format
• Data or Control flow
• Diagram for easier dependency analysis
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
8
Intel® Academic Community
Design Methodology Review (2)
Workloads Partitioning Strategies
• Data decomposition
• Task decomposition
• Pipelining
Task Decomposition
Data Decomposition
……
L
S
P
B
I
…
…
…
D
…
E
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
9
Intel® Academic Community
Design Methodology Review (2)
Dependency and Communications/Synchronizations Analysis
• Dependency between partitioned work units preventing parallelism
• Data dependency
• Control dependency
• Shared/Private resources identified
• Synchronizations on shared objects
• Communications for dependence implementations
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
10
Intel® Academic Community
Design Methodology Review (3)
Implementation Models
• Share memory models
•
•
•
•
•
OpenMP
Win32 API
P-threads
Java
……
• Distributed memory models
• MPI
• ……
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
11
Intel® Academic Community
Design Methodology Review (4)
Tuning for Performance
• Known issues
• Overhead reduction
• Work load balance
• Tools
• Debugging
• Profiling
• Tuning cycles
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
12
Intel® Academic Community
Agenda
Concurrent Program Design Review
Design Examples on Common Program Constructs
Summary
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
13
Intel® Academic Community
Design Examples on Program Constructs
Concurrent Design for Iteration Constructs
Concurrent Design for Function Constructs
Design for Interactive Peers (Pencil & Paper practice)
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
14
Intel® Academic Community
Concurrent Design for Iteration Constructs
Loops indicate
• A considerable workload
• Opportunity for optimizations
• Candidates of data parallelism
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
15
Intel® Academic Community
Concurrent Design for Iteration Constructs
linear Least-Squares Estimation (l-LSqE)
• “Abstract” 2D points with one line
y
y=b1x+b0
x
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
16
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Solution description
• Workloads: Summations (∑s)
• Case study: summation of x2
Loopsumxx:
while (i є scope)
{sumxx= sumxx+xi2}
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
17
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Workload partitioning
• Case study: summation of x2
sumxx= sumx+x12
sumxx= sumx+x22
x0
……
x1
sumxx= sumx+xn2
……
Output
dependency
sumxx
xn
Can be done in parallel
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
18
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Communications and Synchronizations
• Shared resource: summation variables (sumx, sumy , sumxx, sumxy) due to
output dependency
• Private resource: original coordinates of point series (xi, yi)
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
19
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Implementations
• C code segment
sumx = 0.0;
sumy = 0.0;
sumx2 = 0.0;
sumxy = 0.0;
for (x = 0; x < n; x++) {
sumx = sumx + x;
sumy = sumy + y[x];
sumx2 = sumx2 + pow(x, 2.0);
sumxy = sumxy + (x * y[x]);
}
b1 = (sumxy - ((sumx * sumy)/(double)n)) / (sumx2-(pow(sumx,2.0)/(double)n));
b0 = (sumy - ((b1) * sumx)) / (double)n;
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
20
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Implementations
• Threading for shared memory hierarchy
• OpenMP
– #pragma omp parallel for
#pragma omp parallel for
…
i=0
i=1
…
i = m-1
i=m
i = m+1
…
i = 2m-1
…
…
Implicit barrier
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
21
i = t*m
i = t*m+1
…
i=n
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Implementations
• Threaded implementation
sumx = 0.0;
sumy = 0.0;
sumx2 = 0.0;
sumxy = 0.0;
#pragma omp parallel for
Is result correct
if n = 1000?
for (x = 0; x < n; x++) {
sumx = sumx + x;
sumy = sumy + y[x];
sumx2 = sumx2 + pow(x, 2.0);
Shared variables
needs sync.
sumxy = sumxy + (x * y[x]);
}
b1 = (sumxy - ((sumx * sumy)/(double)n)) / (sumx2-(pow(sumx,2.0)/(double)n));
b0 = (sumy - ((b1) * sumx)) / (double)n;
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
22
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Implementations
• Correct threaded implementation
sumx = 0.0;
sumy = 0.0;
sumx2 = 0.0;
sumxy = 0.0;
#pragma omp parallel for reduction(+:sumx, sumy, sumx2, sumxy )
for (x = 0; x < n; x++) {
sumx = sumx + x;
sumy = sumy + y[x];
sumx2 = sumx2 + pow(x, 2.0);
sumxy = sumxy + (x * y[x]);
}
b1 = (sumxy - ((sumx * sumy)/(double)n)) / (sumx2-(pow(sumx,2.0)/(double)n));
b0 = (sumy - ((b1) * sumx)) / (double)n;
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
23
Intel® Academic Community
Concurrent Design for Iteration Constructs
l-LSqE parallelization
• Performance check
• Is it improved for parallel version?
• Not enough workload if small trip count (value of n)
• Parallelize for enough workloads
• Don’t bother for small loop body and counts
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
24
Intel® Academic Community
Optional Demo 1
Compare performance of serial and parallel l-LSqE
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
25
Intel® Academic Community
Concurrent Design for Iteration Summary
Examine workloads size
Explore parallelism between iterations
• Identify dependency
• Locate share/private resources
Map iterations to execution unit
• Threading model selection
• Incremental threading models are good options: OpenMP
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
26
Intel® Academic Community
Concurrent Design for Function Constructs
Functions (Libraries) exemplifies
• Re-usability for maintenance
• Candidate for data parallelism
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
27
Intel® Academic Community
Concurrent Design for Function Constructs
Binary search
• Optimal solution to provide О(log
n) complexity
• Poor candidate for concurrency
due to control dependency
between loops
• Good facility for data parallel
applications
*Figure cited from Wikipedia
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
28
Intel® Academic Community
Concurrent Design for Function Constructs
Functions (Libraries) are designed for
• Same processing on different data set
• Data decomposition
Case study: a spell checker application
……
In computing, a spell checker
is an applications program that
flags words in a document
that may not be spelled correctly.
Spell checkers may be stand-alone capable of
operating on a block of text,
or as part of a larger application,
such as a word processor,
email client, electronic dictionary,
or search engine.
Search the word
in a dictionary
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
29
Intel® Academic Community
Concurrent Design for Function Constructs
Concurrent Spell Checker
• Solution Description
Y
End of text?
N
word = get (textBuffer)
found = search (word, dic)
N
Y
Found?
Done
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
30
Intel® Academic Community
Concurrent Design for Function Constructs
Concurrent Spell Checker
• Workloads Partition: no dependency between fetching words and
between searching in the dictionary
• Communications and Synchronizations: none except handle of text
block
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
31
Intel® Academic Community
Concurrent Design for Function Constructs
Concurrent Spell Checker
• Implementation: Java threads with Boss/Worker model
• Explicit threading
– An object function for searching
• Synchronizations
– Synchronized keyword
– Concurrent collection: Blocking queue
Code segments
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
32
Intel® Academic Community
Optional Demo 2
Illustrate threaded Java Spell Checker
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
33
Intel® Academic Community
Concurrent Design for Function Constructs
Concurrent Spell Checker
• Anything missing?
• Implementation requirement for concurrent function/library
• Thread safety
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
34
Intel® Academic Community
Concurrent Design for Function Constructs
L-LSqE as a function:
double
sumx, sumy, sumx2, sumxy;
void LeastSquareEstimation(double *y, int n, double
yVar, double *b1, double *b0) {
int
i;
sumx = 0.0;
sumy = 0.0;
sumx2 = 0.0;
sumxy = 0.0;
for (i = 0; i < n; i++) {
sumx = sumx + (double)i;
sumy = sumy + (y[i]+yVar);
sumx2 = sumx2 + pow((double)i, 2.0);
sumxy = sumxy + ((double)i * (y[i]+yVar));
}
*b1 = (sumxy - ((sumx * sumy)/(double)n)) /
(sumx2-(pow(sumx,2.0)/(double)n));
*b0 = (sumy - ((*b1) * sumx)) / (double)n;
return;
}
double b1[n], b0[n];
……
#pragma omp parallel for
for (i = 0; i < iter; i++) {
LeastSquareEstimation(y, arrLen(y), (double)i/iter,
&b1[i], &b0[i]);
}
What’s wrong?
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
35
Intel® Academic Community
Concurrent Design for Function Constructs
l-LSqE as a function: Correct version
void LeastSquareEstimation(double *y, int n, double
yVar, double *b1, double *b0) {
int
i;
double
sumx, sumy, sumx2, sumxy;
sumx = 0.0;
sumy = 0.0;
sumx2 = 0.0;
sumxy = 0.0;
for (i = 0; i < n; i++) {
sumx = sumx + (double)i;
sumy = sumy + (y[i]+yVar);
sumx2 = sumx2 + pow((double)i, 2.0);
sumxy = sumxy + ((double)i * (y[i]+yVar));
}
*b1 = (sumxy - ((sumx * sumy)/(double)n)) /
(sumx2-(pow(sumx,2.0)/(double)n));
*b0 = (sumy - ((*b1) * sumx)) / (double)n;
return;
}
double b1[n], b0[n];
……
#pragma omp parallel for
for (i = 0; i < iter; i++) {
LeastSquareEstimation(y, arrLen(y), (double)i/iter,
&b1[i], &b0[i]);
}
Avoid using global
or shared resources
without protection
for function/library
states
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
36
Intel® Academic Community
Optional Demo 3
Implement thread safety for parallel l-LSqE
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
37
Intel® Academic Community
Concurrent Design for Function Summary
Good facility for data parallel algorithm
Thread safety as implementation requirement
• Identification
• Using global variables or resource with global links
• Indirect access by handles or pointers
• Access to unstable variables
• Implementation
•
•
•
•
Re-enterable
Mutual exclusion
TLS
Atomicity
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
38
Intel® Academic Community
Concurrent Design for Interactive Peers
Huffman Coding
• Minimum entropy and redundancy
• Statistics on symbol presence
• Building a Huffman Tree
• Encode (Compress/De-compress) data by the Tree
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
39
Intel® Academic Community
Concurrent Design for Interactive Peers
Example for practice: Building Huffman Tree (for Huffman Coding)
• An extreme example for parallelism paranoid!
c: 000
91
0
1
55
0
24
0
11
c
1
b: 001
36
1
0
31
k
15
n
k: 01
1
21
x
n: 10
x: 11
13
b
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
40
Intel® Academic Community
Concurrent Design for Interactive Peers
Solution description
• Key data structure:
• linked list of nodes
• node
• Key operations
• s: sort or find the smallest two
nodes in the linked list
• m: merge the smallest two nodes
to a new one
weight
symbol
weight
symbol
weight
symbol
weight
symbol
While (list contains more than one node) {
sort or find the smallest two (s);
merge the smallest two (m);
}
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
41
Intel® Academic Community
Concurrent Design for Interactive Peers
Workloads partitioning
• Control dependency between m and s at high (task) level
• Parallelism at sub-task level
•
•
•
•
s1: getting the new node with weight value
s2: iterate list to find the smallest two nodes
m1:get the smallest two nodes
m2:form a new node with summed value of the old two, remove the old
two from the list
m1
• m3:link the old two to the new node
• Create tasks for s and m respectively
s1
m2
s2
m3
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
42
Intel® Academic Community
Concurrent Design for Interactive Peers
Communications and Synchronizations
• Communications for dependency m2->s1 and m1->s2
• Synchronizations on node list update
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
43
Intel® Academic Community
Concurrent Design for Interactive Peers
Pencil and Paper work implementation draft
• Explicit threading
• Create threads for m and s
• Semaphores on list update
• Producer/Consumer model for communications
Performance
• Synchronization overhead is considerable
• Trivial gain if workload (node list length) is small
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
44
Intel® Academic Community
Concurrent Design for Peer Tasks Summary
Intuitive candidate for task decomposition
• If no dependency, or
• If partial dependency
Return of investment evaluation before implementation
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
45
Intel® Academic Community
Summary
Evaluate workloads
Describe the solution
Partition the workloads
• decomposition strategy
• data decomposition for iteration and function constructs
• task decomposition for interactive peers
• dependency identified
• communications and synchronization defined
Select implementation model
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
46
Intel® Software College
Backup
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
48
Intel® Software College
Implementation: Java with Boss/Worker
model
class SpellThread implements Runnable {
BlockingQueue<String> queue;
int eCount = 0, tCount = 0;
String[] dic = null;
UpdateFlags updateFlags = null;
public SpellThread (BlockingQueue<String> q, String[] dict, UpdateFlags uf)
{……}
private void checking() {……}
public void run () {
while (true) {
if (!queue.isEmpty()) {
checking();
} else {
if (updateFlags.workAllLoaded) {
break;
}
}
}
updateFlags.incThreadCount();
}
}
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
49
Intel® Software College
Implementation: Java with Boss/Worker
model
class UpdateFlags {
public volatile boolean workAllLoaded = false;
public int threadCount = 0;
public int errCount = 0;
public synchronized void incThreadCount() {
threadCount++;
}
public synchronized void incErrCount() {
errCount++;
}
public void SetWorkAllLoaded () {
workAllLoaded = true;
}
}
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
50
Intel® Software College
Implementation: Java with Boss/Worker
model
private void checking() {
int i = 0; String wordIn = null; boolean found = false; String[] inStrArr = null;
try {……}
if (wordIn != null) {
inStrArr = wordIn.split("\\W");
i = inStrArr.length;
while (i>0) {
i--;
wordIn = inStrArr[i];
for (String wordDict: dic) {
if (wordIn.equalsIgnoreCase(wordDict)) {
found = true;
break;
};
}
if (!found) {
updateFlags.incErrCount();
}
found = false;
}
}
}
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
51
Intel® Software College
Implementation: Java with Boss/Worker
model
public class SpellMain {
public static void main(String[] args) throws Exception {
//create worker threads
int NTHREADS = 3;
ExecutorService mainT = Executors.newFixedThreadPool(NTHREADS);
BlockingQueue<String> workQ = new LinkedBlockingQueue<String>();
……
UpdateFlags uflags = new UpdateFlags();
……
//start threads
for (int i = 0; i < NTHREADS; i++) {
mainT.execute(new SpellThread (workQ, dict, uflags));
}
//enqueueing
while (in.hasNext()) {
workQ.put(in.next());
}
uflags.SetWorkAllLoaded();
while (!workQ.isEmpty() && (uflags.threadCount < NTHREADS)) ;
……
mainT.shutdownNow();
in.close();
}
Back
}
Copyright © 2008, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States
or other countries. * Other brands and names are the property of their respective owners.
52