def run( opts ): c = fabric.createClient( opts ) pn = c.DG.createNode( "parent" ) pn.addMember( "input", "String" ) pn.addMember( "output", "String" ) pn.setData( "input", 0, "input1" ) print "parent input: " + str( pn.getData( "input", 0 ) ) pop = c.DG.createOperator( "parentOp" ) pop.setEntryPoint( "parentOp" ) pop.setSourceCode( "operator parentOp( String input, io String output ) { output = 'parent: ' + input; }" ) pb = c.DG.createBinding() pb.setOperator( pop ) pb.setParameterLayout( [ "self.input", "self.output" ] ) pn.bindings.append( pb ) n = c.DG.createNode( "child" ) n.addMember( "output", "String" ) n.setDependency( pn, 'parent' ) op = c.DG.createOperator( "childOp" ) op.setEntryPoint( "childOp" ) op.setSourceCode( "operator childOp( io String input, io String output ) { output = 'child: ' + input; }" ) b = c.DG.createBinding() b.setOperator( op ) b.setParameterLayout( [ "parent.output", "self.output" ] ) n.bindings.append( b ) n.evaluate() print "child output: " + n.getData( "output", 0 ) c.close()
def do_GET( self ): client = fabric.createClient() self.send_response( 200 ) self.send_header( 'Content-type', 'text/plain' ) self.end_headers() self.wfile.write( client.build.getName() + ' version ' + client.build.getFullVersion() + '\n' ) client.close()
def do_GET(self): client = fabric.createClient() self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(client.build.getName() + ' version ' + client.build.getFullVersion() + '\n') client.close()
def create_lots_nodes(): p = psutil.Process( os.getpid() ) num = 10000 for i in range(0, 100): print "mem: " + str( round( p.get_memory_info().vms / 1024.0 / 1024.0, 2 ) ) + " MB" print "starting loop " + str(i) + "..." c = fabric.createClient() for i in range(0, num): c.DG.createNode("foo"+str(i)) print "flushing..." c.flush() print "closing..." c.close()
def run(opts): c = fabric.createClient(opts) pn = c.DG.createNode("parent") pn.addMember("input", "String") pn.addMember("output", "String") pn.setData("input", 0, "input1") print "parent input: " + str(pn.getData("input", 0)) pop = c.DG.createOperator("parentOp") pop.setEntryPoint("parentOp") pop.setSourceCode( "operator parentOp( String input, io String output ) { output = 'parent: ' + input; }" ) pb = c.DG.createBinding() pb.setOperator(pop) pb.setParameterLayout(["self.input", "self.output"]) pn.bindings.append(pb) n = c.DG.createNode("child") n.addMember("output", "String") n.setDependency(pn, 'parent') op = c.DG.createOperator("childOp") op.setEntryPoint("childOp") op.setSourceCode( "operator childOp( io String input, io String output ) { output = 'child: ' + input; }" ) b = c.DG.createBinding() b.setOperator(op) b.setParameterLayout(["parent.output", "self.output"]) n.bindings.append(b) n.evaluate() print "child output: " + n.getData("output", 0) c.close()
def run( opts ): c = fabric.createClient( opts ) n = c.DG.createNode( "foo" ) n.addMember( "arr", "Size[]" ) n.setData( "arr", 0, [ 1 ] ) op = c.DG.createOperator( "op" ) op.setSourceCode( "operator entry(Size arr[]) { arr[1]; } " ) op.setEntryPoint( "entry" ) b = c.DG.createBinding() b.setOperator( op ) b.setParameterLayout( [ "self.arr" ] ) n.bindings.append( b ) try: n.evaluate() except Exception as e: print e c.close()
def run(opts): c = fabric.createClient(opts) n = c.DG.createNode("foo") n.addMember("arr", "Size[]") n.setData("arr", 0, [1]) op = c.DG.createOperator("op") op.setSourceCode("operator entry(Size arr[]) { arr[1]; } ") op.setEntryPoint("entry") b = c.DG.createBinding() b.setOperator(op) b.setParameterLayout(["self.arr"]) n.bindings.append(b) try: n.evaluate() except Exception as e: print e c.close()
def pass_lots_data(): c = fabric.createClient() iters = 32 size = 2048 num = 0 start = time.time() n = c.DG.createNode("test") n.addMember( "input", "String" ) n.addMember( "output", "String" ) n.resize( size ) op = c.DG.createOperator( "testOp" ) op.setEntryPoint( "entry" ) op.setSourceCode( "operator entry( io String input, io String output ) { output = 'output: ' + input; }" ) b = c.DG.createBinding() b.setOperator( op ) b.setParameterLayout( [ "self.input", "self.output" ] ) n.bindings.append( b ) for i in range( 0, iters ): for i in range( 0, size ): n.setData( "input", i, "input"+str( num )) num += 1 n.evaluate() for i in range( 0, size ): x = n.getData( "output", i ) c.close() print 'execution time = ' + str( time.time() - start )
def do_GET( self ): query = urlparse.parse_qs(urlparse.urlparse(self.path).query) numTerms = 10 if 'n' in query: numTerms = int( query[ 'n' ][ 0 ] ) fabricClient = fabric.createClient() # Create the operator that computes the value for # each term of the series computeTermOp = fabricClient.DG.createOperator("computeTermOp") computeTermOp.setSourceCode('computeTerm.kl', open('computeTerm.kl').read()) computeTermOp.setEntryPoint('computeTerm') # Create the binding that binds the computeTermOp to the # terms node. A binding binds the members of the node # to the arguments to the operator computeTermBinding = fabricClient.DG.createBinding() computeTermBinding.setOperator(computeTermOp) computeTermBinding.setParameterLayout([ "self.index", # self.index is special: the index of the # slice being operated on "self.result" ]) # Create the node that holds the terms in the series. # The number of terms is the "count" of the node, # ie. the SIMD multiplicity termsNode = fabricClient.DG.createNode("termsNode") termsNode.setCount(numTerms) termsNode.addMember("result", "Scalar") termsNode.bindings.append(computeTermBinding) # Create the operator that sums the terms of the series sumTermsOp = fabricClient.DG.createOperator("sumTermsOp") sumTermsOp.setSourceCode('sumTerms.kl', open('sumTerms.kl').read()) sumTermsOp.setEntryPoint('sumTerms') # Create the binding that binds sumTermsOp to the members of # sumNode sumTermsBinding = fabricClient.DG.createBinding() sumTermsBinding.setOperator(sumTermsOp) sumTermsBinding.setParameterLayout([ "terms", # terms is special: it is an object that # allows you to get and set the number of slices "terms.result<>", # the <> syntax specifies that we want to bind # to all the slices at once "self.result" ]) # Create the node to hold the result, add termsNode as a # dependency and append the binding for sumTermsOp sumNode = fabricClient.DG.createNode("sumNode") sumNode.addMember("result", "Scalar") sumNode.setDependency(termsNode, "terms") sumNode.bindings.append(sumTermsBinding) # Evaluate the sumNode sumNode.evaluate() # Return the result as the HTTP content self.send_response( 200 ) self.send_header( 'Content-type', 'text/plain' ) self.end_headers() self.wfile.write( str(sumNode.getData('result', 0)) + '\n' ) # Close the Fabric Engine client. If the client isn't closed # then Fabric Python client will keep this script alive! fabricClient.close()
# Obtaining the FABRIC object import fabric FABRIC = fabric.createClient() # Registered types class Vec3(): def __init__( self, x = None, y = None, z = None ): if type( x ) is float and type( y ) is float and type( z ) is float: self.x = x self.y = y self.z = z elif x is None and y is None and z is None: self.x = 0 self.y = 0 self.z = 0 else: raise Exception( 'Vec3: invalid arguments' ) FABRIC.RT.registerType('Vec3', { 'members': [{'x': 'Scalar'}, {'y': 'Scalar'}, {'z': 'Scalar'}], 'constructor': Vec3, 'klBindings': { 'filename': "(inline)", 'sourceCode': """ // Construct a Vec3 from three Scalars function Vec3(Scalar x, Scalar y, Scalar z) { this.x = x; this.y = y; this.z = z; } // Add two Vec3s
# # Copyright 2010-2012 Fabric Engine Inc. All rights reserved. # import fabric fabricClient = fabric.createClient() op = fabricClient.DependencyGraph.createOperator("op") op.setEntryPoint("load") op.setSourceCode('\ require FabricPNG;\n\ \n\ struct RGBA\n\ {\n\ Byte r;\n\ Byte g;\n\ Byte b;\n\ Byte a;\n\ };\n\ \n\ operator load( io String url, io FabricResource resource )\n\ {\n\ report("Loaded " + url + " (mime type " + resource.mimeType + ")");\n\ report("PNG data size is " + resource.data.dataSize());\n\ Size imageWidth, imageHeight;\n\ RGBA imagePixels[];\n\ FabricPNGDecode( resource.data.data(), resource.data.dataSize(), imageWidth, imageHeight, imagePixels );\n\ report("Image dimentions are "+imageWidth+" by "+imageHeight);\n\ report("Image pixels size is "+imagePixels.size);\n\ }\n\ ')
# # Copyright 2010-2012 Fabric Engine Inc. All rights reserved. # import fabric fabric.createClient().close()
def do_GET(self): query = urlparse.parse_qs(urlparse.urlparse(self.path).query) numTerms = 10 if 'n' in query: numTerms = int(query['n'][0]) fabricClient = fabric.createClient() # Create the operator that computes the value for # each term of the series computeTermOp = fabricClient.DG.createOperator("computeTermOp") computeTermOp.setSourceCode('computeTerm.kl', open('computeTerm.kl').read()) computeTermOp.setEntryPoint('computeTerm') # Create the binding that binds the computeTermOp to the # terms node. A binding binds the members of the node # to the arguments to the operator computeTermBinding = fabricClient.DG.createBinding() computeTermBinding.setOperator(computeTermOp) computeTermBinding.setParameterLayout([ "self.index", # self.index is special: the index of the # slice being operated on "self.result" ]) # Create the node that holds the terms in the series. # The number of terms is the "count" of the node, # ie. the SIMD multiplicity termsNode = fabricClient.DG.createNode("termsNode") termsNode.setCount(numTerms) termsNode.addMember("result", "Scalar") termsNode.bindings.append(computeTermBinding) # Create the operator that sums the terms of the series sumTermsOp = fabricClient.DG.createOperator("sumTermsOp") sumTermsOp.setSourceCode('sumTerms.kl', open('sumTerms.kl').read()) sumTermsOp.setEntryPoint('sumTerms') # Create the binding that binds sumTermsOp to the members of # sumNode sumTermsBinding = fabricClient.DG.createBinding() sumTermsBinding.setOperator(sumTermsOp) sumTermsBinding.setParameterLayout([ "terms", # terms is special: it is an object that # allows you to get and set the number of slices "terms.result<>", # the <> syntax specifies that we want to bind # to all the slices at once "self.result" ]) # Create the node to hold the result, add termsNode as a # dependency and append the binding for sumTermsOp sumNode = fabricClient.DG.createNode("sumNode") sumNode.addMember("result", "Scalar") sumNode.setDependency(termsNode, "terms") sumNode.bindings.append(sumTermsBinding) def callback(): # Return the result as the HTTP content self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write(str(sumNode.getData('result', 0)) + '\n') # Close the Fabric Engine client. If the client isn't closed # then Fabric Python client will keep this script alive! fabricClient.close() # Evaluate the sumNode asynchronously. The given # callback is issue when the computation is done. sumNode.evaluateAsync(callback)
import fabric c = fabric.createClient({'logWarnings': True}) pn = c.DG.createNode("parent") pn.addMember("input", "String") pn.addMember("output", "String") pn.setData("input", 0, "input1") print "parent input: " + str(pn.getData("input", 0)) pop = c.DG.createOperator("parentOp") pop.setEntryPoint("parentOp") pop.setSourceCode( "operator parentOp( String input, io String output ) { output = 'parent: ' + input; }" ) pb = c.DG.createBinding() pb.setOperator(pop) pb.setParameterLayout(["self.input", "self.output"]) pn.bindings.append(pb) n = c.DG.createNode("child") n.addMember("output", "String") n.resize(2) n.setDependency(pn, 'parent') # test normal parameters op = c.DG.createOperator("childOp") op.setEntryPoint("childOp")
import fabric myvars = {} myvars['completed'] = 0 def launchTest( fabricClient ): o = fabricClient.DG.createOperator("o"+str(testIndex)) o.setSourceCode("inline", "operator entry(in Size index, io Scalar result) { for (Size i=0; i<4096; ++i) result = log(1+index+i); }") o.setEntryFunctionName("entry") b = fabricClient.DG.createBinding(); b.setOperator(o) b.setParameterLayout(["self.index","self.a"]) n = fabricClient.DG.createNode("n"+str(testIndex)) n.addMember("a", "Scalar") n.resize(10000) n.bindings.append(b) def complete(): myvars['completed'] = myvars['completed'] + 1 print "completed " + str(myvars['completed']) fabricClient.close() n.evaluateAsync( complete ) for testIndex in range( 0, 64 ): launchTest( fabric.createClient() ) print("queued "+str(testIndex))
import fabric myvars = {} myvars['completed'] = 0 def launchTest( fabricClient ): o = fabricClient.DG.createOperator("o"+str(testIndex)) o.setSourceCode("inline", "operator entry(in Size index, io Scalar result) { for (Size i=0; i<4096; ++i) result = log(1+index+i); }") o.setEntryPoint("entry") b = fabricClient.DG.createBinding(); b.setOperator(o) b.setParameterLayout(["self.index","self.a"]) n = fabricClient.DG.createNode("n"+str(testIndex)) n.addMember("a", "Scalar") n.resize(10000) n.bindings.append(b) def complete(): myvars['completed'] = myvars['completed'] + 1 print "completed " + str(myvars['completed']) fabricClient.close() n.evaluateAsync( complete ) for testIndex in range( 0, 64 ): launchTest( fabric.createClient() ) print("queued "+str(testIndex))
# Obtaining the FABRIC object import fabric FABRIC = fabric.createClient() # Registered types class Vec3(): def __init__(self, x=None, y=None, z=None): if type(x) is float and type(y) is float and type(z) is float: self.x = x self.y = y self.z = z elif x is None and y is None and z is None: self.x = 0 self.y = 0 self.z = 0 else: raise Exception('Vec3: invalid arguments') FABRIC.RT.registerType( 'Vec3', { 'members': [{ 'x': 'Scalar' }, { 'y': 'Scalar' }, { 'z': 'Scalar' }], 'constructor': Vec3,
import fabric c = fabric.createClient( { 'logWarnings': True } ) pn = c.DG.createNode( "parent" ) pn.addMember( "input", "String" ) pn.addMember( "output", "String" ) pn.setData( "input", 0, "input1" ) print "parent input: " + str( pn.getData( "input", 0 ) ) pop = c.DG.createOperator( "parentOp" ) pop.setEntryPoint( "parentOp" ) pop.setSourceCode( "operator parentOp( String input, io String output ) { output = 'parent: ' + input; }" ) pb = c.DG.createBinding() pb.setOperator( pop ) pb.setParameterLayout( [ "self.input", "self.output" ] ) pn.bindings.append( pb ) n = c.DG.createNode( "child" ) n.addMember( "output", "String" ) n.resize( 2 ) n.setDependency( pn, 'parent' ) # test normal parameters op = c.DG.createOperator( "childOp" ) op.setEntryPoint( "childOp" ) op.setSourceCode( "operator childOp( io String input, io String output ) { output = 'child: ' + input; }" )
# # Copyright 2010-2012 Fabric Engine Inc. All rights reserved. # import fabric fabricClient = fabric.createClient() try: fabricClient.RT.registerType( 'Foobar1', 'foobar' ) fabricClient.flush() except Exception as e: print 'error registering Foobar1: ' + str( e ) try: desc = { 'members': { 'x' }, 'constructor': object } fabricClient.RT.registerType( 'Foobar2', desc ) fabricClient.flush() except Exception as e: print 'error registering Foobar2: ' + str( e ) try: desc = { 'constructor': object } fabricClient.RT.registerType( 'Foobar3', desc ) fabricClient.flush() except Exception as e: print 'error registering Foobar3: ' + str( e )
def do_GET( self ): global numRequests numRequests += 1 numTradingDays = 252 dt = 1.0/numTradingDays sqrtDT = math.sqrt(dt) query = urlparse.parse_qs(urlparse.urlparse(self.path).query) numStocks = 0 initialPrices = [] priceMeans = [] priceDevs = [] priceCorrs = [] while True: i = numStocks initialPriceName = "ip_"+str(i+1) priceMeanName = "pm_"+str(i+1) priceDevName = "pd_"+str(i+1) if initialPriceName in query and priceMeanName in query and priceDevName in query: initialPrices.insert(i, float(query[initialPriceName][0])) priceMeans.insert(i, float(query[priceMeanName][0]) / numTradingDays) priceDevs.insert(i, float(query[priceDevName][0]) / numTradingDays) priceCorrs.insert(i, []) for j in range(0, numStocks): priceCorrelationName = "pc_" + str(j+1) + "_" + str(i+1) if priceCorrelationName in query: priceCorrs[i].insert(j, float(query[priceCorrelationName][0])) else: priceCorrs[i].insert(j, 0.0) priceCorrs[j].insert(i, priceCorrs[i][j]) priceCorrs[i].insert(i, 1.0) numStocks += 1 else: break if numStocks <= 0: self.writeHeader( 400 ) self.wfile.write("You must provide at least one stock!\n") else: if debug: print("priceMeans:") print(priceMeans) print("priceDevs:") print(priceDevs) print("priceCorrs:") print(priceCorrs) priceCovariance = [] for i in range(0, numStocks): priceCovariance.insert(i, []) for j in range(0, numStocks): priceCovariance[i].insert(j, priceDevs[i] * priceDevs[j] * priceCorrs[i][j]) choleskyTrans = MathExt.choleskyTrans(priceCovariance) drifts = [] for i in range(0, numStocks): drifts.insert(i, priceMeans[i] - priceCovariance[i][i]/2) totalInitialPrice = 0.0 for i in range(0, numStocks): totalInitialPrice += initialPrices[i] numTrials = 16384 if useFabric: fabricClient = fabric.createClient() fabricClients.append( fabricClient ) params = fabricClient.DG.createNode("params") params.addMember('numTradingDays', 'Size', numTradingDays) params.addMember('dt', 'Float64', dt) params.addMember('sqrtDT', 'Float64', sqrtDT) params.addMember('initialPrices', 'Float64['+str(numStocks)+']') params.addMember('choleskyTrans', 'Float64['+str(numStocks)+']['+str(numStocks)+']') params.setData('choleskyTrans', choleskyTrans) params.addMember('drifts', 'Float64['+str(numStocks)+']') params.setData('drifts', drifts) runTrialOp = fabricClient.DG.createOperator("runTrial") runTrial = str(numStocks).join( open('runTrial.kl').read().split('%NS%') ) #print(runTrial) runTrialOp.setSourceCode('runTrial.kl', runTrial) runTrialOp.setEntryFunctionName('runTrial') if len( runTrialOp.getDiagnostics() ) > 0: print(runTrialOp.getDiagnostics()) raise Exception( "Compile errors, aborting" ) runTrialBinding = fabricClient.DG.createBinding() runTrialBinding.setOperator(runTrialOp) runTrialBinding.setParameterLayout([ 'self.index', 'params.numTradingDays', 'params.dt', 'params.sqrtDT', 'params.choleskyTrans', 'params.drifts', 'self.value' ]) sortOp = fabricClient.DG.createOperator("sort") sort = str(numStocks).join( open('sort.kl').read().split('%NS%') ) #print(sort) sortOp.setSourceCode('sort.kl', sort) sortOp.setEntryFunctionName('sort') if len( sortOp.getDiagnostics() ) > 0: print(sortOp.getDiagnostics()) raise Exception( "Compile errors, aborting" ) sortBinding = fabricClient.DG.createBinding() sortBinding.setOperator(sortOp) sortBinding.setParameterLayout([ 'self.value<>' ]) trials = fabricClient.DG.createNode('trials') trials.setCount(numTrials) trials.setDependency(params, 'params') trials.addMember('value', 'Float64') trials.bindings.append(runTrialBinding) trials.bindings.append(sortBinding) if len( trials.getErrors() ) > 0: print(trials.getErrors()) raise Exception( "DG errors, aborting" ) def callback(): valueAtRisk = totalInitialPrice - trials.getData('value', int(round(numTrials*0.05))) self.writeHeader( 200 ) self.wfile.write(str(valueAtRisk) + "\n") fabricClient.close() fabricClients.remove( fabricClient ) self.close() trials.evaluateAsync( callback ) else: prng = MathExt.Random.ExpGenerator trialResults = [] for trial in range(0, numTrials): prng.seed(4096*(1+trial)) #print("trial="+trial+" numTradingDays="+numTradingDays+" dt="+dt+" sqrtDT="+sqrtDT) #print("choleskyTrans="+choleskyTrans) #print("drifts="+drifts) amounts = [] for i in range(0, numStocks): amounts.insert(i, initialPrices[i]) for day in range(1, numTradingDays+1): Z = MathExt.normalVec(numStocks, prng) #print("Z = "+Z) X = MathExt.mat.mulVec(choleskyTrans, Z) #print("X = "+X) for i in range(0, numStocks): amounts[i] *= math.exp(drifts[i]*dt + X[i]*sqrtDT) value = 0.0 for i in range(0, numStocks): value += amounts[i] trialResults.append(value) def sort(v): def partition(a, begin, end, pivot): piv = a[pivot] a[pivot] = a[end-1] a[end-1] = piv store = begin for i in range(begin, end-1): if a[i] <= piv: t = a[store] a[store] = a[i] a[i] = t store += 1 t = a[end-1] a[end-1] = a[store] a[store] = t return store def qsort(a, begin, end): if end - begin <= 1: return else: pivot = partition(a, begin, end, begin+int(round((end-begin)/2))) qsort(a, begin, pivot) qsort(a, pivot+1, end) return qsort(v, 0, len(v)) sort(trialResults) valueAtRisk = totalInitialPrice - trialResults[int(round(numTrials*0.05))] self.writeHeader( 200 ) self.wfile.write( str(valueAtRisk) + "\n" ) self.close()