Below is some sample code written
first as a single thread, then divided into four threads.
// Procedure
proc mloop(x,N,its,pos);
local m;
m = 0;
for i(1,its,1);
m = m + moment(x[pos:pos+N-1,.],0);
pos = pos + N;
endfor;
retp(m);
endp;
N = 4000;
x = rndn(N*N,4);
// Single-Threaded Calculation
mlp = mloop( x, N, N, 1 );
// Multithreaded Calculation (split into four threads
for a quad-core machine)
R = rows(x)/4;
ThreadStat m1 = mloop(x, N, N/4, 1 );
ThreadStat m2 = mloop(x, N, N/4, 1+R );
ThreadStat m3 = mloop(x, N, N/4, 1+R*2);
ThreadStat m4 = mloop(x, N, N/4, 1+R*3);
ThreadJoin;
mtmlp = m1 + m2 + m3 + m4;
Running time trials with these calculations on a quad-core machine,
the multithreaded calculation averaged 3.3 times faster than the single-threaded
call!
Additional examples of how to incorporate threading into
programs will be added periodically, so check back here for new examples.
![]() |
© Copyright 2004-2009. Aptech Systems, Inc. Black Diamond, WA. All Rights Reserved Worldwide. |
