def main(): N = 1e8 // 2 print("Data size", N) targets = ["cpu", "parallel"] # run just one target if is specified in the argument for t in targets: if t in sys.argv[1:]: targets = [t] break for target in targets: print("== Target", target) vect_discriminant = vectorize(["f4(f4, f4, f4)", "f8(f8, f8, f8)"], target=target)(discriminant) A, B, C = generate_input(N, dtype=np.float32) D = np.empty(A.shape, dtype=A.dtype) ts = time() D = vect_discriminant(A, B, C) te = time() total_time = te - ts print("Execution time %.4f" % total_time) print("Throughput %.4f" % (N / total_time)) if "-verify" in sys.argv[1:]: check_answer(D, A, B, C)
def test_func(self): A = np.array(np.random.random((n, n)), dtype=np.float32) B = np.array(np.random.random((n, n)), dtype=np.float32) C = np.empty_like(A) s = time() stream = cuda.stream() with stream.auto_synchronize(): dA = cuda.to_device(A, stream) dB = cuda.to_device(B, stream) dC = cuda.to_device(C, stream) cu_square_matrix_mul[(bpg, bpg), (tpb, tpb), stream](dA, dB, dC) dC.copy_to_host(C, stream) e = time() tcuda = e - s # Host compute Amat = np.matrix(A) Bmat = np.matrix(B) s = time() Cans = Amat * Bmat e = time() tcpu = e - s # Check result self.assertTrue(np.allclose(C, Cans))
def run_div_test(fld, exact, title='', show=False, ignore_inexact=False): t0 = time() result_numexpr = viscid.div(fld, preferred="numexpr", only=False) t1 = time() logger.info("numexpr magnitude runtime: %g", t1 - t0) result_diff = viscid.diff(result_numexpr, exact)['x=1:-1, y=1:-1, z=1:-1'] if not ignore_inexact and not (result_diff.data < 5e-5).all(): logger.warning("numexpr result is far from the exact result") logger.info("min/max(abs(numexpr - exact)): %g / %g", np.min(result_diff.data), np.max(result_diff.data)) planes = ["y=0j", "z=0j"] nrows = 2 ncols = len(planes) _, axes = plt.subplots(nrows, ncols, squeeze=False) for i, p in enumerate(planes): vlt.plot(result_numexpr, p, ax=axes[0, i], show=False) vlt.plot(result_diff, p, ax=axes[1, i], show=False) plt.suptitle(title) vlt.auto_adjust_subplots(subplot_params=dict(top=0.9)) plt.savefig(next_plot_fname(__file__)) if show: vlt.mplshow()
def trace_fortran(fld_bx, fld_by, fld_bz): gx, gy, gz = fld_bx.get_crds_cc(('x', 'y', 'z')) nz, ny, nx = fld_bx.shape t0 = time() bx_farr = np.array(np.ravel(fld_bx.data, order='K').reshape((nx, ny, nz), order="F")) # pylint: disable=C0301 by_farr = np.array(np.ravel(fld_by.data, order='K').reshape((nx, ny, nz), order="F")) # pylint: disable=C0301 bz_farr = np.array(np.ravel(fld_bz.data, order='K').reshape((nx, ny, nz), order="F")) # pylint: disable=C0301 topo = np.zeros(gsize, order='F', dtype='int32') nsegs = np.zeros((1,), order='F', dtype='int32') # logger.info((np.ravel(bx_farr, order='K') == np.ravel(fld_bx.data, order='K')).all()) # pylint: disable=C0301 # logger.info((np.ravel(by_farr, order='K') == np.ravel(fld_by.data, order='K')).all()) # pylint: disable=C0301 # logger.info((np.ravel(bz_farr, order='K') == np.ravel(fld_bz.data, order='K')).all()) # pylint: disable=C0301 # logger.info("bx_arr") # logger.info(bx_farr.strides) # logger.info(bx_farr.flags) # logger.info("topo") # logger.info(topo.strides) # logger.info(topo.shape) tracer.get_topo(gx, gy, gz, bx_farr, by_farr, bz_farr, topo, x1, x2, y1, y2, z1, z2, nsegs) t1 = time() t = t1 - t0 print("total segments calculated: {0:.05e}".format(float(nsegs))) print("time: {0:.4}s ... {1:.4}s/segment".format(t, t / float(nsegs))) topo_fld = vol.wrap_field(topo, name="FortTopo") return None, topo_fld
def run(self): print('Running part 5') filename = './' + self.out_dir + '/time.txt' with open(filename, 'w') as text_file: t0 = time() self.nn_pca_cluster_wine() text_file.write('nn_pca_wine: %0.3f seconds\n' % (time() - t0)) t0 = time() self.nn_ica_cluster_wine() text_file.write('nn_ica_wine: %0.3f seconds\n' % (time() - t0)) t0 = time() self.nn_rp_cluster_wine() text_file.write('nn_rp_wine: %0.3f seconds\n' % (time() - t0)) t0 = time() self.nn_lda_cluster_wine() text_file.write('nn_lda_wine: %0.3f seconds\n' % (time() - t0)) t0 = time() self.nn_wine_orig() text_file.write('nn_wine_orig: %0.3f seconds\n' % (time() - t0))
def __init__(self, url, sc, tag): """ Args: url: url to author page on freeclassicebooks.com site sc: SparkContext object tag: name of the tag where text is located in mht files """ self.url = url self.sc = sc self.tag = tag t0 = time() self.words = self.get_words() self.time_message(time() - t0, "get_words") t0 = time() self.words_num = self.count_words() self.time_message(time() - t0, "count_words") t0 = time() self.words_occ = self.count_words_occurrence() self.time_message(time() - t0, "count_words_occurrence") t0 = time() self.top_words = self.top_words() self.time_message(time() - t0, "top_words") t0 = time() self.top_nouns = self.top_nouns() self.time_message(time() - t0, "top_nouns")
def run_mag_test(fld, title="", show=False): vx, vy, vz = fld.component_views() # pylint: disable=W0612 vx, vy, vz = fld.component_fields() try: t0 = time() mag_ne = viscid.magnitude(fld, preferred="numexpr", only=False) t1 = time() logger.info("numexpr mag runtime: %g", t1 - t0) except viscid.verror.BackendNotFound: xfail("Numexpr is not installed") planes = ["z=0", "y=0"] nrows = 4 ncols = len(planes) _, axes = plt.subplots(nrows, ncols, sharex=True, sharey=True, squeeze=False) for ind, p in enumerate(planes): vlt.plot(vx, p, ax=axes[0, ind], show=False) vlt.plot(vy, p, ax=axes[1, ind], show=False) vlt.plot(vz, p, ax=axes[2, ind], show=False) vlt.plot(mag_ne, p, ax=axes[3, ind], show=False) plt.suptitle(title) vlt.auto_adjust_subplots(subplot_params=dict(top=0.9, right=0.9)) plt.gcf().set_size_inches(6, 7) plt.savefig(next_plot_fname(__file__)) if show: vlt.mplshow()
def timeit(message, display=True): """Context to time an execution.""" start = time() yield if not display: return print("{}: {:.3f} s".format(message, time() - start))
def objective(trial: Trial): args = gen_args() # acquisition hyperparam's args.cluster = bool(trial.suggest_int('cluster', 0, 1)) if not args.cluster and random() > 0.5: args.epsilon = trial.suggest_float('epsilon', 0.00, 0.2, step=0.05) args.fps = None if args.model in {'rf', 'nn'} or args.cluster: args.encoder = trial.suggest_categorical('encoder', {'morgan', 'pair', 'rdkit'}) try: exp = Explorer(**vars(args)) except (IncompatibilityError, NotImplementedError) as e: print(e) return float('-inf') start = time() exp.run() total = time() - start m, s = divmod(total, 60) h, m = divmod(int(m), 60) print(f'Total time for trial #{trial.number}: {h}h {m}m {s:0.2f}s\n') return exp.top_k_avg
def main(): N = 1e+8 // 2 print('Data size', N) targets = ['cpu', 'parallel'] # run just one target if is specified in the argument for t in targets: if t in sys.argv[1:]: targets = [t] break for target in targets: print('== Target', target) vect_discriminant = vectorize([f4(f4, f4, f4), f8(f8, f8, f8)], target=target)(discriminant) A, B, C = generate_input(N, dtype=np.float32) D = np.empty(A.shape, dtype=A.dtype) ts = time() D = vect_discriminant(A, B, C) te = time() total_time = (te - ts) print('Execution time %.4f' % total_time) print('Throughput %.4f' % (N / total_time)) if '-verify' in sys.argv[1:]: check_answer(D, A, B, C)
def nn_analysis(self, X_train, X_test, y_train, y_test, data_set_name, analysis_name='Neural Network'): clf = MLPClassifier(activation='relu', learning_rate='constant', shuffle=True, solver='adam', random_state=0, max_iter=1000, batch_size=60) with open(self.nn_time_filename, 'a+') as text_file: t0 = time() clf.fit(X_train, y_train) text_file.write(analysis_name.lower() + ' fit time: %0.3f seconds\n' % (time() - t0)) t0 = time() y_pred = clf.predict(X_test) text_file.write(analysis_name.lower() + ' predict time: %0.3f seconds\n' % (time() - t0)) cv = StratifiedShuffleSplit(n_splits=100, test_size=0.2, random_state=0) title = 'Learning Curve (' + analysis_name + ') for ' + data_set_name name = data_set_name.lower() + '_' + analysis_name.lower() + '_nn.png' filename = './' + self.out_dir + '/' + name ## ## Plots ## ph = plot_helper() ## ## Learning Curve ## ph.plot_learning_curve(clf, title, X_train, y_train, ylim=None, cv=cv, n_jobs=-1, filename=filename)
def main(): targets = ['cpu', 'parallel'] # run just one target if is specified in the argument for t in targets: if t in sys.argv[1:]: targets = [t] break for target in targets: print('== Target', target) vect_sum = vectorize(['f4(f4, f4)', 'f8(f8, f8)'], target=target)(sum) A = np.random.random(N).astype(np.float32) B = np.random.random(N).astype(np.float32) assert A.shape == B.shape assert A.dtype == B.dtype assert len(A.shape) == 1 D = np.empty(A.shape, dtype=A.dtype) print('Data size', N) ts = time() D = vect_sum(A, B) te = time() total_time = (te - ts) print('Execution time %.4f' % total_time) print('Throughput %.4f' % (N / total_time)) if '-verify' in sys.argv[1:]: check_answer(D, A, B, C)
def fill_mem(memories): # get remaining memory count remaining = memories.MEMORY_SIZE - len(memories.ltmemory) print("Filling {} memories".format(remaining)) n_jobs = 4 batch_size = remaining / n_jobs if remaining % n_jobs == 0 else int( remaining / n_jobs) + 1 executor = Parallel(n_jobs=n_jobs, backend="multiprocessing", prefer="processes") start = time() chunks = executor( delayed(worker)(batch_size, testing_agent(MCTS_SIMS, 'best_player')) for _ in range(n_jobs)) for chunk in chunks: for state, value in chunk: memories.commit_stmemory(state) memories.stmemory[-1]["value"] = value memories.commit_ltmemory() total = time() - start print("Total time: {}, time per memory: {}".format( total, total / len(memories.ltmemory))) #print(59*total/len(memories.ltmemory)) pickle.dump(memories, open("bo3texas42_50k.p", "wb"))
def search(graph, state, is_goal, limit, heuristic): tempfinal = 0 start_time = time() frontier = [] # our queue which we interpret as a priority queue heappush(frontier, (0, state)) #putting our instial position with its cost came_from = {} # where we have been cost_so_far = {} #the cost so far came_from[state] = (0, 0) #just setting up the intial destination cost_so_far[state] = 0 #where were starting done = 0 counter = 0 # Implement your search here! Use your heuristic here! # When you find a path to the goal return a list of tuples [(state, action)] # representing the path. Each element (tuple) of the list represents a state # in the path and the action that took you to this state while frontier: (_, current) = heappop(frontier) # gets lowest priority cur_cost = cost_so_far[current] #print("current cost", cur_cost) if is_goal(current): # if it equals our goal destnation done = 1 goback = [] current = (current, 0) while current[0] != 0: goback.insert(0, current) current = came_from[current[0]] goback.pop() print(goback) #print("final length:", len(goback)) tempfinal = cur_cost break #print("loop "+str(counter)) counter += 1 for next in graph(current): # for all elements in adjacent to it h = heuristic(next[1], next[0]) if h == -1: continue if next[1] in cost_so_far: if next[2] + cur_cost < cost_so_far[next[1]]: cost_so_far[next[1]] = next[2] + cur_cost came_from[next[1]] = (current, next[0]) else: cost_so_far[next[1]] = next[2] + cur_cost came_from[next[1]] = (current, next[0]) priority = cost_so_far[next[1]] + h #print("priority", priority, "cost so far", cur_cost, "recipe cost", next[2]) #print("recipe name", next[0]) heappush(frontier, (priority, next[1])) #print("pushed", next) print(time() - start_time, 'seconds.') if done == 1: # if it equals our goal destnation print("final cost:", tempfinal) print("success") else: # Failed to find a path print("Failed to find a path from", state, 'within time limit.') return None
def fibo3(n): fib3Start = time() fib3(n) fib3End = time() fibo3time = fib3End - fib3Start fib3Time.append(fibo3time) return fibo3time
def test(ty): print("Test %s" % ty) data = np.array(np.random.random(1e6 + 1), dtype=ty) ts = time() stream = cuda.stream() device_data = cuda.to_device(data, stream) dresult = cuda_ufunc(device_data, device_data, stream=stream) result = dresult.copy_to_host() stream.synchronize() tnumba = time() - ts ts = time() gold = np_ufunc(data, data) tnumpy = time() - ts print("Numpy time: %fs" % tnumpy) print("Numba time: %fs" % tnumba) if tnumba < tnumpy: print("Numba is FASTER by %fx" % (tnumpy / tnumba)) else: print("Numba is SLOWER by %fx" % (tnumba / tnumpy)) self.assertTrue(np.allclose(gold, result), (gold, result))
def test_gufunc(self): @guvectorize([void(float32[:, :], float32[:, :], float32[:, :])], '(m,n),(n,p)->(m,p)', target='cuda') def matmulcore(A, B, C): m, n = A.shape n, p = B.shape for i in range(m): for j in range(p): C[i, j] = 0 for k in range(n): C[i, j] += A[i, k] * B[k, j] gufunc = matmulcore gufunc.max_blocksize = 512 matrix_ct = 1001 # an odd number to test thread/block division in CUDA A = np.arange(matrix_ct * 2 * 4, dtype=np.float32).reshape(matrix_ct, 2, 4) B = np.arange(matrix_ct * 4 * 5, dtype=np.float32).reshape(matrix_ct, 4, 5) ts = time() C = gufunc(A, B) tcuda = time() - ts ts = time() Gold = ut.matrix_multiply(A, B) tcpu = time() - ts non_stream_speedups.append(tcpu / tcuda) self.assertTrue(np.allclose(C, Gold))
def main(): print('''\ ********************************************************************* * __ __ ______ __ ______ ______ __ * * /\ "-./ \ /\ __ \ /\ \ /\ == \ /\ __ \ /\ \ * * \ \ \-./\ \ \ \ \/\ \ \ \ \____ \ \ _-/ \ \ __ \ \ \ \____ * * \ \_\ \ \_\ \ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_____\ * * \/_/ \/_/ \/_____/ \/_____/ \/_/ \/_/\/_/ \/_____/ * *********************************************************************''') print('Welcome to MolPAL!') params = vars(args.gen_args()) print(f'MolPAL will be run with the following arguments:') for k, v in sorted(params.items()): print(f' {k}: {v}') print(flush=True) explorer = Explorer(**params) start = time() try: explorer.run() except BaseException: state_file = explorer.save() print(f'Exception raised! Intemediate state saved to "{state_file}"') raise stop = time() m, s = divmod(stop - start, 60) h, m = divmod(int(m), 60) d, h = divmod(h, 24) print(f'Total time for exploration: {d}d {h}h {m}m {s:0.2f}s') print('Thanks for using MolPAL!')
def process_answers(questions_asked=[], unprocessed_questions=[], facts={}): # questions_asked - all the question/answer tuples that you want to add to the 'answers' list (of instantiated classes) # unprocessed_questions - all the question/answer tuples that you still need to incorporate into the facts dictionary # facts - the facts dictionary so far (altered in place) # #instantiate classes for each dataset we have #put these instances in the 'answers' array which is #returned. Also alter the facts variable in place. answers = [] for it, qa in enumerate(questions_asked): t0 = time() if 'answer' not in qa: continue #this question doesn't have an answer c = [ cls for cls in ans.Answer.__subclasses__() if cls.dataset == qa['dataset'] ] if len(c) == 0: continue #don't know this sort of data: skip it name = "item%d" % it new_answer = c[0](name, qa['dataitem'], qa['detail'], qa['answer']) answers.append(new_answer) if qa in unprocessed_questions: logging.info("process_answers: adding facts from %s" % new_answer.dataset) new_answer.append_facts(facts, answers) logging.info('Time (class %s) append_facts %fms' % (new_answer.dataset, (time() - t0) * 1000)) return answers
def _bin_data(self, X, rng, is_training_data): """Bin data X. If is_training_data, then set the bin_mapper_ attribute. Else, the binned data is converted to a C-contiguous array. """ description = 'training' if is_training_data else 'validation' if self.verbose: print("Binning {:.3f} GB of {} data: ".format( X.nbytes / 1e9, description), end="", flush=True) tic = time() if is_training_data: X_binned = self.bin_mapper_.fit_transform(X) # F-aligned array else: X_binned = self.bin_mapper_.transform(X) # F-aligned array # We convert the array to C-contiguous since predicting is faster # with this layout (training is faster on F-arrays though) X_binned = np.ascontiguousarray(X_binned) toc = time() if self.verbose: duration = toc - tic print("{:.3f} s".format(duration)) return X_binned
def func2(num): s = time() num = sp.sympify(num) res = num.is_Symbol e = time() print e-s return res
def fn(*args, **kwargs): before = time() return_val = func(*args, **kwargs) after = time() time_ms = (after - before) * 1000 return return_val, time_ms
def main(): targets = ['cpu', 'parallel'] # run just one target if is specified in the argument for t in targets: if t in sys.argv[1:]: targets = [t] break for target in targets: print('== Target', target) vect_sum = vectorize([f4(f4, f4), f8(f8, f8)], target=target)(sum) A = np.random.random(N).astype(np.float32) B = np.random.random(N).astype(np.float32) assert A.shape == B.shape assert A.dtype == B.dtype assert len(A.shape) == 1 D = np.empty(A.shape, dtype=A.dtype) print('Data size', N) ts = time() D = vect_sum(A, B) te = time() total_time = (te - ts) print('Execution time %.4f' % total_time) print('Throughput %.4f' % (N / total_time)) if '-verify' in sys.argv[1:]: assert np.allclose
def main(): N = 1e+8 // 2 print('Data size', N) targets = ['cpu', 'parallel'] # run just one target if is specified in the argument for t in targets: if t in sys.argv[1:]: targets = [t] break for target in targets: print('== Target', target) vect_discriminant = vectorize( [f4(f4, f4, f4), f8(f8, f8, f8)], target=target)(discriminant) A, B, C = generate_input(N, dtype=np.float32) D = np.empty(A.shape, dtype=A.dtype) ts = time() D = vect_discriminant(A, B, C) te = time() total_time = (te - ts) print('Execution time %.4f' % total_time) print('Throughput %.4f' % (N / total_time)) if '-verify' in sys.argv[1:]: check_answer(D, A, B, C)
def generate_evaluations(gate_num): for i in range(1, gate_num + 1): start_t = time() results = {name: [] for name in empty_results.keys()} print(i) print(len(trees_list[i])) cnt = 0 for tree in trees_list[i]: cnt += 1 print(str(tree) + " " + str(cnt), end=" ") start_ti = time() evaluations = generate_all_evaluations(tree) # for expression, evaluation in evaluations.items(): # check(expression, evaluation, i) evaluations = check_all(evaluations, i, results) if i != num_of_gates or next_level_test: add_tree(tree, evaluations, i) # add_tree(tree, evaluations, i) end_ti = time() if i >= 4: print("Tree Time: " + str(end_ti - start_ti), end="") print() if overwrite_results: write_results(results) # print("IM " + str(i) + " : " + str(total_size(intermediate))) print("Loop " + str(i) + " Time: " + str(time() - start_t)) print()
def tick_context(value, sleep=sleep): """Generate a context that controls the duration of its execution.""" start = time() yield sleep_time = start + value - time() if sleep_time > 0: sleep(sleep_time)
def main(): targets = ['cpu', 'stream', 'parallel'] # run just one target if is specified in the argument for t in targets: if t in sys.argv[1:]: targets = [t] break for target in targets: print('== Target', target) vect_sum = vectorize([f4(f4, f4), f8(f8, f8)], target=target)(sum) A = np.fromfile('inputA.dat', dtype=np.float32) B = np.fromfile('inputB.dat', dtype=np.float32) assert A.shape == B.shape assert A.dtype == B.dtype assert len(A.shape) == 1 N = A.shape[0] D = np.empty(A.shape, dtype=A.dtype) print('Data size', N) ts = time() D = vect_sum(A, B) te = time() total_time = (te - ts) print('Execution time %.4f' % total_time) print('Throughput %.4f' % (N / total_time)) if '-verify' in sys.argv[1:]: check_answer(D, A, B, C)
def timeit(f, *args, **kwargs): t0 = time() ret = f(*args, **kwargs) t1 = time() print("Took {0:.03g} secs.".format(t1 - t0)) return ret
def main(): if (len(sys.argv) > 2): print('must pass path to the bash script') print('e.g. python ./validator ./bash.sh') elif (len(sys.argv) < 2): print('path to the bash script was not passed') print('will try using ./script.sh') path = './script.sh' else: path = sys.argv[1] cum_sum = 0 for n_idx, n in enumerate(N): for h_idx, h in enumerate(H): k = Ks[n_idx][h_idx] ref = Ref[n_idx][h_idx] print(f'Will do k: {k} h: {h} n: {n}') original_input = parse_original_input(n, k, h) # print(original_input) start = time() output = check_output(f'{path} {str(n)} {str(k)} {str(h)}', shell=True) end = time() part_time = end - start cum_sum += part_time print(f'Czas: {part_time}') output = output.decode("utf-8") # print(output) validate_output(output, original_input, ref) # calculate_time print(f'Cum_time_sum: {cum_sum}')
def asyncDFListProcessing(function, df, lst, function_name=None, *args): if function_name: print("Starting: {}".format(function_name)) print("This could take several minutes.") start = time() pool = Pool(processes=multiprocessing.cpu_count()) results = [pool.apply_async(function, args=( df, val, )) for val in lst] output = [p.get() for p in results] pool.close() pool.join() end = time() if function_name: print("Completed: {}".format(function_name)) print("Time required: {}".format(end - start)) else: print("Completion time: {}".format(end - start)) return output
def fn(*args, **kwargs): before = time() return_val = func(*args, **kwargs) after = time() time_ms = (after - before) * 1000 print(f'Time take to execute {func.__qualname__}: {time_ms} ms') return return_val
def run_mag_test(fld, title="", show=False): vx, vy, vz = fld.component_views() # pylint: disable=W0612 vx, vy, vz = fld.component_fields() try: t0 = time() mag_ne = viscid.magnitude(fld, preferred="numexpr", only=False) t1 = time() logger.info("numexpr mag runtime: %g", t1 - t0) except viscid.verror.BackendNotFound: xfail("Numexpr is not installed") planes = ["z=0", "y=0"] nrows = 4 ncols = len(planes) ax = plt.subplot2grid((nrows, ncols), (0, 0)) ax.axis("equal") for ind, p in enumerate(planes): plt.subplot2grid((nrows, ncols), (0, ind), sharex=ax, sharey=ax) mpl.plot(vx, p, show=False) plt.subplot2grid((nrows, ncols), (1, ind), sharex=ax, sharey=ax) mpl.plot(vy, p, show=False) plt.subplot2grid((nrows, ncols), (2, ind), sharex=ax, sharey=ax) mpl.plot(vz, p, show=False) plt.subplot2grid((nrows, ncols), (3, ind), sharex=ax, sharey=ax) mpl.plot(mag_ne, p, show=False) mpl.plt.suptitle(title) mpl.auto_adjust_subplots(subplot_params=dict(top=0.9)) mpl.plt.gcf().set_size_inches(6, 7) mpl.plt.savefig(next_plot_fname(__file__)) if show: mpl.mplshow()
def generate_evaluations(gate_num): for i in range(1, gate_num + 1): start_t = time() print(i) print(len(trees_list[i])) cnt = 0 for tree in trees_list[i]: cnt += 1 print(str(tree) + " " + str(cnt), end=" ") start_ti = time() evaluations = generate_all_evaluations(tree) # for expression, evaluation in evaluations.items(): # check(expression, evaluation, i) # 26.5 s w/ 21.5 s w/o 20.8 s w/mod # 20 s w/mod+trees 25.5 s w/mod+trees on 4 evaluations = check_all(evaluations, i) if i != num_of_gates or next_level_test: add_tree(tree, evaluations, i) end_ti = time() if i >= 4: print("Tree Time: " + str(end_ti - start_ti), end="") print() print("Loop " + str(i) + " Time: " + str(time() - start_t)) print()
def generate_yaw_filtered(self): if not hasattr(self, 'x_filtered'): return start = time() self.ya_filtered = [] prevHeading = np.array([.0, .0, .0]) for s, _ in enumerate(self.vx_filtered[1:]): p1 = np.array([self.x_filtered[s], self.y_filtered[s]]) p2 = np.array([self.x_filtered[s + 1], self.y_filtered[s + 1]]) if self.YAW_HEADING[0] == 'center': heading = np.array(self.YAW_HEADING[1]) - p1 elif self.YAW_HEADING[0] == 'axes': heading = np.array(self.YAW_HEADING[1]) else: heading = p2 - p1 heading = (heading / self.norm2(heading) ) if self.norm2(heading) != 0 else prevHeading prevHeading = heading self.ya_filtered.append(math.atan2(heading[1], heading[0])) self.ya_filtered.append(self.ya_filtered[-1]) print('generate_yaw_filtered() runs in {} s'.format(time() - start))
def _bench(meta_info, X_train, y_train, fit_samples, fit_repetitions, predict_samples, predict_repetitions, svc_params_dict): fit_times = [] for it in range(fit_samples): start = time() for __ in range(fit_repetitions): clf = svm.SVC(**svc_params_dict) clf.fit(X_train, y_train) stop = time() fit_times.append(stop - start) predict_times = [] for it in range(predict_samples): start = time() for __ in range(predict_repetitions): res = clf.predict(X_train) stop = time() predict_times.append(stop - start) print("{meta_info},{fit_t:0.6g},{pred_t:0.6g},{acc:0.3f},{sv_len},{cl}". format(meta_info=meta_info, fit_t=min(fit_times) / fit_repetitions, pred_t=min(predict_times) / predict_repetitions, acc=100 * accuracy_score(y_train, res), sv_len=clf.support_.shape[0], cl=clf.n_support_.shape[0]))
def main(): gsize = (2, 2, 2) x1 = -10.0; x2 = -5.0 # pylint: disable=C0321 y1 = -5.0; y2 = 5.0 # pylint: disable=C0321 z1 = -5.0; z2 = 5.0 # pylint: disable=C0321 vol = seed.Volume((z1, y1, x1), (z2, y2, x2), gsize) f3d = readers.load_file("/Users/kmaynard/dev/work/t1/t1.3df.004320.xdmf") fld_bx = f3d["bx"] fld_by = f3d["by"] fld_bz = f3d["bz"] B = field.scalar_fields_to_vector([fld_bx, fld_by, fld_bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) topo_arr = np.empty(gsize, order='C', dtype='int') lines, topo = None, None t0 = time() cProfile.runctx("nsegs = py_get_topo(B, topo_arr, x1, x2, y1, y2, z1, z2)", globals(), locals(), "topo.prof") t1 = time() s = pstats.Stats("topo.prof") s.strip_dirs().sort_stats("tottime").print_stats() nsegs = py_get_topo(B, topo_arr, x1, x2, y1, y2, z1, z2) t = t1 - t0 print(topo_arr) # print("numba time: {0}s, {1}s/seg".format(t, t / nsegs)) print("numba time: {0}s".format(t))
def test_gufunc_stream(self): #cuda.driver.flush_pending_free() matrix_ct = 1001 # an odd number to test thread/block division in CUDA A = np.arange(matrix_ct * 2 * 4, dtype=np.float32).reshape(matrix_ct, 2, 4) B = np.arange(matrix_ct * 4 * 5, dtype=np.float32).reshape(matrix_ct, 4, 5) ts = time() stream = cuda.stream() dA = cuda.to_device(A, stream) dB = cuda.to_device(B, stream) dC = cuda.device_array(shape=(1001, 2, 5), dtype=A.dtype, stream=stream) dC = gufunc(dA, dB, out=dC, stream=stream) C = dC.copy_to_host(stream=stream) stream.synchronize() tcuda = time() - ts ts = time() Gold = ut.matrix_multiply(A, B) tcpu = time() - ts stream_speedups.append(tcpu / tcuda) self.assertTrue(np.allclose(C, Gold))
def wrapper(self, *args, **kwargs): # Open link if self.link is None: self.open() # Init time start = time() no_control = self.callback_timeout >= self.instrument_timeout # Loop over timeouts while True: try: # Run the function result = func(self, *args, **kwargs) except Vxi11Exception as exc: # Time control no_timeout = exc.err != ERR_IO_TIMEOUT expired = time() > start + self.instrument_timeout # Reraise exception if no_control or no_timeout or expired: raise # Callback with exc if self.callback: self.callback(exc) else: # Callback without exc if self.callback: self.callback(None) # Return return result
def test_func(self): np.random.seed(42) A = np.array(np.random.random((n, n)), dtype=np.float32) B = np.array(np.random.random((n, n)), dtype=np.float32) C = np.empty_like(A) s = time() stream = cuda.stream() with stream.auto_synchronize(): dA = cuda.to_device(A, stream) dB = cuda.to_device(B, stream) dC = cuda.to_device(C, stream) cu_square_matrix_mul[(bpg, bpg), (tpb, tpb), stream](dA, dB, dC) dC.copy_to_host(C, stream) e = time() tcuda = e - s # Host compute s = time() Cans = np.dot(A, B) e = time() tcpu = e - s # Check result np.testing.assert_allclose(C, Cans, rtol=1e-5)
def search(graph, state, is_goal, limit, heuristic, goal): start_time = time() next_state = state cost = 0 states = 0 actions = [(state.copy(), None)] if is_goal(next_state): return actions, cost, start_time # Implement your search here! Use your heuristic here! # When you find a path to the goal return a list of tuples [(state, action)] # representing the path. Each element (tuple) of the list represents a state # in the path and the action that took you to this state while time() - start_time < limit: states_to_search = [] for new_state in graph(next_state.copy()): states += 1 heu = heuristic(new_state[1], new_state[2], goal) heappush(states_to_search, (heu, (new_state[0], new_state[1].copy(), new_state[2]))) checker_state = next_state.copy() test = heappop(states_to_search)[1] next_state = test[1].copy() test_tuple = (test[1].copy(), test[0]) cost += test[2] actions.append(test_tuple) if is_goal(next_state): return actions, cost, start_time pass # Failed to find a path print("here") print(time() - start_time, 'seconds.') print("Failed to find a path from", state, 'within time limit.') return None
async def generate(si): # loop over frames from the output stream while True: await asyncio.sleep(0.003) #FIXME: is this necessary? # wait until the lock is acquired frame = await si.get_frame() # check if the output frame is available, otherwise skip # the iteration of the loop if frame is None: continue t1 = time() # encode the frame in JPEG format (flag, encodedImage) = cv2.imencode(".jpg", frame) t2 = time() #print("imencode={:.0f}ms".format((t2-t1)*1000)) # ensure the frame was successfully encoded if not flag: continue # yield the output frame in the byte format t1 = time() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n') t2 = time()
def asyncDictProcessing(function, processing_list, function_name=None, *args): if function_name: print("Starting: {}".format(function_name)) start = time() pool = Pool(processes=multiprocessing.cpu_count()) results = [ pool.apply_async(function, args=( key, val, )) for key, val in processing_list.items() ] output = [p.get() for p in results] pool.close() pool.join() end = time() if function_name: print("Completed: {}".format(function_name)) print("Time required: {}".format(end - start)) else: print("Completion time: {}".format(end - start)) return output
def test_func(self): A = np.array(np.random.random((n, n)), dtype=np.float32) B = np.array(np.random.random((n, n)), dtype=np.float32) C = np.empty_like(A) print("N = %d x %d" % (n, n)) s = time() stream = cuda.stream() with stream.auto_synchronize(): dA = cuda.to_device(A, stream) dB = cuda.to_device(B, stream) dC = cuda.to_device(C, stream) cu_square_matrix_mul[(bpg, bpg), (tpb, tpb), stream](dA, dB, dC) dC.copy_to_host(C, stream) e = time() tcuda = e - s # Host compute Amat = np.matrix(A) Bmat = np.matrix(B) s = time() Cans = Amat * Bmat e = time() tcpu = e - s print('cpu: %f' % tcpu) print('cuda: %f' % tcuda) print('cuda speedup: %.2fx' % (tcpu / tcuda)) # Check result self.assertTrue(np.allclose(C, Cans))
def generate_states(self): start = time() self.x_discretized.extend([self.x_discretized[-1]] * self.EXTRA_POINTS_END) self.y_discretized.extend([self.y_discretized[-1]] * self.EXTRA_POINTS_END) self.z_discretized.extend([self.z_discretized[-1]] * self.EXTRA_POINTS_END) self.ya_discretized = [.0] self.vx_discretized = [.0] self.vy_discretized = [.0] self.vz_discretized = [.0] self.ax_discretized = [.0] self.ay_discretized = [.0] self.az_discretized = [.0] self.ti_discretized = [.0] prevHeading = np.array([.0, .0]) for s, _ in enumerate(self.x_discretized[1:]): p1 = np.array([self.x_discretized[s], self.y_discretized[s]]) p2 = np.array( [self.x_discretized[s + 1], self.y_discretized[s + 1]]) if self.YAW_HEADING[0] == 'center': heading = np.array(self.YAW_HEADING[1]) - p1 elif self.YAW_HEADING[0] == 'axes': heading = np.array(self.YAW_HEADING[1]) else: heading = p2 - p1 heading = (heading / self.norm2(heading) ) if self.norm2(heading) != 0 else prevHeading prevHeading = heading self.ya_discretized.append(math.atan2(heading[1], heading[0])) self.vx_discretized.append( (self.x_discretized[s + 1] - self.x_discretized[s]) * self.FREQUENCY) self.vy_discretized.append( (self.y_discretized[s + 1] - self.y_discretized[s]) * self.FREQUENCY) self.vz_discretized.append( (self.z_discretized[s + 1] - self.z_discretized[s]) * self.FREQUENCY) self.ax_discretized.append( (self.vx_discretized[-1] - self.vx_discretized[-2]) * self.FREQUENCY) self.ay_discretized.append( (self.vy_discretized[-1] - self.vy_discretized[-2]) * self.FREQUENCY) self.az_discretized.append( (self.vz_discretized[-1] - self.vz_discretized[-2]) * self.FREQUENCY) self.ti_discretized.append((s + 1.) / self.FREQUENCY) print('generate_states() runs in {} s'.format(time() - start))
def run_div_test(fld, exact, title='', show=False, ignore_inexact=False): t0 = time() result_numexpr = viscid.div(fld, preferred="numexpr", only=False) t1 = time() logger.info("numexpr magnitude runtime: %g", t1 - t0) result_diff = viscid.diff(result_numexpr, exact)['x=1:-1, y=1:-1, z=1:-1'] if not ignore_inexact and not (result_diff.data < 5e-5).all(): logger.warn("numexpr result is far from the exact result") logger.info("min/max(abs(numexpr - exact)): %g / %g", np.min(result_diff.data), np.max(result_diff.data)) planes = ["y=0f", "z=0f"] nrows = 2 ncols = len(planes) ax = plt.subplot2grid((nrows, ncols), (0, 0)) ax.axis("equal") for i, p in enumerate(planes): plt.subplot2grid((nrows, ncols), (0, i), sharex=ax, sharey=ax) mpl.plot(result_numexpr, p, show=False) plt.subplot2grid((nrows, ncols), (1, i), sharex=ax, sharey=ax) mpl.plot(result_diff, p, show=False) mpl.plt.suptitle(title) mpl.auto_adjust_subplots(subplot_params=dict(top=0.9)) mpl.plt.savefig(next_plot_fname(__file__)) if show: mpl.mplshow()
def gaussian_original(params, n_rep=DEFAULT_TRIALS, rec_time=False): ''' Generates n_rep number of Guassian facilitation curves for Go response for all simulated trials required Parameters ------------- params : sequence (4,) of float a_facGo - amplitude of gaussian curve b_facGo - time to peak of gaussian curve c_facGo - curvature of gaussian curve Returns -------- fac_i : array facilitation curves for all simulated trials t : array sequence of time index ''' # expand params a_facGo_mean, a_facGo_sd, b_facGo_mean, b_facGo_sd, c_facGo_mean, c_facGo_sd, \ inhib_mean, inhib_sd = params t = np.linspace(-.4, .2, 600, endpoint=False) #tau_facGo = 2 # Currently set, but will need to optomize # generates n_rep random numbers from a normal distribution of mean, sd that given into function a_facGo = np.random.normal(a_facGo_mean, a_facGo_sd, size=n_rep) b_facGo = np.random.normal(b_facGo_mean, b_facGo_sd, size=n_rep) c_facGo = np.random.normal(c_facGo_mean, c_facGo_sd, size=n_rep) # had to change from fac_i, t - why does this cause error now?!?! # sets up empty array of zeros for all simulated trials fac_i = np.zeros((n_rep, t.size)) inhib_tonic = np.zeros((n_rep, t.size)) inhib = np.random.normal(inhib_mean, inhib_sd, size=n_rep) inhib_tonic += inhib[:,np.newaxis] # time performance if required if (rec_time): t_start = time() for i in range(n_rep): # for each simulated trial # takes parameters passed into model plus pre_t number randomly generated # for that simulated trial myparams_fac = a_facGo[i], b_facGo[i], c_facGo[i] fac_i[i] = _gaussian_original(t, myparams_fac) # generates curve for that simulated trial #inhib_tonic[i] = get_inhib_tonic(t, inhib[i]) if (rec_time): t_diff = time() - t_start tps = n_rep / t_diff print "Original trials per second: %.0f" % (tps) return fac_i, inhib_tonic, t
def test_func(self): @cuda.jit(argtypes=[float32[:, ::1], float32[:, ::1], float32[:, ::1]]) def cu_square_matrix_mul(A, B, C): sA = cuda.shared.array(shape=SM_SIZE, dtype=float32) sB = cuda.shared.array(shape=(tpb, tpb), dtype=float32) tx = cuda.threadIdx.x ty = cuda.threadIdx.y bx = cuda.blockIdx.x by = cuda.blockIdx.y bw = cuda.blockDim.x bh = cuda.blockDim.y x = tx + bx * bw y = ty + by * bh acc = float32(0) # forces all the math to be f32 for i in range(bpg): if x < n and y < n: sA[ty, tx] = A[y, tx + i * tpb] sB[ty, tx] = B[ty + i * tpb, x] cuda.syncthreads() if x < n and y < n: for j in range(tpb): acc += sA[ty, j] * sB[j, tx] cuda.syncthreads() if x < n and y < n: C[y, x] = acc np.random.seed(42) A = np.array(np.random.random((n, n)), dtype=np.float32) B = np.array(np.random.random((n, n)), dtype=np.float32) C = np.empty_like(A) s = time() stream = cuda.stream() with stream.auto_synchronize(): dA = cuda.to_device(A, stream) dB = cuda.to_device(B, stream) dC = cuda.to_device(C, stream) cu_square_matrix_mul[(bpg, bpg), (tpb, tpb), stream](dA, dB, dC) dC.copy_to_host(C, stream) e = time() tcuda = e - s # Host compute s = time() Cans = np.dot(A, B) e = time() tcpu = e - s # Check result np.testing.assert_allclose(C, Cans, rtol=1e-5)
def timereps(reps, func, *args, **kwargs): arr = [None] * reps for i in range(reps): start = time() func(*args, **kwargs) end = time() arr[i] = end - start return min(arr), max(arr), sum(arr) / reps
def timeit(f, *args, **kwargs): from timeit import default_timer as time t0 = time() ret = f(*args, **kwargs) t1 = time() print("Took {0:.03g} secs.".format(t1 - t0)) return ret
def main(): parser = argparse.ArgumentParser(description="Test xdmf") parser.add_argument("--show", "--plot", action="store_true") parser.add_argument('file', nargs="?", default=None) args = vutil.common_argparse(parser) # f3d = readers.load_file(_viscid_root + '/../../sample/sample.3df.xdmf') # b3d = f3d['b'] # bx, by, bz = b3d.component_fields() # pylint: disable=W0612 if args.file is None: args.file = "/Users/kmaynard/dev/work/cen4000/cen4000.3d.xdmf" f3d = readers.load_file(args.file) bx = f3d["bx"] by = f3d["by"] bz = f3d["bz"] B = field.scalar_fields_to_vector([bx, by, bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) t0 = time() lines_single, topo = trace_cython(B, nr_procs=1) t1 = time() topo_single = vol.wrap_field(topo, name="CyTopo1") print("single proc:", t1 - t0, "s") nr_procs_list = np.array([1, 2, 3]) # nr_procs_list = np.array([1, 2, 3, 4, 5, 6, 7, 8]) times = np.empty_like(nr_procs_list, dtype="float") print("always parallel overhead now...") for i, nr_procs in enumerate(nr_procs_list): t0 = time() lines, topo = trace_cython(B, nr_procs=nr_procs, force_subprocess=True) t1 = time() fld = vol.wrap_field(topo, name="CyTopo") same_topo = (fld.data == topo_single.data).all() same_lines = True for j, line in enumerate(lines): same_lines = same_lines and (line == lines_single[j]).all() print("nr_procs:", nr_procs, "time:", t1 - t0, "s", "same topo:", same_topo, "same lines:", same_lines) times[i] = t1 - t0 plt.plot(nr_procs_list, times, 'k^') plt.plot(nr_procs_list, times[0] / nr_procs_list, 'b--') plt.xscale("log") plt.yscale("log") plt.show() if False: cmap = plt.get_cmap('spectral') levels = [4, 5, 6, 7, 8, 13, 14, 16, 17] norm = BoundaryNorm(levels, cmap.N) mpl.plot(topo_flds[-1], "y=0", cmap=cmap, norm=norm, show=False) #mpl.plot_streamlines2d(lines[::5], "y", topology=topo[::5], show=False) #mpl.plot_streamlines(lines, topology=topo, show=False) mpl.mplshow()
def trace_numba(fld_bx, fld_by, fld_bz): B = field.scalar_fields_to_vector([fld_bx, fld_by, fld_bz], name="B_cc", _force_layout=field.LAYOUT_INTERLACED) topo_arr = np.empty(gsize, order='C', dtype='int') lines, topo = None, None t0 = time() nsegs = topo_numba.get_topo(B, topo_arr, x1, x2, y1, y2, z1, z2) t1 = time() topo_fld = vol.wrap_field(topo, name="CyTopo") return t1 - t0, nsegs, lines, topo_fld
def compute_embedding(high, method, n_components=2, n_neighbors=None): """Reduce dimension of `high` to `n_components` using `method` (parametrized by `n_neighbors`)""" n_neighbors = n_neighbors or (n_components * (n_components + 3) / 2) + 4 try: projector = choose_manifold_method(method, n_components, n_neighbors) except ValueError: projector = choose_decomposition_method(method, n_components) start = time() lower = projector.fit_transform(high) return lower, time() - start
def test_zeros(): st = time() for i in range(10000): np.zeros(1000,dtype=object) en = time() print en-st st = time() for i in range(10000): [0 for i in range(1000)] en = time() print en-st
def backsearch(reverse_graph, end, is_start, limit): start_time = time() end_state = end.copy() times = {end_state: 0} previous_recipe = {end_state: (None, None)} queue = [(0, end_state)] known_recipes = {} current_state = None # Search while time() - start_time < limit and queue: current_game_time, current_state = heappop(queue) # print("cur " + str(current_state)) if is_start(current_state): # print (current_game_time) node = None if previous_recipe[current_state] is not None: node = current_state path = [] while previous_recipe[node][0] is not None: path.append(previous_recipe[node][0]) node = previous_recipe[node][1] # print(previous_recipe[node][0]) total_time = time() - start_time # print (path) # print(current_state) # print(current_game_time) # print(total_time) return (path, total_time) for name, resulting_state, time_cost, heuristic in reverse_graph(current_state): new_time = current_game_time + heuristic(resulting_state) # print("go " + name) # print(resulting_state) # if resulting_state in times: # print("res " + str(times[resulting_state])) # print(new_time) if resulting_state not in times or new_time < times[resulting_state]: # print (new_time) times[resulting_state] = new_time previous_recipe[resulting_state] = (name, current_state) # print("he " + name) if name not in known_recipes: known_recipes[name] = 0 # print(name) new_time /= exploration_factor heappush(queue, (new_time, resulting_state)) # Failed to find a path # print(time() - start_time) print("Failed to find a path from", state, 'within time limit.') return None
def timeit(f, *args, **kwargs): """overly simple timeit wrapper Arguments: f: callable to timeit *args: positional arguments for `f` **kwargs: keyword arguments for `f` Keyword arguments: timeit_repeat (int): number of times to call `f` (Default: 1) timeit_print_stats (bool): print min/max/mean/median when done timeit_quet (bool): quiets all output (useful if you only want the timeit_stats dict filled) timeit_stats (dict): Stats will be stuffed into here Returns: The result of `f(*args, **kwargs)` """ timeit_repeat = kwargs.pop('timeit_repeat', 1) timeit_print_stats = kwargs.pop('timeit_print_stats', True) timeit_quiet = kwargs.pop('timeit_quiet', False) timeit_stats = kwargs.pop('timeit_stats', dict()) times = np.empty((timeit_repeat,), dtype='f8') for i in range(timeit_repeat): ret = None t0 = time() ret = f(*args, **kwargs) t1 = time() s = "{0:.03g}".format(t1 - t0) times[i] = t1 - t0 if not timeit_quiet and (timeit_repeat == 1 or not timeit_print_stats): secs = "second" if s == "1" else "seconds" print("<function {0}.{1}>".format(f.__module__, f.__name__), "took", s, secs) timeit_stats['min'] = np.min(times) timeit_stats['max'] = np.max(times) timeit_stats['mean'] = np.mean(times) timeit_stats['median'] = np.median(times) timeit_stats['repeat'] = timeit_repeat if not timeit_quiet and timeit_repeat > 1 and timeit_print_stats: print("<function {0}.{1}> stats ({2} runs):" "".format(f.__module__, f.__name__, timeit_repeat)) print(" Min: {min:.3g}, Mean: {mean:.3g}, Median: {median:.3g}, " "Max: {max:.3g}".format(**timeit_stats)) return ret