def omp_taskyield(): import omp from time import sleep NUM_TASKS = 25 count = 0 start_id = [0 for _ in range(NUM_TASKS)] current_id = [0 for _ in range(NUM_TASKS)] if 'omp parallel': use_omp = omp.in_parallel() if 'omp single': for i in range(NUM_TASKS): myi = i if 'omp task firstprivate(myi) untied': sleep(0.01) start_id[myi] = omp.get_thread_num() 'omp taskyield' if start_id[myi] % 2 == 0: sleep(0.01) current_id[myi] = omp.get_thread_num() for i in range(NUM_TASKS): if current_id[i] == start_id[i]: count += 1 return count < NUM_TASKS or not use_omp
def omp_task_untied(): import omp from time import sleep NUM_TASKS = 25 start_id = [0 for _ in xrange(NUM_TASKS)] current_id = [0 for _ in xrange(NUM_TASKS)] count = 0 if 'omp parallel': if 'omp single': for i in xrange(NUM_TASKS): myi = i if 'omp task firstprivate(myi) untied': sleep(0.01) start_id[myi] = omp.get_thread_num() 'omp taskwait' if start_id[myi] % 2 != 0: sleep(0.01) current_id[myi] = omp.get_thread_num() for i in xrange(NUM_TASKS): if current_id[i] == start_id[i]: count += 1 return count < NUM_TASKS
def barrier_test(): # omp parallel 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 print str(omp.get_thread_num()) + ' c\n',
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 _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',
def sections_test(): # omp parallel num_threads(2) # omp sections # omp section print 'section 0 from ' + str(omp.get_thread_num()) + '\n', # omp section end # omp section print 'section 1 from ' + str(omp.get_thread_num()) + '\n',
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 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 _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()
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 nowait_test(): # omp parallel num_threads(8) private(i) # omp for nowait for i in range(8): time.sleep(random.randrange(3)) print str(omp.get_thread_num()) + ' thread\n', print "done"
def omp_master_3(): import omp tid_result = 0 nthreads = 0 executing_thread = -1 if 'omp parallel': if 'omp master': tid = omp.get_thread_num() if tid != 0: if 'omp critical': tid_result += 1 if 'omp critical': nthreads += 1 executing_thread = omp.get_thread_num() return nthreads == 1 and executing_thread == 0 and tid_result == 0
def compute(accelerations: "float[:,:,:]", masses: "float[:]", positions: "float[:,:]"): nb_particules = masses.size nthreads = accelerations.shape[0] # omp parallel for schedule(static,8) for index_p0 in range(nb_particules - 1): vector = np.empty(dim) rank = omp.get_thread_num() position0 = positions[index_p0] mass0 = masses[index_p0] for index_p1 in range(index_p0 + 1, nb_particules): mass1 = masses[index_p1] for i in range(dim): vector[i] = position0[i] - positions[index_p1, i] distance = sqrt(sum(vector**2)) coef = 1.0 / distance**3 for i in range(dim): accelerations[rank, index_p0, i] -= coef * mass1 * vector[i] accelerations[rank, index_p1, i] += coef * mass0 * vector[i] # omp parallel for for i_part in range(nb_particules): for i_thread in range(1, nthreads): for i in range(dim): accelerations[0, i_part, i] += accelerations[i_thread, i_part, i]
def _block0(): # omp for for i in omp.prange(8): time.sleep(random.randrange(3)) print str(omp.get_thread_num()) + ' thread\n', omp.barrier() print "done"
def omp_for_schedule_dynamic(): CFDMAX_SIZE = 100 chunk_size = 7 tids = [0] * CFDMAX_SIZE count = 0 tmp_count = 0 result = 0 import omp if 'omp parallel shared(tids)': tid = omp.get_thread_num() 'omp for schedule(dynamic, chunk_size)' for i in range(CFDMAX_SIZE): tids[i] = tid for i in range(CFDMAX_SIZE - 1): if tids[i] != tids[i + 1]: count += 1 tmp = [1] * (count + 1) for i in range(CFDMAX_SIZE - 1): if tids[i] != tids[i + 1]: tmp_count += 1 tmp[tmp_count] = 1 else: tmp[tmp_count] += 1 for i in range(count): if tmp[i] % chunk_size != 0: result += 1 if tmp[count] % chunk_size != CFDMAX_SIZE % chunk_size: result += 1 return result == 0
def omp_for_schedule_dynamic(): CFDMAX_SIZE = 100 chunk_size = 7 tids = [0]*CFDMAX_SIZE count = 0 tmp_count = 0 result = 0 import omp if 'omp parallel shared(tids)': tid = omp.get_thread_num() 'omp for schedule(dynamic, chunk_size)' for i in range(CFDMAX_SIZE): tids[i] = tid for i in range(CFDMAX_SIZE-1): if tids[i] != tids[i+1]: count +=1 tmp = [1] * (count + 1) for i in range(CFDMAX_SIZE-1): if tids[i] != tids[i+1]: tmp_count+=1 tmp[tmp_count] = 1 else: tmp[tmp_count]+=1 for i in range(count): if tmp[i]%chunk_size != 0: result+=1 if tmp[count]%chunk_size != CFDMAX_SIZE%chunk_size: result+=1 return result == 0
def omp_sections_nowait(): import omp from time import sleep result = False count = 0 if 'omp parallel': in_parallel = omp.in_parallel() rank = omp.get_thread_num() if 'omp sections nowait': if 'omp section': sleep(0.01) count = 1 'omp flush(count)' if 'omp section': pass if 'omp sections': if 'omp section': pass if 'omp section': if count == 0: result = True return result or not in_parallel
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 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_master(): import omp threads = 0 executing_thread = -1 if 'omp parallel': if 'omp master': if 'omp critical': threads += 1 executing_thread = omp.get_thread_num() return threads == 1 and executing_thread == 0
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 omp_barrier(): import omp from time import sleep result1 = 0 result2 = 0 if 'omp parallel private(rank)': rank = omp.get_thread_num() if rank == 1: sleep(0.5) result2 = 3 'omp barrier' if rank == 2: result1 = result2 return result1 == 3
def omp_barrier(): import omp from time import sleep result1 = 0 result2 = 0 if 'omp parallel': rank = omp.get_thread_num() if rank == 1: sleep(0.5) result2 = 3 'omp barrier' if rank == 2: result1 = result2 return result1 == 3
def omp_flush(): result1 = 0 result2 = 0 if 'omp parallel private(rank)': rank = omp.get_thread_num() 'omp barrier' if rank == 1: result2 = 3 'omp flush (result2)' dummy = result2 if rank == 0: sleep(0.5) 'omp flush(result2)' result1 = result2 return result1 == result2 and result2 == dummy and result2 == 3
def omp_flush(): result1 = 0 result2 = 0 if 'omp parallel': rank = omp.get_thread_num() 'omp barrier' if rank == 1: result2 = 3 'omp flush (result2)' dummy = result2 if rank == 0: sleep(0.5) 'omp flush(result2)' result1 = result2 return result1 == result2 and result2 == dummy and result2 == 3
def omp_flush(): result1 = 0 result2 = 0 if 'omp parallel': use_omp = omp.in_parallel() rank = omp.get_thread_num() 'omp barrier' if rank == 1: result2 = 3 'omp flush (result2)' dummy = result2 if rank == 0: sleep(0.5) 'omp flush(result2)' result1 = result2 return not use_omp or (result1 == result2 and result2 == dummy and result2 == 3)
def omp_task(): import omp from time import sleep NUM_TASKS = 25 tids = range(NUM_TASKS) if 'omp parallel': for i in xrange(NUM_TASKS): myi = i if 'omp task': sleep(0.01) tids[myi] = omp.get_thread_num() for i in xrange(NUM_TASKS): if tids[0] != tids[i]: return True return False
def omp_task(): import omp from time import sleep NUM_TASKS = 25 tids = list(range(NUM_TASKS)) if 'omp parallel': use_parallel = omp.in_parallel() for i in range(NUM_TASKS): myi = i if 'omp task': sleep(0.01) tids[myi] = omp.get_thread_num() for i in range(NUM_TASKS): if tids[0] != tids[i]: return True return not use_parallel
def omp_for_nowait(): LOOPCOUNT = 1000 myarray = [0]*LOOPCOUNT result = 0 count = 0 import omp if 'omp parallel private(rank)': rank = omp.get_thread_num() 'omp for nowait' for i in range(LOOPCOUNT): if i == 0: while i < LOOPCOUNT**2: i+=1 count = 1 'omp flush(count)' for i in range(LOOPCOUNT): 'omp flush(count)' if count ==0: result = 1 return result == 1
def omp_barrier(): import omp from time import sleep result1 = 0 result2 = 0 #omp parallel num_threads(4) if 1: use_omp = omp.in_parallel() rank = omp.get_thread_num() if rank == 1: sleep(0.5) result2 = 3 #omp barrier if rank == 2: result1 = result2 if use_omp: return result1 == 3 else: return result1 == 0
def omp_for_nowait(): LOOPCOUNT = 1000 myarray = [0]*LOOPCOUNT result = 0 count = 0 import omp if 'omp parallel num_threads(4)': use_omp = omp.in_parallel() rank = omp.get_thread_num() 'omp for nowait' for i in range(LOOPCOUNT): if i == 0: while i < LOOPCOUNT**2: i+=1 count = 1 'omp flush(count)' for i in range(LOOPCOUNT): 'omp flush(count)' if count ==0: result = 1 return result == 1 or not use_omp
def omp_for_nowait(): LOOPCOUNT = 1000 myarray = [0] * LOOPCOUNT result = 0 count = 0 import omp if "omp parallel": rank = omp.get_thread_num() "omp for nowait" for i in range(LOOPCOUNT): if i == 0: while i < LOOPCOUNT ** 2: i += 1 count = 1 "omp flush(count)" for i in range(LOOPCOUNT): "omp flush(count)" if count == 0: result = 1 return result == 1
def omp_task_final(): import omp from time import sleep error = 0 NUM_TASKS = 25 tids = list(range(NUM_TASKS)) if 'omp parallel': if 'omp single': for i in range(NUM_TASKS): myi = i if 'omp task final(i>=10) private(k) firstprivate(myi)': sleep(0.01) tids[myi] = omp.get_thread_num() 'omp taskwait' for i in range(10, NUM_TASKS): if tids[10] != tids[i]: print(i, tids[10], tids[i]) error += 1 print(error) return error == 0
def omp_task_final(): import omp from time import sleep error = 0 NUM_TASKS = 25 tids = range(NUM_TASKS) if 'omp parallel': if 'omp single': for i in range(NUM_TASKS): myi = i if 'omp task final(i>=10) private(k) firstprivate(myi)': sleep(0.01) tids[myi] = omp.get_thread_num() 'omp taskwait' for i in range(10, NUM_TASKS): if tids[10] != tids[i]: print(i, tids[10], tids[i]) error += 1 print(error) return error == 0
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 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_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_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