def gpu_dag(dag): """ The GPU version of a CPU dag - including gpu communication """ nc_dag, sent, recvd = non_comm_dag(dag) recvs = { cpu_to_gpu_var(var)[0].clone(): { 'fn': GpuFromHost(), 'args': (var, ) } for var, it in dag.items() if isrecv(it['fn']) } sends = { it['args'][0]: { 'fn': HostFromGpu(), 'args': (cpu_to_gpu_var(it['args'][0])[0].clone(), ) } for _, it in dag.items() if issend(it['fn']) } def gpu_item((k, v)): i, op, o = v['args'], v['fn'], (k, ) gi, gop, go = gpu_job(i, op, o) return (go[0], {'fn': gop, 'args': gi}) gdag = dict(map(gpu_item, nc_dag.items())) return merge(gdag, recvs, sends)
def gpu_dag_transfers(dag): """ Edges/jobs for moving data from gpu version dag to cpu version >>> dag = {c: {'fn': dot 'args': (a, b)}} >>> gpu_dag_transfers(dag) {c: {'fn': HostFromGpu(), 'args': (gpu_c,)}, gpu_a: {'fn': GpuFromHost(), 'args': (a, )}, gpu_b: {'fn': GpuFromHost(), 'args': (b, )}} """ recv_inputs = { cpu_to_gpu_var(var)[0].clone(): { 'fn': GpuFromHost(), 'args': (var, ) } for var in inputs_of(dag) if isinstance(var, theano.Variable) } send_outputs = { var: { 'fn': HostFromGpu(), 'args': (cpu_to_gpu_var(var)[0].clone(), ) } for var in outputs_of(dag) if isinstance(var, theano.Variable) } return merge(recv_inputs, send_outputs)
def gpu_dag_transfers(dag): """ Edges/jobs for moving data from gpu version dag to cpu version >>> dag = {c: {'fn': dot 'args': (a, b)}} >>> gpu_dag_transfers(dag) {c: {'fn': HostFromGpu(), 'args': (gpu_c,)}, gpu_a: {'fn': GpuFromHost(), 'args': (a, )}, gpu_b: {'fn': GpuFromHost(), 'args': (b, )}} """ recv_inputs = {cpu_to_gpu_var(var)[0].clone(): {'fn': GpuFromHost(), 'args': (var,)} for var in inputs_of(dag) if isinstance(var, theano.Variable)} send_outputs = {var: {'fn': HostFromGpu(), 'args': (cpu_to_gpu_var(var)[0].clone(),)} for var in outputs_of(dag) if isinstance(var, theano.Variable)} return merge(recv_inputs, send_outputs)
def gpu_dag(dag): """ The GPU version of a CPU dag - including gpu communication """ nc_dag, sent, recvd = non_comm_dag(dag) recvs = {cpu_to_gpu_var(var)[0].clone(): {'fn': GpuFromHost(), 'args': (var,)} for var, it in dag.items() if isrecv(it['fn'])} sends = {it['args'][0]: {'fn': HostFromGpu(), 'args':(cpu_to_gpu_var(it['args'][0])[0].clone(),)} for _, it in dag.items() if issend(it['fn'])} def gpu_item((k, v)): i, op, o = v['args'], v['fn'], (k,) gi, gop, go = gpu_job(i, op, o) return (go[0], {'fn': gop, 'args': gi}) gdag = dict(map(gpu_item, nc_dag.items())) return merge(gdag, recvs, sends)
if __name__ == '__main__': import time from sys import argv, stdout from ape.theano_gpu_util import cpu_to_gpu_var, togpu_data import theano import numpy as np import ast ns = argv[1] ns = ast.literal_eval(ns) results = [] # Make function to send variables from gpu to cpu x = theano.tensor.matrix('x') gx, cx = cpu_to_gpu_var(x) send = theano.function((gx,), cx) for n in ns: xx = np.ones((n,1), dtype=np.float32) gxx = togpu_data(xx) starttime = time.time() cxx = send(gxx) endtime = time.time() duration = endtime - starttime results.append((xx.nbytes, duration)) stdout.write("[%s]\n"%(',\n'.join(map(str, results))))
if __name__ == '__main__': import time from sys import argv, stdout from ape.theano_gpu_util import cpu_to_gpu_var, togpu_data import theano import numpy as np import ast ns = argv[1] ns = ast.literal_eval(ns) results = [] # Make function to send variables from gpu to cpu x = theano.tensor.matrix('x') gx, cx = cpu_to_gpu_var(x) send = theano.function((gx, ), cx) for n in ns: xx = np.ones((n, 1), dtype=np.float32) gxx = togpu_data(xx) starttime = time.time() cxx = send(gxx) endtime = time.time() duration = endtime - starttime results.append((xx.nbytes, duration)) stdout.write("[%s]\n" % (',\n'.join(map(str, results))))