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_in_parallel(): import omp serial = 1 isparallel = 0 serial = omp.in_parallel() if 'omp parallel': if 'omp single': isparallel = omp.in_parallel() if 'omp parallel': if 'omp single': pass return not serial and isparallel
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 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_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_nested(): import omp counter = 0 omp.set_nested(1) if 'omp parallel shared(counter) num_threads(4)': use_omp = omp.in_parallel() if 'omp critical': counter += 1 if 'omp parallel num_threads(4)': if 'omp critical': counter -= 1 return counter != 0 or not use_omp
def omp_get_wtime(): import omp from time import sleep wait_time = 2 #omp parallel in_parallel = omp.in_parallel() start = omp.get_wtime() sleep(wait_time) end = omp.get_wtime() measured_time = end - start print(measured_time, wait_time) return (measured_time > 0.9 * wait_time and measured_time < 1.1 * wait_time) or not in_parallel
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': 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_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_task_imp_firstprivate(): i = 5 k = 0 result = False NUM_TASKS = 25 task_result = True if 'omp parallel firstprivate(i)': in_parallel = omp.in_parallel() if 'omp single': for k in range(NUM_TASKS): if 'omp task shared(result, task_result)': if i != 5: task_result = False for j in range(0, NUM_TASKS): i += 1 'omp taskwait' result = task_result and i == 5 return result or not in_parallel
def omp_task_imp_firstprivate(): i = 5 k = 0 result = 0 NUM_TASKS = 25 task_result = 1 if 'omp parallel firstprivate(i)': in_parallel = omp.in_parallel() if 'omp single': for k in range(NUM_TASKS): if 'omp task shared(result, task_result)': if i != 5: task_result = 0 for j in range(0, NUM_TASKS): i += 1 'omp taskwait' result = task_result and i == 5 return result or not in_parallel
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_task_firstprivate(): sum = 1234 result = 0 LOOPCOUNT = 1000 NUM_TASKS = 25 known_sum = 1234 + (LOOPCOUNT * (LOOPCOUNT + 1)) / 2 if 'omp parallel': in_parallel = omp.in_parallel() if 'omp single': for i in range(NUM_TASKS): if 'omp task firstprivate(sum)': for j in range(LOOPCOUNT + 1): 'omp flush' sum += j if sum != known_sum: if 'omp critical': result += 1 return result == 0 or not in_parallel
def omp_single_private(): nr_threads_in_single = 0 nr_iterations = 0 result = 0 LOOPCOUNT = 1000 if 'omp parallel': use_parallel = omp.in_parallel() myresult = 0 myit = 0 for i in range(LOOPCOUNT): if 'omp single private(nr_threads_in_single)': nr_threads_in_single = 0 'omp flush' nr_threads_in_single += 1 'omp flush' myit += 1 myresult += nr_threads_in_single if 'omp critical': result += nr_threads_in_single nr_iterations += myit return not use_parallel or (result == 0 and nr_iterations == LOOPCOUNT)
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_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