def hello_world(): print "i write dazuoye!!!" print omp.get_thread_num(), '/', omp.get_num_threads() a = 2017 # omp parallel num_threads(4) print "i love bianyishixi!" print omp.get_thread_num(), '/', omp.get_num_threads() print "a =", a
def hello_world(): print "i write dazuoye!!!" print omp.get_thread_num(), '/', omp.get_num_threads() a = 2017 # omp parallel num_threads(4) print "i love bianyishixi!\n", print '%d / %d\n' % (omp.get_thread_num(), omp.get_num_threads()), print "a = " + str(a) +'\n',
def get_num_threads(): nthreads = -1 # omp parallel if 1: # omp single nthreads = omp.get_num_threads() return nthreads
def thread_cal(): global local_num time.sleep(1) for _ in range(5): local_num += 1 time.sleep(random.random()) print threading.current_thread().getName(), local_num print omp.get_thread_num(), '/', omp.get_num_threads()
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
def omp_parallel_num_threads(): import omp max_threads = 0 failed = 0 if 'omp parallel': if 'omp master': max_threads = omp.get_num_threads() for threads in range(1, max_threads + 1): nthreads = 0 if 'omp parallel reduction(+:failed) num_threads(threads)': failed += (threads != omp.get_num_threads()) 'omp atomic' nthreads += 1 failed += (nthreads != threads) return not failed
def count(n): s = 0 #omp parallel private(i) num_threads(4) if omp.get_thread_num() == 0: print 'num_threads =', omp.get_num_threads() #omp for reduction(+:s) for i in xrange(n): s += i #omp parallel end return s
def _block0(): if omp.get_thread_num() == 0: print 'num_threads =', omp.get_num_threads() #omp for reduction(+:s) OMP_REDUCTION_VAR_0_0 = omp.reduction_init('+') for i in omp.prange(_dict1['n']): OMP_REDUCTION_VAR_0_0 += i omp.set_internal_lock(0) _dict1['s'] = omp.reduction('+', _dict1['s'], OMP_REDUCTION_VAR_0_0) omp.unset_internal_lock(0) omp.barrier()
def omp_get_num_threads(): import omp nthreads = 0 nthreads_lib = -1 if 'omp parallel': if 'omp critical': nthreads += 1 if 'omp single': nthreads_lib = omp.get_num_threads() return nthreads == nthreads_lib
def omp_parallel_for_if(using=0): num_threads = 0 import omp sum = 0 sum2 = 0 LOOPCOUNT = 1000 'omp parallel for if(using == 1)' for i in range(LOOPCOUNT + 1): num_threads = omp.get_num_threads() sum += i known_sum = (LOOPCOUNT * (LOOPCOUNT + 1)) / 2 return known_sum == sum and num_threads == 1
def omp_parallel_for_if(using=0): num_threads = 0 import omp sum = 0 sum2 = 0 LOOPCOUNT=1000 'omp parallel for if(using == 1)' for i in range(LOOPCOUNT+1): num_threads = omp.get_num_threads() sum+=i known_sum = (LOOPCOUNT * (LOOPCOUNT + 1)) / 2 return known_sum == sum and num_threads == 1
def calc_pi(): ans = [0] * 8 # omp parallel num_threads(8) private(i,tid,nthrds,tmp_ans,x) 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) ans[tid] = tmp_ans # omp parallel end print sum(ans) * step
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 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 omp_for_schedule_auto(): import omp sum = 0 sum0 = 12345 sum1 = 0 if 'omp parallel private(sum1)': if 'omp single': threadsnum = omp.get_num_threads() 'omp for firstprivate(sum0) schedule(auto)' for i in range(1, 1001): sum0 += i sum1 = sum0 if 'omp critical': sum += sum1 known_sum = 12345 * threadsnum + (1000 * (1000 + 1)) / 2 return known_sum == sum
def omp_in_parallel(): import omp serial = 1 isparallel = 0 serial = omp.in_parallel() if 'omp parallel num_threads(2)': num_threads = omp.get_num_threads() if 'omp single': isparallel = omp.in_parallel() if 'omp parallel': if 'omp single': pass return (not serial and isparallel) or num_threads == 1
def omp_for_firstprivate(): sum = 0 sum0 = 12345 sum1 = 0 import omp LOOPCOUNT = 1000 if 'omp parallel private(sum1)': 'omp single' threadsnum = omp.get_num_threads() 'omp for firstprivate(sum0)' for i in range(1, LOOPCOUNT+1): sum0+=i sum1 = sum0 'omp critical' sum+=sum1 known_sum = 12345* threadsnum+ (LOOPCOUNT * (LOOPCOUNT + 1)) / 2 return sum == known_sum
def omp_in_parallel(): import omp serial = 1 isparallel = 0 serial = omp.in_parallel() if 'omp parallel num_threads(2)': num_threads = omp.get_num_threads() if 'omp single': isparallel = omp.in_parallel() if 'omp parallel': if 'omp single': pass return bool(not serial and isparallel) or num_threads == 1
def omp_for_firstprivate(): sum = 0 sum0 = 12345 sum1 = 0 import omp LOOPCOUNT = 1000 if 'omp parallel private(sum1)': 'omp single' threadsnum = omp.get_num_threads() 'omp for firstprivate(sum0)' for i in range(1, LOOPCOUNT + 1): sum0 += i sum1 = sum0 'omp critical' sum += sum1 known_sum = 12345 * threadsnum + (LOOPCOUNT * (LOOPCOUNT + 1)) / 2 return sum == known_sum
def _block0(): print "i love bianyishixi!" print omp.get_thread_num(), '/', omp.get_num_threads() print "a =", _dict1['a']
def omp_for_schedule_static(): NUMBER_OF_THREADS = 10 CFSMAX_SIZE = 10000 MAX_TIME = 0.01 SLEEPTIME = 0.0005 counter = 0 tmp_count=1 lastthreadsstarttid = -1 result = 1 chunk_size = 7 tids = [0] * (CFSMAX_SIZE + 1) notout = 1 maxiter = 0 #omp parallel shared(tids,counter), num_threads(NUMBER_OF_THREADS) #omp single threads = omp.get_num_threads () if threads < 2: print("This test only works with at least two threads"); return True tids[CFSMAX_SIZE] = -1 if "omp parallel shared(tids) num_threads(NUMBER_OF_THREADS)": tid = omp.get_thread_num (); #omp for nowait schedule(static,chunk_size) for j in range(CFSMAX_SIZE): count = 0. #pragma omp flush(maxiter) if j > maxiter: #pragma omp critical maxiter = j while notout and (count < MAX_TIME) and (maxiter == j): #pragma omp flush(maxiter,notout) sleep (SLEEPTIME) count += SLEEPTIME tids[j] = tid notout = 0 #omp flush(maxiter,notout) # analysing the data in array tids lasttid = tids[0] tmp_count = 0; print(lasttid) print(tids) for i in range(CFSMAX_SIZE): # If the work was done by the same thread increase tmp_count by one. if tids[i] == lasttid: tmp_count+=1 continue; # Check if the next thread had has the right thread number. When finding # threadnumber -1 the end should be reached. if (tids[i] == (lasttid + 1) % threads or tids[i] == -1): # checking for the right chunk size if (tmp_count == chunk_size): tmp_count = 1 lasttid = tids[i] # If the chunk size was wrong, check if the end was reached else: if (tids[i] == -1): if (i == CFSMAX_SIZE): print("Last thread had chunk size ", tmp_count) break; else: print("ERROR: Last thread (thread with number -1) was found before the end.") result = 0 else: print("ERROR: chunk size was . (assigned was )\n", tmp_count, chunk_size) result = 0 else: print("ERROR: Found thread with number %d (should be inbetween 0 and %d).", tids[i], threads - 1) result = 0; return result
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',
def omp_for_schedule_static(): NUMBER_OF_THREADS = 10 CFSMAX_SIZE = 10000 MAX_TIME = 0.01 SLEEPTIME = 0.0005 counter = 0 tmp_count=1 lastthreadsstarttid = -1 result = 1 chunk_size = 7 tids = [0] * (CFSMAX_SIZE + 1) notout = 1 maxiter = 0 #omp parallel shared(tids,counter), num_threads(NUMBER_OF_THREADS) #omp single threads = omp.get_num_threads () if threads < 2: print("This test only works with at least two threads"); return 1 tids[CFSMAX_SIZE] = -1 if "omp parallel shared(tids) num_threads(NUMBER_OF_THREADS)": tid = omp.get_thread_num (); #omp for nowait schedule(static,chunk_size) for j in range(CFSMAX_SIZE): count = 0. #pragma omp flush(maxiter) if j > maxiter: #pragma omp critical maxiter = j while notout and (count < MAX_TIME) and (maxiter == j): #pragma omp flush(maxiter,notout) sleep (SLEEPTIME) count += SLEEPTIME tids[j] = tid notout = 0 #omp flush(maxiter,notout) # analysing the data in array tids lasttid = tids[0] tmp_count = 0; print(lasttid) print(tids) for i in range(CFSMAX_SIZE): # If the work was done by the same thread increase tmp_count by one. if tids[i] == lasttid: tmp_count+=1 continue; # Check if the next thread had has the right thread number. When finding # threadnumber -1 the end should be reached. if (tids[i] == (lasttid + 1) % threads or tids[i] == -1): # checking for the right chunk size if (tmp_count == chunk_size): tmp_count = 1 lasttid = tids[i] # If the chunk size was wrong, check if the end was reached else: if (tids[i] == -1): if (i == CFSMAX_SIZE): print("Last thread had chunk size ", tmp_count) break; else: print("ERROR: Last thread (thread with number -1) was found before the end.") result = 0 else: print("ERROR: chunk size was . (assigned was )\n", tmp_count, chunk_size) result = 0 else: print("ERROR: Found thread with number %d (should be inbetween 0 and %d).", tids[i], threads - 1) result = 0; return result
def omp_for_schedule_static(): tmp_count = 1 result = True chunk_size = 7 CFSMAX_SIZE = 1000 tids = [0] * (CFSMAX_SIZE + 1) notout = True maxiter = 0 if 'omp parallel shared(tids)': if 'omp single': threads = omp.get_num_threads() if threads < 2: print 'E: This test only works with at least two threads' return False tids[CFSMAX_SIZE] = -1 if 'omp parallel shared(tids)': tid = omp.get_thread_num() 'omp for nowait schedule(static,chunk_size)' for j in xrange(CFSMAX_SIZE): count = 0 'omp flush(maxiter)' if j > maxiter: 'omp critical' maxiter = j while notout and count < 0.01 and maxiter == j: 'omp flush(maxiter,notout)' sleep(0.0005) count += 0.0005 tids[j] = tid notout = False 'omp flush(maxiter, notout)' lasttid = tids[0] tmp_count = 0 for i in xrange(CFSMAX_SIZE + 1): if tids[i] == lasttid: tmp_count += 1 continue if tids[i] == ((lasttid + 1)%threads) or (tids[i] == -1): if tmp_count == chunk_size: tmp_count = 1 lastid = tids[i] else: if tids[i] == -1: if i == CFSMAX_SIZE: break else: print "E: Last thread (thread with number -1) was found before the end.\n" result = False else: print "E: chunk size was " + str(tmp_count) + ". (assigned was " + str(chunk_size) + ")\n" result = False else: print "E: Found thread with number " + str(tids[i]) + " (should be inbetween 0 and " + str(threads - 1) + ").\n" result = False return result
def omp_for_schedule_guided(): tids = range(1001) maxiter = 0 result = True notout = True if 'omp parallel': if 'omp single': threads = omp.get_num_threads() if threads < 2: print "This test only works with at least two threads" result = False if 'omp parallel shared(tids, maxiter)': tid = omp.get_num_threads() 'omp for nowait schedule(guided)' for j in xrange(1000): count = 0 'omp flush(maxiter)' if j > maxiter: if 'omp critical': maxiter = j 'omp flush(notout, maxiter)' while notout and count < 0.0005 and maxiter == j: 'omp flush(notout, maxiter)' sleep(0.0001) count += 0.0001 tids[j] = tid notout = False 'omp flush(maxiter, notout)' last_threadnr = tids[0] global_chunknr = 0 local_chunknr = [0 for i in xrange(10)] openwork = 1000 tids[1000] = -1 for i in xrange(1, 1001): if last_threadnr == tids[i]: pass else: global_chunknr += 1 local_chunknr[last_threadnr] += 1 last_threadnr = tids[i] chuncksize = range(global_chunknr) global_chunknr = 0 determined_chunksize = 1 last_threadnr = tids[0] for i in xrange(1, 1001): if last_threadnr == tids[i]: determined_chunksize += 1 else: chuncksize[global_chunknr] = determined_chunksize global_chunknr += 1 local_chunknr[last_threadnr] += 1 last_threadnr = tids[i] determined_chunksize = 1 expected_chunk_size = openwork / threads c = chuncksize[0] / expected_chunk_size for i in xrange(global_chunknr): if expected_chunk_size > 1: expected_chunk_size = c * openwork / threads if abs(chuncksize[i] - expected_chunk_size) >= 2: result = False openwork -= chuncksize[i] return result
def omp_for_schedule_guided(): tids = range(1001) maxiter = 0 result = True notout = True if 'omp parallel num_threads(4)': in_parallel = omp.in_parallel() if 'omp single': threads = omp.get_num_threads() if threads<2: print("This test only works with at least two threads") result = False if 'omp parallel shared(tids, maxiter) num_threads(4)': tid = omp.get_num_threads() 'omp for nowait schedule(guided)' for j in range(1000): count = 0 'omp flush(maxiter)' if j > maxiter: if 'omp critical': maxiter = j 'omp flush(notout, maxiter)' while notout and count < 0.0005 and maxiter == j: 'omp flush(notout, maxiter)' sleep(0.0001) count += 0.0001 tids[j] = tid notout = False 'omp flush(maxiter, notout)' last_threadnr = tids[0] global_chunknr = 0 local_chunknr = [0 for i in range(10)] openwork = 1000; tids[1000] = -1 for i in range(1,1001): if last_threadnr == tids[i]: pass else: global_chunknr += 1 local_chunknr[last_threadnr] += 1 last_threadnr = tids[i] chuncksize = range(global_chunknr) global_chunknr = 0 determined_chunksize = 1 last_threadnr = tids[0] for i in range(1,1001): if last_threadnr == tids[i]: determined_chunksize += 1 else: chuncksize[global_chunknr] = determined_chunksize global_chunknr += 1 local_chunknr[last_threadnr] += 1 last_threadnr = tids[i] determined_chunksize = 1 expected_chunk_size = openwork / threads c = chuncksize[0] / expected_chunk_size for i in range(global_chunknr): if expected_chunk_size > 1: expected_chunk_size = c * openwork / threads if abs(chuncksize[i] - expected_chunk_size) >= 2: result = False openwork -= chuncksize[i] return result or not in_parallel
def omp_for_schedule_static_3(): tmp_count = 1 result = True chunk_size = 7 tids = range(1001) notout = True maxiter = 0 if 'omp parallel shared(tids)': if 'omp single': threads = omp.get_num_threads() if threads < 2: print "E: This test only works with at least two threads" return False tids[1000] = -1 if 'omp parallel shared(tids)': tid = omp.get_thread_num() 'omp for nowait schedule(static,chunk_size)' for j in xrange(1000): count = 0 'omp flush(maxiter)' if j > maxiter: if 'omp critical': maxiter = j while notout and count < 0.01 and maxiter == j: 'omp flush(maxiter,notout)' sleep(0.0005) count += 0.0005 tids[j] = tid notout = False lasttid = tids[0] tmp_count = 0 for i in xrange(1001): if tids[i] == lasttid: tmp_count += 1 continue if tids[i] == (lasttid + 1) % threads or tids[i] == -1: if tmp_count == chunk_size: tmp_count = 1 lasttid = tids[i] else: if tids[i] == -1: if i == 1000: break; else: print "E: Last thread (thread with number -1) was\ found before the end.\n" result = False else: print "ERROR: chunk size was " + str(tmp_count) +\ ". (assigned was " + str(chunk_size) + ")\n" result = False else: print "ERROR: Found thread with number " + str(tids[i]) +\ " (should be inbetween 0 and " + str(threads - 1) + ").\n" result = False tids = range(1000) tids2 = range(1000) if 'omp parallel': 'omp for schedule(static) nowait' for n in xrange(1000): if 1000 == n + 1: sleep(0.0005) tids[n] = omp.get_thread_num() 'omp for schedule(static) nowait' for m in xrange(1, 1001): tids2[m-1] = omp.get_thread_num() for i in xrange(1000): if tids[i] != tids2[i]: print "E: Chunk no. " + str(i) + " was assigned once to thread " +\ str(tids[i]) + " and later to thread " + str(tids2[i]) + ".\n" result = False return result
import threading import time import random import omp local_num = 0 def thread_cal(): global local_num time.sleep(1) for _ in range(5): local_num += 1 time.sleep(random.random()) print threading.current_thread().getName(), local_num print omp.get_thread_num(), '/', omp.get_num_threads() tmp_num_threads = omp.get_num_threads() omp.set_num_threads(10) threads = [] for i in range(omp.get_num_threads()): threads.append(threading.Thread(target=thread_cal, name=str(i))) for t in threads: t.start() for t in threads: t.join() omp.set_num_threads(tmp_num_threads) print threading.current_thread().getName()
def omp_for_schedule_static_3(): tmp_count = 1 result = True chunk_size = 7 tids = list(range(1001)) notout = True maxiter = 0 if 'omp parallel shared(tids)': if 'omp single': threads = omp.get_num_threads() if threads < 2: print("E: This test only works with at least two threads") return True tids[1000] = -1 if 'omp parallel shared(tids)': tid = omp.get_thread_num() 'omp for nowait schedule(static,chunk_size)' for j in range(1000): count = 0 'omp flush(maxiter)' if j > maxiter: if 'omp critical': maxiter = j while notout and count < 0.01 and maxiter == j: 'omp flush(maxiter,notout)' sleep(0.0005) count += 0.0005 tids[j] = tid notout = False lasttid = tids[0] tmp_count = 0 for i in range(1001): if tids[i] == lasttid: tmp_count += 1 continue if tids[i] == (lasttid + 1) % threads or tids[i] == -1: if tmp_count == chunk_size: tmp_count = 1 lasttid = tids[i] else: if tids[i] == -1: if i == 1000: break; else: print("E: Last thread (thread with number -1) was " + "found before the end.\n") result = False else: print("ERROR: chunk size was " + str(tmp_count) + ". (assigned was " + str(chunk_size) + ")\n") result = False else: print("ERROR: Found thread with number " + str(tids[i]) + " (should be inbetween 0 and " + str(threads - 1) + ").\n") result = False tids = list(range(1000)) tids2 = list(range(1000)) if 'omp parallel': 'omp for schedule(static) nowait' for n in range(1000): if 1000 == n + 1: sleep(0.0005) tids[n] = omp.get_thread_num() 'omp for schedule(static) nowait' for m in range(1, 1001): tids2[m-1] = omp.get_thread_num() for i in range(1000): if tids[i] != tids2[i]: print("E: Chunk no. " + str(i) + " was assigned once to thread " + str(tids[i]) + " and later to thread " + str(tids2[i]) + ".\n") result = False return result
def omp_for_schedule_static(): tmp_count = 1 result = True chunk_size = 7 CFSMAX_SIZE = 1000 tids = [0] * (CFSMAX_SIZE + 1) notout = True maxiter = 0 if 'omp parallel shared(tids)': if 'omp single': threads = omp.get_num_threads() if threads < 2: print 'E: This test only works with at least two threads' return False tids[CFSMAX_SIZE] = -1 if 'omp parallel shared(tids)': tid = omp.get_thread_num() 'omp for nowait schedule(static,chunk_size)' for j in xrange(CFSMAX_SIZE): count = 0 'omp flush(maxiter)' if j > maxiter: 'omp critical' maxiter = j while notout and count < 0.01 and maxiter == j: 'omp flush(maxiter,notout)' sleep(0.0005) count += 0.0005 tids[j] = tid notout = False 'omp flush(maxiter, notout)' lasttid = tids[0] tmp_count = 0 for i in xrange(CFSMAX_SIZE + 1): if tids[i] == lasttid: tmp_count += 1 continue if tids[i] == ((lasttid + 1) % threads) or (tids[i] == -1): if tmp_count == chunk_size: tmp_count = 1 lastid = tids[i] else: if tids[i] == -1: if i == CFSMAX_SIZE: break else: print "E: Last thread (thread with number -1) was found before the end.\n" result = False else: print "E: chunk size was " + str( tmp_count) + ". (assigned was " + str( chunk_size) + ")\n" result = False else: print "E: Found thread with number " + str( tids[i]) + " (should be inbetween 0 and " + str(threads - 1) + ").\n" result = False return result