``` 1 # Multithreading. DenseDoubleMatrix2D operations using multiple cores 2 # In Parallel Colt threads are used automatically when computations are done on a 3 # machine with multiple CPUs. 4 # Here are some benchmarks of DenseDoubleMatrix2Di done by Piotr Wendykier piotr.wendykier@gmail.com 5 # http://sites.google.com/site/piotrwendykier/files/BenchmarkDenseDoubleMatrix2D.txt 6 # This example is written by S.Chekanov 7 8 ###################### benchmarking multi-threaded colt ############## 9 from cern.colt.matrix import * 10 from edu.emory.mathcs.utils import ConcurrencyUtils 11 import time 12 13 # do some calculations on DenseDoubleMatrix2D 14 def process(M): 15 M.cardinality() 16 M.dctColumns(0) 17 M.dctRows(0) 18 M.dct2(0) 19 M.dht2() 20 M.dhtColumns() 21 M.dhtRows() 22 M.dst2(0) 23 M.dstColumns(0) 24 M.dstRows(0) 25 M.vectorize() 26 M.zSum() 27 M.idct2(0) 28 29 30 ################### two threads ################################### 31 Ncores=2 32 print " benchmarks of DenseDoubleMatrix2D for "+str(Ncores)+" CPU core. Wait!" 33 ConcurrencyUtils.setNumberOfThreads(Ncores) 34 start = time.clock() 35 M=tdouble.DoubleFactory2D.dense.random(2000, 2000) # random matrix 36 process(M) 37 print ' Multiple CPU time (s)=',time.clock()-start 38 39 40 ####################### single core ################################# 41 Ncores=1 42 print " benchmarks of DenseDoubleMatrix2D for "+str(Ncores)+" CPU core. wait!" 43 ConcurrencyUtils.setNumberOfThreads(Ncores) 44 start = time.clock() 45 M=tdouble.DoubleFactory2D.dense.random(2000, 2000) # random matrix 46 process(M) 47 print ' Multiple CPU time (s)=',time.clock()-start ### © jHepWork. S.Chekanov ### ```