User Tools

Site Tools


tutorials:grolink-on-kubernetes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:grolink-on-kubernetes [2024/12/02 15:36] timtutorials:grolink-on-kubernetes [2024/12/02 15:38] (current) – [Generate input data with SALib] tim
Line 270: Line 270:
 </code> </code>
  
-''2**4'' describes the number of input sets, we will define this very low by now (16) because we work on a simulated cluster and don't want to cause trouble.+''%%2**4%%'' describes the number of input sets, we will define this very low by now (16) because we work on a simulated cluster and don't want to cause trouble.
  
  
Line 349: Line 349:
 </code> </code>
  
 +For simplicity you can find the last python code here in one file:
 +<code python>
 +import numpy as np
 +from SALib.sample import saltelli
 +from GroPy import GroPy
 +import multiprocessing
 +import kr8s
 +from kr8s.objects import Pod
  
 +WORKERCOUNT =9
 +
 +# defining the problem
 +problem = {
 +    'num_vars': 2,
 +    'names': ['lenV', 'angle'],
 +    'bounds': [[0.1, 1],[30, 70]]
 +}
 +param_values = saltelli.sample(problem, 2**2) # create parameter set
 +
 +#creating a link for each pod
 +links=[]
 +selector = {'app': 'grolink'}
 +for podS in kr8s.get("pods", namespace="grolinktutorial", label_selector=selector):
 +    print("x"+podS.status.podIP)
 +    links.append(GroPy.GroLink("http://"+podS.status.podIP+":58081/api/"))
 +
 +# create an queue to assign pods to workers 
 +pods = multiprocessing.Queue()
 +n = len(links)
 +for i in range(0,WORKERCOUNT):
 +    pods.put(links[i%n])
 +
 +#initialize each worker
 +def init_worker(function,pods ):
 +    function.cursor = pods.get().openWB(content=open("model.gsz",'rb').read()).run().read()
 +
 +# the actual execution 
 +def grow(val):
 +    lenV, angle = val
 +    results = []
 +    #overwrite the parameters in the file
 +    grow.cursor.updateFile("param/parameters.rgg",bytes("""
 +            static float lenV="""+str(lenV)+""";
 +            static float angle="""+str(angle)+""";
 +            """,'utf-8')).run()
 +    grow.cursor.compile().run()
 +    for x in range(0,10): #execute 10 times
 +        data=grow.cursor.runRGGFunction("run").run().read()
 +        results.append(float(data['console'][0]))
 +    return results
 +    
 +# Multi processing
 +pool = multiprocessing.Pool(processes=WORKERCOUNT,initializer=init_worker, initargs=(grow,pods,))
 +results = pool.map(grow,param_values)
 +pool.close()
 +y = np.array(results)
 +
 +# save result
 +np.savetxt("result.csv", y, delimiter=",")
 +
 +</code>
tutorials/grolink-on-kubernetes.1733150215.txt.gz · Last modified: 2024/12/02 15:36 by tim