def nowait_test(): _dict1={} # omp parallel num_threads(8) private(i) def _block0(): # omp for nowait for i in omp.prange(8): time.sleep(random.randrange(3)) print str(omp.get_thread_num()) + ' thread\n', print "done" # omp parallel end omp.parallel_run(_block0,8)
def barrier_test(): _dict1={} # omp parallel def _block0(): print str(omp.get_thread_num()) + ' a\n', time.sleep(random.randrange(3)) print str(omp.get_thread_num()) + ' b\n', time.sleep(random.randrange(3)) # omp barrier omp.barrier() print str(omp.get_thread_num()) + ' c\n', # omp parallel end omp.parallel_run(_block0,0)
def hello_world(): _dict1 = {} print "i write dazuoye!!!" print omp.get_thread_num(), '/', omp.get_num_threads() _dict1['a'] = 2017 # omp parallel num_threads(4) def _block0(): print "i love bianyishixi!\n", print '%d / %d\n' % (omp.get_thread_num(), omp.get_num_threads()), print "a = " + str(_dict1['a']) + '\n', # omp parallel end omp.parallel_run(_block0, 4)
def hello_world(): _dict1 = {} print "i write dazuoye!!!" print omp.get_thread_num(), '/', omp.get_num_threads() _dict1['a'] = 2017 #omp parallel num_threads(4) def _block0(): print "i love bianyishixi!" print omp.get_thread_num(), '/', omp.get_num_threads() print "a =", _dict1['a'] #omp parallel end omp.parallel_run(_block0, 4)
def calc_pi_critical(): _dict1 = {} _dict1['ans'] = 0 # omp parallel num_threads(8) private(i,x) def _block0(): # omp for nowait for i in omp.prange(num_step): x = (i + 0.5) * step # omp critical omp.set_internal_lock(1) _dict1['ans'] += 4.0 / (1.0 + x * x) omp.unset_internal_lock(1) # omp critical end # omp parallel end omp.parallel_run(_block0, 8) print _dict1['ans'] * step
def calc_pi(): _dict2 = {} _dict2['ans'] = [0] * 8 # omp parallel num_threads(8) private(i,tid,nthrds,tmp_ans,x) def _block1(): tid = omp.get_thread_num() nthrds = omp.get_num_threads() print "working: " + str(tid) + ' / ' + str(nthrds) + '\n', tmp_ans = 0 for i in xrange(tid, num_step, nthrds): x = (i + 0.5) * step tmp_ans += 4.0 / (1.0 + x * x) _dict2['ans'][tid] = tmp_ans # omp parallel end omp.parallel_run(_block1, 8) print sum(_dict2['ans']) * step
def calc_pi_for(): _dict1 = {} _dict1['ans'] = 0 # omp parallel num_threads(2) private(i,x) def _block0(): # omp for reduction(+:ans) OMP_REDUCTION_VAR_0 = omp.reduction_init('+') for i in omp.prange(num_step): x = (i + 0.5) * step OMP_REDUCTION_VAR_0 += 4.0 / (1.0 + x * x) omp.set_internal_lock(0) _dict1['ans'] = omp.reduction('+', _dict1['ans'], OMP_REDUCTION_VAR_0) omp.unset_internal_lock(0) omp.barrier() # omp parallel end omp.parallel_run(_block0, 2) print "%.9f\n" % (_dict1['ans'] * step),
def count(n): _dict1={} _dict1['n']=n _dict1['s'] = 0 #omp parallel private(i) num_threads(2) def _block0(): if omp.get_thread_num()==0: print 'num_threads =', omp.get_num_threads() #omp for reduction(+:s) OMP_REDUCTION_VAR_0 = omp.reduction_init('+') for i in omp.prange(_dict1['n']): OMP_REDUCTION_VAR_0 += i omp.set_internal_lock(0) _dict1['s'] = omp.reduction('+',_dict1['s'],OMP_REDUCTION_VAR_0) omp.unset_internal_lock(0) omp.barrier() #omp parallel end omp.parallel_run(_block0,2) return _dict1['s']
def sections_test(): _dict1 = {} # omp parallel num_threads(2) def _block0(): # omp sections for OMP_SECTIONS_ID in omp.prange(2): # omp section if OMP_SECTIONS_ID == 0: print 'section 0 from ' + str(omp.get_thread_num()) + '\n', # omp section end # omp section if OMP_SECTIONS_ID == 1: print 'section 1 from ' + str(omp.get_thread_num()) + '\n', # omp section end # omp sections end omp.barrier() # omp parallel end omp.parallel_run(_block0, 2)
def calc_pi_critical(): _dict4 = {} _dict4['ans'] = 0 _dict4['lock'] = omp.init_lock() # omp parallel num_threads(8) private(i,x) def _block3(): # omp for for i in omp.prange(num_step): x = (i + 0.5) * step # omp critical omp.set_internal_lock(2) _dict4['ans'] += 4.0 / (1.0 + x * x) omp.unset_internal_lock(2) omp.barrier() # omp critical end # omp parallel end omp.parallel_run(_block3, 8) print _dict4['ans'] * step
def matrixMul(N, a, b): _dict2 = {} _dict2['N'] = N _dict2['a'] = a _dict2['b'] = b _dict2['res'] = [0 for i in range(_dict2['N'] * _dict2['N'])] # omp parallel num_threads(2) private(n,i,j,tmp,k) def _block0(): # omp for for n in omp.prange(_dict2['N'] * _dict2['N']): i = n / _dict2['N'] j = n % _dict2['N'] tmp = 0 for k in range(_dict2['N']): tmp = tmp + _dict2['a'][i * _dict2['N'] + k] * _dict2['b'][k * _dict2['N'] + j] _dict2['res'][n] = tmp omp.barrier() # omp parallel end omp.parallel_run(_block0, 2) return _dict2['res']
import omp import time import random def f(): for i in omp.drange(10): print str(i)+' '+str(omp.get_thread_num())+'\n', time.sleep(omp.get_thread_num()) omp.parallel_run(f,4)