Advertisement
Guest User

Untitled

a guest
Jan 8th, 2012
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #coding: utf8
  2. from scipy import weave
  3. import numpy
  4. from numpy import *
  5. from random import *
  6. from time import time
  7.  
  8. # для приятной печати времени
  9. MACRO = 100
  10.  
  11. # C и openMP код
  12. codeOpenMP = \
  13. """
  14. int i = 0;
  15.  
  16. omp_set_num_threads(2);
  17. #pragma omp parallel shared(matrix, randRow, c) private(i)
  18. {
  19. #pragma omp for
  20. for(i = 0; i < N*M; i++) {
  21.    matrix[0,i] = matrix[0,i] - (c * randRow[i%M]);
  22. }
  23. }
  24. """
  25.  
  26. # C код
  27. codeC = \
  28. """
  29. int i = 0;
  30.  
  31. for(i = 0; i < N*M; i++) {
  32.    matrix[0,i] = matrix[0,i] - (c * randRow[i%M]);
  33. }
  34. """
  35.  
  36. # генерация случайной матрицы x на y
  37. # элементы матрицы - случайные числа от 0 до 99 включ.
  38. def randMat(x,y):  
  39.     randRaw = lambda a:  [ randint(0, 100) for i in xrange(0,a) ]
  40.     randConst = lambda x, y: [ randRaw(x) for e in xrange(0,y)]
  41.     return array(randConst(x, y))
  42.  
  43. def test():
  44.    
  45.     sizeMat = [100,1000,2000,3000]
  46.     results = []
  47.  
  48.     for n in sizeMat:
  49.         sourceMat = randMat(n,n)
  50.         N,M = sourceMat.shape
  51.         randRow = sourceMat[randint(0,N)]
  52.         c = randint(0,n)
  53.  
  54.         print "\nTest on size: %dx%d"%(n,n)
  55.  
  56.         """ python test """
  57.         matrix = array(sourceMat)
  58.         t1 = time()
  59.         for i in xrange(N):
  60.             matrix[i,:] -= c*randRow
  61.         timePython =  (time()-t1)*MACRO
  62.         print "\tPure python: ", timePython
  63.         results.append(matrix)
  64.  
  65.         """ C test """
  66.         matrix = array(sourceMat)
  67.         t1 = time()
  68.         weave.inline(codeC, ['matrix','c', 'randRow','N', 'M'],
  69.             compiler = 'gcc')
  70.         timeOpenMP = (time()-t1)*MACRO
  71.         print "\tPure C: %s"%timeOpenMP
  72.         results.append(matrix)
  73.  
  74.         """ C и OpenMP test """
  75.         matrix = array(sourceMat)
  76.         t1 = time()
  77.         weave.inline(codeOpenMP, ['matrix','c', 'randRow','N', 'M'],
  78.             extra_compile_args =['-O3 -fopenmp'],
  79.             compiler = 'gcc',
  80.             libraries=['gomp'],
  81.             headers=['<omp.h>'])
  82.         timeOpenMP = (time()-t1)*MACRO
  83.         print "\tC plus OpenMP: %s"%(timeOpenMP)
  84.         results.append(matrix)
  85.  
  86.         if array_equal(results[0], results[1]) and \
  87.         array_equal(results[1], results[2]):
  88.                 print "\tTest - ok"
  89.         else:
  90.                 print "\tTest - false"
  91.  
  92.        
  93. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement