tutorials:grolink-on-kubernetes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:grolink-on-kubernetes [2024/12/02 10:57] – [Run a simulation] tim | tutorials:grolink-on-kubernetes [2024/12/02 15:38] (current) – [Generate input data with SALib] tim | ||
---|---|---|---|
Line 225: | Line 225: | ||
podIPs=[] | podIPs=[] | ||
selector = {' | selector = {' | ||
- | for podS in kr8s.get(" | + | for podS in kr8s.get(" |
podIPs.append(podS.status.podIP) | podIPs.append(podS.status.podIP) | ||
#create link to the first pod | #create link to the first pod | ||
Line 251: | Line 251: | ||
To now use the potential of the cluster, we need to send requests to all the pods in parallel. This can be done from our one terminal pod using the python multiprocessing library. | To now use the potential of the cluster, we need to send requests to all the pods in parallel. This can be done from our one terminal pod using the python multiprocessing library. | ||
+ | Using this library we can initialize a pool of " | ||
- | + | With this pool of workers we can than work through a list of parameter sets and push each set in a simulation and collect the results in a file. | |
+ | |||
==== Generate input data with SALib ==== | ==== Generate input data with SALib ==== | ||
Line 269: | Line 270: | ||
</ | </ | ||
- | '' | + | '' |
- | ==== Linking processes to API servers | + | ==== Linking processes/" |
- | ==== Update growth function ==== | + | First we create a list with links to all the API servers: |
+ | <code python> | ||
+ | links=[] | ||
+ | selector | ||
+ | for podS in kr8s.get(" | ||
+ | links.append(GroPy.GroLink(" | ||
+ | </ | ||
- | ==== Run it all ==== | + | Then we can use this list to create a queue long enough to " |
+ | <code python> | ||
+ | WORKERCOUNT | ||
+ | pods = multiprocessing.Queue() | ||
+ | n = len(links) | ||
+ | for i in range(0, | ||
+ | pods.put(links[i%n]) | ||
+ | </ | ||
+ | This queue is required so that the workers can be initialized in parallel using the following function: | ||
+ | |||
+ | <code python> | ||
+ | #initialize each worker | ||
+ | def init_worker(function, | ||
+ | function.cursor = queue.get().openWB(content=open(" | ||
+ | </ | ||
+ | |||
+ | The function.cursor will then later be defined for each worker, by emptying the given queue. | ||
+ | |||
+ | |||
+ | ==== The actual function ==== | ||
+ | |||
+ | The actual growth function is no much different to the one we used above to test our model for the first time. Only that we can use the variable grow.cursor as a workbench because we know already that it will be initialized in that way. And we only get one tuple as an input parameter from the ASlib function, so we split it in the first line: | ||
+ | |||
+ | <code python> | ||
+ | # the actual execution | ||
+ | def grow(val): | ||
+ | lenV, angle = val | ||
+ | results = [] | ||
+ | #overwrite the parameters in the file | ||
+ | grow.cursor.updateFile(" | ||
+ | static float lenV=""" | ||
+ | static float angle=""" | ||
+ | """,' | ||
+ | grow.cursor.compile().run() | ||
+ | for x in range(0, | ||
+ | data=grow.cursor.runRGGFunction(" | ||
+ | results.append(float(data[' | ||
+ | return results | ||
+ | </ | ||
+ | |||
+ | ==== Running and saving ==== | ||
+ | |||
+ | In the final step we initialize a multiprocessing pool using the init_worker function, with the grow function and pods queue as parameters and map this pool on the generated input values. | ||
+ | |||
+ | Finally we can transfrom and save our result in an csv file. | ||
+ | <code python> | ||
+ | # Multi processing | ||
+ | pool = multiprocessing.Pool(processes=WORKERCOUNT, | ||
+ | results = pool.map(grow, | ||
+ | pool.close() | ||
+ | y = np.array(results) | ||
+ | |||
+ | # save result | ||
+ | np.savetxt(" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Running It ==== | ||
+ | |||
+ | After we put this all together and run it as we did above, we can read our csv file through our terminal pod: | ||
+ | <code bash> | ||
+ | kubectl -n grolinktutorial exec terminal-XXXX cat result.csv | ||
+ | </ | ||
+ | |||
+ | 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 = { | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | param_values = saltelli.sample(problem, | ||
+ | |||
+ | #creating a link for each pod | ||
+ | links=[] | ||
+ | selector = {' | ||
+ | for podS in kr8s.get(" | ||
+ | print(" | ||
+ | links.append(GroPy.GroLink(" | ||
+ | |||
+ | # create an queue to assign pods to workers | ||
+ | pods = multiprocessing.Queue() | ||
+ | n = len(links) | ||
+ | for i in range(0, | ||
+ | pods.put(links[i%n]) | ||
+ | |||
+ | #initialize each worker | ||
+ | def init_worker(function, | ||
+ | function.cursor = pods.get().openWB(content=open(" | ||
+ | |||
+ | # the actual execution | ||
+ | def grow(val): | ||
+ | lenV, angle = val | ||
+ | results = [] | ||
+ | #overwrite the parameters in the file | ||
+ | grow.cursor.updateFile(" | ||
+ | static float lenV=""" | ||
+ | static float angle=""" | ||
+ | """,' | ||
+ | grow.cursor.compile().run() | ||
+ | for x in range(0, | ||
+ | data=grow.cursor.runRGGFunction(" | ||
+ | results.append(float(data[' | ||
+ | return results | ||
+ | | ||
+ | # Multi processing | ||
+ | pool = multiprocessing.Pool(processes=WORKERCOUNT, | ||
+ | results = pool.map(grow, | ||
+ | pool.close() | ||
+ | y = np.array(results) | ||
+ | |||
+ | # save result | ||
+ | np.savetxt(" | ||
+ | |||
+ | </ |
tutorials/grolink-on-kubernetes.1733133441.txt.gz · Last modified: 2024/12/02 10:57 by tim